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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
|
/*
* Copyright (c) 2009-2014, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "radix.h"
#include "main.h"
#include "mem_pool.h"
struct radix_compressed_node {
union {
struct {
struct radix_compressed_node *right;
struct radix_compressed_node *left;
} n;
struct {
uint8_t *key;
guint keylen;
guint level;
} s;
} d;
uintptr_t value;
gboolean skipped;
};
struct radix_tree_compressed {
struct radix_compressed_node *root;
rspamd_mempool_t *pool;
size_t size;
};
#ifdef LEGACY_RADIX
static void * radix_alloc (radix_tree_t * tree);
#undef RADIX_DEBUG
#ifndef RADIX_DEBUG
#undef msg_debug
#define msg_debug(...) do {} while (0)
#endif
struct radix_node_s {
radix_node_t *right;
radix_node_t *left;
radix_node_t *parent;
uintptr_t value;
guint32 key;
};
struct radix_tree_s {
radix_node_t *root;
size_t size;
rspamd_mempool_t *pool;
};
radix_tree_t *
radix_tree_create (void)
{
radix_tree_t *tree;
tree = g_malloc (sizeof (radix_tree_t));
if (tree == NULL) {
return NULL;
}
tree->pool = rspamd_mempool_new (rspamd_mempool_suggest_size ());
tree->size = 0;
tree->root = radix_alloc (tree);
if (tree->root == NULL) {
return NULL;
}
tree->root->right = NULL;
tree->root->left = NULL;
tree->root->parent = NULL;
tree->root->value = RADIX_NO_VALUE;
return tree;
}
static uintptr_t
radix32tree_insert_common (radix_tree_t * tree,
guint32 key,
guint32 mask,
uintptr_t value,
enum radix_insert_type type)
{
guint32 bit;
radix_node_t *node, *next;
bit = 0x80000000;
node = tree->root;
next = tree->root;
/* Find a place in trie to insert */
while (bit & mask) {
if (key & bit) {
next = node->right;
}
else {
next = node->left;
}
if (next == NULL) {
break;
}
bit >>= 1;
node = next;
}
if (next) {
if (node->value != RADIX_NO_VALUE) {
/* Value was found, switch on insert type */
switch (type) {
case RADIX_INSERT:
return 1;
case RADIX_ADD:
node->value += value;
return value;
case RADIX_REPLACE:
node->value = value;
return 1;
}
}
node->value = value;
node->key = key;
return 0;
}
/* Inserting value in trie creating all path components */
while (bit & mask) {
next = radix_alloc (tree);
if (next == NULL) {
return -1;
}
next->right = NULL;
next->left = NULL;
next->parent = node;
next->value = RADIX_NO_VALUE;
if (key & bit) {
node->right = next;
}
else {
node->left = next;
}
bit >>= 1;
node = next;
}
node->value = value;
node->key = key;
return 0;
}
gint
radix32tree_insert (radix_tree_t *tree,
guint32 key,
guint32 mask,
uintptr_t value)
{
return (gint)radix32tree_insert_common (tree, key, mask, value,
RADIX_INSERT);
}
uintptr_t
radix32tree_add (radix_tree_t *tree, guint32 key, guint32 mask, uintptr_t value)
{
return radix32tree_insert_common (tree, key, mask, value, RADIX_ADD);
}
gint
radix32tree_replace (radix_tree_t *tree,
guint32 key,
guint32 mask,
uintptr_t value)
{
return (gint)radix32tree_insert_common (tree,
key,
mask,
value,
RADIX_REPLACE);
}
/*
* per recursion step:
* ptr + ptr + ptr + gint = 4 words
* result = 1 word
* 5 words total in stack
*/
static gboolean
radix_recurse_nodes (radix_node_t *node,
radix_tree_traverse_func func,
void *user_data,
gint level)
{
if (node->left) {
if (radix_recurse_nodes (node->left, func, user_data, level + 1)) {
return TRUE;
}
}
if (node->value != RADIX_NO_VALUE) {
if (func (node->key, level, node->value, user_data)) {
return TRUE;
}
}
if (node->right) {
if (radix_recurse_nodes (node->right, func, user_data, level + 1)) {
return TRUE;
}
}
return FALSE;
}
void
radix32tree_traverse (radix_tree_t *tree,
radix_tree_traverse_func func,
void *user_data)
{
radix_recurse_nodes (tree->root, func, user_data, 0);
}
gint
radix32tree_delete (radix_tree_t * tree, guint32 key, guint32 mask)
{
guint32 bit;
radix_node_t *node;
bit = 0x80000000;
node = tree->root;
while (node && (bit & mask)) {
if (key & bit) {
node = node->right;
}
else {
node = node->left;
}
bit >>= 1;
}
if (node == NULL || node->parent == NULL) {
return -1;
}
if (node->right || node->left) {
if (node->value != RADIX_NO_VALUE) {
node->value = RADIX_NO_VALUE;
return 0;
}
return -1;
}
for (;; ) {
if (node->parent->right == node) {
node->parent->right = NULL;
}
else {
node->parent->left = NULL;
}
node = node->parent;
if (node->right || node->left) {
break;
}
if (node->value != RADIX_NO_VALUE) {
break;
}
if (node->parent == NULL) {
break;
}
}
return 0;
}
uintptr_t
radix32tree_find (radix_tree_t * tree, guint32 key)
{
guint32 bit;
uintptr_t value;
radix_node_t *node;
bit = 0x80000000;
value = RADIX_NO_VALUE;
node = tree->root;
while (node) {
if (node->value != RADIX_NO_VALUE) {
value = node->value;
}
if (key & bit) {
node = node->right;
}
else {
node = node->left;
}
bit >>= 1;
}
return value;
}
static void *
radix_alloc (radix_tree_t * tree)
{
gchar *p;
p = rspamd_mempool_alloc (tree->pool, sizeof (radix_node_t));
tree->size += sizeof (radix_node_t);
return p;
}
void
radix_tree_free (radix_tree_t * tree)
{
g_return_if_fail (tree != NULL);
rspamd_mempool_delete (tree->pool);
g_free (tree);
}
uintptr_t
radix32_tree_find_addr (radix_tree_t *tree, rspamd_inet_addr_t *addr)
{
if (addr == NULL || addr->af != AF_INET) {
return RADIX_NO_VALUE;
}
return radix32tree_find (tree, ntohl (addr->addr.s4.sin_addr.s_addr));
}
#endif /* Old radix code */
static gboolean
radix_compare_compressed (struct radix_compressed_node *node,
guint8 *key, guint keylen, guint cur_level)
{
guint8 *nk;
guint8 *k;
guint8 bit;
guint shift, rbits, skip;
if (node->d.s.keylen > keylen) {
/* Obvious case */
return FALSE;
}
/* Compare byte aligned levels of a compressed node */
shift = node->d.s.level / NBBY;
/*
* We know that at least of cur_level bits are the same,
* se we can optimize search slightly
*/
if (shift > 0) {
skip = cur_level / NBBY;
if (shift > skip &&
memcmp (node->d.s.key + skip, key + skip, shift - skip) != 0) {
return FALSE;
}
else {
/* We already know that we checked all elements prior to this one */
return TRUE;
}
}
rbits = node->d.s.level % NBBY;
if (rbits > 0) {
/* Precisely compare remaining bits */
nk = node->d.s.key + shift;
k = key + shift;
bit = 1U << 7;
while (rbits > 0) {
if ((*nk & bit) != (*k & bit)) {
return FALSE;
}
bit >>= 1;
rbits --;
}
}
return TRUE;
}
uintptr_t
radix_find_compressed (radix_compressed_t * tree, guint8 *key, gsize keylen)
{
struct radix_compressed_node *node;
guint32 bit;
gsize kremain = keylen / sizeof (guint32);
uintptr_t value;
guint32 *k = (guint32 *)key;
guint32 kv = ntohl (*k);
guint cur_level = 0;
bit = 1U << 31;
value = RADIX_NO_VALUE;
node = tree->root;
msg_debug ("trying to find key");
while (node && kremain) {
if (node->skipped) {
/* It is obviously a leaf node */
if (radix_compare_compressed (node, key, keylen, cur_level)) {
return node->value;
}
else {
return value;
}
}
if (node->value != RADIX_NO_VALUE) {
value = node->value;
}
msg_debug ("finding value cur value: %ul, left: %p, "
"right: %p, go %s", value, node->d.n.left,
node->d.n.right, (*k & bit) ? "right" : "left");
if (kv & bit) {
node = node->d.n.right;
}
else {
node = node->d.n.left;
}
bit >>= 1;
if (bit == 0) {
k ++;
bit = 1U << 31;
kv = ntohl (*k);
kremain --;
}
cur_level ++;
}
return value;
}
static struct radix_compressed_node *
radix_uncompress_path (radix_compressed_t *tree,
struct radix_compressed_node *node,
guint start_level,
guint levels_uncompress)
{
guint8 *nkey = node->d.s.key + start_level / NBBY;
guint8 bit = 1U << (7 - start_level % NBBY);
struct radix_compressed_node *leaf, *next;
/* Make compressed leaf */
leaf = rspamd_mempool_alloc (tree->pool, sizeof (*node));
memcpy (leaf, node, sizeof (*node));
/* Make compressed node as uncompressed */
node->skipped = FALSE;
node->value = RADIX_NO_VALUE;
msg_debug ("uncompress %ud levels of tree", levels_uncompress);
/* Uncompress the desired path */
while (levels_uncompress) {
next = rspamd_mempool_alloc (tree->pool, sizeof (*node));
next->skipped = FALSE;
next->d.n.right = NULL;
next->d.n.left = NULL;
next->value = RADIX_NO_VALUE;
if (*nkey & bit) {
node->d.n.right = next;
node->d.n.left = NULL;
}
else {
node->d.n.left = next;
node->d.n.right = NULL;
}
bit >>= 1;
if (bit == 0) {
nkey ++;
bit = 1U << 7;
}
node = next;
levels_uncompress --;
}
/* Attach leaf node, that was previously a compressed node */
msg_debug ("attach leaf node to %s with value %p", (*nkey & bit) ? "right" : "left",
leaf->value);
if (*nkey & bit) {
node->d.n.right = leaf;
node->d.n.left = NULL;
}
else {
node->d.n.left = leaf;
node->d.n.right = NULL;
}
/* Return node */
return node;
}
static struct radix_compressed_node *
radix_make_leaf_node (radix_compressed_t *tree,
guint8 *key, guint keylen, guint level,
uintptr_t value,
gboolean compressed)
{
struct radix_compressed_node *node;
node = rspamd_mempool_alloc (tree->pool, sizeof (struct radix_compressed_node));
if (compressed) {
node->skipped = TRUE;
node->d.s.keylen = keylen;
node->d.s.key = rspamd_mempool_alloc (tree->pool, node->d.s.keylen);
node->d.s.level = level;
memcpy (node->d.s.key, key, node->d.s.keylen);
}
else {
/* Uncompressed leaf node */
memset (node, 0, sizeof (*node));
}
node->value = value;
msg_debug ("insert new leaf node with value %p", value);
return node;
}
static void
radix_move_up_compressed_leaf (radix_compressed_t *tree,
struct radix_compressed_node *leaf,
struct radix_compressed_node *parent, uintptr_t value,
guint8 *key, guint keylen, guint leaf_level)
{
parent->value = leaf->value;
leaf->value = value;
//g_slice_free1 (leaf->d.s.keylen, leaf->d.s.key);
leaf->d.s.keylen = keylen;
leaf->d.s.key = rspamd_mempool_alloc (tree->pool, leaf->d.s.keylen);
memcpy (leaf->d.s.key, key, keylen);
leaf->d.s.level = leaf_level;
}
static uintptr_t
radix_replace_node (radix_compressed_t *tree,
struct radix_compressed_node *node,
guint8 *key, gsize keylen,
uintptr_t value)
{
uintptr_t oldval;
if (node->skipped) {
/*
* For leaf nodes we have to deal with the keys as well, since
* we might find that keys are different for the same leaf node
*/
//g_slice_free1 (node->d.s.keylen, node->d.s.key);
node->d.s.keylen = keylen;
node->d.s.key = rspamd_mempool_alloc (tree->pool, node->d.s.keylen);
memcpy (node->d.s.key, key, node->d.s.keylen);
oldval = node->value;
node->value = value;
msg_debug ("replace value for leaf node with: %p, old value: %p",
value, oldval);
}
else {
oldval = node->value;
node->value = value;
msg_debug ("replace value for node with: %p, old value: %p",
value, oldval);
}
return oldval;
}
static uintptr_t
radix_uncompress_node (radix_compressed_t *tree,
struct radix_compressed_node *node,
guint8 *key, gsize keylen,
uintptr_t value,
guint cur_level,
guint target_level,
guint8 bit)
{
/* Find the largest common prefix of the compressed node and target node */
gsize kremain = keylen - cur_level / NBBY;
guint8 *nkey = node->d.s.key + cur_level / NBBY;
guint8 *k = key + cur_level / NBBY;
guint levels_uncompress = 0, start_level = cur_level;
gboolean masked = FALSE;
struct radix_compressed_node *leaf;
msg_debug ("want to uncompress nodes from level %ud to level %ud, "
"compressed node level: %ud",
cur_level, target_level, node->d.s.level);
while (cur_level < target_level) {
guint8 kb = *k & bit;
guint8 nb = *nkey & bit;
if (cur_level >= node->d.s.level) {
msg_debug ("found available masked path at level %ud", cur_level);
masked = TRUE;
break;
}
if (kb != nb) {
msg_debug ("found available path at level %ud", cur_level);
break;
}
cur_level ++;
levels_uncompress ++;
bit >>= 1;
if (bit == 0) {
k ++;
nkey ++;
bit = 1U << 7;
kremain --;
}
}
if (kremain == 0) {
/* Nodes are equal */
return radix_replace_node (tree, node, key, keylen, value);
}
else {
/*
* We need to uncompress the common path
*/
struct radix_compressed_node *nnode;
nnode = radix_uncompress_path (tree, node, start_level, levels_uncompress);
/*
* Now nnode is the last uncompressed node with compressed leaf inside
* and we also know that the current bit is different
*
* - if we have target_level == cur_level, then we can safely assign the
* value of that parent node
* - otherwise we insert new compressed leaf node
*/
if (cur_level == target_level) {
msg_debug ("insert detached leaf node with value: %p", value);
nnode->value = value;
}
else if (masked) {
/*
* Here we just add the previous value of node to the current node
* and replace value in the leaf
*/
if (nnode->d.n.left != NULL) {
leaf = nnode->d.n.left;
}
else {
leaf = nnode->d.n.right;
}
msg_debug ("move leaf node with value: %p, to level %ud, "
"set leaf node value to %p and level %ud", nnode->value,
cur_level, value, target_level);
radix_move_up_compressed_leaf (tree, leaf, nnode, value, key, keylen,
target_level);
}
else {
node = radix_make_leaf_node (tree, key, keylen,
target_level, value, TRUE);
if (nnode->d.n.left == NULL) {
nnode->d.n.left = node;
}
else {
nnode->d.n.right = node;
}
}
}
return value;
}
uintptr_t
radix_insert_compressed (radix_compressed_t * tree,
guint8 *key, gsize keylen,
gsize masklen,
uintptr_t value)
{
struct radix_compressed_node *node, *next = NULL, **prev;
gsize keybits = keylen * NBBY;
guint target_level = (keylen * NBBY - masklen);
guint cur_level = 0;
guint8 bit, *k = key;
gsize kremain = keylen;
uintptr_t oldval = RADIX_NO_VALUE;
bit = 1U << 7;
node = tree->root;
g_assert (keybits >= masklen);
msg_debug ("want insert value %p with mask %z", value, masklen);
node = tree->root;
next = node;
prev = &tree->root;
/* Search for the place to insert element */
while (node && cur_level < target_level) {
if (node->skipped) {
/* We have found skipped node and we need to uncompress it */
return radix_uncompress_node (tree, node, key, keylen, value,
cur_level, target_level, bit);
}
if (*k & bit) {
next = node->d.n.right;
prev = &node->d.n.right;
}
else {
next = node->d.n.left;
prev = &node->d.n.left;
}
if (next == NULL) {
/* Need to insert some nodes */
break;
}
bit >>= 1;
if (bit == 0) {
k ++;
bit = 1U << 7;
kremain --;
}
cur_level ++;
node = next;
}
if (next == NULL) {
next = radix_make_leaf_node (tree, key, keylen, target_level, value,
TRUE);
*prev = next;
tree->size ++;
}
else if (next->value == RADIX_NO_VALUE) {
msg_debug ("insert value node with %p", value);
next->value = value;
}
else {
if (next->skipped) {
/*
* For skipped node we replace value if the level of skipped node
* is equal to the target level
*/
if (next->d.s.level == target_level) {
oldval = radix_replace_node (tree, next, key, keylen, value);
}
else if (next->d.s.level > target_level) {
/*
* Here we must create new normal node and insert compressed leaf
* one level below
*/
node = radix_make_leaf_node (tree, key, keylen,
target_level, value, FALSE);
*prev = node;
if (*k & bit) {
node->d.n.right = next;
}
else {
node->d.n.left = next;
}
oldval = next->value;
}
else {
/*
* We must convert old compressed node to a normal node and
* create new compressed leaf attached to that normal node
*/
node = radix_make_leaf_node (tree, key, keylen,
target_level, value, TRUE);
*prev = next;
msg_debug ("move leaf node with value: %p, to level %ud, "
"set leaf node value to %p and level %ud", next->value,
cur_level, value, target_level);
next->skipped = FALSE;
if (*k & bit) {
next->d.n.right = node;
next->d.n.left = NULL;
}
else {
next->d.n.left = node;
next->d.n.right = NULL;
}
oldval = next->value;
}
}
else {
oldval = radix_replace_node (tree, next, key, keylen, value);
}
return oldval;
}
return next->value;
}
radix_compressed_t *
radix_create_compressed (void)
{
radix_compressed_t *tree;
tree = g_slice_alloc (sizeof (*tree));
if (tree == NULL) {
return NULL;
}
tree->pool = rspamd_mempool_new (rspamd_mempool_suggest_size ());
tree->size = 0;
tree->root = NULL;
return tree;
}
void
radix_destroy_compressed (radix_compressed_t *tree)
{
rspamd_mempool_delete (tree->pool);
g_slice_free1 (sizeof (*tree), tree);
}
uintptr_t
radix_find_compressed_addr (radix_compressed_t *tree, rspamd_inet_addr_t *addr)
{
if (addr == NULL) {
return RADIX_NO_VALUE;
}
if (addr->af == AF_INET) {
return radix_find_compressed (tree, (guint8 *)&addr->addr.s4.sin_addr,
sizeof (addr->addr.s4.sin_addr));
}
else if (addr->af == AF_INET6) {
return radix_find_compressed (tree, (guint8 *)&addr->addr.s6.sin6_addr,
sizeof (addr->addr.s6.sin6_addr));
}
return RADIX_NO_VALUE;
}
gint
rspamd_radix_add_iplist (const gchar *list, const gchar *separators,
radix_compressed_t *tree)
{
gchar *token, *ipnet, *err_str, **strv, **cur;
struct in_addr ina;
struct in6_addr ina6;
guint k = 0;
gint af;
gint res = 0;
/* Split string if there are multiple items inside a single string */
strv = g_strsplit_set (list, separators, 0);
cur = strv;
while (*cur) {
af = AF_UNSPEC;
if (**cur == '\0') {
cur++;
continue;
}
/* Extract ipnet */
ipnet = *cur;
token = strsep (&ipnet, "/");
if (ipnet != NULL) {
errno = 0;
/* Get mask */
k = strtoul (ipnet, &err_str, 10);
if (errno != 0) {
msg_warn (
"invalid netmask, error detected on symbol: %s, erorr: %s",
err_str,
strerror (errno));
k = 32;
}
}
/* Check IP */
if (inet_pton (AF_INET, token, &ina) == 1) {
af = AF_INET;
}
else if (inet_pton (AF_INET6, token, &ina6) == 1) {
af = AF_INET6;
}
else {
msg_warn ("invalid IP address: %s", token);
}
if (af == AF_INET) {
if (k > 32) {
k = 32;
}
radix_insert_compressed (tree, (guint8 *)&ina, sizeof (ina),
32 - k, 1);
res ++;
}
else if (af == AF_INET6){
if (k > 128) {
k = 128;
}
radix_insert_compressed (tree, (guint8 *)&ina6, sizeof (ina6),
128 - k, 1);
res ++;
}
cur++;
}
g_strfreev (strv);
return res;
}
/*
* vi:ts=4
*/
|