From c66bc829eaed80e37201198c899a5ecbf11fa87a Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Sun, 3 Apr 2005 09:31:26 +0000 Subject: [PATCH] varia. now correctly sniffs in non-blocking mode (essential for multi-interface sniffing), while requires at least libpcap 0.8 --- connection.cpp | 2 +- connection.h | 2 +- cui.cpp | 16 ++++++++-------- decpcap.c | 5 +++++ decpcap.h | 2 ++ inode2prog.cpp | 6 +++--- nethogs.cpp | 5 +++-- nethogs.h | 6 +++--- packet.cpp | 4 ++-- process.cpp | 30 +++++++++++++++++++++++++----- process.h | 6 +++--- 11 files changed, 56 insertions(+), 28 deletions(-) diff --git a/connection.cpp b/connection.cpp index 1ee79a9..b3cc56d 100644 --- a/connection.cpp +++ b/connection.cpp @@ -64,7 +64,7 @@ u_int32_t PackList::sumanddel (timeval t) /* packet may be deleted by caller */ Connection::Connection (Packet * packet) { - if (ROBUST) + if (!ROBUST) assert (packet != NULL); connections = new ConnList (this, connections); sent_packets = new PackList (); diff --git a/connection.h b/connection.h index cdf8d03..bb3c7f8 100644 --- a/connection.h +++ b/connection.h @@ -31,7 +31,7 @@ public: } PackList (Packet * m_val) { - if (ROBUST) + if (!ROBUST) assert (m_val != NULL); content = new PackListNode(m_val); } diff --git a/cui.cpp b/cui.cpp index 99d679f..95cb210 100644 --- a/cui.cpp +++ b/cui.cpp @@ -19,7 +19,7 @@ class Line public: Line (const char * name, double n_sent_kbps, double n_recv_kbps, pid_t pid, uid_t uid, const char * n_devicename) { - if (ROBUST) + if (!ROBUST) { assert (uid >= 0); assert (pid >= 0); @@ -30,7 +30,7 @@ public: devicename = n_devicename; m_pid = pid; m_uid = uid; - if (ROBUST) + if (!ROBUST) { assert (m_uid >= 0); assert (m_pid >= 0); @@ -57,7 +57,7 @@ char * uid2username (uid_t uid) if (pwd == NULL) { - if (ROBUST) + if (!ROBUST) { assert(false); } @@ -70,7 +70,7 @@ char * uid2username (uid_t uid) void Line::show (int row) { - if (ROBUST) + if (!ROBUST) { assert (m_uid >= 0); assert (m_pid >= 0); @@ -175,7 +175,7 @@ void do_refresh() double sent_global = 0; double recv_global = 0; - if (ROBUST) + if (!ROBUST) { // initialise to null pointers for (int i = 0; i < nproc; i++) @@ -187,7 +187,7 @@ void do_refresh() // walk though its connections, summing up their data, and // throwing away connections that haven't received a package // in the last PROCESSTIMEOUT seconds. - if (ROBUST) + if (!ROBUST) { assert (curproc != NULL); assert (curproc->getVal() != NULL); @@ -250,7 +250,7 @@ void do_refresh() } } uid_t uid = curproc->getVal()->getUid(); - if (ROBUST) + if (!ROBUST) { assert (getpwuid(uid) != NULL); assert (curproc->getVal()->pid >= 0); @@ -261,7 +261,7 @@ void do_refresh() previousproc = curproc; curproc = curproc->next; n++; - if (ROBUST) + if (!ROBUST) { assert (nproc == processes->size()); if (curproc == NULL) diff --git a/decpcap.c b/decpcap.c index 1f42eae..28a46c3 100644 --- a/decpcap.c +++ b/decpcap.c @@ -3,6 +3,7 @@ #include #include #include +#include // for memcpy #include #include "decpcap.h" @@ -222,3 +223,7 @@ int dp_dispatch (struct dp_handle * handle, int count, u_char *user, int size) { handle->userdata_size = size; return pcap_dispatch (handle->pcap_handle, count, dp_pcap_callback, (u_char *)handle); } + +int dp_setnonblock (struct dp_handle * handle, int i, char * errbuf) { + return pcap_setnonblock (handle->pcap_handle, i, errbuf); +} diff --git a/decpcap.h b/decpcap.h index 6a6b553..103996c 100644 --- a/decpcap.h +++ b/decpcap.h @@ -53,3 +53,5 @@ int dp_dispatch (struct dp_handle * handler, int count, u_char *user, int size); /* functions that simply call libpcap */ int dp_datalink(struct dp_handle * handle); + +int dp_setnonblock (struct dp_handle * handle, int i, char * errbuf); diff --git a/inode2prog.cpp b/inode2prog.cpp index d7ee3ad..148a6ec 100644 --- a/inode2prog.cpp +++ b/inode2prog.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include @@ -61,7 +61,7 @@ char * getprogname (char * pid) { } int length = read (fd, buffer, bufsize); if (close (fd)) { - cout << "Error closing file: " << strerror(errno) << endl; + std::cout << "Error closing file: " << strerror(errno) << std::endl; exit(34); } free (filename); @@ -142,7 +142,7 @@ void get_info_for_pid(char * pid) { free (fromname); continue; } - if (ROBUST) + if (!ROBUST) assert (usedlen < linklen); linkname[usedlen] = '\0'; //std::cout << "Linking to: " << linkname << std::endl; diff --git a/nethogs.cpp b/nethogs.cpp index 7afda79..5e2b38a 100644 --- a/nethogs.cpp +++ b/nethogs.cpp @@ -279,11 +279,12 @@ int main (int argc, char** argv) { /* The following code solves sf.net bug 1019381, but is only available * in newer versions of libpcap */ + // TODO ARN - /*if (pcap_setnonblock (newhandle, 1, errbuf) == -1) + if (dp_setnonblock (newhandle, 1, errbuf) == -1) { // ERROR - }*/ + } handles = new handle (newhandle, current_dev->name, handles); } diff --git a/nethogs.h b/nethogs.h index 08f7fd7..84bdabf 100644 --- a/nethogs.h +++ b/nethogs.h @@ -32,10 +32,10 @@ #define DEBUG 0 -// if '1', do extra checks and +// if '0', do extra checks and // assertions. good for finding bugs // at an early stage of development. -// for production, should be 0. +// for production, should be 1. #define ROBUST 1 // 2 times: 32 characters, 7 ':''s, a ':12345'. @@ -93,7 +93,7 @@ public: int result = inet_pton (AF_INET6, address, &addr6); - if (ROBUST) + if (!ROBUST) assert (result > 0); sa_family = AF_INET6; } diff --git a/packet.cpp b/packet.cpp index 601af10..94c09cf 100644 --- a/packet.cpp +++ b/packet.cpp @@ -60,7 +60,7 @@ void getLocal (const char *device) char address [33]; char ifname [9]; int n_results = sscanf (buffer, "%32[0-9a-f] %*d %*d %*d %*d %8[0-9a-zA-Z]", address, ifname); - if (ROBUST) + if (!ROBUST) assert (n_results = 2); if (strcmp (stripspaces(ifname), device) == 0) @@ -178,7 +178,7 @@ bool Packet::isOlderThan (timeval t) { bool Packet::Outgoing () { /* must be initialised with getLocal("eth0:1");) */ - if (ROBUST) + if (!ROBUST) assert (local_addrs != NULL); switch (dir) { diff --git a/process.cpp b/process.cpp index 3e65d19..c2e60c6 100644 --- a/process.cpp +++ b/process.cpp @@ -49,7 +49,7 @@ int Process::getLastPacket() ConnList * curconn=connections; while (curconn != NULL) { - if (ROBUST) + if (!ROBUST) { assert (curconn != NULL); assert (curconn->getVal() != NULL); @@ -199,7 +199,7 @@ Process * findProcess (struct prg_node * node) while (current != NULL) { Process * currentproc = current->getVal(); - if (ROBUST) + if (!ROBUST) assert (currentproc != NULL); if (node->pid == currentproc->pid) @@ -323,10 +323,30 @@ Process * getProcess (unsigned long inode, char * devicename) char procdir [100]; sprintf(procdir , "/proc/%d", node->pid); struct stat stats; - stat(procdir, &stats); - newproc->setUid(stats.st_uid); + int retval = stat(procdir, &stats); - assert (getpwuid(stats.st_uid) != NULL); + /* 0 seems a proper default. + * used in case the PID disappeared while nethogs was running + * TODO we can store node->uid this while info on the inodes, + * right? */ + /* + if (!ROBUST && (retval != 0)) + { + std::cerr << "Couldn't stat " << procdir << std::endl; + assert (false); + } + */ + + if (retval != 0) + newproc->setUid(0); + else + newproc->setUid(stats.st_uid); + + /*if (getpwuid(stats.st_uid) == NULL) { + std::stderr << "uid for inode + if (!ROBUST) + assert(false); + }*/ processes = new ProcList (newproc, processes); return newproc; } diff --git a/process.h b/process.h index e9fd6fe..40ad202 100644 --- a/process.h +++ b/process.h @@ -14,7 +14,7 @@ class ConnList public: ConnList (Connection * m_val, ConnList * m_next) { - if (ROBUST) + if (!ROBUST) assert (m_val != NULL); val = m_val; next = m_next; } @@ -61,7 +61,7 @@ public: uid = 0; } void check () { - if (ROBUST) { + if (!ROBUST) { assert (pid >= 0); assert (uid >= 0); } @@ -101,7 +101,7 @@ class ProcList public: ProcList (Process * m_val, ProcList * m_next) { - if (ROBUST) + if (!ROBUST) assert (m_val != NULL); val = m_val; next = m_next; }