suricata
ippair-storage.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2021 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 * IPPair wrapper around storage api
24 */
25
26#include "suricata-common.h"
27#include "ippair-storage.h"
28#include "util-unittest.h"
29
30unsigned int IPPairStorageSize(void)
31{
33}
34
39
41{
42 return StorageSetById(h->storage, STORAGE_IPPAIR, id.id, ptr);
43}
44
49
55
56IPPairStorageId IPPairStorageRegister(const char *name, const unsigned int size,
57 void *(*Alloc)(unsigned int), void (*Free)(void *))
58{
59 int id = StorageRegister(STORAGE_IPPAIR, name, size, Alloc, Free);
60 IPPairStorageId ippsi = { .id = id };
61 return ippsi;
62}
63
64#ifdef UNITTESTS
65
66static void *StorageTestAlloc(unsigned int size)
67{
68 void *x = SCMalloc(size);
69 return x;
70}
71static void StorageTestFree(void *x)
72{
73 if (x)
74 SCFree(x);
75}
76
77static int IPPairStorageTest01(void)
78{
80
81 IPPairStorageId id1 = IPPairStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
82 if (id1.id < 0)
83 goto error;
84 IPPairStorageId id2 = IPPairStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
85 if (id2.id < 0)
86 goto error;
87 IPPairStorageId id3 =
88 IPPairStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
89 if (id3.id < 0)
90 goto error;
91
92 if (StorageFinalize() < 0)
93 goto error;
94
96
97 Address a, b;
98 memset(&a, 0x00, sizeof(a));
99 memset(&b, 0x00, sizeof(b));
100 a.addr_data32[0] = 0x01020304;
101 b.addr_data32[0] = 0x04030201;
102 a.family = AF_INET;
103 b.family = AF_INET;
104 IPPair *h = IPPairGetIPPairFromHash(&a, &b);
105 if (h == NULL) {
106 printf("failed to get ippair: ");
107 goto error;
108 }
109
110 void *ptr = IPPairGetStorageById(h, id1);
111 if (ptr != NULL) {
112 goto error;
113 }
114 ptr = IPPairGetStorageById(h, id2);
115 if (ptr != NULL) {
116 goto error;
117 }
118 ptr = IPPairGetStorageById(h, id3);
119 if (ptr != NULL) {
120 goto error;
121 }
122
123 void *ptr1a = IPPairAllocStorageById(h, id1);
124 if (ptr1a == NULL) {
125 goto error;
126 }
127 void *ptr2a = IPPairAllocStorageById(h, id2);
128 if (ptr2a == NULL) {
129 goto error;
130 }
131 void *ptr3a = IPPairAllocStorageById(h, id3);
132 if (ptr3a == NULL) {
133 goto error;
134 }
135
136 void *ptr1b = IPPairGetStorageById(h, id1);
137 if (ptr1a != ptr1b) {
138 goto error;
139 }
140 void *ptr2b = IPPairGetStorageById(h, id2);
141 if (ptr2a != ptr2b) {
142 goto error;
143 }
144 void *ptr3b = IPPairGetStorageById(h, id3);
145 if (ptr3a != ptr3b) {
146 goto error;
147 }
148
149 IPPairRelease(h);
150
153 return 1;
154error:
157 return 0;
158}
159
160static int IPPairStorageTest02(void)
161{
162 StorageInit();
163
164 IPPairStorageId id1 = IPPairStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
165 if (id1.id < 0)
166 goto error;
167
168 if (StorageFinalize() < 0)
169 goto error;
170
172
173 Address a, b;
174 memset(&a, 0x00, sizeof(a));
175 memset(&b, 0x00, sizeof(b));
176 a.addr_data32[0] = 0x01020304;
177 b.addr_data32[0] = 0x04030201;
178 a.family = AF_INET;
179 b.family = AF_INET;
180 IPPair *h = IPPairGetIPPairFromHash(&a, &b);
181 if (h == NULL) {
182 printf("failed to get ippair: ");
183 goto error;
184 }
185
186 void *ptr = IPPairGetStorageById(h, 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 IPPairSetStorageById(h, id1, ptr1a);
196
197 void *ptr1b = IPPairGetStorageById(h, id1);
198 if (ptr1a != ptr1b) {
199 goto error;
200 }
201
202 IPPairRelease(h);
203
206 return 1;
207error:
210 return 0;
211}
212
213static int IPPairStorageTest03(void)
214{
215 StorageInit();
216
217 IPPairStorageId id1 = IPPairStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
218 if (id1.id < 0)
219 goto error;
220 IPPairStorageId id2 = IPPairStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
221 if (id2.id < 0)
222 goto error;
223 IPPairStorageId id3 = IPPairStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
224 if (id3.id < 0)
225 goto error;
226
227 if (StorageFinalize() < 0)
228 goto error;
229
231
232 Address a, b;
233 memset(&a, 0x00, sizeof(a));
234 memset(&b, 0x00, sizeof(b));
235 a.addr_data32[0] = 0x01020304;
236 b.addr_data32[0] = 0x04030201;
237 a.family = AF_INET;
238 b.family = AF_INET;
239 IPPair *h = IPPairGetIPPairFromHash(&a, &b);
240 if (h == NULL) {
241 printf("failed to get ippair: ");
242 goto error;
243 }
244
245 void *ptr = IPPairGetStorageById(h, id1);
246 if (ptr != NULL) {
247 goto error;
248 }
249
250 void *ptr1a = SCMalloc(128);
251 if (unlikely(ptr1a == NULL)) {
252 goto error;
253 }
254 IPPairSetStorageById(h, id1, ptr1a);
255
256 void *ptr2a = SCMalloc(256);
257 if (unlikely(ptr2a == NULL)) {
258 goto error;
259 }
260 IPPairSetStorageById(h, id2, ptr2a);
261
262 void *ptr3a = IPPairAllocStorageById(h, id3);
263 if (ptr3a == NULL) {
264 goto error;
265 }
266
267 void *ptr1b = IPPairGetStorageById(h, id1);
268 if (ptr1a != ptr1b) {
269 goto error;
270 }
271 void *ptr2b = IPPairGetStorageById(h, id2);
272 if (ptr2a != ptr2b) {
273 goto error;
274 }
275 void *ptr3b = IPPairGetStorageById(h, id3);
276 if (ptr3a != ptr3b) {
277 goto error;
278 }
279
280 IPPairRelease(h);
281
284 return 1;
285error:
288 return 0;
289}
290#endif
291
293{
294#ifdef UNITTESTS
295 UtRegisterTest("IPPairStorageTest01", IPPairStorageTest01);
296 UtRegisterTest("IPPairStorageTest02", IPPairStorageTest02);
297 UtRegisterTest("IPPairStorageTest03", IPPairStorageTest03);
298#endif
299}
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void * IPPairGetStorageById(IPPair *h, IPPairStorageId id)
IPPairStorageId IPPairStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
void IPPairFreeStorage(IPPair *h)
void RegisterIPPairStorageTests(void)
int IPPairSetStorageById(IPPair *h, IPPairStorageId id, void *ptr)
void * IPPairAllocStorageById(IPPair *h, IPPairStorageId id)
unsigned int IPPairStorageSize(void)
void IPPairRelease(IPPair *h)
Definition ippair.c:502
void IPPairInitConfig(bool quiet)
initialize the configuration
Definition ippair.c:162
void IPPairShutdown(void)
shutdown the flow engine
Definition ippair.c:290
IPPair * IPPairGetIPPairFromHash(Address *a, Address *b)
Definition ippair.c:521
char family
Definition decode.h:113
Storage storage[]
Definition ippair.h:77
const char * name
#define SCMalloc(sz)
Definition util-mem.h:47
#define SCFree(p)
Definition util-mem.h:61
#define unlikely(expr)
void * StorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
void StorageCleanup(void)
int StorageRegister(const StorageEnum type, const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
Register new storage.
unsigned int StorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
void StorageFreeAll(Storage *storage, StorageEnum type)
int StorageFinalize(void)
int StorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
void StorageInit(void)
void * StorageAllocByIdPrealloc(Storage *storage, StorageEnum type, int id)
AllocById func for prealloc'd base storage (storage ptrs are part of another memory block)
@ STORAGE_IPPAIR