summaryrefslogtreecommitdiff
path: root/serv_hash.c
blob: 279032e8be1844671332a5d6fe77cecc9305522c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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();
}