-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashtable.c
More file actions
237 lines (220 loc) · 7.03 KB
/
hashtable.c
File metadata and controls
237 lines (220 loc) · 7.03 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "UDPProxy.h"
#define INIT_TBL_SZ 997
char addr_buff[INET_ADDRSTRLEN];
/* convert 4-tuple connection to tuple_t struct
assumes long is 8 bytes, short 2 */
void addr_to_tuple(struct sockaddr_in *src, tuple_t *res) {
res->src_ip = src->sin_addr.s_addr;
res->src_port = src->sin_port;
// res->dst_ip = dst->sin_addr.s_addr;
// res->dst_port = dst->sin_port;
}
/* returns 1 if a and b represent the same connection (in either direction)
* a is usually being compared with b in the hashtable */
int compare_tuple(tuple_t *a, tuple_t *b) {
// printf("comparing tuples.a->srcip:%lu a->src_port->%d b->src_ip:%lu b->src_port:%d\n", a->src_ip, a->src_port, b->src_ip, b->src_port);
if ((a->src_ip == b->src_ip) && (a->src_port == b->src_port)) {
return 1;
}
return 0;
}
/* copy tuple_t from src to dst */
void copy_tuple(tuple_t *src, tuple_t *dst) {
dst->src_ip = src->src_ip;
dst->src_port = src->src_port;
}
/* print an entry_t struct */
void print_entry(entry_t *e) {
tuple_t *t = e->key;
printf("Entry: Tuple: src: %lu:%u\n", t->src_ip, ntohs(t->src_port));
inet_ntop(AF_INET, &t->src_ip, addr_buff, INET_ADDRSTRLEN);
printf("src_ip string: %s\n", addr_buff);
//TODO: ADD OTHER ENTRY INFO, INCLUDING ORIGINAL SOURCE, DST ADDR
}
/* print a hashtable */
void print_table(hashtable_t *ht) {
printf("****HASHTABLE*****\n");
int i;
for (i = 0; i < ht->capacity; i++) {
if (ht->table[i]) {
printf("Entry at [%d] ", i);
print_entry(ht->table[i]);
}
}
printf("****END****\n");
}
/* create a new key/value pair */
entry_t *new_entry(tuple_t *key, struct sockaddr_in *orig_src, \
struct sockaddr_in *orig_dst, int sock, int rate) {
entry_t *new_entry = malloc(sizeof(entry_t));
new_entry->key = malloc(sizeof(tuple_t));
copy_tuple(key, new_entry->key);
new_entry->last_use = time(NULL); //start timer
new_entry->rate = (rate > 0) ? rate : DEFAULT_RATE;
new_entry->rate = ntohs(orig_dst->sin_port) - 2000;
new_entry->s_ctr = TOKEN_MAX/2;
new_entry->d_ctr = TOKEN_MAX/2;
new_entry->orig_src.sin_family = AF_INET;
new_entry->orig_src.sin_port = orig_src->sin_port;
new_entry->orig_src.sin_addr.s_addr = orig_src->sin_addr.s_addr;
new_entry->orig_dst.sin_family = AF_INET;
new_entry->orig_dst.sin_port = orig_dst->sin_port;
new_entry->orig_dst.sin_addr.s_addr = orig_dst->sin_addr.s_addr;
new_entry->sock = sock;
return new_entry;
}
/* destroy a key/value pair. */
void destroy_entry(entry_t *e) {
free(e->key);
close(e->sock);
free(e);
e = NULL; /* clear table entry */
}
/* create a new hashtable */
hashtable_t *new_ht() {
hashtable_t *ht = malloc(sizeof(hashtable_t));
ht->table = calloc(INIT_TBL_SZ, sizeof(entry_t *));
ht->size = 0;
ht->capacity = INIT_TBL_SZ;
return ht;
}
/* destroy a hashtable */
void destroy_ht(hashtable_t *ht) {
int i;
for (i = 0; i < ht->capacity; i++) {
if (ht->table[i]) {
destroy_entry(ht->table[i]);
}
}
free(ht->table);
free(ht);
}
/* simple hash function inspired by
* http://stackoverflow.com/questions/14409466/simple-hash-functions
* modified to use tuple_t instead of string */
uint hash(void *tuple_key, hashtable_t *ht) {
uint8_t *key = (uint8_t *)tuple_key;
int i;
uint hash = 0;
for (i = 0; i < TUPLE_SZ; i++) {
hash = key[i] + (hash << 6) + (hash << 16) - hash;
}
hash = hash % ht->capacity;
return hash;
}
/* check if key, value is in table. Returns index on success. Returns -1
* if not found */
int contains(tuple_t *key, hashtable_t *ht) {
int hash_val = hash(key, ht);
//not found
if (!(ht->table[hash_val])) {
return -1;
} else { //entry is occupied
//match
if (compare_tuple(key, ht->table[hash_val]->key)) {
return hash_val;
}
//no match - check following entries
int entry, i = 0;
while(++i < ht->size) {
entry = ((hash_val + i) % ht->capacity);
//found an unoccupied index
if (!ht->table[entry]) {
return -1;
}
//match
else if (compare_tuple(key, ht->table[entry]->key)) {
return entry;
}
}
}
return -1;
}
/* retrieve an entry from a hashtable. Returns entry on success, -1 on failure */
entry_t *get(tuple_t *key, hashtable_t *ht) {
int index = contains(key, ht);
//there is something at the hash index
if (index) {
//match
if (compare_tuple(key, ht->table[index]->key)) {
return ht->table[index];
}
//no match - check following entries
index = hash(key, ht);
int entry, i = 0;
while(++i < ht->size) {
entry = (index + i) % ht->capacity;
//found an unoccupied index
if (!ht->table[entry]) {
return (entry_t *)-1;
}
//match
if (compare_tuple(key, ht->table[entry]->key)) {
return ht->table[entry];
}
}
}
//nothing at the hash index
return (entry_t *)-1;
}
/* remove an entry from the hashtable */
void remove_entry(tuple_t *key, hashtable_t *ht) {
int index = contains(key, ht);
if (index != -1) {
destroy_entry(ht->table[index]);
ht->table[index] = 0;
}
}
/* add an entry to the hash table. Returns the index of the entry
* if an entry already exists, returns the index */
int add(tuple_t *key, struct sockaddr_in *orig_src, \
struct sockaddr_in *orig_dst, int sock, int rate, hashtable_t *ht) {
int hash_val = hash(key, ht);
int idx = hash_val;
entry_t *entry = new_entry(key, orig_src, orig_dst, sock, rate);
print_entry(entry);
//location is occupied
if (ht->table[hash_val]) {
int i;
for(i = 1; i < ht->capacity - 1; i++) {
idx = (hash_val + i) % ht->capacity;
//found an unoccupied container
if(!ht->table[idx]) {
hash_val = idx;
break;
}
//entry is already present
else if (compare_tuple(key, ht->table[idx]->key)) {
destroy_entry(entry);
return idx;
}
}
}
ht->table[hash_val] = entry;
ht->size++;
return idx;
}
/*
int main(int argc, char **argv) {
hashtable_t *ht = new_ht();
struct sockaddr_in addr;
memset((char *) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(4444);
addr.sin_addr.s_addr = htonl(2130706433);
tuple_t *key = calloc(1, sizeof(tuple_t));
addr_to_tuple(&addr, &addr, key);
add(key, 1, ht);
print_table(ht);
memset((char *) &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(4443);
addr.sin_addr.s_addr = htonl(2130706433);
addr_to_tuple(&addr, &addr, key);
add(key, 2, ht);
print_table(ht);
remove_entry(key, ht);
print_table(ht);
free(key);
destroy_ht(ht);
} */