-
Notifications
You must be signed in to change notification settings - Fork 181
/
cidr-trie.c
260 lines (233 loc) · 5.92 KB
/
cidr-trie.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
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
/*
* graftcp
* Copyright (C) 2023 Hmgle <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*/
/* Inspired by nginx: ngx_radix_tree.c */
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include "cidr-trie.h"
static trie_node_t *node_callc()
{
trie_node_t *node = calloc(1, sizeof(*node));
node->left = NULL;
node->right = NULL;
node->value = TRIE_NO_VALUE;
return node;
}
trie_t *trie_new()
{
trie_t *trie = calloc(1, sizeof(*trie));
trie->root = node_callc();
return trie;
}
void trie32_insert(trie_t *trie, struct cidr_s *cidr, int value)
{
trie_node_t *node, *next;
uint32_t bit = 0x80000000;
next = trie->root;
for (node = trie->root; bit & cidr->mask; bit >>= 1) {
next = cidr->addr & bit ? node->right : node->left;
if (next == NULL)
break;
node = next;
}
if (next) {
node->value = value;
return;
}
for (; bit & cidr->mask; bit >>= 1) {
next = node_callc();
if (cidr->addr & bit)
node->right = next;
else
node->left = next;
node = next;
}
node->value = value;
}
#define IPV4_MAX_TEXT_LENGTH 15
#define IPV6_MAX_TEXT_LENGTH 45
static int parse_cidr(const char *line, struct cidr_s *cidr)
{
char *p;
int shift;
char ipbuf[IPV4_MAX_TEXT_LENGTH + 1];
p = strchr(line, '/');
if (p) {
if (p - line > IPV4_MAX_TEXT_LENGTH)
return -1;
strncpy(ipbuf, line, p - line);
ipbuf[p - line] = '\0';
cidr->addr = ntohl(inet_addr(ipbuf));
shift = strtol(++p, NULL, 0);
if (shift < 0 || shift > 32)
return -1;
cidr->mask = shift ? (uint32_t)(0xffffffff << (32 - shift)) : 0;
} else {
cidr->mask = 0xffffffff;
cidr->addr = ntohl(inet_addr(line));
}
return 0;
}
int trie32_insert_str(trie_t *trie, const char *ipstr, int value)
{
struct cidr_s cidr;
if (parse_cidr(ipstr, &cidr))
return -1;
trie32_insert(trie, &cidr, value);
return 0;
}
int trie32_lookup(trie_t *trie, uint32_t ip)
{
uint32_t bit = 0x80000000;
trie_node_t *node;
for (node = trie->root; node;) {
if (node->value != TRIE_NO_VALUE)
return node->value;
node = ip & bit ? node->right : node->left;
bit >>= 1;
}
return TRIE_NO_VALUE;
}
void trie128_insert(trie_t *trie, struct cidr6_s *cidr6, int value)
{
trie_node_t *node, *next;
uint8_t bit = 0x80;
uint i = 0;
next = trie->root;
for (node = trie->root; bit & cidr6->mask.s6_addr[i];) {
next = bit & cidr6->addr.s6_addr[i] ? node->right : node->left;
if (next == NULL)
break;
bit >>= 1;
node = next;
if (bit == 0) {
if (++i == 16)
break;
bit = 0x80;
}
}
if (next) {
node->value = value;
return;
}
for (; bit & cidr6->mask.s6_addr[i];) {
next = node_callc();
if (bit & cidr6->addr.s6_addr[i])
node->right = next;
else
node->left = next;
bit >>= 1;
node = next;
if (bit == 0) {
if (++i == 16)
break;
bit = 0x80;
}
}
node->value = value;
}
static int parse_cidr6(const char *line, struct cidr6_s *cidr6)
{
char *p;
int shift;
char ip6buf[IPV6_MAX_TEXT_LENGTH + 1];
uint8_t *mask;
uint i, s;
p = strchr(line, '/');
if (p) {
if (p - line > IPV6_MAX_TEXT_LENGTH)
return -1;
strncpy(ip6buf, line, p - line);
ip6buf[p - line] = '\0';
if (inet_pton(AF_INET6, ip6buf, &cidr6->addr) != 1)
return -1;
shift = strtol(++p, NULL, 0);
if (shift < 0 || shift > 128)
return -1;
if (shift) {
mask = cidr6->mask.s6_addr;
for (i = 0; i < 16; i++) {
s = (shift > 8) ? 8 : shift;
shift -= s;
mask[i] = (u_char) (0xffu << (8 - s));
}
} else {
memset(cidr6->mask.s6_addr, 0, 16);
}
} else {
if (inet_pton(AF_INET6, line, &cidr6->addr) != 1)
return -1;
memset(cidr6->mask.s6_addr, 0xff, 16);
}
return 0;
}
int trie128_insert_str(trie_t *trie, const char *ipstr, int value)
{
struct cidr6_s cidr6;
if (parse_cidr6(ipstr, &cidr6))
return -1;
trie128_insert(trie, &cidr6, value);
return 0;
}
int trie128_lookup(trie_t *trie, uint8_t *ip)
{
trie_node_t *node;
uint8_t bit = 0x80;
uint i = 0;
for (node = trie->root; node;) {
if (node->value != TRIE_NO_VALUE)
return node->value;
node = bit & ip[i] ? node->right : node->left;
bit >>= 1;
if (bit == 0) {
i++;
bit = 0x80;
}
}
return TRIE_NO_VALUE;
}
cidr_trie_t *cidr_trie_new()
{
cidr_trie_t *cidr_trie = calloc(1, sizeof(*cidr_trie));
cidr_trie->cidr4_trie = NULL;
cidr_trie->cidr6_trie = NULL;
return cidr_trie;
}
void cidr_trie_insert_str(cidr_trie_t *cidr_trie, const char *ipstr, int value)
{
char *p;
p = strchr(ipstr, ':');
if (p) {
if (cidr_trie->cidr6_trie == NULL)
cidr_trie->cidr6_trie = trie_new();
trie128_insert_str(cidr_trie->cidr6_trie, ipstr, value);
} else {
if (cidr_trie->cidr4_trie == NULL)
cidr_trie->cidr4_trie = trie_new();
trie32_insert_str(cidr_trie->cidr4_trie, ipstr, value);
}
}
int cidr4_trie_lookup(cidr_trie_t *cidr_trie, uint32_t ip)
{
if (cidr_trie->cidr4_trie == NULL)
return TRIE_NO_VALUE;
return trie32_lookup(cidr_trie->cidr4_trie, ip);
}
int cidr6_trie_lookup(cidr_trie_t *cidr_trie, uint8_t *ip)
{
if (cidr_trie->cidr6_trie == NULL)
return TRIE_NO_VALUE;
return trie128_lookup(cidr_trie->cidr6_trie, ip);
}