-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlsa.c
More file actions
419 lines (326 loc) · 8.73 KB
/
lsa.c
File metadata and controls
419 lines (326 loc) · 8.73 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
/*
* dvpn, a multipoint vpn implementation
* Copyright (C) 2016 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <iv_list.h>
#include <string.h>
#include "lsa.h"
#include "lsa_serialise.h"
static size_t lsa_attr_size(const struct lsa_attr *attr);
static int
lsa_attr_compare_keys(const struct lsa_attr *a, const struct lsa_attr *b)
{
int len;
int ret;
if (a->type < b->type)
return -1;
if (a->type > b->type)
return 1;
len = a->keylen;
if (len > b->keylen)
len = b->keylen;
ret = memcmp(lsa_attr_key((struct lsa_attr *)a),
lsa_attr_key((struct lsa_attr *)b), len);
if (ret < 0)
return -1;
if (ret > 0)
return 1;
if (a->keylen < b->keylen)
return -1;
if (a->keylen > b->keylen)
return 1;
return 0;
}
static int compare_attr_keys(const struct iv_avl_node *_a,
const struct iv_avl_node *_b)
{
const struct lsa_attr *a = iv_container_of(_a, struct lsa_attr, an);
const struct lsa_attr *b = iv_container_of(_b, struct lsa_attr, an);
return lsa_attr_compare_keys(a, b);
}
struct lsa *lsa_alloc(const uint8_t *id)
{
struct lsa *lsa;
lsa = malloc(sizeof(*lsa));
if (lsa == NULL)
return NULL;
lsa->refcount = 1;
lsa->bytes = MAX_SERIALISED_INT_LEN + NODE_ID_LEN;
memcpy(lsa->id, id, NODE_ID_LEN);
INIT_IV_AVL_TREE(&lsa->root.attrs, compare_attr_keys);
return lsa;
}
struct lsa *lsa_get(struct lsa *lsa)
{
if (lsa != NULL)
lsa->refcount++;
return lsa;
}
static void attr_tree_free(struct lsa *lsa, struct iv_avl_node *root)
{
struct lsa_attr *attr = iv_container_of(root, struct lsa_attr, an);
lsa->bytes -= lsa_attr_size(attr);
if (attr->an.left != NULL)
attr_tree_free(lsa, attr->an.left);
if (attr->an.right != NULL)
attr_tree_free(lsa, attr->an.right);
if (attr->data_is_attr_set) {
struct lsa_attr_set *set;
set = lsa_attr_data(attr);
attr_tree_free(lsa, set->attrs.root);
}
free(attr);
}
void lsa_put(struct lsa *lsa)
{
if (lsa != NULL && !--lsa->refcount) {
if (!iv_avl_tree_empty(&lsa->root.attrs))
attr_tree_free(lsa, lsa->root.attrs.root);
free(lsa);
}
}
static void lsa_attr_set_clone(struct lsa *lsa, struct lsa_attr_set *dst,
const struct lsa_attr_set *src)
{
struct iv_avl_node *an;
iv_avl_tree_for_each (an, (struct iv_avl_tree *)&src->attrs) {
struct lsa_attr *attr;
attr = iv_container_of(an, struct lsa_attr, an);
if (attr->data_is_attr_set) {
struct lsa_attr_set *s;
struct lsa_attr_set *d;
s = lsa_attr_data(attr);
d = lsa_attr_set_add_attr_set(lsa, dst, attr->type,
!!(attr->attr_signed),
lsa_attr_key(attr), attr->keylen);
lsa_attr_set_clone(lsa, d, s);
} else {
lsa_attr_set_add_attr(lsa, dst, attr->type,
!!(attr->attr_signed),
lsa_attr_key(attr), attr->keylen,
lsa_attr_data(attr), attr->datalen);
}
}
}
struct lsa *lsa_clone(const struct lsa *lsa)
{
struct lsa *newlsa;
newlsa = lsa_alloc(lsa->id);
if (newlsa == NULL)
return NULL;
lsa_attr_set_clone(newlsa, &newlsa->root, &lsa->root);
return newlsa;
}
#define ROUND_UP(size) (((size) + 7) & ~7)
void *lsa_attr_key(struct lsa_attr *attr)
{
if (attr->keylen)
return attr->buf;
return NULL;
}
void *lsa_attr_data(struct lsa_attr *attr)
{
if (attr->datalen)
return attr->buf + ROUND_UP(attr->keylen);
return NULL;
}
struct lsa_attr *lsa_find_attr(struct lsa *lsa, int type,
const void *key, size_t keylen)
{
return lsa_attr_set_find_attr(&lsa->root, type, key, keylen);
}
struct lsa_attr *lsa_attr_set_find_attr(struct lsa_attr_set *set, int type,
const void *key, size_t keylen)
{
struct {
struct lsa_attr skey;
uint8_t kkey[0];
} *s;
struct iv_avl_node *an;
if (keylen > 65536)
return NULL;
s = alloca(sizeof(*s) + keylen);
s->skey.type = type;
s->skey.keylen = keylen;
memcpy(lsa_attr_key(&s->skey), key, keylen);
an = set->attrs.root;
while (an != NULL) {
struct lsa_attr *attr;
int ret;
attr = iv_container_of(an, struct lsa_attr, an);
ret = lsa_attr_compare_keys(&s->skey, attr);
if (ret == 0)
return attr;
if (ret < 0)
an = an->left;
else
an = an->right;
}
return NULL;
}
static size_t lsa_attr_size(const struct lsa_attr *attr)
{
size_t size;
size = 2 * MAX_SERIALISED_INT_LEN;
if (attr->keylen) {
size += MAX_SERIALISED_INT_LEN;
if (attr->keylen > SIZE_MAX - size)
abort();
size += attr->keylen;
}
if (MAX_SERIALISED_INT_LEN > SIZE_MAX - size)
abort();
size += MAX_SERIALISED_INT_LEN;
if (!attr->data_is_attr_set) {
if (attr->datalen > SIZE_MAX - size)
abort();
size += attr->datalen;
}
return size;
}
int lsa_add_attr(struct lsa *lsa, int type, int sign, const void *key,
size_t keylen, const void *data, size_t datalen)
{
if (lsa->refcount != 1) {
fprintf(stderr, "lsa_add_attr: called on an LSA with "
"refcount %d\n", lsa->refcount);
abort();
}
return lsa_attr_set_add_attr(lsa, &lsa->root, type, sign,
key, keylen, data, datalen);
}
static struct lsa_attr *attr_alloc(int type, size_t keylen, size_t datalen)
{
struct lsa_attr *attr;
size_t size;
size = sizeof(*attr);
if (ROUND_UP(keylen) > SIZE_MAX - size)
abort();
size += ROUND_UP(keylen);
if (datalen > SIZE_MAX - size)
abort();
size += datalen;
attr = malloc(size);
if (attr == NULL)
abort();
attr->type = type;
attr->data_is_attr_set = 0;
attr->attr_signed = 0;
attr->keylen = keylen;
attr->datalen = datalen;
return attr;
}
int lsa_attr_set_add_attr(struct lsa *lsa, struct lsa_attr_set *set,
int type, int sign, const void *key, size_t keylen,
const void *data, size_t datalen)
{
struct lsa_attr *attr;
if (lsa->refcount != 1) {
fprintf(stderr, "lsa_attr_set_add_attr: called on an LSA "
"with refcount %d\n", lsa->refcount);
abort();
}
attr = lsa_attr_set_find_attr(set, type, key, keylen);
if (attr != NULL)
abort();
attr = attr_alloc(type, keylen, datalen);
if (sign)
attr->attr_signed = 1;
if (keylen)
memcpy(lsa_attr_key(attr), key, keylen);
if (datalen)
memcpy(lsa_attr_data(attr), data, datalen);
if (iv_avl_tree_insert(&set->attrs, &attr->an) < 0) {
free(attr);
return -1;
}
lsa->bytes += lsa_attr_size(attr);
return 0;
}
struct lsa_attr_set *lsa_add_attr_set(struct lsa *lsa, int type, int sign,
const void *key, size_t keylen)
{
if (lsa->refcount != 1) {
fprintf(stderr, "lsa_add_attr_set: called on an LSA "
"with refcount %d\n", lsa->refcount);
abort();
}
return lsa_attr_set_add_attr_set(lsa, &lsa->root, type,
sign, key, keylen);
}
struct lsa_attr_set *
lsa_attr_set_add_attr_set(struct lsa *lsa, struct lsa_attr_set *set,
int type, int sign, const void *key, size_t keylen)
{
struct lsa_attr *attr;
struct lsa_attr_set *child;
if (lsa->refcount != 1) {
fprintf(stderr, "lsa_attr_set_add_attr_set: called on an "
"LSA with refcount %d\n", lsa->refcount);
abort();
}
attr = lsa_attr_set_find_attr(set, type, key, keylen);
if (attr != NULL)
abort();
attr = attr_alloc(type, keylen, sizeof(struct lsa_attr_set));
attr->data_is_attr_set = 1;
if (sign)
attr->attr_signed = 1;
if (keylen)
memcpy(lsa_attr_key(attr), key, keylen);
if (iv_avl_tree_insert(&set->attrs, &attr->an) < 0) {
free(attr);
return NULL;
}
child = lsa_attr_data(attr);
INIT_IV_AVL_TREE(&child->attrs, compare_attr_keys);
lsa->bytes += lsa_attr_size(attr);
return child;
}
void lsa_del_attr(struct lsa *lsa, struct lsa_attr *attr)
{
if (lsa->refcount != 1) {
fprintf(stderr, "lsa_del_attr: called on an LSA with "
"refcount %d\n", lsa->refcount);
abort();
}
lsa->bytes -= lsa_attr_size(attr);
iv_avl_tree_delete(&lsa->root.attrs, &attr->an);
if (attr->data_is_attr_set) {
struct lsa_attr_set *set;
set = lsa_attr_data(attr);
if (!iv_avl_tree_empty(&set->attrs))
attr_tree_free(lsa, set->attrs.root);
}
free(attr);
}
void lsa_del_attr_bykey(struct lsa *lsa, int type,
const void *key, size_t keylen)
{
struct lsa_attr *attr;
if (lsa->refcount != 1) {
fprintf(stderr, "lsa_del_attr_bykey: called on an LSA with "
"refcount %d\n", lsa->refcount);
abort();
}
attr = lsa_find_attr(lsa, type, key, keylen);
if (attr == NULL)
abort();
lsa_del_attr(lsa, attr);
}