1 /*-
2  * Copyright 2016 Vsevolod Stakhov
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef RADIX_H
17 #define RADIX_H
18 
19 #include "config.h"
20 #include "util.h"
21 
22 #define RADIX_NO_VALUE   (uintptr_t)-1
23 
24 
25 typedef struct radix_tree_compressed radix_compressed_t;
26 
27 /**
28  * Insert new key to the radix trie
29  * @param tree radix trie
30  * @param key key to insert (bitstring)
31  * @param keylen length of the key (in bytes)
32  * @param masklen lenght of mask that should be applied to the key (in bits)
33  * @param value opaque value pointer
34  * @return previous value of the key or `RADIX_NO_VALUE`
35  */
36 uintptr_t
37 radix_insert_compressed (radix_compressed_t * tree,
38 	guint8 *key, gsize keylen,
39 	gsize masklen,
40 	uintptr_t value);
41 
42 /**
43  * Find a key in a radix trie
44  * @param tree radix trie
45  * @param key key to find (bitstring)
46  * @param keylen length of a key
47  * @return opaque pointer or `RADIX_NO_VALUE` if no value has been found
48  */
49 uintptr_t radix_find_compressed (radix_compressed_t * tree, const guint8 *key,
50 		gsize keylen);
51 
52 /**
53  * Destroy the complete radix trie
54  * @param tree
55  */
56 void radix_destroy_compressed (radix_compressed_t *tree);
57 
58 /**
59  * Create new radix trie
60  * @return
61  */
62 radix_compressed_t *radix_create_compressed (void);
63 
64 /**
65  * Insert list of ip addresses and masks to the radix tree
66  * @param list string line of addresses
67  * @param separators string of characters used as separators
68  * @param tree target tree
69  * @return number of elements inserted
70  */
71 gint rspamd_radix_add_iplist (const gchar *list, const gchar *separators,
72 		radix_compressed_t *tree, gconstpointer value, gboolean resolve);
73 
74 /**
75  * Generic version of @see rspamd_radix_add_iplist. This function creates tree
76  * if `tree` is NULL.
77  */
78 gboolean radix_add_generic_iplist (const gchar *ip_list,
79 		radix_compressed_t **tree, gboolean resolve);
80 
81 /**
82  * Returns number of elements in the tree
83  * @param tree
84  * @return
85  */
86 gsize radix_get_size (radix_compressed_t *tree);
87 
88 /**
89  * Return string that describes this radix tree (memory, nodes, compression etc)
90  * @param tree
91  * @return constant string
92  */
93 const gchar * radix_get_info (radix_compressed_t *tree);
94 
95 uintptr_t radix_find_rmilter_addr (radix_compressed_t * tree,
96 		const struct rmilter_inet_address *addr);
97 
98 #endif
99