suricata
util-var.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 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 
29 #include "util-var.h"
30 
31 #include "flow-var.h"
32 #include "flow-bit.h"
33 #include "pkt-var.h"
34 #include "host-bit.h"
35 #include "ippair-bit.h"
36 
37 #include "util-debug.h"
38 
39 void XBitFree(XBit *fb)
40 {
41  if (fb == NULL)
42  return;
43 
44  SCFree(fb);
45 }
46 
48 {
49  if (gv == NULL)
50  return;
51 
52  SCLogDebug("gv %p, gv->type %" PRIu32 "", gv, gv->type);
53  GenericVar *next_gv = gv->next;
54 
55  switch (gv->type) {
56  case DETECT_FLOWBITS:
57  {
58  FlowBit *fb = (FlowBit *)gv;
59  //printf("GenericVarFree: fb %p, removing\n", fb);
60  FlowBitFree(fb);
61  break;
62  }
63  case DETECT_XBITS:
64  {
65  XBit *fb = (XBit *)gv;
66  //printf("GenericVarFree: fb %p, removing\n", fb);
67  XBitFree(fb);
68  break;
69  }
70  case DETECT_FLOWVAR:
71  {
72  FlowVar *fv = (FlowVar *)gv;
73  FlowVarFree(fv);
74  break;
75  }
76  case DETECT_PKTVAR:
77  {
78  PktVar *pv = (PktVar *)gv;
79  PktVarFree(pv);
80  break;
81  }
82  default:
83  {
84  printf("ERROR: GenericVarFree unknown type %" PRIu32 "\n", gv->type);
85  break;
86  }
87  }
88 
89  GenericVarFree(next_gv);
90 }
91 
93 {
94  gv->next = NULL;
95 
96  if (*list == NULL) {
97  *list = gv;
98  } else {
99  GenericVar *tgv = *list;
100  while(tgv) {
101  if (tgv->next == NULL) {
102  tgv->next = gv;
103  return;
104  }
105 
106  tgv = tgv->next;
107  }
108  }
109 }
110 
112 {
113  if (*list == NULL)
114  return;
115 
116  GenericVar *listgv = *list, *prevgv = NULL;
117  while (listgv != NULL) {
118  if (listgv == gv) {
119  if (prevgv == NULL)
120  *list = gv->next;
121  else
122  prevgv->next = gv->next;
123 
124  return;
125  }
126 
127  prevgv = listgv;
128  listgv = listgv->next;
129  }
130 }
131 
132 // Checks if a variable is already in a resolve list and if it's not, adds it.
133 int AddVariableToResolveList(ResolvedVariablesList *list, const char *var)
134 {
135  ResolvedVariable *p_item;
136 
137  if (list == NULL || var == NULL)
138  return 0;
139 
140  if (var[0] != '$') {
141  return 0;
142  }
143 
144  TAILQ_FOREACH(p_item, list, next) {
145  if (!strcmp(p_item->var_name, var)) {
146  return -1;
147  }
148  }
149 
150  p_item = SCMalloc(sizeof(ResolvedVariable));
151 
152  if (unlikely(p_item == NULL)) {
153  return -1;
154  }
155 
156  strlcpy(p_item->var_name, var, sizeof(p_item->var_name) - 1);
157  TAILQ_INSERT_TAIL(list, p_item, next);
158 
159  return 0;
160 }
161 
162 void CleanVariableResolveList(ResolvedVariablesList *var_list)
163 {
164  if (var_list == NULL) {
165  return;
166  }
167 
168  ResolvedVariable *p_item;
169 
170  while ((p_item = TAILQ_FIRST(var_list))) {
171  TAILQ_REMOVE(var_list, p_item, next);
172  SCFree(p_item);
173  }
174 }
uint8_t type
Definition: util-var.h:49
#define SCLogDebug(...)
Definition: util-debug.h:335
void CleanVariableResolveList(ResolvedVariablesList *var_list)
Definition: util-var.c:162
#define TAILQ_FIRST(head)
Definition: queue.h:339
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:350
struct HtpBodyChunk_ * next
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
Definition: util-var.h:55
#define unlikely(expr)
Definition: util-optimize.h:35
void FlowBitFree(FlowBit *fb)
Definition: flow-bit.c:128
void FlowVarFree(FlowVar *fv)
Definition: flow-var.c:161
struct GenericVar_ * next
Definition: util-var.h:52
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:412
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:385
#define SCMalloc(a)
Definition: util-mem.h:222
void PktVarFree(PktVar *pv)
Definition: pkt-var.c:111
void GenericVarRemove(GenericVar **list, GenericVar *gv)
Definition: util-var.c:111
#define SCFree(a)
Definition: util-mem.h:322
void GenericVarFree(GenericVar *gv)
Definition: util-var.c:47
void GenericVarAppend(GenericVar **list, GenericVar *gv)
Definition: util-var.c:92
char var_name[256]
Definition: util-var.h:68
void XBitFree(XBit *fb)
Definition: util-var.c:39
int AddVariableToResolveList(ResolvedVariablesList *list, const char *var)
Definition: util-var.c:133