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