/src/suricata8/src/util-var.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2024 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 | | * |
23 | | * Generic variable utility functions |
24 | | */ |
25 | | |
26 | | #include "suricata-common.h" |
27 | | #include "detect.h" |
28 | | #include "detect-engine-threshold.h" |
29 | | |
30 | | #include "util-var.h" |
31 | | |
32 | | #include "flow-var.h" |
33 | | #include "flow-bit.h" |
34 | | #include "pkt-var.h" |
35 | | #include "host-bit.h" |
36 | | #include "ippair-bit.h" |
37 | | #include "util-validate.h" |
38 | | #include "util-debug.h" |
39 | | |
40 | | void XBitFree(XBit *fb) |
41 | 0 | { |
42 | 0 | if (fb == NULL) |
43 | 0 | return; |
44 | | |
45 | 0 | SCFree(fb); |
46 | 0 | } |
47 | | |
48 | | void GenericVarFree(GenericVar *gv) |
49 | 202k | { |
50 | 202k | if (gv == NULL) |
51 | 202k | return; |
52 | | |
53 | 55 | SCLogDebug("gv %p, gv->type %" PRIu32 "", gv, gv->type); |
54 | 55 | GenericVar *next_gv = gv->next; |
55 | | |
56 | 55 | switch (gv->type) { |
57 | 52 | case DETECT_FLOWBITS: |
58 | 52 | { |
59 | 52 | FlowBit *fb = (FlowBit *)gv; |
60 | | //printf("GenericVarFree: fb %p, removing\n", fb); |
61 | 52 | FlowBitFree(fb); |
62 | 52 | break; |
63 | 0 | } |
64 | 0 | case DETECT_XBITS: |
65 | 0 | { |
66 | 0 | XBit *fb = (XBit *)gv; |
67 | | //printf("GenericVarFree: fb %p, removing\n", fb); |
68 | 0 | XBitFree(fb); |
69 | 0 | break; |
70 | 0 | } |
71 | 0 | case DETECT_THRESHOLD: { |
72 | 0 | FlowThresholdVarFree(gv); |
73 | 0 | break; |
74 | 0 | } |
75 | 3 | case DETECT_FLOWVAR: |
76 | 3 | { |
77 | 3 | FlowVar *fv = (FlowVar *)gv; |
78 | 3 | FlowVarFree(fv); |
79 | 3 | break; |
80 | 0 | } |
81 | 0 | case DETECT_PKTVAR: |
82 | 0 | { |
83 | 0 | PktVar *pv = (PktVar *)gv; |
84 | 0 | PktVarFree(pv); |
85 | 0 | break; |
86 | 0 | } |
87 | 0 | default: |
88 | 0 | { |
89 | 0 | SCLogDebug("GenericVarFree unknown type %" PRIu32, gv->type); |
90 | 0 | DEBUG_VALIDATE_BUG_ON(1); |
91 | 0 | break; |
92 | 0 | } |
93 | 55 | } |
94 | | |
95 | 55 | GenericVarFree(next_gv); |
96 | 55 | } |
97 | | |
98 | | void GenericVarAppend(GenericVar **list, GenericVar *gv) |
99 | 4.64k | { |
100 | 4.64k | gv->next = NULL; |
101 | | |
102 | 4.64k | if (*list == NULL) { |
103 | 3.46k | *list = gv; |
104 | 3.46k | } else { |
105 | 1.18k | GenericVar *tgv = *list; |
106 | 2.27k | while(tgv) { |
107 | 2.27k | if (tgv->next == NULL) { |
108 | 1.18k | tgv->next = gv; |
109 | 1.18k | return; |
110 | 1.18k | } |
111 | | |
112 | 1.08k | tgv = tgv->next; |
113 | 1.08k | } |
114 | 1.18k | } |
115 | 4.64k | } |
116 | | |
117 | | void GenericVarRemove(GenericVar **list, GenericVar *gv) |
118 | 77 | { |
119 | 77 | if (*list == NULL) |
120 | 0 | return; |
121 | | |
122 | 77 | GenericVar *listgv = *list, *prevgv = NULL; |
123 | 105 | while (listgv != NULL) { |
124 | 105 | if (listgv == gv) { |
125 | 77 | if (prevgv == NULL) |
126 | 49 | *list = gv->next; |
127 | 28 | else |
128 | 28 | prevgv->next = gv->next; |
129 | | |
130 | 77 | return; |
131 | 77 | } |
132 | | |
133 | 28 | prevgv = listgv; |
134 | 28 | listgv = listgv->next; |
135 | 28 | } |
136 | 77 | } |
137 | | |
138 | | // Checks if a variable is already in a resolve list and if it's not, adds it. |
139 | | int AddVariableToResolveList(ResolvedVariablesList *list, const char *var) |
140 | 4.12M | { |
141 | 4.12M | ResolvedVariable *p_item; |
142 | | |
143 | 4.12M | if (list == NULL || var == NULL) |
144 | 4.12M | return 0; |
145 | | |
146 | 0 | if (var[0] != '$') { |
147 | 0 | return 0; |
148 | 0 | } |
149 | | |
150 | 0 | TAILQ_FOREACH(p_item, list, next) { |
151 | 0 | if (!strcmp(p_item->var_name, var)) { |
152 | 0 | return -1; |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | 0 | p_item = SCMalloc(sizeof(ResolvedVariable)); |
157 | |
|
158 | 0 | if (unlikely(p_item == NULL)) { |
159 | 0 | return -1; |
160 | 0 | } |
161 | | |
162 | 0 | strlcpy(p_item->var_name, var, sizeof(p_item->var_name) - 1); |
163 | 0 | TAILQ_INSERT_TAIL(list, p_item, next); |
164 | |
|
165 | 0 | return 0; |
166 | 0 | } |
167 | | |
168 | | void CleanVariableResolveList(ResolvedVariablesList *var_list) |
169 | 0 | { |
170 | 0 | if (var_list == NULL) { |
171 | 0 | return; |
172 | 0 | } |
173 | | |
174 | 0 | ResolvedVariable *p_item; |
175 | |
|
176 | 0 | while ((p_item = TAILQ_FIRST(var_list))) { |
177 | 0 | TAILQ_REMOVE(var_list, p_item, next); |
178 | 0 | SCFree(p_item); |
179 | 0 | } |
180 | 0 | } |