diff options
author | pdw <> | 2002-03-24 16:22:26 +0000 |
---|---|---|
committer | pdw <> | 2002-03-24 16:22:26 +0000 |
commit | 3004ea063cbb186a63af99a2e13d7cb09f23360d (patch) | |
tree | d617af0ad0619334f899d3405350edb9b1b2407b /ns_hash.c | |
download | iftop-3004ea063cbb186a63af99a2e13d7cb09f23360d.zip |
iftop
Diffstat (limited to 'ns_hash.c')
-rw-r--r-- | ns_hash.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/ns_hash.c b/ns_hash.c new file mode 100644 index 0000000..bee461e --- /dev/null +++ b/ns_hash.c @@ -0,0 +1,64 @@ +/* hash table */ + +#include <stdio.h> +#include <stdlib.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include "ns_hash.h" +#include "hash.h" + +#define hash_table_size 256 + +int ns_hash_compare(void* a, void* b) { + struct in_addr* aa = (struct in_addr*)a; + struct in_addr* bb = (struct in_addr*)b; + return (aa->s_addr == bb->s_addr); +} + +int ns_hash_hash(void* key) { + int hash; + long addr; + + addr = (long)((struct in_addr*)key)->s_addr; + + hash = ((addr & 0x000000FF) + + (addr & 0x0000FF00 >> 8) + + (addr & 0x00FF0000 >> 16) + + (addr & 0xFF000000 >> 24)) % 0xFF; + + return hash; +} + +void* ns_hash_copy_key(void* orig) { + struct in_addr* copy = malloc(sizeof(struct in_addr)); + if(copy == NULL) { + printf("Out of memory\n"); + exit(1); + } + *copy = *(struct in_addr*)orig; + return copy; +} + +void ns_hash_delete_key(void* key) { + free(key); +} + +/* + * Allocate and return a hash + */ +hash_type* ns_hash_create() { + hash_type* hash_table; + if ((hash_table = calloc(hash_table_size, sizeof(hash_type*))) == 0) { + fprintf (stderr, "out of memory (hashTable)\n"); + exit(1); + } + hash_table->size = hash_table_size; + hash_table->compare = &ns_hash_compare; + hash_table->hash = &ns_hash_hash; + hash_table->delete_key = &ns_hash_delete_key; + hash_table->copy_key = &ns_hash_copy_key; + hash_initialise(hash_table); + return hash_table; +} + |