summaryrefslogtreecommitdiff
path: root/serv_hash.c
diff options
context:
space:
mode:
authorpdw <>2002-10-10 14:11:12 +0000
committerpdw <>2002-10-10 14:11:12 +0000
commitc1f017231c5a6b5314804ea390512e336a53c2ea (patch)
treee34dbaf994f62530d19e88a1aa6fdeddc914f385 /serv_hash.c
parentacad1ac4b5df2808981e38a9436070693982a1ed (diff)
downloadiftop-c1f017231c5a6b5314804ea390512e336a53c2ea.zip
Added service hash for resolving port/protocol numbers to names.
Minor fix to handling of netmasks end /32.
Diffstat (limited to 'serv_hash.c')
-rw-r--r--serv_hash.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/serv_hash.c b/serv_hash.c
new file mode 100644
index 0000000..f32e4e9
--- /dev/null
+++ b/serv_hash.c
@@ -0,0 +1,66 @@
+/* hash table */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <netdb.h>
+#include "serv_hash.h"
+#include "hash.h"
+#include "iftop.h"
+
+// Deliberately not a power of 2 or 10
+#define hash_table_size 123
+
+int serv_hash_compare(void* a, void* b) {
+ ip_service* aa = (ip_service*)a;
+ ip_service* bb = (ip_service*)b;
+ return (aa->port == bb->port &&
+ aa->protocol == bb->protocol);
+}
+
+int serv_hash_hash(void* key) {
+ ip_service* serv = (ip_service*)key;
+ return serv->protocol;
+}
+
+void* serv_hash_copy_key(void* orig) {
+ ip_service* copy;
+ copy = xmalloc(sizeof *copy);
+ *copy = *(ip_service*)orig;
+ return copy;
+}
+
+void serv_hash_delete_key(void* key) {
+ free(key);
+}
+
+/*
+ * Allocate and return a hash
+ */
+hash_type* serv_hash_create() {
+ hash_type* hash_table;
+ hash_table = xcalloc(hash_table_size, sizeof *hash_table);
+ hash_table->size = hash_table_size;
+ hash_table->compare = &serv_hash_compare;
+ hash_table->hash = &serv_hash_hash;
+ hash_table->delete_key = &serv_hash_delete_key;
+ hash_table->copy_key = &serv_hash_copy_key;
+ hash_initialise(hash_table);
+ return hash_table;
+}
+
+void serv_hash_initialise(hash_type* sh) {
+ struct servent* ent;
+ struct protoent* pent;
+ ip_service* service;
+ setprotoent(1);
+ while((ent = getservent()) != NULL) {
+ pent = getprotobyname(ent->s_proto);
+ if(pent != NULL) {
+ service = xmalloc(sizeof(ip_service));
+ service->port = ntohs(ent->s_port);
+ service->protocol = pent->p_proto;
+ hash_insert(sh, service, xstrdup(ent->s_name));
+ }
+ }
+ endprotoent();
+}