/src/suricata7/src/ippair-storage.c
Line | Count | Source |
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 | 99 | { |
32 | 99 | return StorageGetSize(STORAGE_IPPAIR); |
33 | 99 | } |
34 | | |
35 | | void *IPPairGetStorageById(IPPair *h, IPPairStorageId id) |
36 | 2.75k | { |
37 | 2.75k | return StorageGetById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id.id); |
38 | 2.75k | } |
39 | | |
40 | | int IPPairSetStorageById(IPPair *h, IPPairStorageId id, void *ptr) |
41 | 397 | { |
42 | 397 | return StorageSetById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id.id, ptr); |
43 | 397 | } |
44 | | |
45 | | void *IPPairAllocStorageById(IPPair *h, IPPairStorageId id) |
46 | 0 | { |
47 | 0 | return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id.id); |
48 | 0 | } |
49 | | |
50 | | void IPPairFreeStorageById(IPPair *h, IPPairStorageId id) |
51 | 0 | { |
52 | 0 | StorageFreeById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id.id); |
53 | 0 | } |
54 | | |
55 | | void IPPairFreeStorage(IPPair *h) |
56 | 0 | { |
57 | 0 | if (IPPairStorageSize() > 0) |
58 | 0 | StorageFreeAll((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR); |
59 | 0 | } |
60 | | |
61 | | IPPairStorageId IPPairStorageRegister(const char *name, const unsigned int size, |
62 | | void *(*Alloc)(unsigned int), void (*Free)(void *)) |
63 | 100 | { |
64 | 100 | int id = StorageRegister(STORAGE_IPPAIR, name, size, Alloc, Free); |
65 | 100 | IPPairStorageId ippsi = { .id = id }; |
66 | 100 | return ippsi; |
67 | 100 | } |
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 | | |
297 | | void RegisterIPPairStorageTests(void) |
298 | 0 | { |
299 | | #ifdef UNITTESTS |
300 | | UtRegisterTest("IPPairStorageTest01", IPPairStorageTest01); |
301 | | UtRegisterTest("IPPairStorageTest02", IPPairStorageTest02); |
302 | | UtRegisterTest("IPPairStorageTest03", IPPairStorageTest03); |
303 | | #endif |
304 | 0 | } |