suricata
device-storage.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 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 Eric Leblond <eric@regit.org>
22  *
23  * Device wrapper around storage api
24  */
25 
26 #include "suricata-common.h"
27 #include "device-storage.h"
28 #include "util-unittest.h"
29 
30 unsigned int LiveDevStorageSize(void)
31 {
33 }
34 
35 /** \defgroup devicestorage Device storage API
36  *
37  * The device storage API is a per-device storage. It is a mean to extend
38  * the LiveDevice structure with arbitrary data.
39  *
40  * You have first to register the storage via LiveDevStorageRegister() during
41  * the init of your module. Then you can attach data via LiveDevSetStorageById()
42  * and access them via LiveDevGetStorageById().
43  * @{
44  */
45 
46 /**
47  * \brief Register a LiveDevice storage
48  *
49  * \param name the name of the storage
50  * \param size integer coding the size of the stored value (sizeof(void *) is best choice here)
51  * \param Alloc allocation function for the storage (can be null)
52  * \param Free free function for the new storage
53  *
54  * \retval The ID of the newly register storage that will be used to access data
55  *
56  * It has to be called once during the init of the sub system
57  */
58 
59 int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
60  return StorageRegister(STORAGE_DEVICE, name, size, Alloc, Free);
61 }
62 
63 /**
64  * \brief Store a pointer in a given LiveDevice storage
65  *
66  * \param d a pointer to the LiveDevice
67  * \param id the id of the storage (return of HostStorageRegister() call)
68  * \param ptr pointer to the data to store
69  */
70 
71 int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr)
72 {
73  return StorageSetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id, ptr);
74 }
75 
76 /**
77  * \brief Get a value from a given LiveDevice storage
78  *
79  * \param d a pointer to the LiveDevice
80  * \param id the id of the storage (return of LiveDevStorageRegister() call)
81  *
82  */
83 
85 {
86  return StorageGetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
87 }
88 
89 /**
90  * @}
91  */
92 
93 /* Start of "private" function */
94 
96 {
97  return StorageAllocByIdPrealloc((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
98 }
99 
101 {
102  StorageFreeById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id);
103 }
104 
106 {
107  if (LiveDevStorageSize() > 0)
108  StorageFreeAll((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE);
109 }
110 
111 
void LiveDevFreeStorage(LiveDevice *d)
int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr)
Store a pointer in a given LiveDevice storage.
int StorageSetById(Storage *storage, const StorageEnum type, const int id, void *ptr)
set storage for id
Definition: util-storage.c:227
void * LiveDevAllocStorageById(LiveDevice *d, int id)
unsigned int LiveDevStorageSize(void)
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 * 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
void * Storage
Definition: util-storage.h:39
void * LiveDevGetStorageById(LiveDevice *d, int id)
Get a value from a given LiveDevice storage.
void StorageFreeAll(Storage *storage, StorageEnum type)
Definition: util-storage.c:310
unsigned int StorageGetSize(StorageEnum type)
get the size of the void array used to store the pointers
Definition: util-storage.c:211
void * StorageGetById(const Storage *storage, const StorageEnum type, const int id)
get storage for id
Definition: util-storage.c:216
int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void(*Free)(void *))
Register a LiveDevice storage.
void LiveDevFreeStorageById(LiveDevice *d, int id)
void StorageFreeById(Storage *storage, StorageEnum type, int id)
Definition: util-storage.c:288