aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/suricata/src/host-storage.c
blob: fe157692725c938bd1b747f2e43e51a066569f13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Copyright (C) 2007-2013 Open Information Security Foundation
 *
 * You can copy, redistribute or modify this Program under the terms of
 * the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * \file
 *
 * \author Victor Julien <victor@inliniac.net>
 *
 * Host wrapper around storage api
 */

#include "suricata-common.h"
#include "host-storage.h"
#include "util-unittest.h"

unsigned int HostStorageSize(void)
{
    return StorageGetSize(STORAGE_HOST);
}

/** \defgroup hoststorage Host storage API
 *
 * The Host storage API is a per-host storage. It is a mean to extend
 * the Host structure with arbitrary data.
 *
 * You have first to register the storage via HostStorageRegister() during
 * the init of your module. Then you can attach data via HostSetStorageById()
 * and access them via HostGetStorageById().
 * @{
 */

/**
 * \brief Register a Host storage
 *
 * \param name the name of the storage
 * \param size integer coding the size of the stored value (sizeof(void *) is best choice here)
 * \param Alloc allocation function for the storage (can be null)
 * \param Free free function for the new storage
 *
 * \retval The ID of the newly register storage that will be used to access data
 *
 * It has to be called once during the init of the sub system
 */

int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
    return StorageRegister(STORAGE_HOST, name, size, Alloc, Free);
}

/**
 * \brief Store a pointer in a given Host storage
 *
 * \param h a pointer to the Host
 * \param id the id of the storage (return of HostStorageRegister() call)
 * \param ptr pointer to the data to store
 */

int HostSetStorageById(Host *h, int id, void *ptr)
{
    return StorageSetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id, ptr);
}

/**
 * \brief Get a value from a given Host storage
 *
 * \param h a pointer to the Host
 * \param id the id of the storage (return of HostStorageRegister() call)
 *
 */

void *HostGetStorageById(Host *h, int id)
{
    return StorageGetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
}

/**
 * @}
 */

/* Start of "private" function */

void *HostAllocStorageById(Host *h, int id)
{
    return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
}

void HostFreeStorageById(Host *h, int id)
{
    StorageFreeById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id);
}

void HostFreeStorage(Host *h)
{
    if (HostStorageSize() > 0)
        StorageFreeAll((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST);
}


#ifdef UNITTESTS

static void *StorageTestAlloc(unsigned int size)
{
    void *x = SCMalloc(size);
    return x;
}
static void StorageTestFree(void *x)
{
    if (x)
        SCFree(x);
}

static int HostStorageTest01(void)
{
    StorageInit();

    int id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
    if (id1 < 0)
        goto error;
    int id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
    if (id2 < 0)
        goto error;
    int id3 = HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
    if (id3 < 0)
        goto error;

    if (StorageFinalize() < 0)
        goto error;

    HostInitConfig(1);

    Address a;
    memset(&a, 0x00, sizeof(a));
    a.addr_data32[0] = 0x01020304;
    a.family = AF_INET;
    Host *h = HostGetHostFromHash(&a);
    if (h == NULL) {
        printf("failed to get host: ");
        goto error;
    }

    void *ptr = HostGetStorageById(h, id1);
    if (ptr != NULL) {
        goto error;
    }
    ptr = HostGetStorageById(h, id2);
    if (ptr != NULL) {
        goto error;
    }
    ptr = HostGetStorageById(h, id3);
    if (ptr != NULL) {
        goto error;
    }

    void *ptr1a = HostAllocStorageById(h, id1);
    if (ptr1a == NULL) {
        goto error;
    }
    void *ptr2a = HostAllocStorageById(h, id2);
    if (ptr2a == NULL) {
        goto error;
    }
    void *ptr3a = HostAllocStorageById(h, id3);
    if (ptr3a == NULL) {
        goto error;
    }

    void *ptr1b = HostGetStorageById(h, id1);
    if (ptr1a != ptr1b) {
        goto error;
    }
    void *ptr2b = HostGetStorageById(h, id2);
    if (ptr2a != ptr2b) {
        goto error;
    }
    void *ptr3b = HostGetStorageById(h, id3);
    if (ptr3a != ptr3b) {
        goto error;
    }

    HostRelease(h);

    HostShutdown();
    StorageCleanup();
    return 1;
error:
    HostShutdown();
    StorageCleanup();
    return 0;
}

static int HostStorageTest02(void)
{
    StorageInit();

    int id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
    if (id1 < 0)
        goto error;

    if (StorageFinalize() < 0)
        goto error;

    HostInitConfig(1);

    Address a;
    memset(&a, 0x00, sizeof(a));
    a.addr_data32[0] = 0x01020304;
    a.family = AF_INET;
    Host *h = HostGetHostFromHash(&a);
    if (h == NULL) {
        printf("failed to get host: ");
        goto error;
    }

    void *ptr = HostGetStorageById(h, id1);
    if (ptr != NULL) {
        goto error;
    }

    void *ptr1a = SCMalloc(128);
    if (unlikely(ptr1a == NULL)) {
        goto error;
    }
    HostSetStorageById(h, id1, ptr1a);

    void *ptr1b = HostGetStorageById(h, id1);
    if (ptr1a != ptr1b) {
        goto error;
    }

    HostRelease(h);

    HostShutdown();
    StorageCleanup();
    return 1;
error:
    HostShutdown();
    StorageCleanup();
    return 0;
}

static int HostStorageTest03(void)
{
    StorageInit();

    int id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
    if (id1 < 0)
        goto error;
    int id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
    if (id2 < 0)
        goto error;
    int id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
    if (id3 < 0)
        goto error;

    if (StorageFinalize() < 0)
        goto error;

    HostInitConfig(1);

    Address a;
    memset(&a, 0x00, sizeof(a));
    a.addr_data32[0] = 0x01020304;
    a.family = AF_INET;
    Host *h = HostGetHostFromHash(&a);
    if (h == NULL) {
        printf("failed to get host: ");
        goto error;
    }

    void *ptr = HostGetStorageById(h, id1);
    if (ptr != NULL) {
        goto error;
    }

    void *ptr1a = SCMalloc(128);
    if (unlikely(ptr1a == NULL)) {
        goto error;
    }
    HostSetStorageById(h, id1, ptr1a);

    void *ptr2a = SCMalloc(256);
    if (unlikely(ptr2a == NULL)) {
        goto error;
    }
    HostSetStorageById(h, id2, ptr2a);

    void *ptr3a = HostAllocStorageById(h, id3);
    if (ptr3a == NULL) {
        goto error;
    }

    void *ptr1b = HostGetStorageById(h, id1);
    if (ptr1a != ptr1b) {
        goto error;
    }
    void *ptr2b = HostGetStorageById(h, id2);
    if (ptr2a != ptr2b) {
        goto error;
    }
    void *ptr3b = HostGetStorageById(h, id3);
    if (ptr3a != ptr3b) {
        goto error;
    }

    HostRelease(h);

    HostShutdown();
    StorageCleanup();
    return 1;
error:
    HostShutdown();
    StorageCleanup();
    return 0;
}
#endif

void RegisterHostStorageTests(void)
{
#ifdef UNITTESTS
    UtRegisterTest("HostStorageTest01", HostStorageTest01, 1);
    UtRegisterTest("HostStorageTest02", HostStorageTest02, 1);
    UtRegisterTest("HostStorageTest03", HostStorageTest03, 1);
#endif
}