summaryrefslogtreecommitdiff
path: root/serv_hash.c
diff options
context:
space:
mode:
authorGraydon, Tracy <tracy.graydon@intel.com>2013-01-31 17:53:28 -0800
committerGraydon, Tracy <tracy.graydon@intel.com>2013-01-31 17:53:28 -0800
commit2f9aed644fc28f5bece6ab1711a9fca7362aeceb (patch)
treeccd6c62f1adf3255ed0b2095feab505e4611374e /serv_hash.c
downloadiftop-accepted/2.0alpha/20130201.020536.tar.gz
iftop-accepted/2.0alpha/20130201.020536.tar.bz2
iftop-accepted/2.0alpha/20130201.020536.zip
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..279032e
--- /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 % hash_table_size;
+}
+
+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();
+}