suricata
util-var.c
Go to the documentation of this file.
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"
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 {
42  if (fb == NULL)
43  return;
44 
45  SCFree(fb);
46 }
47 
49 {
50  if (gv == NULL)
51  return;
52 
53  SCLogDebug("gv %p, gv->type %" PRIu32 "", gv, gv->type);
54  GenericVar *next_gv = gv->next;
55 
56  switch (gv->type) {
57  case DETECT_FLOWBITS:
58  {
59  FlowBit *fb = (FlowBit *)gv;
60  //printf("GenericVarFree: fb %p, removing\n", fb);
61  FlowBitFree(fb);
62  break;
63  }
64  case DETECT_XBITS:
65  {
66  XBit *fb = (XBit *)gv;
67  //printf("GenericVarFree: fb %p, removing\n", fb);
68  XBitFree(fb);
69  break;
70  }
71  case DETECT_THRESHOLD: {
73  break;
74  }
75  case DETECT_FLOWVAR:
76  {
77  FlowVar *fv = (FlowVar *)gv;
78  FlowVarFree(fv);
79  break;
80  }
81  case DETECT_PKTVAR:
82  {
83  PktVar *pv = (PktVar *)gv;
84  PktVarFree(pv);
85  break;
86  }
87  default:
88  {
89  SCLogDebug("GenericVarFree unknown type %" PRIu32, gv->type);
91  break;
92  }
93  }
94 
95  GenericVarFree(next_gv);
96 }
97 
99 {
100  gv->next = NULL;
101 
102  if (*list == NULL) {
103  *list = gv;
104  } else {
105  GenericVar *tgv = *list;
106  while(tgv) {
107  if (tgv->next == NULL) {
108  tgv->next = gv;
109  return;
110  }
111 
112  tgv = tgv->next;
113  }
114  }
115 }
116 
118 {
119  if (*list == NULL)
120  return;
121 
122  GenericVar *listgv = *list, *prevgv = NULL;
123  while (listgv != NULL) {
124  if (listgv == gv) {
125  if (prevgv == NULL)
126  *list = gv->next;
127  else
128  prevgv->next = gv->next;
129 
130  return;
131  }
132 
133  prevgv = listgv;
134  listgv = listgv->next;
135  }
136 }
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 {
141  ResolvedVariable *p_item;
142 
143  if (list == NULL || var == NULL)
144  return 0;
145 
146  if (var[0] != '$') {
147  return 0;
148  }
149 
150  TAILQ_FOREACH(p_item, list, next) {
151  if (!strcmp(p_item->var_name, var)) {
152  return -1;
153  }
154  }
155 
156  p_item = SCMalloc(sizeof(ResolvedVariable));
157 
158  if (unlikely(p_item == NULL)) {
159  return -1;
160  }
161 
162  strlcpy(p_item->var_name, var, sizeof(p_item->var_name) - 1);
163  TAILQ_INSERT_TAIL(list, p_item, next);
164 
165  return 0;
166 }
167 
168 void CleanVariableResolveList(ResolvedVariablesList *var_list)
169 {
170  if (var_list == NULL) {
171  return;
172  }
173 
174  ResolvedVariable *p_item;
175 
176  while ((p_item = TAILQ_FIRST(var_list))) {
177  TAILQ_REMOVE(var_list, p_item, next);
178  SCFree(p_item);
179  }
180 }
GenericVar_::type
uint8_t type
Definition: util-var.h:51
GenericVarAppend
void GenericVarAppend(GenericVar **list, GenericVar *gv)
Definition: util-var.c:98
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
ippair-bit.h
ResolvedVariable_
Definition: util-var.h:69
FlowVarFree
void FlowVarFree(FlowVar *fv)
Definition: flow-var.c:161
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
flow-bit.h
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
CleanVariableResolveList
void CleanVariableResolveList(ResolvedVariablesList *var_list)
Definition: util-var.c:168
util-var.h
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
TAILQ_REMOVE
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:312
util-debug.h
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
GenericVar_::next
struct GenericVar_ * next
Definition: util-var.h:54
DETECT_FLOWVAR
@ DETECT_FLOWVAR
Definition: detect-engine-register.h:61
DETECT_THRESHOLD
@ DETECT_THRESHOLD
Definition: detect-engine-register.h:59
detect.h
pkt-var.h
ResolvedVariable_::var_name
char var_name[256]
Definition: util-var.h:70
DETECT_XBITS
@ DETECT_XBITS
Definition: detect-engine-register.h:65
GenericVarRemove
void GenericVarRemove(GenericVar **list, GenericVar *gv)
Definition: util-var.c:117
suricata-common.h
GenericVar_
Definition: util-var.h:50
DETECT_FLOWBITS
@ DETECT_FLOWBITS
Definition: detect-engine-register.h:60
util-validate.h
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
FlowBit_
Definition: flow-bit.h:30
FlowBitFree
void FlowBitFree(FlowBit *fb)
Definition: flow-bit.c:128
SCFree
#define SCFree(p)
Definition: util-mem.h:61
PktVarFree
void PktVarFree(PktVar *pv)
Definition: pkt-var.c:111
AddVariableToResolveList
int AddVariableToResolveList(ResolvedVariablesList *list, const char *var)
Definition: util-var.c:139
PktVar_
Definition: decode.h:294
XBit_
Definition: util-var.h:57
GenericVarFree
void GenericVarFree(GenericVar *gv)
Definition: util-var.c:48
FlowThresholdVarFree
void FlowThresholdVarFree(void *ptr)
Definition: detect-engine-threshold.c:533
flow-var.h
FlowVar_
Definition: flow-var.h:48
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
host-bit.h
detect-engine-threshold.h
DETECT_PKTVAR
@ DETECT_PKTVAR
Definition: detect-engine-register.h:66
XBitFree
void XBitFree(XBit *fb)
Definition: util-var.c:40