various cleanups

This commit is contained in:
Arnout Engelen
2004-08-30 14:29:18 +00:00
parent b4073a4cf5
commit 3b5ac96754
5 changed files with 29 additions and 13 deletions

View File

@@ -100,6 +100,11 @@ void Connection::add (Packet * packet)
} }
} }
/* finds connection to which this packet belongs.
* a packet belongs to a connection if it matches
* to its reference packet */
/* the incoming and outgoing streams of a connection
* are 2 sepetate 'connections' in nethogs. */
Connection * findConnection (Packet * packet) Connection * findConnection (Packet * packet)
{ {
ConnList * current = connections; ConnList * current = connections;

View File

@@ -50,20 +50,22 @@ void process (u_char * args, const struct pcap_pkthdr * header, const u_char * m
return; return;
Connection * connection = findConnection(packet); Connection * connection = findConnection(packet);
if (connection != NULL) if (connection != NULL)
{ {
/* add packet to the connection */
connection->add(packet); connection->add(packet);
return; } else {
/* else: unknown connection, create new */
connection = new Connection (packet);
Process * process = getProcess(connection, currentdevice);
} }
connection = new Connection (packet);
Process * process = getProcess(connection, currentdevice);
if (needrefresh) if (needrefresh)
{ {
do_refresh(); do_refresh();
needrefresh = false; needrefresh = false;
} }
return;
} }
void quit_cb (int i) void quit_cb (int i)

View File

@@ -19,7 +19,7 @@
#define NEEDROOT 1 #define NEEDROOT 1
#endif #endif
#define DEBUG 0 #define DEBUG 1
#define PROGNAME_WIDTH 27 #define PROGNAME_WIDTH 27

View File

@@ -107,7 +107,7 @@ Packet * getPacket (const struct pcap_pkthdr * header, const u_char * packet)
if (ethernet->ether_type != 8) if (ethernet->ether_type != 8)
{ {
#if DEBUG #if DEBUG
cerr << "Dropped non-ip packet of type " << ethernet->ether_type << endl; std::cerr << "Dropped non-ip packet of type " << ethernet->ether_type << std::endl;
#endif #endif
return NULL; return NULL;
} }
@@ -116,7 +116,7 @@ Packet * getPacket (const struct pcap_pkthdr * header, const u_char * packet)
if (ip->ip_p != 6) if (ip->ip_p != 6)
{ {
#if DEBUG #if DEBUG
cerr << "Dropped non-tcp packet of type " << (int)(ip->ip_p) << endl; std::cerr << "Dropped non-tcp packet of type " << (int)(ip->ip_p) << std::endl;
#endif #endif
return NULL; return NULL;
} }
@@ -166,8 +166,11 @@ char * Packet::gethashstring ()
return retval; return retval;
} }
/* 2 packets match if they have the same
* source and destination ports and IP's,
* or inverted. */
bool Packet::match (Packet * other) bool Packet::match (Packet * other)
{ {
return ((sport == other->sport) && (dport == other->dport) return (sport == other->sport) && (dport == other->dport)
&& (sameinaddr(sip, other->sip)) && (sameinaddr(dip, other->dip))); && (sameinaddr(sip, other->sip)) && (sameinaddr(dip, other->dip));
} }

View File

@@ -439,6 +439,12 @@ Process * getProcess (unsigned long inode, char * devicename)
return newproc; return newproc;
} }
/* Used when a new connection is encountered. Finds corresponding
* process and adds the connection. If the connection doesn't belong
* to any known process, the process list is updated and a new process
* is made. If no process can be found even then, it's added to the
* 'unknown' process.
*/
Process * getProcess (Connection * connection, char * devicename) Process * getProcess (Connection * connection, char * devicename)
{ {
ProcList * curproc = processes; ProcList * curproc = processes;
@@ -446,7 +452,7 @@ Process * getProcess (Connection * connection, char * devicename)
// see if we already know the inode for this connection // see if we already know the inode for this connection
if (DEBUG) if (DEBUG)
{ {
std::cout << "Connection reference packet found at "; std::cout << "New connection reference packet.. ";
std::cout << connection->refpacket << std::endl; std::cout << connection->refpacket << std::endl;
} }
@@ -456,14 +462,14 @@ Process * getProcess (Connection * connection, char * devicename)
{ {
// no? refresh and check conn/inode table // no? refresh and check conn/inode table
#if DEBUG #if DEBUG
cerr << "Not in table, refreshing it.\n"; std::cerr << "Not in table, refreshing table from /proc/net/tcp.\n";
#endif #endif
refreshconninode(); refreshconninode();
inode = (unsigned long *) conninode->get(connection->refpacket->gethashstring()); inode = (unsigned long *) conninode->get(connection->refpacket->gethashstring());
if (inode == NULL) if (inode == NULL)
{ {
#if DEBUG #if DEBUG
//cerr << connection->refpacket->gethashstring() << " STILL not in table - dropping\n"; std::cerr << connection->refpacket->gethashstring() << " STILL not in table - dropping\n";
#endif #endif
return NULL; return NULL;
} }