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 "util-storage.h"
31 
32 unsigned int SCFlowStorageSize(void)
33 {
35 }
36 
38 {
39  return SCStorageGetById(f->storage, STORAGE_FLOW, id.id);
40 }
41 
43 {
44  return SCStorageSetById(f->storage, STORAGE_FLOW, id.id, ptr);
45 }
46 
48 {
50 }
51 
53 {
54  if (SCFlowStorageSize() > 0)
56 }
57 
58 SCFlowStorageId SCFlowStorageRegister(const char *name, void (*Free)(void *))
59 {
60  int id = SCStorageRegister(STORAGE_FLOW, name, Free);
61  SCFlowStorageId fsi = { .id = id };
62  return fsi;
63 }
64 
65 #ifdef UNITTESTS
66 
67 #include "flow-util.h"
68 
69 static void StorageTestFree(void *x)
70 {
71  if (x)
72  SCFree(x);
73 }
74 
75 static int FlowStorageTest01(void)
76 {
78  SCStorageInit();
79 
80  SCFlowStorageId id1 = SCFlowStorageRegister("test", StorageTestFree);
81  FAIL_IF(id1.id < 0);
82  SCFlowStorageId id2 = SCFlowStorageRegister("variable", StorageTestFree);
83  FAIL_IF(id2.id < 0);
84  SCFlowStorageId id3 = SCFlowStorageRegister("store", StorageTestFree);
85  FAIL_IF(id3.id < 0);
86 
88 
90 
91  Flow *f = FlowAlloc();
92  FAIL_IF_NULL(f);
93 
94  void *ptr = SCFlowGetStorageById(f, id1);
95  FAIL_IF_NOT_NULL(ptr);
96  ptr = SCFlowGetStorageById(f, id2);
97  FAIL_IF_NOT_NULL(ptr);
98  ptr = SCFlowGetStorageById(f, id3);
99  FAIL_IF_NOT_NULL(ptr);
100 
101  void *ptr1a = SCMalloc(8);
102  FAIL_IF_NULL(ptr1a);
103  FAIL_IF(SCFlowSetStorageById(f, id1, ptr1a) != 0);
104  void *ptr2a = SCMalloc(24);
105  FAIL_IF_NULL(ptr2a);
106  FAIL_IF(SCFlowSetStorageById(f, id2, ptr2a) != 0);
107  void *ptr3a = SCMalloc(16);
108  FAIL_IF_NULL(ptr3a);
109  FAIL_IF(SCFlowSetStorageById(f, id3, ptr3a) != 0);
110 
111  void *ptr1b = SCFlowGetStorageById(f, id1);
112  FAIL_IF(ptr1a != ptr1b);
113  void *ptr2b = SCFlowGetStorageById(f, id2);
114  FAIL_IF(ptr2a != ptr2b);
115  void *ptr3b = SCFlowGetStorageById(f, id3);
116  FAIL_IF(ptr3a != ptr3b);
117 
118  FlowClearMemory(f, 0);
119  FlowFree(f);
120  FlowShutdown();
122  PASS;
123 }
124 
125 static int FlowStorageTest02(void)
126 {
128  SCStorageInit();
129 
130  SCFlowStorageId id1 = SCFlowStorageRegister("test", StorageTestFree);
131  FAIL_IF(id1.id < 0);
132 
133  FAIL_IF(SCStorageFinalize() < 0);
134 
136  Flow *f = FlowAlloc();
137  FAIL_IF_NULL(f);
138 
139  void *ptr = SCFlowGetStorageById(f, id1);
140  FAIL_IF_NOT_NULL(ptr);
141 
142  void *ptr1a = SCMalloc(128);
143  FAIL_IF_NULL(ptr1a);
144  SCFlowSetStorageById(f, id1, ptr1a);
145 
146  void *ptr1b = SCFlowGetStorageById(f, id1);
147  FAIL_IF(ptr1a != ptr1b);
148 
149  FlowClearMemory(f, 0);
150  FlowFree(f);
151  FlowShutdown();
153  PASS;
154 }
155 
156 static int FlowStorageTest03(void)
157 {
159  SCStorageInit();
160 
161  SCFlowStorageId id1 = SCFlowStorageRegister("test1", StorageTestFree);
162  FAIL_IF(id1.id < 0);
163  SCFlowStorageId id2 = SCFlowStorageRegister("test2", StorageTestFree);
164  FAIL_IF(id2.id < 0);
165  SCFlowStorageId id3 = SCFlowStorageRegister("test3", StorageTestFree);
166  FAIL_IF(id3.id < 0);
167 
168  FAIL_IF(SCStorageFinalize() < 0);
169 
171  Flow *f = FlowAlloc();
172  FAIL_IF_NULL(f);
173 
174  void *ptr = SCFlowGetStorageById(f, id1);
175  FAIL_IF_NOT_NULL(ptr);
176 
177  void *ptr1a = SCMalloc(128);
178  FAIL_IF_NULL(ptr1a);
179  SCFlowSetStorageById(f, id1, ptr1a);
180 
181  void *ptr2a = SCMalloc(256);
182  FAIL_IF_NULL(ptr2a);
183  SCFlowSetStorageById(f, id2, ptr2a);
184 
185  void *ptr3a = SCMalloc(32);
186  FAIL_IF_NULL(ptr3a);
187  SCFlowSetStorageById(f, id3, ptr3a);
188 
189  void *ptr1b = SCFlowGetStorageById(f, id1);
190  FAIL_IF(ptr1a != ptr1b);
191  void *ptr2b = SCFlowGetStorageById(f, id2);
192  FAIL_IF(ptr2a != ptr2b);
193  void *ptr3b = SCFlowGetStorageById(f, id3);
194  FAIL_IF(ptr3a != ptr3b);
195 
196  FlowClearMemory(f, 0);
197  FlowFree(f);
198  FlowShutdown();
200  PASS;
201 }
202 #endif
203 
205 {
206 #ifdef UNITTESTS
207  UtRegisterTest("FlowStorageTest01", FlowStorageTest01);
208  UtRegisterTest("FlowStorageTest02", FlowStorageTest02);
209  UtRegisterTest("FlowStorageTest03", FlowStorageTest03);
210 #endif
211 }
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
SCStorageInit
void SCStorageInit(void)
Definition: util-storage.c:67
flow-util.h
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SCFlowGetStorageById
void * SCFlowGetStorageById(const Flow *f, SCFlowStorageId id)
Definition: flow-storage.c:37
name
const char * name
Definition: detect-engine-proto.c:47
SCStorageFreeById
void SCStorageFreeById(Storage *storage, StorageEnum type, int id)
Definition: util-storage.c:229
Flow_
Flow data structure.
Definition: flow.h:359
SCFlowStorageSize
unsigned int SCFlowStorageSize(void)
Definition: flow-storage.c:32
SCFlowStorageId
Definition: flow-storage.h:33
SCStorageSetById
int SCStorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
Definition: util-storage.c:217
SCFlowStorageRegister
SCFlowStorageId SCFlowStorageRegister(const char *name, void(*Free)(void *))
Definition: flow-storage.c:58
FlowInitConfig
void FlowInitConfig(bool quiet)
initialize the configuration
Definition: flow.c:574
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
STORAGE_FLOW
@ STORAGE_FLOW
Definition: util-storage.h:31
SCFlowSetStorageById
int SCFlowSetStorageById(Flow *f, SCFlowStorageId id, void *ptr)
Definition: flow-storage.c:42
SCStorageGetById
void * SCStorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:206
SCStorageRegister
int SCStorageRegister(const StorageEnum type, const char *name, void(*Free)(void *))
Register new storage.
Definition: util-storage.c:99
SCFlowFreeStorageById
void SCFlowFreeStorageById(Flow *f, SCFlowStorageId id)
Definition: flow-storage.c:47
SCFlowFreeStorage
void SCFlowFreeStorage(Flow *f)
Definition: flow-storage.c:52
SCStorageGetSize
unsigned int SCStorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
Definition: util-storage.c:201
FlowClearMemory
int FlowClearMemory(Flow *f, uint8_t proto_map)
Function clear the flow memory before queueing it to spare flow queue.
Definition: flow.c:1124
flow-storage.h
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
FlowFree
void FlowFree(Flow *f)
cleanup & free the memory of a flow
Definition: flow-util.c:85
FlowShutdown
void FlowShutdown(void)
shutdown the flow engine
Definition: flow.c:718
SCStorageFreeAll
void SCStorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:251
SCRegisterFlowStorageTests
void SCRegisterFlowStorageTests(void)
Definition: flow-storage.c:204
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SCStorageCleanup
void SCStorageCleanup(void)
Definition: util-storage.c:75
FLOW_QUIET
#define FLOW_QUIET
Definition: flow.h:43
SCFlowStorageId::id
int id
Definition: flow-storage.h:34
FlowAlloc
Flow * FlowAlloc(void)
allocate a flow
Definition: flow-util.c:57
Flow_::storage
Storage storage[]
Definition: flow.h:505
util-storage.h
SCStorageFinalize
int SCStorageFinalize(void)
Definition: util-storage.c:134
f
Flow f
Definition: fuzz_dataset.c:32