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