forked from dancrossnyc/44ripd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestipmapinsert.c
192 lines (178 loc) · 4.29 KB
/
testipmapinsert.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
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
#include <sys/types.h>
#include <arpa/inet.h>
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "dat.h"
#include "lib.h"
typedef struct Entry Entry;
struct Entry {
Entry *next;
uint32_t key;
size_t keylen;
void *datum;
};
uint32_t
mkkey(const char *addr)
{
return ntohl(inet_addr(addr));
}
size_t
mkkeylen(const char *subnetmask)
{
in_addr_t netmask = ntohl(inet_addr(subnetmask));
assert(isvalidnetmask(netmask));
return netmask2cidr(netmask);
}
void
u32tobin(uint32_t w, size_t len, char bin[static 33])
{
assert(len <= 32);
for (int k = 0; k < len; k++)
bin[k] = '0' + ((w >> k) & 0x01);
bin[len] = '\0';
}
static void
rdumptree(IPMap *map, int i)
{
char kb[33];
if (map == NULL) return;
u32tobin(map->key, map->keylen, kb);
printf("%-*sKey: %s/%zd Datum: %s\n", i, "",
kb, map->keylen, (char *)map->datum);
if (map->left != NULL) {
printf("%-*sLeft:\n", i, "");
rdumptree(map->left, i + 1);
}
if (map->right != NULL) {
printf("%-*sRight:\n", i, "");
rdumptree(map->right, i + 1);
}
}
void
dumptree(IPMap *root)
{
assert(root->datum == NULL);
assert(root->key == 0U);
assert(root->keylen == 0ULL);
printf("Root (no key)\n");
if (root->left) {
printf("Left:\n");
rdumptree(root->left, 1);
}
if (root->right) {
printf("Right:\n");
rdumptree(root->right, 1);
}
}
int
main(void)
{
IPMap *root;
Entry *entries, *entry, *prev;
void *v, *datum;
size_t keylen;
uint32_t key;
int c;
char buf[256], kb[33];
c = 0;
entries = NULL;
root = mkipmap();
assert(root != NULL);
while (fgets(buf, sizeof buf, stdin) != NULL) {
char *bp = buf;
char *ip = strsep(&bp, " \t\r\n");
char *subnetmask = strsep(&bp, " \t\r\n");
assert(ip != NULL);
assert(subnetmask != NULL);
key = mkkey(ip);
keylen = mkkeylen(subnetmask);
snprintf(buf, sizeof buf, "%d", ++c);
datum = strdup(buf);
assert(datum != NULL);
ipmapinsert(root, key, keylen, datum);
v = ipmapfind(root, key, keylen);
if (v != datum) {
u32tobin(revbits(key), keylen, kb);
printf("entry %d.%d.%d.%d/%zd %s = %s expected %s\n",
(key >> 24) & 0xFF, (key >> 16) & 0xFF,
(key >> 8) & 0xFF, key & 0xFF, keylen,
kb, (v == NULL) ? "NULL" : (char *)v,
(datum == NULL) ? "NULL" : (char *)datum);
dumptree(root);
exit(EXIT_FAILURE);
}
entry = calloc(1, sizeof(*entry));
assert(entry != NULL);
entry->key = key;
entry->keylen = keylen;
entry->datum = datum;
entry->next = entries;
entries = entry;
}
for (entry = entries; entry != NULL; entry = entry->next) {
v = ipmapfind(root, entry->key, entry->keylen);
datum = entry->datum;
if (v != datum) {
key = entry->key;
u32tobin(revbits(key), entry->keylen, kb);
printf("entry %d.%d.%d.%d/%zd %s = %s expected %s\n",
(key >> 24) & 0xFF, (key >> 16) & 0xFF,
(key >> 8) & 0xFF, key & 0xFF, entry->keylen,
kb, (v == NULL) ? "NULL" : (char *)v,
(datum == NULL) ? "NULL" : (char *)datum);
dumptree(root);
exit(EXIT_FAILURE);
}
}
prev = NULL;
entry = entries;
while (entry != NULL) {
Entry *current = entry;
entry = entry->next;
current->next = prev;
prev = current;
}
entries = prev;
entry = entries;
while (entry != NULL) {
Entry *tmp = entry;
datum = entry->datum;
v = ipmapremove(root, entry->key, entry->keylen);
if (v != datum) {
key = entry->key;
u32tobin(revbits(key), entry->keylen, kb);
printf("entry %d.%d.%d.%d/%zd %s = %s expected %s\n",
(key >> 24) & 0xFF, (key >> 16) & 0xFF,
(key >> 8) & 0xFF, key & 0xFF, entry->keylen,
kb, (v == NULL) ? "NULL" : (char *)v,
(datum == NULL) ? "NULL" : (char *)datum);
dumptree(root);
exit(EXIT_FAILURE);
}
entry = entry->next;
free(datum);
free(tmp);
for (tmp = entry; tmp != NULL; tmp = tmp->next) {
v = ipmapfind(root, tmp->key, tmp->keylen);
datum = tmp->datum;
if (v != datum) {
key = tmp->key;
u32tobin(revbits(key), tmp->keylen, kb);
printf("entry %d.%d.%d.%d/%zd %s = %s "
"expected %s\n",
(key >> 24) & 0xFF, (key >> 16) & 0xFF,
(key >> 8) & 0xFF, key & 0xFF,
tmp->keylen,
kb, (v == NULL) ? "NULL" : (char *)v,
(datum == NULL) ? "NULL" : (char *)datum);
dumptree(root);
exit(EXIT_FAILURE);
}
}
}
freeipmap(root, free);
return 0;
}