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