suricata
flow-storage.c
Go to the documentation of this file.
1 /* Copyright (C) 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 Eric Leblond <eric@regit.org>
22  *
23  * based on host-storage by Victor Julien <victor@inliniac.net>
24  *
25  * Flow wrapper around storage api
26  */
27 
28 #include "suricata-common.h"
29 #include "flow-storage.h"
30 #include "flow-hash.h"
31 #include "flow-util.h"
32 #include "util-unittest.h"
33 
34 unsigned int FlowStorageSize(void)
35 {
37 }
38 
40 {
41  return StorageGetById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id.id);
42 }
43 
44 int FlowSetStorageById(Flow *f, FlowStorageId id, void *ptr)
45 {
46  return StorageSetById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id.id, ptr);
47 }
48 
50 {
51  return StorageAllocByIdPrealloc((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id.id);
52 }
53 
55 {
56  StorageFreeById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id.id);
57 }
58 
60 {
61  if (FlowStorageSize() > 0)
62  StorageFreeAll((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW);
63 }
64 
65 FlowStorageId FlowStorageRegister(const char *name, const unsigned int size,
66  void *(*Alloc)(unsigned int), void (*Free)(void *))
67 {
68  int id = StorageRegister(STORAGE_FLOW, name, size, Alloc, Free);
69  FlowStorageId fsi = { .id = id };
70  return fsi;
71 }
72 
73 #ifdef UNITTESTS
74 
75 static void *StorageTestAlloc(unsigned int size)
76 {
77  void *x = SCMalloc(size);
78  return x;
79 }
80 static void StorageTestFree(void *x)
81 {
82  if (x)
83  SCFree(x);
84 }
85 
86 static int FlowStorageTest01(void)
87 {
88  Flow *f = NULL;
89 
90  StorageInit();
91 
92  FlowStorageId id1 = FlowStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
93  if (id1.id < 0)
94  goto error;
95  FlowStorageId id2 = FlowStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
96  if (id2.id < 0)
97  goto error;
98  FlowStorageId id3 =
99  FlowStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
100  if (id3.id < 0)
101  goto error;
102 
103  if (StorageFinalize() < 0)
104  goto error;
105 
107 
108  f = FlowAlloc();
109  if (f == NULL) {
110  goto error;
111  }
112 
113  void *ptr = FlowGetStorageById(f, id1);
114  if (ptr != NULL) {
115  goto error;
116  }
117  ptr = FlowGetStorageById(f, id2);
118  if (ptr != NULL) {
119  goto error;
120  }
121  ptr = FlowGetStorageById(f, id3);
122  if (ptr != NULL) {
123  goto error;
124  }
125 
126  void *ptr1a = FlowAllocStorageById(f, id1);
127  if (ptr1a == NULL) {
128  goto error;
129  }
130  void *ptr2a = FlowAllocStorageById(f, id2);
131  if (ptr2a == NULL) {
132  goto error;
133  }
134  void *ptr3a = FlowAllocStorageById(f, id3);
135  if (ptr3a == NULL) {
136  goto error;
137  }
138 
139  void *ptr1b = FlowGetStorageById(f, id1);
140  if (ptr1a != ptr1b) {
141  goto error;
142  }
143  void *ptr2b = FlowGetStorageById(f, id2);
144  if (ptr2a != ptr2b) {
145  goto error;
146  }
147  void *ptr3b = FlowGetStorageById(f, id3);
148  if (ptr3a != ptr3b) {
149  goto error;
150  }
151 
152  FlowClearMemory(f, 0);
153  FlowFree(f);
154  FlowShutdown();
155  StorageCleanup();
156  return 1;
157 error:
158  if (f != NULL) {
159  FlowClearMemory(f, 0);
160  FlowFree(f);
161  }
162  FlowShutdown();
163  StorageCleanup();
164  return 0;
165 }
166 
167 static int FlowStorageTest02(void)
168 {
169  Flow *f = NULL;
170 
171  StorageInit();
172 
173  FlowStorageId id1 = FlowStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
174  if (id1.id < 0)
175  goto error;
176 
177  if (StorageFinalize() < 0)
178  goto error;
179 
181  f = FlowAlloc();
182  if (f == NULL) {
183  goto error;
184  }
185 
186  void *ptr = FlowGetStorageById(f, id1);
187  if (ptr != NULL) {
188  goto error;
189  }
190 
191  void *ptr1a = SCMalloc(128);
192  if (unlikely(ptr1a == NULL)) {
193  goto error;
194  }
195  FlowSetStorageById(f, id1, ptr1a);
196 
197  void *ptr1b = FlowGetStorageById(f, id1);
198  if (ptr1a != ptr1b) {
199  goto error;
200  }
201 
202 
203  FlowClearMemory(f, 0);
204  FlowFree(f);
205  FlowShutdown();
206  StorageCleanup();
207  return 1;
208 error:
209  if (f != NULL) {
210  FlowClearMemory(f, 0);
211  FlowFree(f);
212  }
213  FlowShutdown();
214  StorageCleanup();
215  return 0;
216 }
217 
218 static int FlowStorageTest03(void)
219 {
220  Flow *f = NULL;
221 
222  StorageInit();
223 
224  FlowStorageId id1 = FlowStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
225  if (id1.id < 0)
226  goto error;
227  FlowStorageId id2 = FlowStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
228  if (id2.id < 0)
229  goto error;
230  FlowStorageId id3 = FlowStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
231  if (id3.id < 0)
232  goto error;
233 
234  if (StorageFinalize() < 0)
235  goto error;
236 
238  f = FlowAlloc();
239  if (f == NULL) {
240  goto error;
241  }
242 
243  void *ptr = FlowGetStorageById(f, id1);
244  if (ptr != NULL) {
245  goto error;
246  }
247 
248  void *ptr1a = SCMalloc(128);
249  if (unlikely(ptr1a == NULL)) {
250  goto error;
251  }
252  FlowSetStorageById(f, id1, ptr1a);
253 
254  void *ptr2a = SCMalloc(256);
255  if (unlikely(ptr2a == NULL)) {
256  goto error;
257  }
258  FlowSetStorageById(f, id2, ptr2a);
259 
260  void *ptr3a = FlowAllocStorageById(f, id3);
261  if (ptr3a == NULL) {
262  goto error;
263  }
264 
265  void *ptr1b = FlowGetStorageById(f, id1);
266  if (ptr1a != ptr1b) {
267  goto error;
268  }
269  void *ptr2b = FlowGetStorageById(f, id2);
270  if (ptr2a != ptr2b) {
271  goto error;
272  }
273  void *ptr3b = FlowGetStorageById(f, id3);
274  if (ptr3a != ptr3b) {
275  goto error;
276  }
277 
278  FlowClearMemory(f, 0);
279  FlowFree(f);
280  FlowShutdown();
281  StorageCleanup();
282  return 1;
283 error:
284  if (f != NULL) {
285  FlowClearMemory(f, 0);
286  FlowFree(f);
287  }
288  FlowShutdown();
289  StorageCleanup();
290  return 0;
291 }
292 #endif
293 
295 {
296 #ifdef UNITTESTS
297  UtRegisterTest("FlowStorageTest01", FlowStorageTest01);
298  UtRegisterTest("FlowStorageTest02", FlowStorageTest02);
299  UtRegisterTest("FlowStorageTest03", FlowStorageTest03);
300 #endif
301 }
FlowStorageId
Definition: flow-storage.h:32
StorageFreeAll
void StorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:310
StorageInit
void StorageInit(void)
Definition: util-storage.c:67
flow-util.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
Flow_
Flow data structure.
Definition: flow.h:353
flow-hash.h
StorageCleanup
void StorageCleanup(void)
Definition: util-storage.c:75
FlowAllocStorageById
void * FlowAllocStorageById(Flow *f, FlowStorageId id)
Definition: flow-storage.c:49
util-unittest.h
FlowInitConfig
void FlowInitConfig(bool quiet)
initialize the configuration
Definition: flow.c:523
STORAGE_FLOW
@ STORAGE_FLOW
Definition: util-storage.h:31
FlowStorageSize
unsigned int FlowStorageSize(void)
Definition: flow-storage.c:34
FlowSetStorageById
int FlowSetStorageById(Flow *f, FlowStorageId id, void *ptr)
Definition: flow-storage.c:44
FlowStorageRegister
FlowStorageId FlowStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
Definition: flow-storage.c:65
StorageFinalize
int StorageFinalize(void)
Definition: util-storage.c:139
FlowClearMemory
int FlowClearMemory(Flow *f, uint8_t proto_map)
Function clear the flow memory before queueing it to spare flow queue.
Definition: flow.c:1067
FlowGetStorageById
void * FlowGetStorageById(Flow *f, FlowStorageId id)
Definition: flow-storage.c:39
flow-storage.h
suricata-common.h
FlowFree
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Definition: flow-util.c:80
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:651
StorageSetById
int StorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
Definition: util-storage.c:227
StorageAllocByIdPrealloc
void * StorageAllocByIdPrealloc(Storage *storage, StorageEnum type, int id)
AllocById func for prealloc'd base storage (storage ptrs are part of another memory block)
Definition: util-storage.c:239
StorageFreeById
void StorageFreeById(Storage *storage, StorageEnum type, int id)
Definition: util-storage.c:288
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
FlowFreeStorageById
void FlowFreeStorageById(Flow *f, FlowStorageId id)
Definition: flow-storage.c:54
SCFree
#define SCFree(p)
Definition: util-mem.h:61
FlowFreeStorage
void FlowFreeStorage(Flow *f)
Definition: flow-storage.c:59
Storage
void * Storage
Definition: util-storage.h:39
FlowStorageId::id
int id
Definition: flow-storage.h:33
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:42
FlowAlloc
Flow * FlowAlloc(void)
allocate a flow
Definition: flow-util.c:51
StorageGetById
void * StorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:216
RegisterFlowStorageTests
void RegisterFlowStorageTests(void)
Definition: flow-storage.c:294
StorageGetSize
unsigned int StorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
Definition: util-storage.c:211
StorageRegister
int StorageRegister(const StorageEnum type, const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
Register new storage.
Definition: util-storage.c:99