diff --git a/hashtbl.cpp b/hashtbl.cpp index 99ff58f..75821b8 100644 --- a/hashtbl.cpp +++ b/hashtbl.cpp @@ -14,7 +14,6 @@ HashNode::~HashNode () HashTable::HashTable(int n_size) { - // TODO allow for variable size size = n_size; table = (HashNode **) malloc (size * sizeof(HashNode *)); for (unsigned int i=0; i #include #include +#include +#include #include #define _BSD_SOURCE 1 @@ -43,6 +45,8 @@ public: addr = m_addr; next = m_next; sa_family = AF_INET; + string = (char*) malloc (16); + inet_ntop (AF_INET, &m_addr, string, 15); } /* this constructor takes an char address[33] */ local_addr (char m_address [33], local_addr * m_next = NULL) @@ -73,6 +77,7 @@ public: address[35] = m_address[28]; address[36] = m_address[29]; address[37] = m_address[30]; address[38] = m_address[31]; address[39] = 0; + string = strdup(address); if (DEBUG) std::cout << "Converting address " << address << std::endl; @@ -85,11 +90,12 @@ public: bool contains (const in_addr_t & n_addr); bool contains (const struct in6_addr & n_addr); + char * string; + local_addr * next; private: in_addr_t addr; struct in6_addr addr6; - local_addr * next; short int sa_family; }; diff --git a/process.cpp b/process.cpp index 006b571..c4b4c83 100644 --- a/process.cpp +++ b/process.cpp @@ -17,6 +17,7 @@ extern timeval curtime; extern std::string * caption; +extern local_addr * local_addrs; static int INET6_getsock(char *bufp, struct sockaddr *sap) { @@ -89,7 +90,6 @@ void addtoconninode (char * buffer) // the following line leaks memory. unsigned long * inode = (unsigned long *) malloc (sizeof(unsigned long)); - // TODO check it matched sscanf(buffer, "%*d: %64[0-9A-Fa-f]:%X %64[0-9A-Fa-f]:%X %*X %*lX:%*lX %*X:%*lX %*lX %*d %*d %ld %*512s\n", local_addr, &local_port, rem_addr, &rem_port, inode); @@ -142,14 +142,24 @@ void addtoconninode (char * buffer) snprintf(hashkey, HASHKEYSIZE * sizeof(char), "%s:%d-%s:%d", local_string, local_port, remote_string, rem_port); free (local_string); - free (remote_string); //if (DEBUG) // fprintf (stderr, "Hashkey: %s\n", hashkey); conninode->add(hashkey, (void *)inode); - // TODO maybe also add this inode for our other local addresses with that destination + /* workaround: sometimes, when a connection is actually from 172.16.3.1 to + * 172.16.3.3, packages arrive from 195.169.216.157 to 172.16.3.3, where + * 172.16.3.1 and 195.169.216.157 are the local addresses of different + * interfaces */ + struct local_addr * current_local_addr = local_addrs; + while (current_local_addr != NULL) { + hashkey = (char *) malloc (HASHKEYSIZE * sizeof(char)); + snprintf(hashkey, HASHKEYSIZE * sizeof(char), "%s:%d-%s:%d", current_local_addr->string, local_port, remote_string, rem_port); + conninode->add(hashkey, (void *)inode); + current_local_addr = current_local_addr->next; + } + free (remote_string); } void refreshconninode ()