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 SCIPPairStorageSize(void)
31 {
33 }
34 
36 {
37  return SCStorageGetById(h->storage, STORAGE_IPPAIR, id.id);
38 }
39 
41 {
42  return SCStorageSetById(h->storage, STORAGE_IPPAIR, id.id, ptr);
43 }
44 
46 {
47  if (SCIPPairStorageSize() > 0)
49 }
50 
51 SCIPPairStorageId SCIPPairStorageRegister(const char *name, void (*Free)(void *))
52 {
53  int id = SCStorageRegister(STORAGE_IPPAIR, name, Free);
54  SCIPPairStorageId ippsi = { .id = id };
55  return ippsi;
56 }
57 
58 #ifdef UNITTESTS
59 
60 static void StorageTestFree(void *x)
61 {
62  if (x)
63  SCFree(x);
64 }
65 
66 static int IPPairStorageTest01(void)
67 {
69  SCStorageInit();
70 
71  SCIPPairStorageId id1 = SCIPPairStorageRegister("test", StorageTestFree);
72  FAIL_IF(id1.id < 0);
73  SCIPPairStorageId id2 = SCIPPairStorageRegister("variable", StorageTestFree);
74  FAIL_IF(id2.id < 0);
75  SCIPPairStorageId id3 = SCIPPairStorageRegister("store", StorageTestFree);
76  FAIL_IF(id3.id < 0);
77 
79 
81 
82  Address a, b;
83  memset(&a, 0x00, sizeof(a));
84  memset(&b, 0x00, sizeof(b));
85  a.addr_data32[0] = 0x01020304;
86  b.addr_data32[0] = 0x04030201;
87  a.family = AF_INET;
88  b.family = AF_INET;
89  IPPair *h = IPPairGetIPPairFromHash(&a, &b);
90  FAIL_IF_NULL(h);
91 
92  void *ptr = SCIPPairGetStorageById(h, id1);
93  FAIL_IF_NOT_NULL(ptr);
94  ptr = SCIPPairGetStorageById(h, id2);
95  FAIL_IF_NOT_NULL(ptr);
96  ptr = SCIPPairGetStorageById(h, id3);
97  FAIL_IF_NOT_NULL(ptr);
98 
99  void *ptr1a = SCMalloc(8);
100  FAIL_IF(ptr1a == NULL);
101  FAIL_IF(SCIPPairSetStorageById(h, id1, ptr1a) != 0);
102  void *ptr2a = SCMalloc(24);
103  FAIL_IF(ptr2a == NULL);
104  FAIL_IF(SCIPPairSetStorageById(h, id2, ptr2a) != 0);
105  void *ptr3a = SCMalloc(16);
106  FAIL_IF(ptr3a == NULL);
107  FAIL_IF(SCIPPairSetStorageById(h, id3, ptr3a) != 0);
108 
109  void *ptr1b = SCIPPairGetStorageById(h, id1);
110  FAIL_IF(ptr1a != ptr1b);
111  void *ptr2b = SCIPPairGetStorageById(h, id2);
112  FAIL_IF(ptr2a != ptr2b);
113  void *ptr3b = SCIPPairGetStorageById(h, id3);
114  FAIL_IF(ptr3a != ptr3b);
115 
116  IPPairRelease(h);
117  IPPairShutdown();
119  PASS;
120 }
121 
122 static int IPPairStorageTest02(void)
123 {
125  SCStorageInit();
126 
127  SCIPPairStorageId id1 = SCIPPairStorageRegister("test", StorageTestFree);
128  FAIL_IF(id1.id < 0);
129 
130  FAIL_IF(SCStorageFinalize() < 0);
131 
132  IPPairInitConfig(1);
133 
134  Address a, b;
135  memset(&a, 0x00, sizeof(a));
136  memset(&b, 0x00, sizeof(b));
137  a.addr_data32[0] = 0x01020304;
138  b.addr_data32[0] = 0x04030201;
139  a.family = AF_INET;
140  b.family = AF_INET;
141  IPPair *h = IPPairGetIPPairFromHash(&a, &b);
142  FAIL_IF(h == NULL);
143 
144  void *ptr = SCIPPairGetStorageById(h, id1);
145  FAIL_IF_NOT_NULL(ptr);
146 
147  void *ptr1a = SCMalloc(128);
148  FAIL_IF(ptr1a == NULL);
149 
150  SCIPPairSetStorageById(h, id1, ptr1a);
151 
152  void *ptr1b = SCIPPairGetStorageById(h, id1);
153  FAIL_IF(ptr1a != ptr1b);
154 
155  IPPairRelease(h);
156  IPPairShutdown();
158  PASS;
159 }
160 
161 static int IPPairStorageTest03(void)
162 {
164  SCStorageInit();
165 
166  SCIPPairStorageId id1 = SCIPPairStorageRegister("test1", StorageTestFree);
167  FAIL_IF(id1.id < 0);
168  SCIPPairStorageId id2 = SCIPPairStorageRegister("test2", StorageTestFree);
169  FAIL_IF(id2.id < 0);
170  SCIPPairStorageId id3 = SCIPPairStorageRegister("test3", StorageTestFree);
171  FAIL_IF(id3.id < 0);
172 
173  FAIL_IF(SCStorageFinalize() < 0);
174 
175  IPPairInitConfig(1);
176 
177  Address a, b;
178  memset(&a, 0x00, sizeof(a));
179  memset(&b, 0x00, sizeof(b));
180  a.addr_data32[0] = 0x01020304;
181  b.addr_data32[0] = 0x04030201;
182  a.family = AF_INET;
183  b.family = AF_INET;
184  IPPair *h = IPPairGetIPPairFromHash(&a, &b);
185  FAIL_IF(h == NULL);
186 
187  void *ptr = SCIPPairGetStorageById(h, id1);
188  FAIL_IF_NOT_NULL(ptr);
189 
190  void *ptr1a = SCMalloc(128);
191  FAIL_IF(ptr1a == NULL);
192 
193  SCIPPairSetStorageById(h, id1, ptr1a);
194 
195  void *ptr2a = SCMalloc(256);
196  FAIL_IF(ptr2a == NULL);
197 
198  SCIPPairSetStorageById(h, id2, ptr2a);
199 
200  void *ptr3a = SCMalloc(32);
201  FAIL_IF(ptr3a == NULL);
202  SCIPPairSetStorageById(h, id3, ptr3a);
203 
204  void *ptr1b = SCIPPairGetStorageById(h, id1);
205  FAIL_IF(ptr1a != ptr1b);
206  void *ptr2b = SCIPPairGetStorageById(h, id2);
207  FAIL_IF(ptr2a != ptr2b);
208  void *ptr3b = SCIPPairGetStorageById(h, id3);
209  FAIL_IF(ptr3a != ptr3b);
210 
211  IPPairRelease(h);
212  IPPairShutdown();
214  PASS;
215 }
216 #endif
217 
219 {
220 #ifdef UNITTESTS
221  UtRegisterTest("IPPairStorageTest01", IPPairStorageTest01);
222  UtRegisterTest("IPPairStorageTest02", IPPairStorageTest02);
223  UtRegisterTest("IPPairStorageTest03", IPPairStorageTest03);
224 #endif
225 }
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
IPPairInitConfig
void IPPairInitConfig(bool quiet)
initialize the configuration
Definition: ippair.c:162
SCIPPairStorageId
Definition: ippair-storage.h:31
IPPairRelease
void IPPairRelease(IPPair *h)
Definition: ippair.c:502
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
name
const char * name
Definition: detect-engine-proto.c:48
SCIPPairGetStorageById
void * SCIPPairGetStorageById(IPPair *h, SCIPPairStorageId id)
Definition: ippair-storage.c:35
SCIPPairStorageRegister
SCIPPairStorageId SCIPPairStorageRegister(const char *name, void(*Free)(void *))
Definition: ippair-storage.c:51
STORAGE_IPPAIR
@ STORAGE_IPPAIR
Definition: util-storage.h:32
Address_
Definition: decode.h:113
SCStorageSetById
int SCStorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
Definition: util-storage.c:218
SCIPPairSetStorageById
int SCIPPairSetStorageById(IPPair *h, SCIPPairStorageId id, void *ptr)
Definition: ippair-storage.c:40
util-unittest.h
IPPairShutdown
void IPPairShutdown(void)
shutdown the flow engine
Definition: ippair.c:290
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
IPPair_::storage
Storage storage[]
Definition: ippair.h:77
SCRegisterIPPairStorageTests
void SCRegisterIPPairStorageTests(void)
Definition: ippair-storage.c:218
SCIPPairStorageId::id
int id
Definition: ippair-storage.h:32
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
IPPairGetIPPairFromHash
IPPair * IPPairGetIPPairFromHash(Address *a, Address *b)
Definition: ippair.c:521
SCStorageGetSize
unsigned int SCStorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
Definition: util-storage.c:202
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
IPPair_
Definition: ippair.h:58
SCStorageFreeAll
void SCStorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:252
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
Address_::family
char family
Definition: decode.h:114
SCIPPairStorageSize
unsigned int SCIPPairStorageSize(void)
Definition: ippair-storage.c:30
ippair-storage.h
SCStorageFinalize
int SCStorageFinalize(void)
Definition: util-storage.c:135
SCIPPairFreeStorage
void SCIPPairFreeStorage(IPPair *h)
Definition: ippair-storage.c:45