varia. now correctly sniffs in non-blocking mode (essential for multi-interface

sniffing), while requires at least libpcap 0.8
This commit is contained in:
Arnout Engelen
2005-04-03 09:31:26 +00:00
parent a142648f45
commit c66bc829ea
11 changed files with 56 additions and 28 deletions

View File

@@ -64,7 +64,7 @@ u_int32_t PackList::sumanddel (timeval t)
/* packet may be deleted by caller */ /* packet may be deleted by caller */
Connection::Connection (Packet * packet) Connection::Connection (Packet * packet)
{ {
if (ROBUST) if (!ROBUST)
assert (packet != NULL); assert (packet != NULL);
connections = new ConnList (this, connections); connections = new ConnList (this, connections);
sent_packets = new PackList (); sent_packets = new PackList ();

View File

@@ -31,7 +31,7 @@ public:
} }
PackList (Packet * m_val) PackList (Packet * m_val)
{ {
if (ROBUST) if (!ROBUST)
assert (m_val != NULL); assert (m_val != NULL);
content = new PackListNode(m_val); content = new PackListNode(m_val);
} }

16
cui.cpp
View File

@@ -19,7 +19,7 @@ class Line
public: public:
Line (const char * name, double n_sent_kbps, double n_recv_kbps, pid_t pid, uid_t uid, const char * n_devicename) 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 (uid >= 0);
assert (pid >= 0); assert (pid >= 0);
@@ -30,7 +30,7 @@ public:
devicename = n_devicename; devicename = n_devicename;
m_pid = pid; m_pid = pid;
m_uid = uid; m_uid = uid;
if (ROBUST) if (!ROBUST)
{ {
assert (m_uid >= 0); assert (m_uid >= 0);
assert (m_pid >= 0); assert (m_pid >= 0);
@@ -57,7 +57,7 @@ char * uid2username (uid_t uid)
if (pwd == NULL) if (pwd == NULL)
{ {
if (ROBUST) if (!ROBUST)
{ {
assert(false); assert(false);
} }
@@ -70,7 +70,7 @@ char * uid2username (uid_t uid)
void Line::show (int row) void Line::show (int row)
{ {
if (ROBUST) if (!ROBUST)
{ {
assert (m_uid >= 0); assert (m_uid >= 0);
assert (m_pid >= 0); assert (m_pid >= 0);
@@ -175,7 +175,7 @@ void do_refresh()
double sent_global = 0; double sent_global = 0;
double recv_global = 0; double recv_global = 0;
if (ROBUST) if (!ROBUST)
{ {
// initialise to null pointers // initialise to null pointers
for (int i = 0; i < nproc; i++) for (int i = 0; i < nproc; i++)
@@ -187,7 +187,7 @@ void do_refresh()
// walk though its connections, summing up their data, and // walk though its connections, summing up their data, and
// throwing away connections that haven't received a package // throwing away connections that haven't received a package
// in the last PROCESSTIMEOUT seconds. // in the last PROCESSTIMEOUT seconds.
if (ROBUST) if (!ROBUST)
{ {
assert (curproc != NULL); assert (curproc != NULL);
assert (curproc->getVal() != NULL); assert (curproc->getVal() != NULL);
@@ -250,7 +250,7 @@ void do_refresh()
} }
} }
uid_t uid = curproc->getVal()->getUid(); uid_t uid = curproc->getVal()->getUid();
if (ROBUST) if (!ROBUST)
{ {
assert (getpwuid(uid) != NULL); assert (getpwuid(uid) != NULL);
assert (curproc->getVal()->pid >= 0); assert (curproc->getVal()->pid >= 0);
@@ -261,7 +261,7 @@ void do_refresh()
previousproc = curproc; previousproc = curproc;
curproc = curproc->next; curproc = curproc->next;
n++; n++;
if (ROBUST) if (!ROBUST)
{ {
assert (nproc == processes->size()); assert (nproc == processes->size());
if (curproc == NULL) if (curproc == NULL)

View File

@@ -3,6 +3,7 @@
#include <netinet/ip.h> #include <netinet/ip.h>
#include <netinet/ip6.h> #include <netinet/ip6.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <string.h> // for memcpy
#include <pcap.h> #include <pcap.h>
#include "decpcap.h" #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; handle->userdata_size = size;
return pcap_dispatch (handle->pcap_handle, count, dp_pcap_callback, (u_char *)handle); 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);
}

View File

@@ -53,3 +53,5 @@ int dp_dispatch (struct dp_handle * handler, int count, u_char *user, int size);
/* functions that simply call libpcap */ /* functions that simply call libpcap */
int dp_datalink(struct dp_handle * handle); int dp_datalink(struct dp_handle * handle);
int dp_setnonblock (struct dp_handle * handle, int i, char * errbuf);

View File

@@ -3,7 +3,7 @@
#include <string.h> #include <string.h>
#include <dirent.h> #include <dirent.h>
#include <ctype.h> #include <ctype.h>
#include <iostream.h> #include <iostream>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <string> #include <string>
@@ -61,7 +61,7 @@ char * getprogname (char * pid) {
} }
int length = read (fd, buffer, bufsize); int length = read (fd, buffer, bufsize);
if (close (fd)) { if (close (fd)) {
cout << "Error closing file: " << strerror(errno) << endl; std::cout << "Error closing file: " << strerror(errno) << std::endl;
exit(34); exit(34);
} }
free (filename); free (filename);
@@ -142,7 +142,7 @@ void get_info_for_pid(char * pid) {
free (fromname); free (fromname);
continue; continue;
} }
if (ROBUST) if (!ROBUST)
assert (usedlen < linklen); assert (usedlen < linklen);
linkname[usedlen] = '\0'; linkname[usedlen] = '\0';
//std::cout << "Linking to: " << linkname << std::endl; //std::cout << "Linking to: " << linkname << std::endl;

View File

@@ -279,11 +279,12 @@ int main (int argc, char** argv)
{ {
/* The following code solves sf.net bug 1019381, but is only available /* The following code solves sf.net bug 1019381, but is only available
* in newer versions of libpcap */ * in newer versions of libpcap */
// TODO ARN
/*if (pcap_setnonblock (newhandle, 1, errbuf) == -1) if (dp_setnonblock (newhandle, 1, errbuf) == -1)
{ {
// ERROR // ERROR
}*/ }
handles = new handle (newhandle, current_dev->name, handles); handles = new handle (newhandle, current_dev->name, handles);
} }

View File

@@ -32,10 +32,10 @@
#define DEBUG 0 #define DEBUG 0
// if '1', do extra checks and // if '0', do extra checks and
// assertions. good for finding bugs // assertions. good for finding bugs
// at an early stage of development. // at an early stage of development.
// for production, should be 0. // for production, should be 1.
#define ROBUST 1 #define ROBUST 1
// 2 times: 32 characters, 7 ':''s, a ':12345'. // 2 times: 32 characters, 7 ':''s, a ':12345'.
@@ -93,7 +93,7 @@ public:
int result = inet_pton (AF_INET6, address, &addr6); int result = inet_pton (AF_INET6, address, &addr6);
if (ROBUST) if (!ROBUST)
assert (result > 0); assert (result > 0);
sa_family = AF_INET6; sa_family = AF_INET6;
} }

View File

@@ -60,7 +60,7 @@ void getLocal (const char *device)
char address [33]; char address [33];
char ifname [9]; char ifname [9];
int n_results = sscanf (buffer, "%32[0-9a-f] %*d %*d %*d %*d %8[0-9a-zA-Z]", address, ifname); 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); assert (n_results = 2);
if (strcmp (stripspaces(ifname), device) == 0) if (strcmp (stripspaces(ifname), device) == 0)
@@ -178,7 +178,7 @@ bool Packet::isOlderThan (timeval t) {
bool Packet::Outgoing () { bool Packet::Outgoing () {
/* must be initialised with getLocal("eth0:1");) */ /* must be initialised with getLocal("eth0:1");) */
if (ROBUST) if (!ROBUST)
assert (local_addrs != NULL); assert (local_addrs != NULL);
switch (dir) { switch (dir) {

View File

@@ -49,7 +49,7 @@ int Process::getLastPacket()
ConnList * curconn=connections; ConnList * curconn=connections;
while (curconn != NULL) while (curconn != NULL)
{ {
if (ROBUST) if (!ROBUST)
{ {
assert (curconn != NULL); assert (curconn != NULL);
assert (curconn->getVal() != NULL); assert (curconn->getVal() != NULL);
@@ -199,7 +199,7 @@ Process * findProcess (struct prg_node * node)
while (current != NULL) while (current != NULL)
{ {
Process * currentproc = current->getVal(); Process * currentproc = current->getVal();
if (ROBUST) if (!ROBUST)
assert (currentproc != NULL); assert (currentproc != NULL);
if (node->pid == currentproc->pid) if (node->pid == currentproc->pid)
@@ -323,10 +323,30 @@ Process * getProcess (unsigned long inode, char * devicename)
char procdir [100]; char procdir [100];
sprintf(procdir , "/proc/%d", node->pid); sprintf(procdir , "/proc/%d", node->pid);
struct stat stats; struct stat stats;
stat(procdir, &stats); int retval = stat(procdir, &stats);
/* 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); newproc->setUid(stats.st_uid);
assert (getpwuid(stats.st_uid) != NULL); /*if (getpwuid(stats.st_uid) == NULL) {
std::stderr << "uid for inode
if (!ROBUST)
assert(false);
}*/
processes = new ProcList (newproc, processes); processes = new ProcList (newproc, processes);
return newproc; return newproc;
} }

View File

@@ -14,7 +14,7 @@ class ConnList
public: public:
ConnList (Connection * m_val, ConnList * m_next) ConnList (Connection * m_val, ConnList * m_next)
{ {
if (ROBUST) if (!ROBUST)
assert (m_val != NULL); assert (m_val != NULL);
val = m_val; next = m_next; val = m_val; next = m_next;
} }
@@ -61,7 +61,7 @@ public:
uid = 0; uid = 0;
} }
void check () { void check () {
if (ROBUST) { if (!ROBUST) {
assert (pid >= 0); assert (pid >= 0);
assert (uid >= 0); assert (uid >= 0);
} }
@@ -101,7 +101,7 @@ class ProcList
public: public:
ProcList (Process * m_val, ProcList * m_next) ProcList (Process * m_val, ProcList * m_next)
{ {
if (ROBUST) if (!ROBUST)
assert (m_val != NULL); assert (m_val != NULL);
val = m_val; next = m_next; val = m_val; next = m_next;
} }