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