forked from dancrossnyc/44ripd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testipmapfind.c
98 lines (82 loc) · 1.78 KB
/
testipmapfind.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
#include <sys/types.h>
#include <arpa/inet.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "dat.h"
#include "lib.h"
IPMap root, a, b, c, d, e;
const char *av = "a";
const char *bv = "b";
const char *cv = "c";
const char *dv = "d";
const char *ev = "e";
uint32_t
mkkey(const char *addr)
{
return ntohl(inet_addr(addr));
}
void
setup(void)
{
memset(&root, 0, sizeof(root));
memset(&a, 0, sizeof(a));
memset(&b, 0, sizeof(b));
memset(&c, 0, sizeof(c));
memset(&d, 0, sizeof(d));
root.key = revbits(mkkey("44.0.0.0"));
root.keylen = 8;
root.datum = NULL;
root.left = &a;
root.right = &b;
a.key = (revbits(mkkey("44.0.0.1")) >> 8);
a.keylen = 24;
a.datum = (void *)av;
a.left = NULL;
a.right = NULL;
b.key = (revbits(mkkey("44.130.0.0")) >> 8);
b.keylen = 8;
b.datum = (void *)bv;
b.left = &c;
b.right = &d;
c.key = (revbits(mkkey("44.130.24.0")) >> 16);
c.keylen = 8;
c.datum = (void *)cv;
c.left = &e;
c.right = NULL;
d.key = (revbits(mkkey("44.130.130.0")) >> 16);
d.keylen = 8;
d.datum = (void *)dv;
d.left = NULL;
d.right = NULL;
e.key = (revbits(mkkey("44.130.24.25")) >> 24);
e.keylen = 8;
e.datum = (void *)ev;
e.left = NULL;
e.right = NULL;
}
void
test(const char *key, uint32_t keylen, const char *expected)
{
void *v = ipmapfind(&root, mkkey(key), keylen);
if (v != expected) {
const char *exp = expected ? expected : "(NULL)";
printf("ipmapnearest(&root, \"%s\", %d) != %s (%p)\n",
key, keylen, exp, v);
}
}
int
main(void)
{
setup();
test("44.0.0.1", 24, NULL);
test("44.0.0.1", 32, av);
test("44.130.24.25", 32, ev);
test("44.130.24.1", 32, NULL);
test("44.188.0.1", 32, NULL);
test("44.130.130.0", 24, dv);
test("44.130.130.0", 27, NULL);
test("44.130.131.0", 27, NULL);
test("44.130.24.0", 24, cv);
return 0;
}