forked from seclab-ucr/INTANG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmycache.c
More file actions
516 lines (467 loc) · 15.1 KB
/
mycache.c
File metadata and controls
516 lines (467 loc) · 15.1 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Cache with validity period
* There're two kinds of cache. One is implemented with hash table,
* and the other is with array.
*/
#include "mycache.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "helper.h"
#include "protocol.h"
#include "logging.h"
static inline int is_expired(time_t ts)
{
return ts + DEFAULT_VALIDITY_PERIOD < time(NULL);
}
/*
* Array cache
*/
void array_cache_init(struct array_cache *ac, unsigned int size, int validity)
{
ac->size = size;
ac->cache = (struct cache_entry*)malloc(sizeof(struct cache_entry) * size);
ac->idx = 0;
ac->validity = validity;
}
// put the data into the oldest slot.
int array_cache_set(struct array_cache *ac, unsigned int key, void *value)
{
time_t now = time(NULL);
unsigned int i;
if (ac->cache[ac->idx].ts + ac->validity < now) {
if (ac->cache[ac->idx].value != NULL) {
free(ac->cache[ac->idx].value);
}
ac->cache[ac->idx].key = key;
ac->cache[ac->idx].value = value;
ac->cache[ac->idx].ts = now;
ac->idx = (ac->idx + 1) % ac->size;
}
else {
log_error("CACHE IS FULL!!!");
return -1;
}
return 0;
}
// get the most recent data with the key
void *array_cache_get(struct array_cache *ac, unsigned int key)
{
time_t now = time(NULL);
unsigned int i = (ac->idx - 1) % ac->size;
while (i != ac->idx) {
if (ac->cache[i].ts + ac->validity < now)
break;
if (ac->cache[i].key == key) {
return ac->cache[i].value;
}
i = (i - 1) % ac->size;
}
log_debug("array_cache_get: key not found!");
return NULL;
}
void array_cache_summary(struct array_cache *ac)
{
time_t now = time(NULL);
unsigned int valid_num = 0;
unsigned int i = (ac->idx - 1) % ac->size;
while (i != ac->idx) {
if (ac->cache[i].ts + ac->validity < now) {
break;
}
valid_num++;
}
printf("Idx: %u\n", ac->idx);
printf("Utilization rate: %u/%u(%f)\n", valid_num, ac->size, ((float)valid_num)/ac->size);
printf("Validity period: %d\n", ac->validity);
}
void array_cache_dump(struct array_cache *ac)
{
unsigned int i;
for (i = 0; i < ac->size; i++) {
printf("%d. %u\t%p\t%s", i, ac->cache[i].key, ac->cache[i].value, ctime(&ac->cache[i].ts));
}
}
/*
* Hash table cache (deprecated)
*/
int icache_get(struct fourtuple *key, int *value)
{
//log_debug("%u, %u, %u, %u", key->sip, key->dip, key->sport, key->dport);
unsigned int hash_val = make_hash(key);
unsigned int index = hash_val % HASH_TBL_SIZE;
//log_debug("index: %d", index);
if (rst_hashtbl[index]) {
struct inode *cur, *last = NULL;
for (cur = rst_hashtbl[index]; cur != NULL; cur = cur->next) {
if (cur->key->sip == key->sip &&
cur->key->dip == key->dip &&
cur->key->sport == key->sport &&
cur->key->dport == key->dport) {
if (cur->ts + RST_VALIDITY_PERIOD < time(NULL)) {
// expired
log_debug("Cache expired.");
if (last == NULL)
rst_hashtbl[index] = NULL;
else
last->next = cur->next;
free(cur->key);
free(cur);
return ENTRY_NOT_FOUND;
}
*value = cur->value;
return ENTRY_FOUND;
}
last = cur;
}
}
return ENTRY_NOT_FOUND;
}
int icache_set(struct fourtuple *key, int value)
{
unsigned int hash_val = make_hash(key);
unsigned int index = hash_val % HASH_TBL_SIZE;
if (rst_hashtbl[index]) {
struct inode *cur, *last;
for (cur = rst_hashtbl[index]; cur != NULL; cur = cur->next) {
if (cur->key->sip == key->sip &&
cur->key->dip == key->dip &&
cur->key->sport == key->sport &&
cur->key->dport == key->dport) {
cur->value = value;
cur->ts = time(NULL);
printf("set value to %d\n", value);
return ENTRY_EXISTS;
}
last = cur;
}
// append to the linked list, don't need to consider the order for now
struct inode *new_node = (struct inode*)malloc(sizeof(struct inode));
new_node->key->sip = key->sip;
new_node->key->dip = key->dip;
new_node->key->sport = key->sport;
new_node->key->dport = key->dport;
new_node->value = value;
new_node->ts = time(NULL);
new_node->next = NULL;
last->next = new_node;
printf("inserted a new node\n");
return ENTRY_NOT_EXIST;
}
// not found, create one
struct inode *new_node = (struct inode*)malloc(sizeof(struct inode));
struct fourtuple *new_key = (struct fourtuple*)malloc(sizeof(struct fourtuple));
new_key->sip = key->sip;
new_key->dip = key->dip;
new_key->sport = key->sport;
new_key->dport = key->dport;
new_node->key = new_key;
new_node->value = value;
new_node->ts = time(NULL);
new_node->next = NULL;
rst_hashtbl[index] = new_node;
printf("inserted a new node\n");
return ENTRY_NOT_EXIST;
}
int icache_clear(struct fourtuple *key)
{
unsigned int hash_val = make_hash(key);
unsigned int index = hash_val % HASH_TBL_SIZE;
if (rst_hashtbl[index]) {
struct inode *cur, *last = NULL;
for (cur = rst_hashtbl[index]; cur != NULL; cur = cur->next) {
if (cur->key->sip == key->sip &&
cur->key->dip == key->dip &&
cur->key->sport == key->sport &&
cur->key->dport == key->dport) {
if (last == NULL)
rst_hashtbl[index] = NULL;
else
last->next = cur->next;
free(cur->key);
free(cur);
return ENTRY_FOUND;
}
last = cur;
}
}
return ENTRY_NOT_FOUND;
}
// An awful copy from icache, should have a better way to manage string cache in memeory
int scache_get(struct fourtuple *key, char **value)
{
unsigned int hash_val = make_hash(key);
unsigned int index = hash_val % HASH_TBL_SIZE;
if (httpreq_hashtbl[index]) {
struct snode *cur, *last = NULL;
for (cur = httpreq_hashtbl[index]; cur != NULL; cur = cur->next) {
if (cur->key->sip == key->sip &&
cur->key->dip == key->dip &&
cur->key->sport == key->sport &&
cur->key->dport == key->dport) {
if (cur->ts + HTTPREQ_VALIDITY_PERIOD < time(NULL)) {
// expired
if (last == NULL)
httpreq_hashtbl[index] = NULL;
else
last->next = cur->next;
free(cur->value);
free(cur->key);
free(cur);
return ENTRY_NOT_FOUND;
}
*value = cur->value;
return ENTRY_FOUND;
}
last = cur;
}
}
return ENTRY_NOT_FOUND;
}
int scache_set(struct fourtuple *key, char *value)
{
unsigned int hash_val = make_hash(key);
unsigned int index = hash_val % HASH_TBL_SIZE;
if (httpreq_hashtbl[index]) {
struct snode *cur, *last;
for (cur = httpreq_hashtbl[index]; cur != NULL; cur = cur->next) {
if (cur->key->sip == key->sip &&
cur->key->dip == key->dip &&
cur->key->sport == key->sport &&
cur->key->dport == key->dport) {
cur->value[0] = 0;
strncat(cur->value, value, MAX_REQLINE_LEN);
cur->ts = time(NULL);
printf("set value to %s\n", value);
return ENTRY_EXISTS;
}
last = cur;
}
// append to the linked list, don't need to consider the order for now
struct snode *new_node = (struct snode*)malloc(sizeof(struct snode));
struct fourtuple *new_key = (struct fourtuple*)malloc(sizeof(struct fourtuple));
char *new_value = (char*)malloc(MAX_REQLINE_LEN+1);
new_key->sip = key->sip;
new_key->dip = key->dip;
new_key->sport = key->sport;
new_key->dport = key->dport;
new_value[0] = 0;
strncat(new_value, value, MAX_REQLINE_LEN);
new_node->key = new_key;
new_node->value = new_value;
new_node->ts = time(NULL);
new_node->next = NULL;
last->next = new_node;
printf("inserted a new node\n");
return ENTRY_NOT_EXIST;
}
// not found, create one
struct snode *new_node = (struct snode*)malloc(sizeof(struct snode));
struct fourtuple *new_key = (struct fourtuple*)malloc(sizeof(struct fourtuple));
char *new_value = (char*)malloc(MAX_REQLINE_LEN+1);
new_key->sip = key->sip;
new_key->dip = key->dip;
new_key->sport = key->sport;
new_key->dport = key->dport;
new_value[0] = 0;
strncat(new_value, value, MAX_REQLINE_LEN);
new_node->key = new_key;
new_node->value = new_value;
new_node->ts = time(NULL);
new_node->next = NULL;
httpreq_hashtbl[index] = new_node;
printf("inserted a new node\n");
return ENTRY_NOT_EXIST;
}
int scache_clear(struct fourtuple *key)
{
unsigned int hash_val = make_hash(key);
unsigned int index = hash_val % HASH_TBL_SIZE;
if (httpreq_hashtbl[index]) {
struct snode *cur, *last = NULL;
for (cur = httpreq_hashtbl[index]; cur != NULL; cur = cur->next) {
if (cur->key->sip == key->sip &&
cur->key->dip == key->dip &&
cur->key->sport == key->sport &&
cur->key->dport == key->dport) {
if (last == NULL)
httpreq_hashtbl[index] = NULL;
else
last->next = cur->next;
free(cur->key);
free(cur->value);
free(cur);
return ENTRY_FOUND;
}
last = cur;
}
}
return ENTRY_NOT_FOUND;
}
// Debug functions
void dump_icache()
{
int i;
char buffer[64];
printf("---------------------------------------------------------------------------\n");
for (i=0; i<HASH_TBL_SIZE; i++) {
if (rst_hashtbl[i]) {
struct inode *cur;
for (cur=rst_hashtbl[i]; cur!=NULL; cur=cur->next) {
if (cur == rst_hashtbl[i])
printf("| %5u |", i);
else
printf("| |");
snprintf(buffer, 64, " <%u, %u, %u, %u>, %d, [%u] ",
rst_hashtbl[i]->key->sip,
rst_hashtbl[i]->key->dip,
rst_hashtbl[i]->key->sport,
rst_hashtbl[i]->key->dport,
rst_hashtbl[i]->value,
(unsigned int)rst_hashtbl[i]->ts);
printf("%-64s |\n", buffer);
}
}
}
printf("---------------------------------------------------------------------------\n");
}
void summary_icache()
{
int i;
int count = 0;
int max = 0;
for (i=0; i<HASH_TBL_SIZE; i++) {
if (rst_hashtbl[i]) {
count++;
int x = 0;
struct inode *cur;
for (cur=rst_hashtbl[i]; cur!=NULL; cur=cur->next)
x++;
if (x > max)
max = x;
}
}
printf("Utilization rate: %f\n", ((float)count)/HASH_TBL_SIZE);
printf("Max length: %d\n", max);
}
void dump_scache()
{
int i;
char buffer[1000];
printf("---------------------------------------------------------------------------\n");
for (i=0; i<HASH_TBL_SIZE; i++) {
if (httpreq_hashtbl[i]) {
struct snode *cur;
for (cur=httpreq_hashtbl[i]; cur!=NULL; cur=cur->next) {
if (cur == httpreq_hashtbl[i])
printf("| %5u |", i);
else
printf("| |");
snprintf(buffer, 1000, " <%u, %u, %u, %u>, %s, [%u] ",
httpreq_hashtbl[i]->key->sip,
httpreq_hashtbl[i]->key->dip,
httpreq_hashtbl[i]->key->sport,
httpreq_hashtbl[i]->key->dport,
httpreq_hashtbl[i]->value,
(unsigned int)httpreq_hashtbl[i]->ts);
printf("%-80s |\n", buffer);
}
}
}
printf("---------------------------------------------------------------------------\n");
}
void summary_scache()
{
int i;
int count = 0;
int max = 0;
for (i=0; i<HASH_TBL_SIZE; i++) {
if (httpreq_hashtbl[i]) {
count++;
int x = 0;
struct snode *cur;
for (cur=httpreq_hashtbl[i]; cur!=NULL; cur=cur->next)
x++;
if (x > max)
max = x;
}
}
printf("Utilization rate: %f\n", ((float)count)/HASH_TBL_SIZE);
printf("Max length: %d\n", max);
}
/*
* RST cache
* records received RST packets in last a few seconds
*/
static struct array_cache rst_cache;
void rst_cache_init()
{
array_cache_init(&rst_cache, 100, 3);
}
int get_rst_count(struct fourtuple *fourtp)
{
unsigned int key = make_hash(fourtp);
int *cnt;
if ((cnt = (int*)array_cache_get(&rst_cache, key)) == NULL)
return 0;
else
return *cnt;
}
int set_rst_count(struct fourtuple *fourtp, int cnt)
{
unsigned int key = make_hash(fourtp);
int *v = (int*)malloc(sizeof(int));
*v = cnt;
return array_cache_set(&rst_cache, key, v);
}
/*
* HTTP cache
* records HTTP requests sent in last a few seconds
*/
static struct array_cache http_cache;
void http_cache_init()
{
array_cache_init(&http_cache, 5000, 5);
}
char* get_last_http_request(struct fourtuple *fourtp)
{
unsigned int key = make_hash(fourtp);
return (char*)array_cache_get(&http_cache, key);
}
int set_last_http_request(struct fourtuple *fourtp, char *httpreq)
{
unsigned int key = make_hash(fourtp);
char *h = (char*)malloc(1024);
h[0] = 0;
strncat(h, httpreq, 1024);
return array_cache_set(&http_cache, key, h);
}
/*
* DNS cache
* records DNS requests sent over TCP in last a few seconds
*/
static struct array_cache dns_cache;
void dns_cache_init()
{
array_cache_init(&dns_cache, 1000, 10);
}
struct fourtuple* dns_cache_get(unsigned int key)
{
return (struct fourtuple*)array_cache_get(&dns_cache, key);
}
int dns_cache_set(unsigned int key, struct fourtuple *fourtp)
{
struct fourtuple *f = (struct fourtuple*)malloc(sizeof(struct fourtuple));
f->sip = fourtp->sip;
f->dip = fourtp->dip;
f->sport = fourtp->sport;
f->dport = fourtp->dport;
return array_cache_set(&dns_cache, key, f);
}
int init_cache()
{
rst_cache_init();
http_cache_init();
dns_cache_init();
}