diff options
Diffstat (limited to 'serv_hash.c')
-rw-r--r-- | serv_hash.c | 66 |
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(); +} |