Permalink
Browse files

Initial import.

  • Loading branch information...
0 parents commit be624683f0aa27c222b20657ed3030c1ade059e3 @raboof committed Jun 29, 2004
Showing with 1,804 additions and 0 deletions.
  1. +7 −0 INSTALL
  2. +48 −0 Makefile
  3. +131 −0 connection.cpp
  4. +86 −0 connection.h
  5. +90 −0 hashtbl.cpp
  6. +30 −0 hashtbl.h
  7. BIN hashtest
  8. +11 −0 hashtest.cpp
  9. +169 −0 inet6.c
  10. +226 −0 inodeproc.cpp
  11. +27 −0 nethogs.8
  12. +175 −0 nethogs.cpp
  13. +28 −0 nethogs.h
  14. +178 −0 packet.cpp
  15. +45 −0 packet.h
  16. +463 −0 process.cpp
  17. +72 −0 process.h
  18. +17 −0 refresh.cpp
  19. +1 −0 refresh.h
  20. 0 structs.cpp
  21. 0 structs.h
@@ -0,0 +1,7 @@
+make ; make install
+
+you need the 'libpcap-dev' and 'libpcap' packages.
+
+let me know if you have any other problems on nethogs@bzzt.net
+
+
@@ -0,0 +1,48 @@
+VERSION := 0
+SUBVERSION := 6
+MINORVERSION := pre
+
+bin := /usr/local/bin
+man8 := /usr/local/man/man8/
+
+all: nethogs
+
+CFLAGS=-g
+OBJS=structs.o packet.o connection.o process.o hashtbl.o refresh.o
+GCC=g++
+
+.PHONY tgz
+
+tgz: clean
+ cd .. ; tar czvf nethogs-$(VERSION).$(SUBVERSION).$(MINORVERSION).tar.gz nethogs-$(VERSION).$(SUBVERSION)/*
+
+.PHONY check
+check:
+ echo "Not implemented"
+
+install: nethogs nethogs.8
+ cp nethogs $(bin)
+ cp nethogs.8 $(man8)
+
+nethogs: nethogs.cpp $(OBJS)
+ $(GCC) $(CFLAGS) nethogs.cpp $(OBJS) -o nethogs -lpcap -lncurses -DVERSION=\"$(VERSION)\" -DSUBVERSION=\"$(SUBVERSION)\" -DMINORVERSION=\"$(MINORVERSION)\"
+
+#-lefence
+
+refresh.o: refresh.cpp refresh.h nethogs.h
+ $(GCC) $(CFLAGS) -c refresh.cpp
+structs.o: structs.cpp structs.h nethogs.h
+ $(GCC) $(CFLAGS) -c structs.cpp
+process.o: process.cpp process.h inodeproc.cpp nethogs.h
+ $(GCC) $(CFLAGS) -c process.cpp
+packet.o: packet.cpp packet.h nethogs.h
+ $(GCC) $(CFLAGS) -c packet.cpp
+connection.o: connection.cpp connection.h nethogs.h
+ $(GCC) $(CFLAGS) -c connection.cpp
+hashtbl.o: hashtbl.cpp hashtbl.h nethogs.h
+ $(GCC) $(CFLAGS) -c hashtbl.cpp
+
+.PHONY clean
+clean:
+ rm -f $(OBJS)
+ rm -f nethogs
@@ -0,0 +1,131 @@
+#include <iostream>
+#include <assert.h>
+#include <malloc.h>
+#include "nethogs.h"
+#include "connection.h"
+
+class ConnList
+{
+public:
+ ConnList (Connection * m_val = NULL, ConnList * m_next = NULL)
+ {
+ val = m_val; next = m_next;
+ }
+ Connection * val;
+ ConnList * next;
+};
+
+ConnList * connections = NULL;
+
+void PackList::add (Packet * p)
+{
+ if (content == NULL)
+ {
+ content = new PackListNode (p);
+ return;
+ }
+
+ if (content->val->time.tv_sec == p->time.tv_sec)
+ {
+ content->val->len += p->len;
+ return;
+ }
+
+ content = new PackListNode(p, content);
+}
+
+/* sums up the total bytes used and removes 'old' packets */
+bpf_u_int32 PackList::sumanddel (timeval t)
+{
+ bpf_u_int32 retval = 0;
+ PackListNode * current = content;
+ PackListNode * previous = NULL;
+
+ while (current != NULL)
+ {
+ if (current->val->isOlderThan(t))
+ {
+ if (current == content)
+ content = NULL;
+ else if (previous != NULL)
+ previous->next = NULL;
+ delete current;
+ return retval;
+ }
+ retval += current->val->len;
+ previous = current;
+ current = current->next;
+ }
+ return retval;
+}
+
+Connection::Connection (Packet * packet)
+{
+ if (DEBUG)
+ assert (packet != NULL);
+ connections = new ConnList (this, connections);
+ sent_packets = new PackList ();
+ recv_packets = new PackList ();
+ if (packet->Outgoing())
+ {
+ sent_packets->add(packet);
+ } else {
+ recv_packets->add(packet);
+ }
+ refpacket = packet->newPacket ();
+ lastpacket = packet->time.tv_sec;
+ if (DEBUG)
+ std::cout << "New reference packet created at " << refpacket << std::endl;
+}
+
+Connection::~Connection ()
+{
+ if (DEBUG)
+ std::cout << "Deleting connection" << std::endl;
+ delete refpacket;
+ if (sent_packets != NULL)
+ delete sent_packets;
+ if (recv_packets != NULL)
+ delete recv_packets;
+}
+
+void Connection::add (Packet * packet)
+{
+ lastpacket = packet->time.tv_sec;
+ if (packet->Outgoing())
+ {
+ sent_packets->add (packet);
+ } else {
+ recv_packets->add (packet);
+ }
+}
+
+Connection * findConnection (Packet * packet)
+{
+ ConnList * current = connections;
+ while (current != NULL)
+ {
+ if (packet->match(current->val->refpacket))
+ return current->val;
+
+ current = current->next;
+ }
+ return NULL;
+}
+
+/*
+ * Connection::sumanddel
+ *
+ * sums up the total bytes used
+ * and removes 'old' packets.
+ *
+ * Returns sum of sent packages (by address)
+ * sum of recieved packages (by address)
+ */
+void Connection::sumanddel (timeval t, bpf_u_int32 * sent, bpf_u_int32 * recv)
+{
+ (*sent)=(*recv)=0;
+
+ *sent = sent_packets->sumanddel(t);
+ *recv = recv_packets->sumanddel(t);
+}
@@ -0,0 +1,86 @@
+#ifndef __CONNECTION_H
+#define __CONNECTION_H
+
+#include <iostream>
+#include "packet.h"
+
+class PackListNode
+{
+public:
+ PackListNode (Packet * m_val, PackListNode * m_next = NULL)
+ {
+ val = m_val;
+ next = m_next;
+ }
+ ~PackListNode ()
+ {
+ delete val;
+ if (next != NULL)
+ delete next;
+ }
+ PackListNode * next;
+ Packet * val;
+};
+
+class PackList
+{
+public:
+ PackList ()
+ {
+ content = NULL;
+ }
+ PackList (Packet * m_val)
+ {
+ if (DEBUG)
+ assert (m_val != NULL);
+ content = new PackListNode(m_val);
+ }
+ ~PackList ()
+ {
+ if (content != NULL)
+ delete content;
+ }
+
+ /* sums up the total bytes used and removes 'old' packets */
+ bpf_u_int32 sumanddel (timeval t);
+
+ void add (Packet * p);
+private:
+ PackListNode * content;
+};
+
+class Connection
+{
+public:
+ /* constructs a connection, makes a copy of
+ * the packet as 'refpacket', and adds the
+ * packet to the packlist */
+ Connection (Packet * packet);
+
+ ~Connection();
+
+ /* add a packet to the packlist
+ * will delete the packet when it is
+ * 'merged' with another packet
+ */
+ void add (Packet * packet);
+
+ int getLastPacket ()
+ { return lastpacket; }
+
+ /* sums up the total bytes used
+ * and removes 'old' packets. */
+ void sumanddel(timeval curtime, bpf_u_int32 * sent, bpf_u_int32 * recv);
+
+ /* for checking if a packet is part of this connection */
+ Packet * refpacket;
+private:
+ PackList * sent_packets;
+ PackList * recv_packets;
+ int lastpacket;
+};
+
+/* Find the connection this packet belongs to */
+Connection * findConnection (Packet * packet);
+
+#endif
@@ -0,0 +1,90 @@
+#include <iostream>
+#include <values.h>
+#include <malloc.h>
+#include <string.h>
+#include "hashtbl.h"
+
+HashNode::~HashNode ()
+{
+ free (key);
+ //delete (content);
+ if (next)
+ delete (next);
+}
+
+HashTable::HashTable(int n_size)
+{
+ // size = n_size;
+ // TODO allow for variable size
+ size = n_size;
+ table = (HashNode **) malloc (size * sizeof(HashNode *));
+ for (unsigned int i=0; i<size; i++)
+ {
+ table[i] = NULL;
+ }
+}
+
+HashTable::~HashTable()
+{
+ for (unsigned int i=0; i<size; i++)
+ {
+ if (table[i])
+ delete table[i];
+ }
+ free (table);
+}
+
+unsigned int HashTable::HashString (const char * str)
+{
+ unsigned int retval = 0;
+ int length = strlen(str);
+
+ unsigned int top5bits = 0xf8000000;
+ unsigned int carry = 0;
+
+ const int kleftmove=5;
+ const int krightmove=27;
+
+ for (int i=0; i<length; i++)
+ {
+ carry = retval & top5bits;
+ carry = carry >> krightmove;
+ retval = retval << kleftmove;
+ retval ^= carry;
+ retval ^= str[i];
+ }
+ return retval % size;
+}
+
+HashNode * HashTable::newHashNode(char * key, void * content, HashNode * next)
+{
+ HashNode * retval = new HashNode ();
+ retval->key = key;
+ retval->content = content;
+ retval->next = next;
+ return retval;
+}
+
+
+void HashTable::add(char * key, void * content)
+{
+ unsigned int hkey = HashString (key);
+ //cout << "Adding node: " << key << " key " << hkey << endl;
+ table[hkey] = newHashNode(key, content, table[hkey]);
+}
+
+void * HashTable::get(char * key)
+{
+ HashNode * current_node = table[HashString (key)];
+ //cout << "looking for node " << HashString (key) << endl;
+ while (current_node != NULL)
+ {
+ //cout << "found node, key = " << current_node->key << endl;
+ if (strcmp(current_node->key, key) == 0)
+ {
+ return current_node->content;
+ }
+ current_node = current_node->next;
+ }
+ return NULL;
+}
@@ -0,0 +1,30 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+class HashNode
+{
+public:
+ ~HashNode();
+
+ char * key;
+ void * content;
+ HashNode * next;
+};
+
+class HashTable
+{
+public:
+ HashTable(int n_size);
+ ~HashTable();
+ void add(char * key, void * content);
+ void * get(char * key);
+
+private:
+ int size;
+ HashNode ** table;
+
+ HashNode * newHashNode(char * key, void * content, HashNode * next);
+ unsigned int HashString(const char * str);
+
+ HashTable(); // We leave this unimplemented ;)
+};
Binary file not shown.
Oops, something went wrong.

0 comments on commit be62468

Please sign in to comment.