diff --git a/src/decpcap.c b/src/decpcap.c index 37e2fcf..61134e8 100644 --- a/src/decpcap.c +++ b/src/decpcap.c @@ -112,17 +112,15 @@ struct dp_handle *dp_open_live(const char *device, int snaplen, int promisc, /* function to get packet statistics, e.g. dropped packets */ -dp_stat dp_stats(struct dp_handle* handle) -{ +dp_stat dp_stats(struct dp_handle *handle) { struct pcap_stat ps; - if(pcap_stats(handle->pcap_handle, &ps) == PCAP_ERROR) - { - fprintf(stderr, "Error getting pcap_stats: %s\n", - pcap_geterr(handle->pcap_handle)); - ps.ps_recv = 0; - ps.ps_drop = 0; - ps.ps_ifdrop = 0; - return ps; + if (pcap_stats(handle->pcap_handle, &ps) == PCAP_ERROR) { + fprintf(stderr, "Error getting pcap_stats: %s\n", + pcap_geterr(handle->pcap_handle)); + ps.ps_recv = 0; + ps.ps_drop = 0; + ps.ps_ifdrop = 0; + return ps; } return ps; } diff --git a/src/decpcap.h b/src/decpcap.h index 6d2c86f..7352674 100644 --- a/src/decpcap.h +++ b/src/decpcap.h @@ -72,7 +72,7 @@ struct dp_handle *dp_open_offline(char *fname, char *ebuf); /* function to get packet statistics, e.g. dropped packets */ -dp_stat dp_stats(struct dp_handle* handle); +dp_stat dp_stats(struct dp_handle *handle); /* functions to add callbacks */ diff --git a/src/libnethogs.cpp b/src/libnethogs.cpp index 81c02b6..92a6226 100644 --- a/src/libnethogs.cpp +++ b/src/libnethogs.cpp @@ -7,10 +7,10 @@ extern "C" { #include #include #include +#include #include #include #include -#include ////////////////////////////// extern ProcList *processes; @@ -249,7 +249,6 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) { NHM_UPDATE_ONE_FIELD(data.sent_kbs, sent_kbs) NHM_UPDATE_ONE_FIELD(data.recv_kbs, recv_kbs) - #undef NHM_UPDATE_ONE_FIELD if (data_change) { @@ -265,7 +264,8 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) { static void nethogsmonitor_clean_up() { // clean up - for(auto current_handle = handles.begin(); current_handle != handles.end(); current_handle++){ + for (auto current_handle = handles.begin(); current_handle != handles.end(); + current_handle++) { pcap_close(current_handle->content->pcap_handle); } handles.clear(); @@ -303,7 +303,8 @@ int nethogsmonitor_loop_devices(NethogsMonitorCallback cb, char *filter, while (monitor_run_flag) { bool packets_read = false; - for(auto current_handle = handles.begin(); current_handle != handles.end(); current_handle++) { + for (auto current_handle = handles.begin(); current_handle != handles.end(); + current_handle++) { userdata->device = current_handle->devicename; userdata->sa_family = AF_UNSPEC; int retval = dp_dispatch(current_handle->content, -1, (u_char *)userdata, @@ -340,13 +341,14 @@ void nethogsmonitor_breakloop() { write(self_pipe.second, "x", 1); } -void nethogs_packet_stats(NethogsPackageStats **stats, int *stats_size) -{ - - *stats = static_cast(malloc(handles.size() * sizeof(NethogsPackageStats))); +void nethogs_packet_stats(NethogsPackageStats **stats, int *stats_size) { + + *stats = static_cast( + malloc(handles.size() * sizeof(NethogsPackageStats))); int i = 0; - for(auto current_handle = handles.begin(); current_handle != handles.end(); current_handle ++){ + for (auto current_handle = handles.begin(); current_handle != handles.end(); + current_handle++) { dp_stat stat = dp_stats(current_handle->content); stats[i]->ps_recv = stat.ps_recv; stats[i]->ps_drop = stat.ps_drop; diff --git a/src/libnethogs.h b/src/libnethogs.h index 26849f9..87da861 100644 --- a/src/libnethogs.h +++ b/src/libnethogs.h @@ -33,11 +33,13 @@ typedef struct NethogsMonitorRecord { float recv_kbs; } NethogsMonitorRecord; -typedef struct NethogsPackageStats -{ - u_int ps_recv; /** number of packets received */ - u_int ps_drop; /** number of packets dropped because there was no room in the operating system's buffer when they arrived, because packets weren't being read fast enough */ - u_int ps_ifdrop; /** number of packets dropped by the network interface or its driver. */ +typedef struct NethogsPackageStats { + u_int ps_recv; /** number of packets received */ + u_int ps_drop; /** number of packets dropped because there was no room in the + operating system's buffer when they arrived, because packets + weren't being read fast enough */ + u_int ps_ifdrop; /** number of packets dropped by the network interface or its + driver. */ const char *devicename; /** name of the network interface */ } NethogsPackageStats; @@ -106,11 +108,12 @@ NETHOGS_DSO_VISIBLE void nethogsmonitor_breakloop(); /** * @brief returns the pcap packet stats per device - * + * * @param stats C-Style array the will hold the stats * @param stats_size elements and therefore devices in stats */ -NETHOGS_DSO_VISIBLE void nethogs_packet_stats(NethogsPackageStats **stats, int *stats_size); +NETHOGS_DSO_VISIBLE void nethogs_packet_stats(NethogsPackageStats **stats, + int *stats_size); #undef NETHOGS_DSO_VISIBLE #undef NETHOGS_DSO_HIDDEN diff --git a/src/main.cpp b/src/main.cpp index 7501461..e7ccaf3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,8 @@ #include "nethogs.cpp" #include +#include #include #include -#include #ifdef __linux__ #include @@ -309,7 +309,8 @@ int main(int argc, char **argv) { while (1) { bool packets_read = false; - for (auto current_handle = handles.begin(); current_handle != handles.end(); current_handle ++) { + for (auto current_handle = handles.begin(); current_handle != handles.end(); + current_handle++) { userdata->device = current_handle->devicename; userdata->sa_family = AF_UNSPEC; int retval = dp_dispatch(current_handle->content, -1, (u_char *)userdata, diff --git a/src/process.cpp b/src/process.cpp index bf61efd..a6cb7b8 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -196,7 +196,7 @@ void Process::gettotalb(float *recvd, float *sent) { void Process::getlast(u_int64_t *recvd, u_int64_t *sent) { u_int64_t sum_sent = 0, sum_recv = 0; gettotal(&sum_recv, &sum_sent); - + *sent = sum_sent - this->sent_last_reported; *recvd = sum_recv - this->rcvd_last_reported; @@ -204,8 +204,6 @@ void Process::getlast(u_int64_t *recvd, u_int64_t *sent) { this->rcvd_last_reported = sum_recv; } - - Process *findProcess(struct prg_node *node) { ProcList *current = processes; while (current != NULL) {