Annotation of embedaddon/iftop/ns_hash.c, revision 1.1.1.2

1.1       misho       1: /* hash table */
                      2: 
                      3: #include <stdio.h>
                      4: #include <stdlib.h>
1.1.1.2 ! misho       5: #include <string.h>
1.1       misho       6: #include <sys/types.h>
                      7: #include <sys/socket.h>
                      8: #include <netinet/in_systm.h>
                      9: #include <netinet/in.h>
                     10: #include <arpa/inet.h>
                     11: #include "ns_hash.h"
                     12: #include "hash.h"
                     13: #include "iftop.h"
                     14: 
                     15: #define hash_table_size 256
                     16: 
                     17: int ns_hash_compare(void* a, void* b) {
1.1.1.2 ! misho      18:     struct in6_addr* aa = (struct in6_addr*)a;
        !            19:     struct in6_addr* bb = (struct in6_addr*)b;
        !            20:     return IN6_ARE_ADDR_EQUAL(aa, bb);
        !            21: }
        !            22: 
        !            23: static int __inline__ hash_uint32(uint32_t n) {
        !            24:     return ((n & 0x000000FF)
        !            25:             + ((n & 0x0000FF00) >> 8)
        !            26:             + ((n & 0x00FF0000) >> 16)
        !            27:             + ((n & 0xFF000000) >> 24));
1.1       misho      28: }
                     29: 
                     30: int ns_hash_hash(void* key) {
                     31:     int hash;
1.1.1.2 ! misho      32:     uint32_t* addr6 = (uint32_t*)((struct in6_addr *) key)->s6_addr;
        !            33: 
        !            34:     hash = ( hash_uint32(addr6[0])
        !            35:             + hash_uint32(addr6[1])
        !            36:             + hash_uint32(addr6[2])
        !            37:             + hash_uint32(addr6[3])) % 0xFF;
1.1       misho      38: 
                     39:     return hash;
                     40: }
                     41: 
                     42: void* ns_hash_copy_key(void* orig) {
1.1.1.2 ! misho      43:     struct in6_addr* copy;
        !            44: 
1.1       misho      45:     copy = xmalloc(sizeof *copy);
1.1.1.2 ! misho      46:     memcpy(copy, orig, sizeof *copy);
        !            47: 
1.1       misho      48:     return copy;
                     49: }
                     50: 
                     51: void ns_hash_delete_key(void* key) {
                     52:     free(key);
                     53: }
                     54: 
                     55: /*
                     56:  * Allocate and return a hash
                     57:  */
                     58: hash_type* ns_hash_create() {
                     59:     hash_type* hash_table;
                     60:     hash_table = xcalloc(hash_table_size, sizeof *hash_table);
                     61:     hash_table->size = hash_table_size;
                     62:     hash_table->compare = &ns_hash_compare;
                     63:     hash_table->hash = &ns_hash_hash;
                     64:     hash_table->delete_key = &ns_hash_delete_key;
                     65:     hash_table->copy_key = &ns_hash_copy_key;
                     66:     hash_initialise(hash_table);
                     67:     return hash_table;
                     68: }
                     69: 

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