void sqlflow(const string &ss,long long liu,long long kai,long long tt) //向数据库写入流量信息 //ss是ID字符串,liu是流量,kai是起始时间,tt是终止时间 { char **jieguo=NULL; int hang=0,lie=0; string mac=getmac(ss),ip=getip(ss); yuju="SELECT * FROM flow WHERE mac='"+mac+"' AND ip='"+ip+"' AND ("+str(tt)+"-start<"+str(shezhi.pian)+");"; //查询语句,用于检查是否含有间隔小于时间片的记录 sqlf=sqlite3_get_table(db,yuju.c_str(),&jieguo,&hang,&lie,&sqlerr); if (jieguo!=NULL) sqlite3_free_table(jieguo); sqlgeterr(sqlf); if (!hang)//如果没有 { yuju="INSERT INTO flow VALUES ('"+mac+"','"+ip+"',"+str(liu)+",'"+str(kai)+"','"+str(tt)+"');"; //插入语句,插入一条记录 sqlf=sqlite3_exec(db,yuju.c_str(),NULL,NULL,&sqlerr); sqlgeterr(sqlf); } else { if (hang>1) { exit(-1); } yuju="UPDATE flow SET data=data+"+str(liu)+",end='"+str(tt)+"' WHERE mac='"+mac+"' AND ip='"+ip+"' AND ("; yuju+=str(tt)+"-start<"+str(shezhi.pian)+");"; //更新语句,更新间隔小于时间片的记录,最后,同一个IP MAC组合的每条记录间隔都大于时间片,期间的流量累加 sqlf=sqlite3_exec(db,yuju.c_str(),NULL,NULL,&sqlerr); sqlgeterr(sqlf); } }
void sqlspeed(const string &ss,long long liu,long long tt) //向数据库写入速度信息 //ss是ID串,liu是流量,tt是截止时间 { yuju="INSERT INTO speed VALUES ('"+getmac(ss)+"','"+getip(ss)+"',"+str(liu/shezhi.jiange)+",'"+str(tt)+"')"; //插入语句,每次插入此IP MAC组合数据库更新间隔中的平均速度 sqlf=sqlite3_exec(db,yuju.c_str(),NULL,NULL,&sqlerr); sqlgeterr(sqlf); }
int main(int argc, char *argv[]) { int i, c; unsigned char mac[6]; char *flash = DEFAULTFLASH; char *mtdname = NULL; off_t macoffset = 0x24000; char *redboot = NULL; while ((c = getopt(argc, argv, "f:h?m:n:o:r:")) > 0) { switch (c) { case '?': case 'h': usage(0); case 'f': flash = optarg; break; case 'm': mtdname = optarg; break; case 'n': numeths = atoi(optarg); if ((numeths < 0) || (numeths > MAXETHS)) { printf("ERROR: bad number of ethernets?\n"); exit(1); } break; case 'o': macoffset = strtoul(optarg, NULL, 0); break; case 'r': redboot = optarg; break; default: usage(1); } } if (mtdname) flash = findmtddevice(mtdname); if (flash) { if (redboot) readmacredboot(flash, redboot); else readmacflash(flash, macoffset); } for (i = 0; (i < numeths); i++) { getmac(i, &mac[0]); setmac(i, &mac[0]); } return 0; }
int main(void) { char pp[14]={"\0"}; char bufy[33]={"\0"}; char macadd[18]={"\0"}; char ipStr[16]={"\0"}; getmac(macadd); get_timestamp(pp); getIp(ipStr); getMd5((unsigned char *)pp,bufy); printf("TIME:%s\n",pp); printf("MD5:%s\n",bufy); printf("IP:%s\n",ipStr); printf("MAC:%s\n",macadd); getchar(); }
int main(void) { char mac_param[128]; write_int("/sys/devices/platform/msm_sdcc.3/polling", 1); getmac(mac_param); LOGI("Loading Libra.ko"); if ((insmod(DRIVER_MODULE_PATH, mac_param)) < 0){ rmmod("librasdioif"); } else { usleep(WIFI_DRIVER_LOADER_DELAY); property_set("wlan.driver.status", "ok"); } write_int("/sys/devices/platform/msm_sdcc.3/polling", 0); return 0; }
int main (int ac, char **av) { FILE *ifh; char *p; int n; int linenum; char *line = NULL; size_t linel = 0; for (;;) { int c = getopt_long (ac, av, "c:s", lopt, NULL); if (c == -1) break; switch (c) { default: case '?': usage (); case 'c': fname = optarg; break; case 's': use_syslog = 1; break; } } if (use_syslog) openlog ("nameif", 0, LOG_LOCAL0); while (optind < ac) { struct change *ch = xmalloc (sizeof (struct change)); char pos[30]; if ((ac - optind) & 1) usage (); if (strlen (av[optind]) + 1 > IFNAMSIZ) complain (_("interface name `%s' too long"), av[optind]); strcpy (ch->ifname, av[optind]); optind++; sprintf (pos, _("argument %d"), optind); addchange (av[optind], ch, pos); optind++; } if (!clist || fname != default_conf) readconf (); ifh = fopen ("/proc/net/dev", "r"); if (!ifh) complain (_("open of /proc/net/dev: %s"), strerror (errno)); linenum = 0; while (getdelim (&line, &linel, '\n', ifh) > 0) { struct change *ch; unsigned char mac[6]; if (linenum++ < 2) continue; p = line; while (isspace (*p)) ++p; n = strcspn (p, ": \t"); p[n] = 0; if (n > IFNAMSIZ - 1) complain (_("interface name `%s' too long"), p); if (getmac (p, mac) < 0) continue; ch = lookupmac (mac); if (!ch) continue; *ch->pprev = ch->next; if (strcmp (p, ch->ifname)) { if (setname (p, ch->ifname) < 0) complain (_("cannot change name of %s to %s: %s"), p, ch->ifname, strerror (errno)); } free (ch); } fclose (ifh); while (clist) { struct change *ch = clist; clist = clist->next; warning (_("interface '%s' not found"), ch->ifname); free (ch); } if (use_syslog) closelog (); return 0; }
/* * No doc. we are doing what Linux does as closely * as we can. */ static int ctlrinit(Ether *ether) { Dev *d; int i; int bmcr; int gpio; int ee17; int rc; d = ether->dev; switch(ether->cid){ case A8817x: case A88179: fprint(2, "%s: card known but not implemented\n", argv0); /* fall through */ default: return -1; case A88178: deprint(2, "%s: setting up A88178\n", argv0); gpio = getgpio(d); if(gpio < 0) return -1; deprint(2, "%s: gpio sts %#x\n", argv0, gpio); asixset(d, Cwena, 0); ee17 = eepromread(d, 0x0017); asixset(d, Cwdis, 0); asixset(d, Cwgpio, Gpiorse|Gpiogpo1|Gpiogpo1en); if((ee17 >> 8) != 1){ asixset(d, Cwgpio, 0x003c); asixset(d, Cwgpio, 0x001c); asixset(d, Cwgpio, 0x003c); }else{ asixset(d, Cwgpio, Gpiogpo1en); asixset(d, Cwgpio, Gpiogpo1|Gpiogpo1en); } asixset(d, Creset, Rclear); sleep(150); asixset(d, Creset, Rippd|Rprl); sleep(150); asixset(d, Cwrxctl, 0); if(getmac(d, ether->addr) < 0) return -1; ether->phy = getphy(d); if(ee17 < 0 || (ee17 & 0x7) == 0){ miiwrite(d, ether->phy, Miimctl, Mtxrxdly); sleep(60); } miiwrite(d, ether->phy, Miibmcr, Bmcrreset|Bmcranena); miiwrite(d, ether->phy, Miiad, Adall|Adcsma|Adpause); miiwrite(d, ether->phy, Miic1000, Ad1000f); bmcr = miiread(d, ether->phy, Miibmcr); if((bmcr & Bmcranena) != 0){ bmcr |= Bmcrar; miiwrite(d, ether->phy, Miibmcr, bmcr); } asixset(d, Cwmedium, Mall178); asixset(d, Cwrxctl, Rxctlso|Rxctlab); break; case A88772: deprint(2, "%s: setting up A88772\n", argv0); if(asixset(d, Cwgpio, Gpiorse|Gpiogpo2|Gpiogpo2en) < 0) return -1; ether->phy = getphy(d); dprint(2, "%s: phy %#x\n", argv0, ether->phy); if((ether->phy & Pmask) == Pembed){ /* embedded 10/100 ethernet */ rc = asixset(d, Cwphy, 1); }else rc = asixset(d, Cwphy, 0); if(rc < 0) return -1; if(asixset(d, Creset, Rippd|Rprl) < 0) return -1; sleep(150); if((ether->phy & Pmask) == Pembed) rc = asixset(d, Creset, Riprl); else rc = asixset(d, Creset, Rprte); if(rc < 0) return -1; sleep(150); rc = getrxctl(d); deprint(2, "%s: rxctl is %#x\n", argv0, rc); if(asixset(d, Cwrxctl, 0) < 0) return -1; if(getmac(d, ether->addr) < 0) return -1; if(asixset(d, Creset, Rprl) < 0) return -1; sleep(150); if(asixset(d, Creset, Riprl|Rprl) < 0) return -1; sleep(150); miiwrite(d, ether->phy, Miibmcr, Bmcrreset); miiwrite(d, ether->phy, Miiad, Adall|Adcsma); bmcr = miiread(d, ether->phy, Miibmcr); if((bmcr & Bmcranena) != 0){ bmcr |= Bmcrar; miiwrite(d, ether->phy, Miibmcr, bmcr); } if(asixset(d, Cwmedium, Mall772) < 0) return -1; if(asixset(d, Cwipg, Ipgdflt) < 0) return -1; if(asixset(d, Cwrxctl, Rxctlso|Rxctlab) < 0) return -1; deprint(2, "%s: final rxctl: %#x\n", argv0, getrxctl(d)); break; }
int main(int argc, char **argv) { InitDHT(1); char* json; int lckStatus; int res; int sleepTimeout; struct config configstr; char *passwd; char *username; char msproxyUrl[MAXBUF]; //setup the syslog logging setlogmask(LOG_UPTO(LOGLEVEL)); openlog("iot", LOG_PID | LOG_CONS, LOG_USER); syslog(LOG_INFO, "**** IoT Raspberry Pi Sample has started ****"); // register the signal handler for USR1-user defined signal 1 if (signal(SIGUSR1, sig_handler) == SIG_ERR) syslog(LOG_CRIT, "Not able to register the signal handler\n"); if (signal(SIGINT, sig_handler) == SIG_ERR) syslog(LOG_CRIT, "Not able to register the signal handler\n"); //read the config file, to decide whether to goto quickstart or registered mode of operation isRegistered = get_config(configFile, &configstr); if (isRegistered) { syslog(LOG_INFO, "Running in Registered mode\n"); sprintf(msproxyUrl, "ssl://%s.messaging.internetofthings.ibmcloud.com:8883", configstr.org); if(strcmp(configstr.authmethod ,"token") != 0) { syslog(LOG_ERR, "Detected that auth-method is not token. Currently other authentication mechanisms are not supported, IoT process will exit."); syslog(LOG_INFO, "**** IoT Raspberry Pi Sample has ended ****"); closelog(); exit(1); } else { username = "******"; passwd = configstr.authtoken; } } else { syslog(LOG_INFO, "Running in Quickstart mode\n"); strcpy(msproxyUrl,"tcp://quickstart.messaging.internetofthings.ibmcloud.com:1883"); } // read the events char* mac_address = getmac("eth0"); getClientId(&configstr, mac_address); //the timeout between the connection retry int connDelayTimeout = 1; // default sleep for 1 sec int retryAttempt = 0; // initialize the MQTT connection init_mqtt_connection(&client, msproxyUrl, isRegistered, clientId, username, passwd); // Wait till we get a successful connection to IoT MQTT server while (!MQTTAsync_isConnected(client)) { connDelayTimeout = 1; // add extra delay(3,60,600) only when reconnecting if (connected == -1) { connDelayTimeout = reconnect_delay(++retryAttempt); //Try to reconnect after the retry delay syslog(LOG_ERR, "Failed connection attempt #%d. Will try to reconnect " "in %d seconds\n", retryAttempt, connDelayTimeout); connected = 0; init_mqtt_connection(&client, msproxyUrl, isRegistered, clientId, username, passwd); } fflush(stdout); sleep(connDelayTimeout); } // resetting the counters connDelayTimeout = 1; retryAttempt = 0; // count for the sine wave int count = 1; sleepTimeout = EVENTS_INTERVAL; //subscribe for commands - only on registered mode if (isRegistered) { subscribe(&client, subscribeTopic); } while (1) { JsonMessage json_message = { DEVICE_NAME, getCPUTemp(), sineVal( MIN_VALUE, MAX_VALUE, 16, count), GetCPULoad() , getHumidity(), getTemp()}; json = generateJSON(json_message); res = publishMQTTMessage(&client, publishTopic, json); syslog(LOG_DEBUG, "Posted the message with result code = %d\n", res); if (res == -3) { //update the connected to connection failed connected = -1; while (!MQTTAsync_isConnected(client)) { if (connected == -1) { connDelayTimeout = reconnect_delay(++retryAttempt); //Try to reconnect after the retry delay syslog(LOG_ERR, "Failed connection attempt #%d. " "Will try to reconnect in %d " "seconds\n", retryAttempt, connDelayTimeout); sleep(connDelayTimeout); connected = 0; reconnect(&client, isRegistered, username,passwd); } fflush(stdout); sleep(1); } // resetting the counters connDelayTimeout = 1; retryAttempt = 0; } fflush(stdout); free(json); count++; sleep(sleepTimeout); } return 0; }
int main(int argc, char *argv[]) { int i, p, c; unsigned char mac[6]; char *flash = DEFAULTFLASH; char *mtdname = NULL; off_t macoffset = 0x24000; char *redboot = NULL; int swapmacs = 0; int runflash = 0; while ((c = getopt(argc, argv, "h?b:dspm:n:o:r:f:")) > 0) { switch (c) { case '?': case 'h': usage(0); case 'b': if (basemac(optarg) < 0) { printf("ERROR: invalid base MAC\n"); exit(1); } flash = NULL; break; case 's': swapmacs++; break; case 'p': runflash++; break; case 'd': debug++; break; case 'f': flash = optarg; break; case 'm': mtdname = optarg; break; case 'n': numeths = atoi(optarg); if ((numeths < 0) || (numeths > MAXETHS)) { printf("ERROR: bad number of ethernets?\n"); exit(1); } break; case 'o': macoffset = strtoul(optarg, NULL, 0); break; case 'r': redboot = optarg; break; default: usage(1); } } if (mtdname) flash = findmtddevice(mtdname); if (runflash) { runflashmac(); } else if (flash) { if (redboot) readmacredboot(flash, redboot); else readmacflash(flash, macoffset); } for (i = 0; (i < numeths); i++) { p = (swapmacs) ? (i^1) : i; getmac(p, &mac[0]); setmac(i, &mac[0]); } return 0; }
/* * Comand line arguments parsing */ int optparse(struct _config *config, char argc, char ** argv, char ** envp) { int option_index = 0; char optac; int longopt, rc = 0; struct option long_options[] = { {"version",0,&longopt,1}, {"config",1,0,'c'}, {"no-daemon",0,0,'n'}, {"verbose",0,0,'v'}, {"help",0,0,'h'}, {"pid",1,0,'f'}, {"key",1,0,'k'}, {"pwd",1,0,'w'}, {"port",1,0,'p'}, {"dsslport",1,0,'D'}, {"ip",1,0,'i'}, {"src-interface",1,0,'s'}, {"dst-interface",1,0,'d'}, {0,0,0,0} }; while ((optac = getopt_long (argc,argv,"vnhw:f:k:s:d:p:D:i:c::",long_options,&option_index)) != -1) switch (optac) { case 'c': if (optarg == NULL) strcpy(config->config, DEFAULT_CONFIG_FILE); else strcpy(config->config, optarg); if (load_config(config->config, config) == -1) { fprintf(stderr,"ERROR: Can't load config file \"%s\".", config->config); return(-1); } break; case 'v': config->loglevel = 1; break; case 'n': config->daemon = 0; break; case 'h': usage(); return(1); break; case 'f': strcpy(config->pidfilename, optarg); break; case 0: if (longopt == 1) { versioninfo(); exit(EXIT_SUCCESS); } longopt = 0; break; default: if (!config->cmdl) { usage(); return(-1); } if (config->index == 0) { config->cap[0] = malloc(sizeof(struct _cap)); memset(config->cap[0],0,sizeof(struct _cap)); config->index=1; } switch (optac) { case 'k': strcpy(config->cap[0]->keyfile, optarg); break; case 'w': strcpy(config->cap[0]->pwd, optarg); break; case 's': if (getmac(optarg, config->cap[0]->src_interface_mac) != -1) { strcpy(config->cap[0]->src_interface, optarg); } else { fprintf(stderr,"ERROR: %s fetching interface information error: " \ "Device not found.\n", optarg); return(-1); } break; case 'd': if (getmac(optarg, config->cap[0]->dst_interface_mac) != -1) strcpy(config->cap[0]->dst_interface, optarg); else { fprintf(stderr, "ERROR: %s fetching interface information error: " \ "Device not found.\n", optarg); return(-1); } break; case 'p': config->cap[0]->port = (uint16_t) atoi(optarg); if (config->cap[0]->port == 0) // it will always < 65535 due to limited range of data type { fprintf(stderr, "Invalid TCP port value \"%d\".\n", \ config->cap[0]->port); return(-1); } break; case 'D': config->cap[0]->dsslport = (uint16_t) atoi(optarg); if (config->cap[0]->dsslport == 0) // it will always < 65535 due to limited range of data type { fprintf(stderr, "Invalid DSSL TCP port value \"%d\".\n", \ config->cap[0]->dsslport); return(-1); } break; case 'i': if (inet_aton(optarg, &config->cap[0]->server_ip) == 0) { fprintf(stderr, "Invalid IP address format \"%s\".\n", optarg); return(-1); } break; default: usage(); return(-1); } break; } return(0); }
/* for load config */ int load_config(const char *path, struct _config *config) { char strbuf[MAXSTRLEN]; FILE *fd = NULL; int n = 0, line = 0, block = 0, index=0; fd = fopen(path, "r"); if (fd == NULL) { fprintf(stderr, "ERROR: Can't open config file \"%s\".\n", path); return(-1); } while(!feof(fd)) { register char *p = strbuf; register char *key = malloc((KEYLEN+1) * sizeof(char)); register char *val = malloc((VALLEN+1) * sizeof(char)); char title[TITLELEN]; *key = '\0'; *val = '\0'; *title = '\0'; line++; if (fgets(strbuf, sizeof(strbuf)-1, fd) == NULL) continue; rmspace(p); /* blank lines and comments get ignored */ if (!*p || *p == '#') continue; if (p[0] == '[' && p[strlen(p)-1] == ']') { sscanf(p, "[%255[^]]", title); block = 1; index = config->index; config->cap[index] = malloc(sizeof(struct _cap)); if (config->index == 0) memset(config->cap[index],0,sizeof(struct _cap)); else { memcpy(config->cap[index],config->cap[index-1],sizeof(struct _cap)); memset(config->cap[index],0,sizeof(struct _cap)); } strcpy(config->cap[index]->title, title); config->cmdl=0; config->index++; continue; } /* parse */ n = sscanf(p, "%255[^=\n\r\t]=%255[^\n\r\t]", key, val); if (n != 2) { fprintf(stderr, "ERROR: Can't parse config file %s at line %d.\n", path, line); continue; } key = tolow(key); if (!strcmp(key,"src")) { if (getmac(val, config->cap[index]->src_interface_mac) != -1) strcpy(config->cap[index]->src_interface, val); else { fprintf(stderr, "ERROR: %s fetching interface information error: Device not found.\n", val); return(-1); } } else if (!strcmp(key,"dst")) { if (getmac(val, config->cap[index]->dst_interface_mac) != -1) strcpy(config->cap[index]->dst_interface, val); else { fprintf(stderr, "ERROR: %s fetching interface information error: Device not found.\n", val); return(-1); } } else if (!strcmp(key,"ip")) { if (inet_aton(val, &config->cap[index]->server_ip) == 0) { fprintf(stderr, "Invalid IP address format \"%s\".\n", val); return(-1); } } else if (!strcmp(key,"port")) { config->cap[index]->port = (uint16_t) atoi(val); if (config->cap[index]->port == 0) // it will always < 65535 due to limited range of data type { fprintf(stderr, "Invalid TCP port value \"%d\".\n", config->cap[index]->port); return(-1); } } else if (!strcmp(key,"dsslport")) { config->cap[index]->dsslport = (uint16_t) atoi(val); if (config->cap[index]->dsslport == 0) // it will always < 65535 due to limited range of data type { fprintf(stderr, "Invalid TCP dsslport value \"%d\".\n", config->cap[index]->dsslport); return(-1); } } else if (!strcmp(key,"key")) { strcpy(config->cap[index]->keyfile, val); } else if (!strcmp(key,"pwd")) { strcpy(config->cap[index]->pwd, val); } // common options else if (!strcmp(key,"loglevel")) { if (block) fprintf(stderr,"WARNING: The option \"loglevel\" is common and must placed not in block \"[title]\".\n"); config->loglevel = atoi(val); } else if (!strcmp(key,"daemon")) { if (block) fprintf(stderr,"WARNING: The option \"daemon\" is common and must placed not in block \"[title]\".\n"); if (!strcmp(tolow(val),"off")) config->daemon = 0; else if (!strcmp(tolow(val),"on")) config->daemon = 1; else fprintf(stderr, "Invalid value in config in option \"%s\".\n", key); } else if (!strcmp(key,"pid")) { if (block) fprintf(stderr,"WARNING: The option \"pid\" is common and must placed not in block \"[title]\".\n"); strcpy(config->pidfilename, val); } free(key); free(val); } // while fclose(fd); return(0); }