Annotation of embedaddon/iftop/serv_hash.c, revision 1.1

1.1     ! misho       1: /* hash table */
        !             2: 
        !             3: #include <stdio.h>
        !             4: #include <stdlib.h>
        !             5: #include <netdb.h>
        !             6: #include "serv_hash.h"
        !             7: #include "hash.h"
        !             8: #include "iftop.h"
        !             9: 
        !            10: // Deliberately not a power of 2 or 10
        !            11: #define hash_table_size 123
        !            12: 
        !            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);
        !            18: }
        !            19: 
        !            20: int serv_hash_hash(void* key) {
        !            21:     ip_service* serv = (ip_service*)key;
        !            22:     return serv->protocol % hash_table_size;
        !            23: }
        !            24: 
        !            25: void* serv_hash_copy_key(void* orig) {
        !            26:     ip_service* copy;
        !            27:     copy = xmalloc(sizeof *copy);
        !            28:     *copy = *(ip_service*)orig;
        !            29:     return copy;
        !            30: }
        !            31: 
        !            32: void serv_hash_delete_key(void* key) {
        !            33:     free(key);
        !            34: }
        !            35: 
        !            36: /*
        !            37:  * Allocate and return a hash
        !            38:  */
        !            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);
        !            48:     return hash_table;
        !            49: }
        !            50: 
        !            51: void serv_hash_initialise(hash_type* sh) {
        !            52:   struct servent* ent;
        !            53:   struct protoent* pent;
        !            54:   ip_service* service;
        !            55:   setprotoent(1);
        !            56:   while((ent = getservent()) != NULL) {
        !            57:     pent = getprotobyname(ent->s_proto);
        !            58:     if(pent != NULL) {
        !            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));
        !            63:     }
        !            64:   }
        !            65:   endprotoent();
        !            66: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>