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 */
Connection::Connection (Packet * packet)
{
if (ROBUST)
if (!ROBUST)
assert (packet != NULL);
connections = new ConnList (this, connections);
sent_packets = new PackList ();

View File

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

16
cui.cpp
View File

@@ -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)

View File

@@ -3,6 +3,7 @@
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/tcp.h>
#include <string.h> // for memcpy
#include <pcap.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;
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 */
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 <dirent.h>
#include <ctype.h>
#include <iostream.h>
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include <string>
@@ -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;

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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) {

View File

@@ -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;
}

View File

@@ -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;
}