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(h->storage, STORAGE_IPPAIR, id.id);
38 }
39 
41 {
42  return StorageSetById(h->storage, STORAGE_IPPAIR, id.id, ptr);
43 }
44 
46 {
48 }
49 
51 {
52  if (IPPairStorageSize() > 0)
54 }
55 
56 IPPairStorageId 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 
66 static void *StorageTestAlloc(unsigned int size)
67 {
68  void *x = SCMalloc(size);
69  return x;
70 }
71 static void StorageTestFree(void *x)
72 {
73  if (x)
74  SCFree(x);
75 }
76 
77 static int IPPairStorageTest01(void)
78 {
79  StorageInit();
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 
151  IPPairShutdown();
152  StorageCleanup();
153  return 1;
154 error:
155  IPPairShutdown();
156  StorageCleanup();
157  return 0;
158 }
159 
160 static 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 
171  IPPairInitConfig(1);
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 
204  IPPairShutdown();
205  StorageCleanup();
206  return 1;
207 error:
208  IPPairShutdown();
209  StorageCleanup();
210  return 0;
211 }
212 
213 static 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 
230  IPPairInitConfig(1);
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 
282  IPPairShutdown();
283  StorageCleanup();
284  return 1;
285 error:
286  IPPairShutdown();
287  StorageCleanup();
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 }
IPPairStorageRegister
IPPairStorageId IPPairStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
Definition: ippair-storage.c:56
StorageFreeAll
void StorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:278
IPPairInitConfig
void IPPairInitConfig(bool quiet)
initialize the configuration
Definition: ippair.c:162
StorageInit
void StorageInit(void)
Definition: util-storage.c:70
IPPairRelease
void IPPairRelease(IPPair *h)
Definition: ippair.c:505
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:78
STORAGE_IPPAIR
@ STORAGE_IPPAIR
Definition: util-storage.h:32
Address_
Definition: decode.h:108
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:293
IPPair_::storage
Storage storage[]
Definition: ippair.h:77
IPPairGetIPPairFromHash
IPPair * IPPairGetIPPairFromHash(Address *a, Address *b)
Definition: ippair.c:524
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:292
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:226
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:238
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
Address_::family
char family
Definition: decode.h:109
IPPairFreeStorage
void IPPairFreeStorage(IPPair *h)
Definition: ippair-storage.c:50
StorageGetById
void * StorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:215
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:210
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:102