]> gitweb.fperrin.net Git - iftop.git/blob - counter_hash.c
Import iftop-1.0pre4
[iftop.git] / counter_hash.c
1 /* hash table */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <netinet/in.h>
6 #include "counter_hash.h"
7 #include "hash.h"
8 #include "iftop.h"
9
10 #define hash_table_size 256
11
12 int counter_hash_compare(void* a, void* b) {
13     return *(long*)a == *(long*)b;
14 }
15
16 int counter_hash_hash(void* key) {
17     int hash;
18     long addr;
19         
20     addr = *(long*)key;
21
22     hash = ((addr & 0x000000FF)
23             + (addr & 0x0000FF00 >> 8)
24             + (addr & 0x00FF0000 >> 16)
25             + (addr & 0xFF000000 >> 24)
26            ) % 0xFF;
27
28     return hash;
29 }
30
31 void* counter_hash_copy_key(void* orig) {
32     struct in_addr* copy;
33     copy = xmalloc(sizeof *copy);
34     *copy = *(struct in_addr*)orig;
35     return copy;
36 }
37
38 void counter_hash_delete_key(void* key) {
39     free(key);
40 }
41
42 /*
43  * Allocate and return a hash
44  */
45 hash_type* counter_hash_create() {
46     hash_type* hash_table;
47     hash_table = xcalloc(hash_table_size, sizeof *hash_table);
48     hash_table->size = hash_table_size;
49     hash_table->compare = &counter_hash_compare;
50     hash_table->hash = &counter_hash_hash;
51     hash_table->delete_key = &counter_hash_delete_key;
52     hash_table->copy_key = &counter_hash_copy_key;
53     hash_initialise(hash_table);
54     return hash_table;
55 }
56