/
radix.diff
332 lines (314 loc) · 10.2 KB
/
radix.diff
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
Only in py-radix: .gitignore
diff -r -u py-radix-0.5/radix.c py-radix/radix.c
--- py-radix-0.5/radix.c 2007-10-24 02:04:31.000000000 -0400
+++ py-radix/radix.c 2014-02-05 16:20:32.254589252 -0500
@@ -328,6 +328,73 @@
}
+/* if inclusive != 0, "best" may be the given prefix itself */
+// Return array of all less specific radix nodes.
+int
+radix_stack (radix_tree_t *radix, prefix_t *prefix, int inclusive,
+ radix_node_t **final_stack)
+{
+ radix_node_t *node;
+ static radix_node_t *stack[RADIX_MAXBITS + 1];
+ u_char *addr;
+ u_int bitlen;
+ int cnt = 0;
+ int pass = 0;
+ int num_matched = 0;
+
+ if (radix->head == NULL)
+ return (NULL);
+
+ node = radix->head;
+ addr = prefix_touchar(prefix);
+ bitlen = prefix->bitlen;
+
+ while (node->bit < bitlen) {
+ if (node->prefix)
+ stack[cnt++] = node;
+ if (BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
+ node = node->r;
+ else
+ node = node->l;
+
+ if (node == NULL)
+ break;
+ }
+
+ if (inclusive && node && node->prefix)
+ stack[cnt++] = node;
+
+ //printf("prefix is %s %d\n", inet_ntoa(prefix->add.sin), prefix->bitlen);
+
+ if (cnt <= 0)
+ return (NULL);
+
+ //printf("here4\n");
+
+ while (--cnt >= 0) {
+ node = stack[cnt];
+
+ if (!pass) {
+ if (!comp_with_mask(prefix_tochar(node->prefix),
+ prefix_tochar(prefix), node->prefix->bitlen))
+ continue;
+ pass = 1;
+ }
+
+ //printf("in loop %d\n", num_matched++);
+
+ if (node == NULL) {
+ printf("err, should not be null\n");
+ }
+ final_stack[num_matched] = node;
+ num_matched++;
+ }
+
+ //printf("about to return with %d\n", num_matched);
+ return (num_matched);
+}
+
+
radix_node_t
*radix_lookup(radix_tree_t *radix, prefix_t *prefix)
{
diff -r -u py-radix-0.5/radix.h py-radix/radix.h
--- py-radix-0.5/radix.h 2007-10-24 02:03:08.000000000 -0400
+++ py-radix/radix.h 2014-02-05 16:20:32.254589252 -0500
@@ -124,6 +124,7 @@
radix_node_t *radix_search_exact(radix_tree_t *radix, prefix_t *prefix);
radix_node_t *radix_search_best(radix_tree_t *radix, prefix_t *prefix);
void radix_process(radix_tree_t *radix, rdx_cb_t func, void *cbctx);
+int radix_stack (radix_tree_t *radix, prefix_t *prefix, int inclusive, radix_node_t **final_stack);
#define RADIX_MAXBITS 128
diff -r -u py-radix-0.5/radix_python.c py-radix/radix_python.c
--- py-radix-0.5/radix_python.c 2007-12-17 21:49:01.000000000 -0500
+++ py-radix/radix_python.c 2014-02-05 16:20:32.254589252 -0500
@@ -221,8 +221,23 @@
PyObject_Del(self);
}
+# if 0
+// IPv4 demo of inet_ntop() and inet_pton()
+
+struct sockaddr_in sa;
+char str[INET_ADDRSTRLEN];
+
+// store this IP address in sa:
+inet_pton(AF_INET, "192.0.2.33", &(sa.sin_addr));
+
+// now get it back and print it
+inet_ntop(AF_INET, &(sa.sin_addr), str, INET_ADDRSTRLEN);
+
+printf("%s\n", str); // prints "192.0.2.33"
+#endif
+
static prefix_t
-*args_to_prefix(char *addr, char *packed, int packlen, long prefixlen)
+*args_to_prefix(char *addr, char *packed, int packlen, long prefixlen, char *family)
{
prefix_t *prefix = NULL;
const char *errmsg;
@@ -239,7 +254,47 @@
return NULL;
}
- if (addr != NULL) { /* Parse a string address */
+ // We can save a lot of work trying to guess family if told explicitly
+ if ((addr != NULL) && (family != NULL)) {
+ int ipv4 = !strncmp(family, "ipv4", 4);
+ int ipv6 = !strncmp(family, "ipv6", 4);
+ if(!(ipv4 || ipv6))
+ {
+ PyErr_SetString(PyExc_TypeError,
+ "Family must be ipv4 or ipv6");
+ return NULL;
+ }
+ int buflen = 4;
+ int domain = AF_INET;
+ if(ipv6)
+ {
+ buflen = 16;
+ domain = AF_INET6;
+ }
+
+ char *cp = NULL;
+ char *tmp = strdup(addr);
+ if ((cp = strchr(tmp, '/')) != NULL) {
+ *cp++ = '\0';
+ prefixlen = atoi(cp);
+ }
+ unsigned char buf[sizeof(struct in6_addr)];
+ int r = inet_pton(domain, tmp, buf);
+ if(r == 0) {
+ PyErr_SetString(PyExc_TypeError, "Not in presentation format");
+ free(tmp);
+ return NULL;
+ }
+ else if(r < 0) {
+ PyErr_SetString(PyExc_TypeError, "inet_pton failure");
+ free(tmp);
+ return NULL;
+ }
+
+ prefix = prefix_from_blob(buf, buflen, prefixlen);
+ free(tmp);
+ }
+ else if (addr != NULL) { /* Parse a string address */
if ((prefix = prefix_pton(addr, prefixlen, &errmsg)) == NULL) {
PyErr_SetString(PyExc_ValueError, errmsg ? errmsg :
"Invalid address format");
@@ -251,6 +306,8 @@
"Invalid packed address format");
}
}
+
+
if (prefix != NULL &&
prefix->family != AF_INET && prefix->family != AF_INET6) {
Deref_Prefix(prefix);
@@ -314,17 +371,17 @@
Radix_add(RadixObject *self, PyObject *args, PyObject *kw_args)
{
prefix_t *prefix;
- static char *keywords[] = { "network", "masklen", "packed", NULL };
+ static char *keywords[] = { "network", "masklen", "packed", "family", NULL};
PyObject *node_obj;
- char *addr = NULL, *packed = NULL;
+ char *addr = NULL, *packed = NULL, *family = NULL;
long prefixlen = -1;
int packlen = -1;
- if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#:add", keywords,
- &addr, &prefixlen, &packed, &packlen))
+ if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#s:add", keywords,
+ &addr, &prefixlen, &packed, &packlen, &family))
return NULL;
- if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen)) == NULL)
+ if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen, family)) == NULL)
return NULL;
node_obj = create_add_node(self, prefix);
@@ -344,16 +401,16 @@
radix_node_t *node;
RadixNodeObject *node_obj;
prefix_t *prefix;
- static char *keywords[] = { "network", "masklen", "packed", NULL };
+ static char *keywords[] = { "network", "masklen", "packed", "family", NULL };
- char *addr = NULL, *packed = NULL;
+ char *addr = NULL, *packed = NULL, *family = NULL;
long prefixlen = -1;
int packlen = -1;
- if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#:delete", keywords,
- &addr, &prefixlen, &packed, &packlen))
+ if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#s:delete", keywords,
+ &addr, &prefixlen, &packed, &packlen, &family))
return NULL;
- if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen)) == NULL)
+ if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen, family)) == NULL)
return NULL;
if ((node = radix_search_exact(PICKRT(prefix, self), prefix)) == NULL) {
Deref_Prefix(prefix);
@@ -389,16 +446,16 @@
radix_node_t *node;
RadixNodeObject *node_obj;
prefix_t *prefix;
- static char *keywords[] = { "network", "masklen", "packed", NULL };
+ static char *keywords[] = { "network", "masklen", "packed", "family", NULL };
- char *addr = NULL, *packed = NULL;
+ char *addr = NULL, *packed = NULL, *family = NULL;
long prefixlen = -1;
int packlen = -1;
- if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#:search_exact", keywords,
- &addr, &prefixlen, &packed, &packlen))
+ if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#s:search_exact", keywords,
+ &addr, &prefixlen, &packed, &packlen, &family))
return NULL;
- if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen)) == NULL)
+ if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen, family)) == NULL)
return NULL;
node = radix_search_exact(PICKRT(prefix, self), prefix);
@@ -429,16 +486,16 @@
radix_node_t *node;
RadixNodeObject *node_obj;
prefix_t *prefix;
- static char *keywords[] = { "network", "masklen", "packed", NULL };
+ static char *keywords[] = { "network", "masklen", "packed", "family", NULL };
- char *addr = NULL, *packed = NULL;
+ char *addr = NULL, *packed = NULL, *family = NULL;
long prefixlen = -1;
int packlen = -1;
- if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#:search_best", keywords,
- &addr, &prefixlen, &packed, &packlen))
+ if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#s:search_best", keywords,
+ &addr, &prefixlen, &packed, &packlen, &family))
return NULL;
- if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen)) == NULL)
+ if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen, family)) == NULL)
return NULL;
if ((node = radix_search_best(PICKRT(prefix, self), prefix)) == NULL ||
@@ -484,6 +541,59 @@
return (ret);
}
+
+PyDoc_STRVAR(Radix_stack_doc,
+"Radix.stack(prefix) -> List of prefix strings\n\
+\n\
+Returns a list nodes that much the prefix\n\
+This list may be empty if no prefixes have been\n");
+
+static PyObject *
+Radix_stack(RadixObject *self, PyObject *args, PyObject *kw_args)
+{
+ radix_node_t *node;
+ radix_node_t *final_stack[RADIX_MAXBITS + 1];
+ //RadixNodeObject *node_obj;
+ prefix_t *prefix;
+ static char *keywords[] = { "network", "masklen", "packed", "family", NULL };
+ int i, n;
+ PyObject *ret;
+
+ char *addr = NULL, *packed = NULL, *family = NULL;
+ long prefixlen = -1;
+ int packlen = -1;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kw_args, "|sls#s:search_exact", keywords,
+ &addr, &prefixlen, &packed, &packlen, &family))
+ return NULL;
+ if ((prefix = args_to_prefix(addr, packed, packlen, prefixlen, family)) == NULL)
+ return NULL;
+
+ if ((ret = PyList_New(0)) == NULL)
+ return NULL;
+
+ n = radix_stack(PICKRT(prefix, self), prefix, 1, final_stack);
+ //printf("n = %d\n", n);
+
+ for (i=0; i < n; i++) {
+ if ((node = final_stack[i]) == NULL)
+ break;
+
+ //printf("n=%d %d\n", n, i);
+
+ //if (node->data != NULL)
+ //printf("not null\n");
+ PyList_Append(ret, (PyObject *)node->data);
+ }
+
+ //node_obj = node->data;
+ //Py_XINCREF(node_obj);
+ //return (PyObject *)node_obj;
+
+ return (ret);
+}
+
+
PyDoc_STRVAR(Radix_prefixes_doc,
"Radix.prefixes(prefix) -> List of prefix strings\n\
\n\
@@ -642,6 +752,7 @@
{"search_best", (PyCFunction)Radix_search_best, METH_VARARGS|METH_KEYWORDS, Radix_search_best_doc },
{"nodes", (PyCFunction)Radix_nodes, METH_VARARGS, Radix_nodes_doc },
{"prefixes", (PyCFunction)Radix_prefixes, METH_VARARGS, Radix_prefixes_doc },
+ {"stack", (PyCFunction)Radix_stack, METH_VARARGS|METH_KEYWORDS, Radix_stack_doc },
{"__getstate__",(PyCFunction)Radix_getstate, METH_VARARGS, NULL },
{"__setstate__",(PyCFunction)Radix_setstate, METH_VARARGS, NULL },
{"__reduce__", (PyCFunction)Radix_reduce, METH_VARARGS, NULL },