1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <netinet/in.h>
5 #include "config.h"
6 #include "smurflog.h"
7 #include "hash.h"
8 
9 extern struct hash hashtable[256];
10 
hashfunc(const char * ip)11 inline char hashfunc(const char *ip)
12 {
13    return (ip[0] ^ ip[1] ^ ip[2]);
14 }
15 
clear_collision(struct hash * hptr)16 void clear_collision(struct hash *hptr)
17 {
18    if (hptr->next != NULL)
19       clear_collision(hptr->next);
20    free(hptr);
21 }
22 
clear_hash(void)23 void clear_hash(void)
24 {
25    int i;
26 
27    for(i=0;i<256;++i)
28       if (hashtable[i].next != NULL)
29          clear_collision(hashtable[i].next);
30 
31    memset(hashtable, 0, sizeof(hashtable));
32 }
33 
unnamed(u_long ip)34 inline int unnamed(u_long ip)
35 {
36    u_char hashval;
37    struct hash *hptr;
38 
39    hashval = hashfunc((u_char *) &ip);
40 
41    if (hashtable[hashval].network == 0) {
42       hashtable[hashval].network = ip;
43       return 1;
44    }
45    else {
46       if (hashtable[hashval].network == ip)
47          return 0;
48       else {
49          hptr = hashtable+hashval;
50          while(hptr->next != NULL) {
51             if (hptr->network == ip)
52                return 0;
53             hptr = hptr->next;
54          }
55 
56          if ((hptr->next = malloc(sizeof(struct hash))) == NULL)
57             return 0;
58          hptr->next->network = ip;
59          hptr->next->next = NULL;
60       }
61    }
62 
63    return 1;
64 }
65