forked from dancrossnyc/44ripd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestipmapnearest.c
108 lines (91 loc) · 2.04 KB
/
testipmapnearest.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
#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, rroot, a, b, c, d, e;
const char *rv = "root";
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(&rroot, 0, sizeof(rroot));
memset(&a, 0, sizeof(a));
memset(&b, 0, sizeof(b));
memset(&c, 0, sizeof(c));
memset(&d, 0, sizeof(d));
root.key = 0;
root.keylen = 0;
root.datum = NULL;
root.left = &rroot;
root.right = NULL;
rroot.key = revbits(mkkey("44.0.0.0"));
rroot.keylen = 8;
rroot.datum = (void *)rv;
rroot.left = &a;
rroot.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, size_t keylen, const char *expected)
{
void *v = ipmapnearest(&root, mkkey(key), keylen);
if (v != expected) {
const char *exp = expected ? expected : "NULL";
printf("ipmapnearest(&root, \"%s\", %zu) != %s (%p -> %s)\n",
key, keylen, exp, v, (v == NULL) ? "NULL" : (char *)v);
}
}
int
main(void)
{
setup();
test("130.0.0.1", 32, NULL);
test("44.0.0.1", 24, rv);
test("44.0.0.12", 32, rv);
test("44.0.0.1", 32, av);
test("44.130.24.25", 32, ev);
test("44.130.24.1", 32, cv);
test("44.188.0.1", 32, rv);
test("44.130.130.0", 24, dv);
test("44.130.130.0", 27, dv);
test("44.130.131.0", 27, bv);
test("44.130.24.0", 24, cv);
return 0;
}