Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/decpcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/decpcap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down
20 changes: 11 additions & 9 deletions src/libnethogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ extern "C" {
#include <errno.h>
#include <fcntl.h>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <vector>
#include <list>

//////////////////////////////
extern ProcList *processes;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<NethogsPackageStats *>(malloc(handles.size() * sizeof(NethogsPackageStats)));
void nethogs_packet_stats(NethogsPackageStats **stats, int *stats_size) {

*stats = static_cast<NethogsPackageStats *>(
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;
Expand Down
17 changes: 10 additions & 7 deletions src/libnethogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "nethogs.cpp"
#include <fcntl.h>
#include <list>
#include <set>
#include <vector>
#include <list>

#ifdef __linux__
#include <linux/capability.h>
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,14 @@ 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;

this->sent_last_reported = sum_sent;
this->rcvd_last_reported = sum_recv;
}



Process *findProcess(struct prg_node *node) {
ProcList *current = processes;
while (current != NULL) {
Expand Down