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
31struct RadixUserData;
32
33/**
34 * \brief Structure for the node in the radix tree
35 */
36typedef 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 */
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 */
66typedef struct SCRadix4Tree_ {
67 /** the root node in the radix tree */
70
71typedef 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
86SCRadix4Node *SCRadix4AddKeyIPV4(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, void *);
88 SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, uint8_t, void *);
89bool SCRadix4AddKeyIPV4String(SCRadix4Tree *, const SCRadix4Config *, const char *, void *);
90
92 SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, uint8_t);
93void SCRadix4RemoveKeyIPV4(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *);
94
95SCRadix4Node *SCRadix4TreeFindExactMatch(const SCRadix4Tree *, const uint8_t *, void **);
97 const SCRadix4Tree *, const uint8_t *, const uint8_t, void **);
98SCRadix4Node *SCRadix4TreeFindBestMatch(const SCRadix4Tree *, const uint8_t *, void **);
99SCRadix4Node *SCRadix4TreeFindBestMatch2(const SCRadix4Tree *, const uint8_t *, void **, uint8_t *);
100
101void SCRadix4PrintTree(SCRadix4Tree *, const SCRadix4Config *config);
102void SCRadix4PrintNodeInfo(SCRadix4Node *, int, void (*PrintData)(void *));
103
104void SCRadix4RegisterTests(void);
105
107 const SCRadix4Node *node, void *user_data, const uint8_t netmask, void *data);
108
109int 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 */
115typedef 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 */
Structure that hold the user data and the netmask associated with it.
void(* Free)(void *)
void(* PrintData)(void *)
Structure for the node in the radix tree.
struct SCRadix4Node_ * right
uint8_t prefix_stream[4]
struct SCRadix4Node_ * parent
struct SCRadix4Node_ * left
struct RadixUserData * user_data
Structure for the radix tree.
SCRadix4Node * head
bool SCRadix4AddKeyIPV4String(SCRadix4Tree *, const SCRadix4Config *, const char *, void *)
Adds a new IPV4/netblock to the Radix4 tree from a string.
void SCRadix4PrintNodeInfo(SCRadix4Node *, int, void(*PrintData)(void *))
struct SCRadix4Config_ SCRadix4Config
SCRadix4Tree SCRadix4TreeInitialize(void)
void SCRadix4RemoveKeyIPV4Netblock(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, uint8_t)
Removes an IPV4 address netblock key from the Radix4 tree.
SCRadix4Node * SCRadix4TreeFindBestMatch(const SCRadix4Tree *, const uint8_t *, void **)
SCRadix4Node * SCRadix4TreeFindBestMatch2(const SCRadix4Tree *, const uint8_t *, void **, uint8_t *)
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,...
struct SCRadix4Tree_ SCRadix4Tree
Structure for the radix tree.
struct SCRadix4Node_ SCRadix4Node
Structure for the node in the radix tree.
bool(* SCRadix4TreeCompareFunc)(const void *ud1, const void *ud2)
compare content of 2 user data entries
int(* SCRadix4ForEachNodeFunc)(const SCRadix4Node *node, void *user_data, const uint8_t netmask, void *data)
void SCRadix4PrintTree(SCRadix4Tree *, const SCRadix4Config *config)
SCRadix4Node * SCRadix4AddKeyIPV4(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, void *)
Adds a new IPV4 address to the Radix4 tree.
SCRadix4Node * SCRadix4AddKeyIPV4Netblock(SCRadix4Tree *, const SCRadix4Config *, const uint8_t *, uint8_t, void *)
Adds a new IPV4 netblock to the Radix4 tree.
int SCRadix4ForEachNode(const SCRadix4Tree *tree, SCRadix4ForEachNodeFunc Callback, void *data)
SCRadix4Node * SCRadix4TreeFindExactMatch(const SCRadix4Tree *, const uint8_t *, void **)
void SCRadix4RegisterTests(void)
void SCRadix4TreeRelease(SCRadix4Tree *, const SCRadix4Config *)
bool SCRadix4CompareTrees(const SCRadix4Tree *t1, const SCRadix4Tree *t2, SCRadix4TreeCompareFunc Callback)
SCRadix4Node * SCRadix4TreeFindNetblock(const SCRadix4Tree *, const uint8_t *, const uint8_t, void **)