Repository navigation
Expand file tree
/
Copy pathmpool.c
More file actions
242 lines (203 loc) · 5.66 KB
/
Copy pathmpool.c
File metadata and controls
242 lines (203 loc) · 5.66 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
#include "mpool.h"
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static int bit_cmp(const struct elem *p, const struct elem *q)
{
int len = q->len < p->len ? q->len : p->len;
int df = len % 8, bytes = len / 8;
int cmp = memcmp(p->data, q->data, bytes);
if (cmp || !df) {
return cmp;
}
uint8_t c1 = p->data[bytes] >> (8 - df);
uint8_t c2 = q->data[bytes] >> (8 - df);
if (c1 != c2) {
if (c1 < c2) return -1;
else return 1;
}
return 0;
}
static int byte_cmp(const struct elem *p, const struct elem *q)
{
if (p->len != q->len) {
return p->len < q->len ? -1 : 1;
}
return memcmp(p->data, q->data, p->len);
}
static int host_cmp(const struct elem *p, const struct elem *q)
{
int len = q->len < p->len ? q->len : p->len;
char *pd = p->data + p->len, *qd = q->data + q->len;
while (len-- > 0) {
if (*--pd != *--qd) {
return *pd < *qd ? -1 : 1;
}
}
if (p->len == q->len
|| (p->len > q->len ? pd[-1] : qd[-1]) == '.')
return 0;
return p->len > q->len ? 1 : -1;
}
static int scmp(const struct elem *p, const struct elem *q)
{
switch (p->cmp_type) {
case CMP_BITS:
return bit_cmp(p, q);
case CMP_HOST:
return host_cmp(p, q);
default:
return byte_cmp(p, q);
}
}
KAVL_INIT(my, struct elem, head, scmp)
struct mphdr *mem_pool(unsigned short flags, unsigned char cmp_type)
{
struct mphdr *hdr = calloc(1, sizeof(struct mphdr));
if (hdr) {
hdr->flags = flags;
hdr->cmp_type = cmp_type;
}
return hdr;
}
void *mem_get(const struct mphdr *hdr, const char *str, int len)
{
struct elem temp = {
.cmp_type = hdr->cmp_type,
.len = len, .data = (char *)str
};
return kavl_find(my, hdr->root, &temp, 0);
}
static void destroy_elem(struct mphdr *hdr, struct elem *e)
{
if (!(hdr->flags & MF_STATIC)) {
free(e->data);
}
if (hdr->flags & MF_EXTRA) {
free(((struct elem_ex *)e)->extra);
}
free(e);
}
void *mem_add(struct mphdr *hdr, char *str, int len, size_t struct_size)
{
struct elem *v, *e = calloc(1, struct_size);
if (!e) {
return 0;
}
e->len = len;
e->cmp_type = hdr->cmp_type;
e->data = str;
v = kavl_insert(my, &hdr->root, e, 0);
while (e != v && e->len < v->len) {
mem_delete(hdr, v->data, v->len);
v = kavl_insert(my, &hdr->root, e, 0);
}
if (e != v) {
destroy_elem(hdr, e);
}
else hdr->count++;
return v;
}
void mem_delete(struct mphdr *hdr, const char *str, int len)
{
struct elem temp = {
.cmp_type = hdr->cmp_type,
.len = len, .data = (char *)str
};
struct elem *e = kavl_erase(my, &hdr->root, &temp, 0);
if (!e) {
return;
}
destroy_elem(hdr, e);
hdr->count--;
}
void mem_destroy(struct mphdr *hdr)
{
while (hdr && hdr->root) {
struct elem *e = kavl_erase_first(my, &hdr->root);
if (!e) {
break;
}
destroy_elem(hdr, e);
}
free(hdr);
}
void dump_cache(struct mphdr *hdr, FILE *out, struct desync_params *dp)
{
if (!hdr->root) {
return;
}
kavl_itr_t(my) itr;
kavl_itr_first(my, hdr->root, &itr);
do {
struct elem_i *p = (struct elem_i *)kavl_at(&itr);
struct cache_key *key = (struct cache_key *)p->main.data;
if (p->dp != dp) {
continue;
}
char ADDR_STR[INET6_ADDRSTRLEN];
if (key->family == AF_INET)
inet_ntop(AF_INET, &key->ip.v4, ADDR_STR, sizeof(ADDR_STR));
else
inet_ntop(AF_INET6, &key->ip.v6, ADDR_STR, sizeof(ADDR_STR));
int bitlen = p->main.len - offsetof(struct cache_key, ip.v4) * 8;
fprintf(out, "0 %s %d %d %jd %.*s\n",
ADDR_STR, bitlen, ntohs(key->port),
(intmax_t)p->time, p->extra_len ? p->extra_len : 1, p->extra ? p->extra : "-");
}
while (kavl_itr_next(my, &itr));
fflush(out);
}
void load_cache(struct mphdr *hdr, FILE *in, struct desync_params *dp)
{
for (int i = 0; ; i++) {
char addr_str[INET6_ADDRSTRLEN] = { 0 };
char host[256] = { 0 };
int bitlen;
uint16_t port;
time_t cache_time;
int c = fscanf(in, "0 %39s %d %hu %jd %255s\n",
addr_str, &bitlen, &port, &cache_time, host);
if (c < 1) {
return;
}
struct cache_key key = { 0 };
int key_size = offsetof(struct cache_key, ip.v4);
bitlen += key_size * 8;
if (inet_pton(AF_INET, addr_str, &key.ip.v4) <= 0) {
if (inet_pton(AF_INET6, addr_str, &key.ip.v6) <= 0) {
continue;
} else {
key.family = AF_INET6;
key_size += sizeof(key.ip.v6);
}
}
else {
key.family = AF_INET;
key_size += sizeof(key.ip.v4);
}
if (key_size * 8 < bitlen) {
continue;
}
key.port = htons(port);
struct cache_key *data = calloc(1, key_size);
if (!data) {
return;
}
memcpy(data, &key, key_size);
struct elem_i *e = mem_add(hdr, (char *)data, bitlen, sizeof(struct elem_i));
if (!e) {
free(data);
return;
}
e->time = cache_time;
e->extra_len = strlen(host);
e->dp = dp;
if (e->extra_len > 1) {
e->extra = malloc(e->extra_len + 1);
memcpy(e->extra, host, e->extra_len + 1);
}
}
}