File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / iftop / ns_hash.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue Oct 18 14:04:50 2016 UTC (7 years, 8 months ago) by misho
Branches: iftop, MAIN
CVS tags: v1_0rc4, HEAD
iftop 1.0pre4

    1: /* hash table */
    2: 
    3: #include <stdio.h>
    4: #include <stdlib.h>
    5: #include <string.h>
    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) {
   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));
   28: }
   29: 
   30: int ns_hash_hash(void* key) {
   31:     int hash;
   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;
   38: 
   39:     return hash;
   40: }
   41: 
   42: void* ns_hash_copy_key(void* orig) {
   43:     struct in6_addr* copy;
   44: 
   45:     copy = xmalloc(sizeof *copy);
   46:     memcpy(copy, orig, sizeof *copy);
   47: 
   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>