Annotation of embedaddon/iftop/addr_hash.c, revision 1.1.1.1
1.1 misho 1: /* hash table */
2:
3: #include <stdio.h>
4: #include <stdlib.h>
5: #include "addr_hash.h"
6: #include "hash.h"
7: #include "iftop.h"
8:
9: #define hash_table_size 256
10:
11: int compare(void* a, void* b) {
12: addr_pair* aa = (addr_pair*)a;
13: addr_pair* bb = (addr_pair*)b;
14: return (aa->src.s_addr == bb->src.s_addr
15: && aa->src_port == bb->src_port
16: && aa->dst.s_addr == bb->dst.s_addr
17: && aa->dst_port == bb->dst_port
18: && aa->protocol == bb->protocol);
19: }
20:
21: int hash(void* key) {
22: int hash;
23: long addr;
24: addr_pair* ap = (addr_pair*)key;
25:
26: addr = (long)ap->src.s_addr;
27:
28: hash = ((addr & 0x000000FF)
29: + (addr & 0x0000FF00 >> 8)
30: + (addr & 0x00FF0000 >> 16)
31: + (addr & 0xFF000000 >> 24)
32: + ap->src_port) % 0xFF;
33:
34: addr = (long)ap->dst.s_addr;
35: hash = ( hash + (addr & 0x000000FF)
36: + (addr & 0x0000FF00 >> 8)
37: + (addr & 0x00FF0000 >> 16)
38: + (addr & 0xFF000000 >> 24)
39: + ap->dst_port) % 0xFF;
40:
41: return hash;
42: }
43:
44: void* copy_key(void* orig) {
45: addr_pair* copy;
46: copy = xmalloc(sizeof *copy);
47: *copy = *(addr_pair*)orig;
48: return copy;
49: }
50:
51: void delete_key(void* key) {
52: free(key);
53: }
54:
55: /*
56: * Allocate and return a hash
57: */
58: hash_type* addr_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 = &compare;
63: hash_table->hash = &hash;
64: hash_table->delete_key = &delete_key;
65: hash_table->copy_key = ©_key;
66: hash_initialise(hash_table);
67: return hash_table;
68: }
69:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>