suricata
host-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  * Host wrapper around storage api
24  */
25 
26 #include "suricata-common.h"
27 #include "host-storage.h"
28 
29 unsigned int SCHostStorageSize(void)
30 {
32 }
33 
34 /** \defgroup hoststorage Host storage API
35  *
36  * The Host storage API is a per-host storage. It is a mean to extend
37  * the Host structure with arbitrary data.
38  *
39  * You have first to register the storage via SCHostStorageRegister() during
40  * the init of your module. Then you can attach data via SCHostSetStorageById()
41  * and access them via SCHostGetStorageById().
42  * @{
43  */
44 
45 /**
46  * \brief Register a Host storage
47  *
48  * \param name the name of the storage
49  * \param Free free function for the new storage
50  *
51  * \retval The ID of the newly register storage that will be used to access data
52  *
53  * It has to be called once during the init of the sub system
54  */
55 
56 SCHostStorageId SCHostStorageRegister(const char *name, void (*Free)(void *))
57 {
58  int id = SCStorageRegister(STORAGE_HOST, name, Free);
59  SCHostStorageId hsi = { .id = id };
60  return hsi;
61 }
62 
63 /**
64  * \brief Store a pointer in a given Host storage
65  *
66  * \param h a pointer to the Host
67  * \param id the id of the storage (return of SCHostStorageRegister() call)
68  * \param ptr pointer to the data to store
69  */
70 
72 {
73  return SCStorageSetById(h->storage, STORAGE_HOST, id.id, ptr);
74 }
75 
76 /**
77  * \brief Get a value from a given Host storage
78  *
79  * \param h a pointer to the Host
80  * \param id the id of the storage (return of SCHostStorageRegister() call)
81  *
82  */
83 
85 {
86  return SCStorageGetById(h->storage, STORAGE_HOST, id.id);
87 }
88 
89 /**
90  * @}
91  */
92 
93 /* Start of "private" function */
94 
96 {
97  if (SCHostStorageSize() > 0)
99 }
100 
101 
102 #ifdef UNITTESTS
103 
104 static void StorageTestFree(void *x)
105 {
106  if (x)
107  SCFree(x);
108 }
109 
110 static int HostStorageTest01(void)
111 {
113  SCStorageInit();
114 
115  SCHostStorageId id1 = SCHostStorageRegister("test", StorageTestFree);
116  FAIL_IF(id1.id < 0);
117  SCHostStorageId id2 = SCHostStorageRegister("variable", StorageTestFree);
118  FAIL_IF(id2.id < 0);
119  SCHostStorageId id3 = SCHostStorageRegister("store", StorageTestFree);
120  FAIL_IF(id3.id < 0);
121 
122  FAIL_IF(SCStorageFinalize() < 0);
123 
124  HostInitConfig(1);
125 
126  Address a;
127  memset(&a, 0x00, sizeof(a));
128  a.addr_data32[0] = 0x01020304;
129  a.family = AF_INET;
130  Host *h = HostGetHostFromHash(&a);
131  FAIL_IF_NULL(h);
132 
133  void *ptr = SCHostGetStorageById(h, id1);
134  FAIL_IF_NOT_NULL(ptr);
135  ptr = SCHostGetStorageById(h, id2);
136  FAIL_IF_NOT_NULL(ptr);
137  ptr = SCHostGetStorageById(h, id3);
138  FAIL_IF_NOT_NULL(ptr);
139 
140  void *ptr1a = SCMalloc(8);
141  FAIL_IF_NULL(ptr1a);
142  FAIL_IF(SCHostSetStorageById(h, id1, ptr1a) != 0);
143  void *ptr2a = SCMalloc(24);
144  FAIL_IF_NULL(ptr2a);
145  FAIL_IF(SCHostSetStorageById(h, id2, ptr2a) != 0);
146  void *ptr3a = SCMalloc(16);
147  FAIL_IF_NULL(ptr3a);
148  FAIL_IF(SCHostSetStorageById(h, id3, ptr3a) != 0);
149 
150  void *ptr1b = SCHostGetStorageById(h, id1);
151  FAIL_IF(ptr1a != ptr1b);
152  void *ptr2b = SCHostGetStorageById(h, id2);
153  FAIL_IF(ptr2a != ptr2b);
154  void *ptr3b = SCHostGetStorageById(h, id3);
155  FAIL_IF(ptr3a != ptr3b);
156 
157  HostRelease(h);
158 
159  HostShutdown();
161  PASS;
162 }
163 
164 static int HostStorageTest02(void)
165 {
167  SCStorageInit();
168 
169  SCHostStorageId id1 = SCHostStorageRegister("test", StorageTestFree);
170  FAIL_IF(id1.id < 0);
171 
172  FAIL_IF(SCStorageFinalize() < 0);
173 
174  HostInitConfig(1);
175 
176  Address a;
177  memset(&a, 0x00, sizeof(a));
178  a.addr_data32[0] = 0x01020304;
179  a.family = AF_INET;
180  Host *h = HostGetHostFromHash(&a);
181  FAIL_IF_NULL(h);
182 
183  void *ptr = SCHostGetStorageById(h, id1);
184  FAIL_IF_NOT_NULL(ptr);
185 
186  void *ptr1a = SCMalloc(128);
187  FAIL_IF_NULL(ptr1a);
188  SCHostSetStorageById(h, id1, ptr1a);
189 
190  void *ptr1b = SCHostGetStorageById(h, id1);
191  FAIL_IF(ptr1a != ptr1b);
192 
193  HostRelease(h);
194 
195  HostShutdown();
197  PASS;
198 }
199 
200 static int HostStorageTest03(void)
201 {
203  SCStorageInit();
204 
205  SCHostStorageId id1 = SCHostStorageRegister("test1", StorageTestFree);
206  FAIL_IF(id1.id < 0);
207  SCHostStorageId id2 = SCHostStorageRegister("test2", StorageTestFree);
208  FAIL_IF(id2.id < 0);
209  SCHostStorageId id3 = SCHostStorageRegister("test3", StorageTestFree);
210  FAIL_IF(id3.id < 0);
211 
212  FAIL_IF(SCStorageFinalize() < 0);
213 
214  HostInitConfig(1);
215 
216  Address a;
217  memset(&a, 0x00, sizeof(a));
218  a.addr_data32[0] = 0x01020304;
219  a.family = AF_INET;
220  Host *h = HostGetHostFromHash(&a);
221  FAIL_IF_NULL(h);
222 
223  void *ptr = SCHostGetStorageById(h, id1);
224  FAIL_IF_NOT_NULL(ptr);
225 
226  void *ptr1a = SCMalloc(128);
227  FAIL_IF_NULL(ptr1a);
228  SCHostSetStorageById(h, id1, ptr1a);
229 
230  void *ptr2a = SCMalloc(256);
231  FAIL_IF_NULL(ptr2a);
232  SCHostSetStorageById(h, id2, ptr2a);
233 
234  void *ptr3a = SCMalloc(32);
235  FAIL_IF_NULL(ptr3a);
236  SCHostSetStorageById(h, id3, ptr3a);
237 
238  void *ptr1b = SCHostGetStorageById(h, id1);
239  FAIL_IF(ptr1a != ptr1b);
240  void *ptr2b = SCHostGetStorageById(h, id2);
241  FAIL_IF(ptr2a != ptr2b);
242  void *ptr3b = SCHostGetStorageById(h, id3);
243  FAIL_IF(ptr3a != ptr3b);
244 
245  HostRelease(h);
246 
247  HostShutdown();
249  PASS;
250 }
251 #endif
252 
254 {
255 #ifdef UNITTESTS
256  UtRegisterTest("HostStorageTest01", HostStorageTest01);
257  UtRegisterTest("HostStorageTest02", HostStorageTest02);
258  UtRegisterTest("HostStorageTest03", HostStorageTest03);
259 #endif
260 }
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
host-storage.h
SCStorageInit
void SCStorageInit(void)
Definition: util-storage.c:67
SCHostGetStorageById
void * SCHostGetStorageById(Host *h, SCHostStorageId id)
Get a value from a given Host storage.
Definition: host-storage.c:84
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
name
const char * name
Definition: detect-engine-proto.c:47
SCHostSetStorageById
int SCHostSetStorageById(Host *h, SCHostStorageId id, void *ptr)
Store a pointer in a given Host storage.
Definition: host-storage.c:71
Host_::storage
Storage storage[]
Definition: host.h:80
HostRelease
void HostRelease(Host *h)
Definition: host.c:461
HostGetHostFromHash
Host * HostGetHostFromHash(Address *a)
Definition: host.c:486
SCHostFreeStorage
void SCHostFreeStorage(Host *h)
Definition: host-storage.c:95
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:217
STORAGE_HOST
@ STORAGE_HOST
Definition: util-storage.h:30
SCHostStorageSize
unsigned int SCHostStorageSize(void)
Definition: host-storage.c:29
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
SCStorageGetById
void * SCStorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:206
SCStorageRegister
int SCStorageRegister(const StorageEnum type, const char *name, void(*Free)(void *))
Register new storage.
Definition: util-storage.c:99
SCStorageGetSize
unsigned int SCStorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
Definition: util-storage.c:201
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
SCStorageFreeAll
void SCStorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:251
HostShutdown
void HostShutdown(void)
shutdown the flow engine
Definition: host.c:296
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
HostStorageId_
Definition: host-storage.h:31
HostStorageId_::id
int id
Definition: host-storage.h:32
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SCRegisterHostStorageTests
void SCRegisterHostStorageTests(void)
Definition: host-storage.c:253
SCStorageCleanup
void SCStorageCleanup(void)
Definition: util-storage.c:75
Address_::family
char family
Definition: decode.h:114
HostInitConfig
void HostInitConfig(bool quiet)
initialize the configuration
Definition: host.c:168
Host_
Definition: host.h:58
SCHostStorageRegister
SCHostStorageId SCHostStorageRegister(const char *name, void(*Free)(void *))
Register a Host storage.
Definition: host-storage.c:56
SCStorageFinalize
int SCStorageFinalize(void)
Definition: util-storage.c:134