suricata
ippair-storage.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-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 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 
35 void *IPPairGetStorageById(IPPair *h, int id)
36 {
37  return StorageGetById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id);
38 }
39 
40 int IPPairSetStorageById(IPPair *h, int id, void *ptr)
41 {
42  return StorageSetById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id, ptr);
43 }
44 
45 void *IPPairAllocStorageById(IPPair *h, int id)
46 {
47  return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id);
48 }
49 
50 void IPPairFreeStorageById(IPPair *h, int id)
51 {
52  StorageFreeById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id);
53 }
54 
56 {
57  if (IPPairStorageSize() > 0)
58  StorageFreeAll((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR);
59 }
60 
61 int IPPairStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
62  return StorageRegister(STORAGE_IPPAIR, name, size, Alloc, Free);
63 }
64 
65 #ifdef UNITTESTS
66 
67 static void *StorageTestAlloc(unsigned int size)
68 {
69  void *x = SCMalloc(size);
70  return x;
71 }
72 static void StorageTestFree(void *x)
73 {
74  if (x)
75  SCFree(x);
76 }
77 
78 static int IPPairStorageTest01(void)
79 {
80  StorageInit();
81 
82  int id1 = IPPairStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
83  if (id1 < 0)
84  goto error;
85  int id2 = IPPairStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
86  if (id2 < 0)
87  goto error;
88  int id3 = IPPairStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
89  if (id3 < 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  int id1 = IPPairStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
165  if (id1 < 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  int id1 = IPPairStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
218  if (id1 < 0)
219  goto error;
220  int id2 = IPPairStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
221  if (id2 < 0)
222  goto error;
223  int id3 = IPPairStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
224  if (id3 < 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 }
int IPPairStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
int StorageFinalize(void)
Definition: util-storage.c:139
#define unlikely(expr)
Definition: util-optimize.h:35
int StorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
Definition: util-storage.c:227
IPPair * IPPairGetIPPairFromHash(Address *a, Address *b)
Definition: ippair.c:544
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:99
void RegisterIPPairStorageTests(void)
void IPPairInitConfig(char quiet)
initialize the configuration
Definition: ippair.c:164
void * StorageAllocByIdPrealloc(Storage *storage, StorageEnum type, int id)
AllocById func for prealloc&#39;d base storage (storage ptrs are part of another memory block) ...
Definition: util-storage.c:239
char family
Definition: decode.h:112
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void IPPairFreeStorageById(IPPair *h, int id)
Definition: ippair.h:58
void * IPPairGetStorageById(IPPair *h, int id)
void * Storage
Definition: util-storage.h:39
#define SCMalloc(a)
Definition: util-mem.h:222
void StorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:310
int IPPairSetStorageById(IPPair *h, int id, void *ptr)
#define SCFree(a)
Definition: util-mem.h:322
unsigned int StorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
Definition: util-storage.c:211
void StorageInit(void)
Definition: util-storage.c:67
void StorageCleanup(void)
Definition: util-storage.c:75
void * StorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:216
void * IPPairAllocStorageById(IPPair *h, int id)
void IPPairFreeStorage(IPPair *h)
void StorageFreeById(Storage *storage, StorageEnum type, int id)
Definition: util-storage.c:288
void IPPairRelease(IPPair *h)
Definition: ippair.c:520
unsigned int IPPairStorageSize(void)
void IPPairShutdown(void)
shutdown the flow engine
Definition: ippair.c:296