suricata
util-radix4-tree.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  * Based on util-radix-tree.[ch] by:
23  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
24  */
25 
26 #ifndef SURICATA_UTIL_RADIX4_TREE_H
27 #define SURICATA_UTIL_RADIX4_TREE_H
28 
29 #include "suricata-common.h"
30 
31 struct RadixUserData;
32 
33 /**
34  * \brief Structure for the node in the radix tree
35  */
36 typedef struct SCRadix4Node_ {
37  /** holds bitmap of netmasks that come under this node in the tree */
38  uint64_t masks : 33;
39  uint64_t pad1 : 31;
40 
41  /** the bit position where the bits differ in the nodes children. Used
42  * to determine the path to be taken during a lookup */
43  uint8_t bit;
44 
45  /** bool to see if prefix_stream is filled */
46  bool has_prefix;
47 
48  /** the key that has been stored in the tree */
49  uint8_t prefix_stream[4];
50 
51  /** User data that is associated with this key. We need a user data field
52  * for each netblock value possible since one ip can be associated
53  * with any of the 32 netblocks. */
55 
56  /** the left and the right children of a node */
58 
59  /** the parent node for this tree */
62 
63 /**
64  * \brief Structure for the radix tree
65  */
66 typedef struct SCRadix4Tree_ {
67  /** the root node in the radix tree */
70 
71 typedef struct SCRadix4Config_ {
72  void (*Free)(void *);
73  /** function pointer that is supplied by the user to free the user data
74  * held by the user field of SCRadix4Node */
75  void (*PrintData)(void *); // debug only?
77 
78 #define SC_RADIX4_TREE_INITIALIZER \
79  { \
80  .head = NULL \
81  }
82 
85 
86 SCRadix4Node *SCRadix4AddKeyIPV4(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, void *);
88  SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, uint8_t, void *);
89 bool SCRadix4AddKeyIPV4String(SCRadix4Tree *, const SCRadix4Config *, const char *, void *);
90 
92  SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, uint8_t);
93 void SCRadix4RemoveKeyIPV4(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *);
94 
95 SCRadix4Node *SCRadix4TreeFindExactMatch(const SCRadix4Tree *, const uint8_t *, void **);
97  const SCRadix4Tree *, const uint8_t *, const uint8_t, void **);
98 SCRadix4Node *SCRadix4TreeFindBestMatch(const SCRadix4Tree *, const uint8_t *, void **);
99 SCRadix4Node *SCRadix4TreeFindBestMatch2(const SCRadix4Tree *, const uint8_t *, void **, uint8_t *);
100 
101 void SCRadix4PrintTree(SCRadix4Tree *, const SCRadix4Config *config);
102 void SCRadix4PrintNodeInfo(SCRadix4Node *, int, void (*PrintData)(void *));
103 
104 void SCRadix4RegisterTests(void);
105 
106 typedef int (*SCRadix4ForEachNodeFunc)(
107  const SCRadix4Node *node, void *user_data, const uint8_t netmask, void *data);
108 
109 int SCRadix4ForEachNode(const SCRadix4Tree *tree, SCRadix4ForEachNodeFunc Callback, void *data);
110 
111 /** \brief compare content of 2 user data entries
112  * \retval true equal
113  * \retval false not equal
114  */
115 typedef bool (*SCRadix4TreeCompareFunc)(const void *ud1, const void *ud2);
117  const SCRadix4Tree *t1, const SCRadix4Tree *t2, SCRadix4TreeCompareFunc Callback);
118 
119 #endif /* SURICATA_UTIL_RADIX4_TREE_H */
SCRadix4RemoveKeyIPV4Netblock
void SCRadix4RemoveKeyIPV4Netblock(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, uint8_t)
Removes an IPV4 address netblock key from the Radix4 tree.
Definition: util-radix4-tree.c:296
SCRadix4Node_
Structure for the node in the radix tree.
Definition: util-radix4-tree.h:36
SCRadix4PrintNodeInfo
void SCRadix4PrintNodeInfo(SCRadix4Node *, int, void(*PrintData)(void *))
SCRadix4Node_::parent
struct SCRadix4Node_ * parent
Definition: util-radix4-tree.h:60
SCRadix4Node_::pad1
uint64_t pad1
Definition: util-radix4-tree.h:39
SCRadix4Config_::PrintData
void(* PrintData)(void *)
Definition: util-radix4-tree.h:75
SCRadix4Node_::masks
uint64_t masks
Definition: util-radix4-tree.h:38
SCRadix4Node_::has_prefix
bool has_prefix
Definition: util-radix4-tree.h:46
RadixUserData
Structure that hold the user data and the netmask associated with it.
Definition: util-radix-tree-common.h:41
SCRadix4Config_::Free
void(* Free)(void *)
Definition: util-radix4-tree.h:72
SCRadix4TreeFindExactMatch
SCRadix4Node * SCRadix4TreeFindExactMatch(const SCRadix4Tree *, const uint8_t *, void **)
Definition: util-radix4-tree.c:142
SCRadix4PrintTree
void SCRadix4PrintTree(SCRadix4Tree *, const SCRadix4Config *config)
Definition: util-radix4-tree.c:303
SCRadix4TreeFindBestMatch
SCRadix4Node * SCRadix4TreeFindBestMatch(const SCRadix4Tree *, const uint8_t *, void **)
Definition: util-radix4-tree.c:154
SCRadix4Node_::user_data
struct RadixUserData * user_data
Definition: util-radix4-tree.h:54
SCRadix4Config
struct SCRadix4Config_ SCRadix4Config
SCRadix4RemoveKeyIPV4
void SCRadix4RemoveKeyIPV4(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *)
Removes an IPV4 address key(not a netblock) from the Radix4 tree. Instead of using this function,...
Definition: util-radix4-tree.c:282
SCRadix4Node
struct SCRadix4Node_ SCRadix4Node
Structure for the node in the radix tree.
SCRadix4Tree_
Structure for the radix tree.
Definition: util-radix4-tree.h:66
SCRadix4Node_::prefix_stream
uint8_t prefix_stream[4]
Definition: util-radix4-tree.h:49
SCRadix4TreeInitialize
SCRadix4Tree SCRadix4TreeInitialize(void)
Definition: util-radix4-tree.c:166
SCRadix4CompareTrees
bool SCRadix4CompareTrees(const SCRadix4Tree *t1, const SCRadix4Tree *t2, SCRadix4TreeCompareFunc Callback)
Definition: util-radix4-tree.c:359
SCRadix4TreeCompareFunc
bool(* SCRadix4TreeCompareFunc)(const void *ud1, const void *ud2)
compare content of 2 user data entries
Definition: util-radix4-tree.h:115
SCRadix4ForEachNode
int SCRadix4ForEachNode(const SCRadix4Tree *tree, SCRadix4ForEachNodeFunc Callback, void *data)
Definition: util-radix4-tree.c:352
SCRadix4AddKeyIPV4String
bool SCRadix4AddKeyIPV4String(SCRadix4Tree *, const SCRadix4Config *, const char *, void *)
Adds a new IPV4/netblock to the Radix4 tree from a string.
Definition: util-radix4-tree.c:227
SCRadix4TreeRelease
void SCRadix4TreeRelease(SCRadix4Tree *, const SCRadix4Config *)
Definition: util-radix4-tree.c:172
SCRadix4Tree_::head
SCRadix4Node * head
Definition: util-radix4-tree.h:68
SCRadix4RegisterTests
void SCRadix4RegisterTests(void)
Definition: util-radix4-tree.c:922
SCRadix4Config_
Definition: util-radix4-tree.h:71
suricata-common.h
SCRadix4TreeFindBestMatch2
SCRadix4Node * SCRadix4TreeFindBestMatch2(const SCRadix4Tree *, const uint8_t *, void **, uint8_t *)
Definition: util-radix4-tree.c:160
SCRadix4Node_::bit
uint8_t bit
Definition: util-radix4-tree.h:43
SCRadix4AddKeyIPV4
SCRadix4Node * SCRadix4AddKeyIPV4(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, void *)
Adds a new IPV4 address to the Radix4 tree.
Definition: util-radix4-tree.c:188
SCRadix4ForEachNodeFunc
int(* SCRadix4ForEachNodeFunc)(const SCRadix4Node *node, void *user_data, const uint8_t netmask, void *data)
Definition: util-radix4-tree.h:106
SCRadix4Node_::right
struct SCRadix4Node_ * right
Definition: util-radix4-tree.h:57
SCRadix4Tree
struct SCRadix4Tree_ SCRadix4Tree
Structure for the radix tree.
SCRadix4TreeFindNetblock
SCRadix4Node * SCRadix4TreeFindNetblock(const SCRadix4Tree *, const uint8_t *, const uint8_t, void **)
Definition: util-radix4-tree.c:148
SCRadix4Node_::left
struct SCRadix4Node_ * left
Definition: util-radix4-tree.h:57
SCRadix4AddKeyIPV4Netblock
SCRadix4Node * SCRadix4AddKeyIPV4Netblock(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, uint8_t, void *)
Adds a new IPV4 netblock to the Radix4 tree.
Definition: util-radix4-tree.c:206