-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.c
135 lines (109 loc) · 3.25 KB
/
cache.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
#include <time.h>
#include <limits.h>
#include "extypes.h"
#include "smartalloc.h"
#include "cache.h"
#include "dns.h"
extern ht_entry *cache;
void add_record(hash_key cache_key, rr_t rr) {
ht_entry *cache_entry;
record_t *curr_record;
record_t *new_record;
bool record_exists = RECORD_DNE;
cache_entry = find_entry(cache_key);
if (cache_entry == NULL) {
add_entry(cache_key);
cache_entry = find_entry(cache_key);
}
if (cache_entry->record == NULL)
cache_entry->record = record_alloc();
curr_record = cache_entry->record;
do {
if (rr_cmp(curr_record->rr, rr))
record_exists = RECORD_EXISTS;
else
curr_record = curr_record->next;
}while (curr_record->next != NULL && record_exists == RECORD_DNE);
if (!record_exists) {
new_record = record_alloc();
curr_record->next = new_record;
new_record->prev = curr_record;
new_record->rr = rr;
time(&(new_record->time_created));
}
}
record_t *record_alloc(void) {
record_t *record = (record_t *)malloc(sizeof(record_t));
record->next = NULL;
record->prev = NULL;
return record;
}
void add_entry(hash_key cache_key) {
ht_entry *cache_entry = (ht_entry *)malloc(sizeof(ht_entry));
cache_entry->key = cache_key;
cache_entry->record = NULL;
HASH_ADD_INT(cache, key, cache_entry);
}
ht_entry *find_entry(hash_key cache_key) {
ht_entry *cache_entry;
/* return null and remove cache entry if ttl expires*/
HASH_FIND_INT(cache, &cache_key, cache_entry);
return cache_entry;
}
void load_root_servers(void) {
load_root_ns();
load_root_a();
}
void load_root_a(void) {
hash_key cache_key;
rr_t rr;
uint8_t a_name[20] = {0x01, 'a',
0x0C, 'r', 'o', 'o', 't', '-', 's', 'e', 'r', 'v', 'e', 'r', 's',
0x03, 'n', 'e', 't', '\0'};
cache_key.name[0] = '.';
cache_key.name[1] = '\0';
memcpy(cache_key.name, a_name, 20);
memcpy(rr.name, a_name, 20);
rr.type = RR_TYPE_A;
rr.type = RR_CLASS_IN;
rr.rdata = a_rdata_alloc();
(*(a_rdata *)rr.rdata).address = htonl(0xC6290004); //"198.41.0.4"
add_record(cache_key, rr);
}
void load_root_ns(void) {
// static ht_entry cache_entry;
hash_key cache_key;
rr_t rr;
uint8_t a_ns[20] = {0x01, 'a',
0x0C, 'r', 'o', 'o', 't', '-', 's', 'e', 'r', 'v', 'e', 'r', 's',
0x03, 'n', 'e', 't', '\0'};
/* uint8_t b_ns[20] = {0x01, 'a', */
/* 0x0C, 'r', 'o', 'o', 't', '-', 's', 'e', 'r', 'v', 'e', 'r', 's', */
/* 0x03, 'n', 'e', 't', '\0'}; */
/* uint8_t c_ns[20] = {0x01, 'a', */
/* 0x0C, 'r', 'o', 'o', 't', '-', 's', 'e', 'r', 'v', 'e', 'r', 's', */
/* 0x03, 'n', 'e', 't', '\0'}; */
/* uint8_t d_ns[20] = {0x01, 'a', */
/* 0x0C, 'r', 'o', 'o', 't', '-', 's', 'e', 'r', 'v', 'e', 'r', 's', */
/* 0x03, 'n', 'e', 't', '\0'}; */
cache_key.name[0] = '.';
cache_key.name[1] = '\0';
rr.name[0] = '\0';
rr.type = RR_TYPE_NS;
rr.type = RR_CLASS_IN;
rr.ttl = UINT_MAX;
rr.rdata = malloc(20);
memcpy(rr.rdata, a_ns, 20);
add_record(cache_key, rr);
}
a_rdata *a_rdata_alloc(void) {
a_rdata *a = (a_rdata*)malloc(sizeof(a_rdata));
bzero(a, sizeof(a_rdata));
return a;
}
hash_key key_generate(char *name) {
static hash_key key;
bzero(key.name, DNS_NAME_MAX_LEN);
strncpy((char *)key.name, name, DNS_NAME_MAX_LEN);
return key;
}