/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ #include #include #include #include #include #include #include #include "network.h" static int procnetdev_version(char *buf) { if (strstr(buf, "compressed")) return 3; if (strstr(buf, "bytes")) return 2; return 1; } static int get_dev_fields(char *bp, int version, NetDeviceStats *stats) { switch (version) { case 3: sscanf(bp, "%llu %llu %lu %lu %lu %lu %lu %lu %llu %llu %lu %lu %lu %lu %lu %lu", &stats->rx_bytes, &stats->rx_packets, &stats->rx_errors, &stats->rx_dropped, &stats->rx_fifo_errors, &stats->rx_frame_errors, &stats->rx_compressed, &stats->rx_multicast, &stats->tx_bytes, &stats->tx_packets, &stats->tx_errors, &stats->tx_dropped, &stats->tx_fifo_errors, &stats->collisions, &stats->tx_carrier_errors, &stats->tx_compressed); break; case 2: sscanf(bp, "%llu %llu %lu %lu %lu %lu %llu %llu %lu %lu %lu %lu %lu", &stats->rx_bytes, &stats->rx_packets, &stats->rx_errors, &stats->rx_dropped, &stats->rx_fifo_errors, &stats->rx_frame_errors, &stats->tx_bytes, &stats->tx_packets, &stats->tx_errors, &stats->tx_dropped, &stats->tx_fifo_errors, &stats->collisions, &stats->tx_carrier_errors); stats->rx_multicast = 0; break; case 1: sscanf(bp, "%llu %lu %lu %lu %lu %llu %lu %lu %lu %lu %lu", &stats->rx_packets, &stats->rx_errors, &stats->rx_dropped, &stats->rx_fifo_errors, &stats->rx_frame_errors, &stats->tx_packets, &stats->tx_errors, &stats->tx_dropped, &stats->tx_fifo_errors, &stats->collisions, &stats->tx_carrier_errors); stats->rx_bytes = 0; stats->tx_bytes = 0; stats->rx_multicast = 0; break; } return 0; } void readNetworkUsageFromProc(NetworkUsage *buf) { //Clear Buffer memset(buf,0,sizeof(NetworkUsage)); //Open /proc/net/dev FILE *fh; fh = fopen("/proc/net/dev", "r"); char buffer[512]; // eat headers fgets(buffer, sizeof buffer, fh); fgets(buffer, sizeof buffer, fh); // end eat headers :D int version=procnetdev_version(buffer); NetDeviceStats stat; while(fgets(buffer, sizeof buffer, fh)){ get_dev_fields(buffer,version,&stat); buf->download+=stat.rx_bytes; buf->upload+=stat.tx_bytes; } fclose(fh); } void readNetworkUsageFromSys(NetworkUsage *buf){ memset(buf,0,sizeof(NetworkUsage)); DIR* sysDir=opendir("/sys/class/net"); struct dirent* sysDirEntry=0; if (!sysDir){ return; } while((sysDirEntry = readdir(sysDir))){ if (strcmp(sysDirEntry->d_name,".")==0 || strcmp(sysDirEntry->d_name,"..") == 0) continue; char path[PATH_MAX]; memset(path,0,sizeof(path)); char buffer[512]; FILE* fh; sprintf(path,"/sys/class/net/%s/statistics/rx_bytes",sysDirEntry->d_name); fh=fopen(path,"r"); if (fh){ fgets(buffer,512,fh); unsigned long long x; sscanf(buffer,"%llu",&x); buf->download+=x; fclose(fh); } sprintf(path,"/sys/class/net/%s/statistics/tx_bytes",sysDirEntry->d_name); fh=fopen(path,"r"); if (fh){ fgets(buffer,512,fh); unsigned long long x; sscanf(buffer,"%llu",&x); buf->upload+=x; fclose(fh); } } closedir(sysDir); } char *formatNetworkUsage(long upOrDownValue) { }