forked from RakshaBangera/dpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.c
224 lines (169 loc) · 5.44 KB
/
hash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include"types.h"
#define hsize 2048
unsigned int hkey;
int count;
NODE htable[hsize];
NODE search(unsigned long bigger_ip, unsigned long smaller_ip,
unsigned char ip_protocol, unsigned short bigger_port,
unsigned short smaller_port);
void init_hash()
{
int i;
/* Initialize the hash table */
for (i=0; i<hsize; i++) {
htable[i] = NULL;
}
}
NODE getnode()
{
NODE x;
x = (NODE) malloc (sizeof(struct node_));
return x;
}
unsigned int hash(NODE x)
{
unsigned int sum = 0;
unsigned int key = 0;
sum = x->bigger_ip + x->smaller_ip + x->ip_protocol + x->bigger_port +
x->smaller_port;
key = sum % hsize;
return key;
}
void insert(unsigned long bigger_ip, unsigned long smaller_ip,
unsigned char ip_protocol, unsigned short bigger_port,
unsigned short smaller_port, unsigned int syn,
unsigned int syn_ack, unsigned int ack,
unsigned int protocol_type)
{
NODE temp, cur;
temp = getnode();
if (temp == NULL) {
printf("Out of Memory\n");
} else {
temp->bigger_ip = bigger_ip;
temp->smaller_ip = smaller_ip;
temp->ip_protocol = ip_protocol;
temp->bigger_port = bigger_port;
temp->smaller_port = smaller_port;
temp->syn = syn;
temp->syn_ack = syn_ack;
temp->ack = ack;
temp-> protocol_type = protocol_type;
temp->next=NULL;
hkey = hash(temp);
if (htable[hkey] == NULL) {
htable[hkey] = temp;
} else {
cur = htable[hkey];
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = temp;
}
}
}
void delete(unsigned long bigger_ip, unsigned long smaller_ip,
unsigned char ip_protocol, unsigned short bigger_port,
unsigned short smaller_port)
{
NODE temp, prev, cur;
temp = getnode();
if (temp == NULL) {
printf("Out of memory\n");
} else {
temp->bigger_ip = bigger_ip;
temp->smaller_ip = smaller_ip;
temp->ip_protocol = ip_protocol;
temp->bigger_port = bigger_port;
temp->smaller_port = smaller_port;
hkey = hash(temp);
cur = htable[hkey];
prev = NULL;
if (cur == NULL) {
printf("The bucket is empty\n");
return;
}
if ((temp->bigger_ip == cur->bigger_ip) &&
(temp->smaller_ip == cur->smaller_ip) &&
(temp->ip_protocol == cur->ip_protocol) &&
(temp->bigger_port == cur->bigger_port) &&
(temp->smaller_port == cur->smaller_port)) {
htable[hkey] = cur->next;
free(cur);
return;
}
while (cur != NULL) {
if ((temp->bigger_ip == cur->bigger_ip) &&
(temp->smaller_ip == cur->smaller_ip) &&
(temp->ip_protocol == cur->ip_protocol) &&
(temp->bigger_port == cur->bigger_port) &&
(temp->smaller_port == cur->smaller_port)) {
break;
}
prev = cur;
cur = cur->next;
}
if (cur == NULL) {
printf("Specified tuple was not found\n");
} else {
prev->next = cur->next;
free(cur);
}
}
}
NODE search(unsigned long bigger_ip, unsigned long smaller_ip,
unsigned char ip_protocol, unsigned short bigger_port,
unsigned short smaller_port)
{
NODE temp,cur;
temp = getnode();
if (temp == NULL) {
printf("Out of memory\n");
return NULL;
} else {
temp->bigger_ip = bigger_ip;
temp->smaller_ip = smaller_ip;
temp->ip_protocol = ip_protocol;
temp->bigger_port = bigger_port;
temp->smaller_port = smaller_port;
hkey = hash(temp);
cur = htable[hkey];
while (cur != NULL) {
if ((temp->bigger_ip == cur->bigger_ip)&&
(temp->smaller_ip == cur->smaller_ip)&&
(temp->ip_protocol == cur->ip_protocol)&&
(temp->bigger_port == cur->bigger_port)&&
(temp->smaller_port == cur->smaller_port)) {
return cur;
} else {
cur = cur->next;
}
}
return NULL;
}
}
void display()
{
NODE temp;
for (hkey = 0; hkey < hsize; hkey++) {
temp = htable[hkey];
if (htable[hkey] != NULL) {
printf("\nhtable[ %d ]\n",hkey);
}
while(temp != NULL) {
printf("\nBigger ip = %x\n",temp->bigger_ip);
printf("Smaller ip = %x\n",temp->smaller_ip);
printf("IP protocol = %x\n",temp->ip_protocol);
printf("Bigger port = %hu\n",temp->bigger_port);
printf("Smaller port = %hu\n",temp->smaller_port);
printf("SYN = %d\n", temp->syn);
printf("SYN-ACK = %d\n", temp->syn_ack);
printf("ACK = %d\n", temp->ack);
printf("Protocol type=%d\n",temp->protocol_type);
getchar();
temp = temp->next;
}
}
}