END_TEST START_TEST(loadcfg_nonexistent_file) { suppress_output(); ck_assert_int_eq(loadcfg("_nosuchfile_"), 0); }
int main(int argc, char *argv[]) { int iRet=0; if ((argc < 2) || (argc > 3)) { usage(argv[0]); return -1; } if ((argc == 3) && (strcmp("load", argv[1]) != 0)) { usage(argv[0]); return -2; } if (strcmp("clear", argv[1])==0) { clearcfg(); printf("psitree restored to factory default\n"); } else if (strcmp("dump", argv[1])==0) { if (dumpcfg() != 0) { printf("Error dumping psitree\n"); iRet = -3; } } else if (strcmp("load", argv[1])==0) { if (loadcfg(argv[2]) == 0) { printf("Configuration file loaded, the system must be rebooted\n"); } else { printf("Error dumping psitree\n"); iRet = -4; } } else { usage(argv[0]); iRet = -5; } return iRet; }
END_TEST START_TEST(ibwget_from_config) { ck_assert_int_eq(loadcfg("../cfg/vnstat.conf"), 1); ck_assert_int_eq(ibwloadcfg("../cfg/vnstat.conf"), 1); cfg.maxbw = 10; ck_assert_int_eq(ibwget("ethnone"), 8); }
int main() { PNO H = NULL; strcpy(cfg_file, "c:\\prog\\cfg.dat"); loadcfg(); system("cls"); navega(H); system("cls"); return 0; }
int init_cfg() { char cfgfile[MAXLEN]; strcpy(cfgfile, getenv("HOME")); strcat(cfgfile, BLUEMOTEDIR); strcat(cfgfile, CFGFILE); free(conf); conf = loadcfg(cfgfile); if(conf==NULL) { strcpy(cfgfile, "./"); strcat(cfgfile, CFGFILE); conf = loadcfg(cfgfile); } if(conf==NULL) return 0; else return 1; }
int main( void ) { /* greeting */ puts("OpenPONIKO 3.0 - Compiled on " __DATE__ " " __TIME__); /* shoutbot */ if ( shout_init() ) return bail("Couldn't init shoutbot core\n"); /* activity */ if ( lseen_init() ) return bail("Couldn't init lastseen core\n"); /* load config */ if ( loadcfg() ) return bail("Couldn't load configuration\n"); if ( ircopen() ) return bail("Couldn't connect to default irc server\n"); while ( active ) process(); return (ircclose()||shout_quit()||lseen_quit())!=0; }
void handleintsignals(DSTATE *s) { switch (intsignal) { case SIGHUP: snprintf(errorstring, 512, "SIGHUP received, flushing data to disk and reloading config."); printe(PT_Info); cacheflush(s->dirname); s->dbcount = 0; ibwflush(); if (loadcfg(s->cfgfile)) { strncpy_nt(s->dirname, cfg.dbdir, 512); } ibwloadcfg(s->cfgfile); break; case SIGINT: snprintf(errorstring, 512, "SIGINT received, exiting."); printe(PT_Info); s->running = 0; break; case SIGTERM: snprintf(errorstring, 512, "SIGTERM received, exiting."); printe(PT_Info); s->running = 0; break; case 42: break; case 0: break; default: snprintf(errorstring, 512, "Unkown signal %d received, ignoring.", intsignal); printe(PT_Info); break; } intsignal = 0; }
int main(int argc, char **argv) { struct gopherus g; struct url start_url; char start_url_str[] = "gopher://#welcome"; memset(&g, '\0', sizeof g); /* Load configuration (or defaults) */ loadcfg(&g.cfg); ui_init(); parse_url(start_url_str, &start_url); if (history_add(&g.history, &start_url) != 0) { ui_puts("Out of memory."); return 2; } if (argc > 1) { /* if some params have been received, parse them */ int i; int goturl = 0; for (i = 1; i < argc; i++) { struct url next_url; if ((argv[i][0] == '/') || (argv[i][0] == '-')) { /* unknown parameter */ ui_puts("Gopherus v" VERSION " Copyright (C) Mateusz Viste " DATE); ui_puts(""); ui_puts("Usage: gopherus [url]"); ui_puts(""); return 1; } if (goturl != 0) { ui_puts("Invalid parameters list."); return 1; } if (parse_url(argv[1], &next_url) != 0) { ui_puts("Invalid URL!"); return 1; } goturl = 1; history_add(&g.history, &next_url); if (g.history == NULL) { ui_puts("Out of memory."); return 2; } } } g.buf = malloc(buffersize); if (g.buf == NULL) { char message[128]; sprintf(message, "Out of memory. Could not allocate buffer of %d bytes.", buffersize); ui_puts(message); return 2; } if (net_init() != 0) { ui_puts("Network subsystem initialization failed!"); free(g.buf); return 3; } ui_cursor_hide(); ui_cls(); mainloop(&g); ui_cls(); ui_cursor_show(); if (g.statusbar[0] != 0) ui_puts(g.statusbar); /* we might have here an error message to show */ /* Free the main buffer */ free(g.buf); /* unallocate all the history */ history_flush(g.history); return 0; }
int main(int argc, char *argv[]) { int i, currentarg; DIR *dir = NULL; struct dirent *di = NULL; PARAMS p; initparams(&p); /* early check for debug and config parameter */ if (argc > 1) { for (currentarg=1; currentarg<argc; currentarg++) { if ((strcmp(argv[currentarg],"-D")==0) || (strcmp(argv[currentarg],"--debug")==0)) { debug = 1; } else if (strcmp(argv[currentarg],"--config")==0) { if (currentarg+1<argc) { strncpy_nt(p.cfgfile, argv[currentarg+1], 512); if (debug) printf("Used config file: %s\n", p.cfgfile); currentarg++; continue; } else { printf("Error: File for --config missing.\n"); return 1; } } } } /* load config if available */ if (!loadcfg(p.cfgfile)) { return 1; } if (!ibwloadcfg(p.cfgfile)) { return 1; } configlocale(); strncpy_nt(p.interface, "default", 32); strncpy_nt(p.definterface, cfg.iface, 32); strncpy_nt(p.nick, "none", 32); /* init dirname */ strncpy_nt(p.dirname, cfg.dbdir, 512); /* parse parameters, maybe not the best way but... */ for (currentarg=1; currentarg<argc; currentarg++) { if (debug) printf("arg %d: \"%s\"\n",currentarg,argv[currentarg]); if (strcmp(argv[currentarg],"--longhelp")==0) { showlonghelp(&p); return 0; } else if ((strcmp(argv[currentarg],"-?")==0) || (strcmp(argv[currentarg],"--help")==0)) { showhelp(&p); return 0; } else if ((strcmp(argv[currentarg],"-i")==0) || (strcmp(argv[currentarg],"--iface")==0)) { if (currentarg+1<argc) { strncpy_nt(p.interface, argv[currentarg+1], 32); p.defaultiface = 0; if (debug) printf("Used interface: %s\n", p.interface); currentarg++; continue; } else { printf("Error: Interface for -i missing.\n"); return 1; } } else if (strcmp(argv[currentarg],"--config")==0) { /* config has already been parsed earlier so nothing to do here */ currentarg++; continue; } else if ((strcmp(argv[currentarg],"--nick"))==0) { if (currentarg+1<argc) { strncpy_nt(p.nick, argv[currentarg+1], 32); if (debug) printf("Used nick: %s\n", p.nick); currentarg++; continue; } else { printf("Error: Nick for --nick missing.\n"); return 1; } } else if ((strcmp(argv[currentarg],"--style"))==0) { if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) { if (cfg.ostyle > 4 || cfg.ostyle < 0) { printf("Error: Invalid style parameter \"%d\" for --style.\n", cfg.ostyle); printf(" Valid parameters:\n"); printf(" 0 - a more narrow output\n"); printf(" 1 - enable bar column if available\n"); printf(" 2 - average traffic rate in summary and weekly outputs\n"); printf(" 3 - average traffic rate in all outputs if available\n"); printf(" 4 - disable terminal control characters in -l / --live\n"); return 1; } cfg.ostyle = atoi(argv[currentarg+1]); if (debug) printf("Style changed: %d\n", cfg.ostyle); currentarg++; continue; } else { printf("Error: Style parameter for --style missing.\n"); printf(" Valid parameters:\n"); printf(" 0 - a more narrow output\n"); printf(" 1 - enable bar column if available\n"); printf(" 2 - average traffic rate in summary and weekly outputs\n"); printf(" 3 - average traffic rate in all outputs if available\n"); printf(" 4 - disable terminal control characters in -l / --live\n"); return 1; } } else if ((strcmp(argv[currentarg],"--dbdir"))==0) { if (currentarg+1<argc) { strncpy_nt(p.dirname, argv[currentarg+1], 512); if (debug) printf("DatabaseDir: \"%s\"\n", p.dirname); currentarg++; continue; } else { printf("Error: Directory for --dbdir missing.\n"); return 1; } } else if ((strcmp(argv[currentarg],"--locale"))==0) { if (currentarg+1<argc) { setlocale(LC_ALL, argv[currentarg+1]); if (debug) printf("Locale: \"%s\"\n", argv[currentarg+1]); currentarg++; continue; } else { printf("Error: Locale for --locale missing.\n"); return 1; } } else if (strcmp(argv[currentarg],"--create")==0) { p.create=1; p.query=0; if (debug) printf("Creating database...\n"); } else if ((strcmp(argv[currentarg],"-u")==0) || (strcmp(argv[currentarg],"--update")==0)) { p.update=1; p.query=0; if (debug) printf("Updating database...\n"); } else if (strcmp(argv[currentarg],"--importdb")==0) { if (currentarg+1<argc) { p.import=1; strncpy_nt(p.filename, argv[currentarg+1], 512); if (debug) printf("Used import file: %s\n", p.filename); currentarg++; continue; } else { printf("Error: File parameter for --importdb missing.\n"); return 1; } } else if ((strcmp(argv[currentarg],"-q")==0) || (strcmp(argv[currentarg],"--query")==0)) { p.query=1; } else if ((strcmp(argv[currentarg],"-D")==0) || (strcmp(argv[currentarg],"--debug")==0)) { debug=1; } else if ((strcmp(argv[currentarg],"-d")==0) || (strcmp(argv[currentarg],"--days")==0)) { cfg.qmode=1; } else if ((strcmp(argv[currentarg],"-m")==0) || (strcmp(argv[currentarg],"--months")==0)) { cfg.qmode=2; } else if ((strcmp(argv[currentarg],"-t")==0) || (strcmp(argv[currentarg],"--top10")==0)) { cfg.qmode=3; } else if ((strcmp(argv[currentarg],"-s")==0) || (strcmp(argv[currentarg],"--short")==0)) { cfg.qmode=5; } else if ((strcmp(argv[currentarg],"-w")==0) || (strcmp(argv[currentarg],"--weeks")==0)) { cfg.qmode=6; } else if ((strcmp(argv[currentarg],"-h")==0) || (strcmp(argv[currentarg],"--hours")==0)) { cfg.qmode=7; } else if ((strcmp(argv[currentarg],"--exportdb")==0) || (strcmp(argv[currentarg],"--dumpdb")==0)) { cfg.qmode=4; } else if (strcmp(argv[currentarg],"--oneline")==0) { cfg.qmode=9; } else if (strcmp(argv[currentarg],"--xml")==0) { if (currentarg+1<argc && argv[currentarg+1][0]!='-') { p.xmlmode = argv[currentarg+1][0]; if (strlen(argv[currentarg+1])!=1 || strchr("ahdmt", p.xmlmode)==NULL) { printf("Error: Invalid mode parameter \"%s\" for --xml.\n", argv[currentarg+1]); printf(" Valid parameters:\n"); printf(" a - all (default)\n"); printf(" h - only hours\n"); printf(" d - only days\n"); printf(" m - only months\n"); printf(" t - only top 10\n"); return 1; } currentarg++; } cfg.qmode=8; } else if (strcmp(argv[currentarg],"--json")==0) { if (currentarg+1<argc && argv[currentarg+1][0]!='-') { p.jsonmode = argv[currentarg+1][0]; if (strlen(argv[currentarg+1])!=1 || strchr("ahdmt", p.jsonmode)==NULL) { printf("Error: Invalid mode parameter \"%s\" for --json.\n", argv[currentarg+1]); printf(" Valid parameters:\n"); printf(" a - all (default)\n"); printf(" h - only hours\n"); printf(" d - only days\n"); printf(" m - only months\n"); printf(" t - only top 10\n"); return 1; } currentarg++; } cfg.qmode=10; } else if (strcmp(argv[currentarg],"--savemerged")==0) { p.savemerged=1; } else if ((strcmp(argv[currentarg],"-ru")==0) || (strcmp(argv[currentarg],"--rateunit"))==0) { if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) { if (cfg.rateunit > 1 || cfg.rateunit < 0) { printf("Error: Invalid parameter \"%d\" for --rateunit.\n", cfg.rateunit); printf(" Valid parameters:\n"); printf(" 0 - bytes\n"); printf(" 1 - bits\n"); return 1; } cfg.rateunit = atoi(argv[currentarg+1]); if (debug) printf("Rateunit changed: %d\n", cfg.rateunit); currentarg++; continue; } else { cfg.rateunit = !cfg.rateunit; if (debug) printf("Rateunit changed: %d\n", cfg.rateunit); } } else if (strcmp(argv[currentarg],"--enable")==0) { p.active=1; p.query=0; } else if ((strcmp(argv[currentarg],"-tr")==0) || (strcmp(argv[currentarg],"--traffic")==0)) { if (currentarg+1<argc && isdigit(argv[currentarg+1][0])) { cfg.sampletime=atoi(argv[currentarg+1]); currentarg++; p.traffic=1; p.query=0; continue; } p.traffic=1; p.query=0; } else if ((strcmp(argv[currentarg],"-l")==0) || (strcmp(argv[currentarg],"--live")==0)) { if (currentarg+1<argc && argv[currentarg+1][0]!='-') { if (!isdigit(argv[currentarg+1][0]) || p.livemode > 1 || p.livemode < 0) { printf("Error: Invalid mode parameter \"%s\" for -l / --live.\n", argv[currentarg+1]); printf(" Valid parameters:\n"); printf(" 0 - show packets per second (default)\n"); printf(" 1 - show transfer counters\n"); return 1; } p.livemode = atoi(argv[currentarg+1]); currentarg++; } p.livetraffic=1; p.query=0; } else if (strcmp(argv[currentarg],"--force")==0) { p.force=1; } else if (strcmp(argv[currentarg],"--cleartop")==0) { p.cleartop=1; } else if (strcmp(argv[currentarg],"--rebuildtotal")==0) { p.rebuildtotal=1; } else if (strcmp(argv[currentarg],"--disable")==0) { p.active=0; p.query=0; } else if (strcmp(argv[currentarg],"--testkernel")==0) { i=kerneltest(); return i; } else if (strcmp(argv[currentarg],"--showconfig")==0) { printcfgfile(); return 0; } else if (strcmp(argv[currentarg],"--delete")==0) { p.delete=1; p.query=0; } else if (strcmp(argv[currentarg],"--iflist")==0) {
END_TEST START_TEST(loadcfg_not_a_cfgfile) { ck_assert_int_eq(loadcfg("Makefile"), 1); }
END_TEST START_TEST(loadcfg_no_file) { ck_assert_int_eq(loadcfg(""), 1); }
END_TEST START_TEST(loadcfg_included_default) { ck_assert_int_eq(loadcfg("../cfg/vnstat.conf"), 1); }
int main(int argc, char const *argv[]) { if(argc != 2) { std::cout<<"You forgot to give the node number :P\nExiting.......\n"; return -1; } int node_id = atoi(argv[1]),c; char recieve_buffer[1000]; loadcfg(); int sockfd; struct sockaddr_in my_addr, client, dest_node; socklen_t clientlength = sizeof(client); if((sockfd = socket(PF_INET,SOCK_DGRAM,0)) == -1)//it means socket was not created { std::cout<<"Unable to create socket\n"; return -1; } std::cout<<"sockfd value : "<<sockfd<<"\n"; my_addr.sin_family = AF_INET; //assigning family set my_addr.sin_port = htons(atoi(port[node_id].c_str())); //assigning port number my_addr.sin_addr.s_addr = inet_addr(addr[node_id].c_str());//assigning ip address memset(&(my_addr.sin_zero),'\0',8);//zero the rest of the struct if((bind(sockfd,(struct sockaddr *)&my_addr, sizeof(struct sockaddr))) == -1)//it means binding failed { std::cout<<"Unable to bind to the socket.\n"; return -1; } std::cout<<"Binded!!\n"; do{ //k gives the number of bytes recieved int k = recvfrom(sockfd,recieve_buffer,1000,0,(struct sockaddr*)&client,&clientlength); if(k < 0) { std::cout<<"Error in Receiving data :(.\nContinuing to listen\n"; continue; } else if(k > 0) { /* Packet Format IPAdress Port Request md5sum eg: 10.6.162.77 1300 1 c19a6e1f83945a2f92193a85cf4b44d4 */ std::string pkt = std::string(recieve_buffer);//converting the msg to string format int psn; psn = pkt.find(" "); std::string client_addr = pkt.substr(0,psn); pkt = pkt.substr(psn + 1); psn = pkt.find(" "); int client_port = atoi((pkt.substr(0,psn)).c_str()); pkt = pkt.substr(psn + 1); psn = pkt.find(" "); std::string req = pkt.substr(0,psn); std::string md5str = pkt.substr(psn + 1); int z = md5modn(md5str,n); if(z == node_id)//current node is the destination node for the client { } else //current node is not the destination node for the client { std::string dst_addr = addr[z],dst_finfo = finfo[z];//get the info of the destination node int dst_port = atoi(port[z].c_str()); //To send data to that node, create a struct sockaddr for that node dest_node.sin_family = AF_INET; //assigning family set dest_node.sin_port = htons(dst_port); //assigning port number dest_node.sin_addr.s_addr = inet_addr(dst_addr.c_str()); //assigning ip address memset(&(dest_node.sin_zero),'\0',8); //zero the rest of the struct //Recieved data is now transferred to the destination node. //k1 gives the number of bytes sent int k1 = sendto(sockfd,recieve_buffer,1000,0,(struct sockaddr*)&dest_node,sizeof(struct sockaddr)); if(k1<0) //Sending failed as number of bytes sent is less than 0 :/ { std::cout<<"Sending to the Destination Node "<<z<<" failed :/\nExiting.......\n"; return 0; } } } } while(1); c = close(sockfd); std::cout<<addr[0]<<" "<<addr[1]<<"\n"; std::cout<<port[0]<<" "<<port[1]<<"\n"; std::cout<<"number of nodes is: "<<n<<"\n"; return 0; }