10 // Deliberately not a power of 2 or 10
11 #define hash_table_size 123
13 int serv_hash_compare(void* a, void* b) {
14 ip_service* aa = (ip_service*)a;
15 ip_service* bb = (ip_service*)b;
16 return (aa->port == bb->port &&
17 aa->protocol == bb->protocol);
20 int serv_hash_hash(void* key) {
21 ip_service* serv = (ip_service*)key;
22 return serv->protocol % hash_table_size;
25 void* serv_hash_copy_key(void* orig) {
27 copy = xmalloc(sizeof *copy);
28 *copy = *(ip_service*)orig;
32 void serv_hash_delete_key(void* key) {
37 * Allocate and return a hash
39 hash_type* serv_hash_create() {
40 hash_type* hash_table;
41 hash_table = xcalloc(hash_table_size, sizeof *hash_table);
42 hash_table->size = hash_table_size;
43 hash_table->compare = &serv_hash_compare;
44 hash_table->hash = &serv_hash_hash;
45 hash_table->delete_key = &serv_hash_delete_key;
46 hash_table->copy_key = &serv_hash_copy_key;
47 hash_initialise(hash_table);
51 void serv_hash_initialise(hash_type* sh) {
53 struct protoent* pent;
56 while((ent = getservent()) != NULL) {
57 pent = getprotobyname(ent->s_proto);
59 service = xmalloc(sizeof(ip_service));
60 service->port = ntohs(ent->s_port);
61 service->protocol = pent->p_proto;
62 hash_insert(sh, service, xstrdup(ent->s_name));