From: Juliana Fajardini Date: Wed, 14 Apr 2021 22:16:07 +0000 (+0100) Subject: device/storage: use dedicated 'id' type X-Git-Tag: suricata-7.0.0-beta1~1679 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3b1a653467e09a51711c7b48c7666be3d7884855;p=thirdparty%2Fsuricata.git device/storage: use dedicated 'id' type - Wrap the id in a new LiveDevStorageId struct, to avoid id confusion with other storage API calls. - Formatting fixes by clang. --- diff --git a/src/device-storage.c b/src/device-storage.c index 2035eb8e97..d01ff41f2b 100644 --- a/src/device-storage.c +++ b/src/device-storage.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2018 Open Information Security Foundation +/* Copyright (C) 2018-2021 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 @@ -56,8 +56,12 @@ unsigned int LiveDevStorageSize(void) * It has to be called once during the init of the sub system */ -int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) { - return StorageRegister(STORAGE_DEVICE, name, size, Alloc, Free); +LiveDevStorageId LiveDevStorageRegister(const char *name, const unsigned int size, + void *(*Alloc)(unsigned int), void (*Free)(void *)) +{ + int id = StorageRegister(STORAGE_DEVICE, name, size, Alloc, Free); + LiveDevStorageId ldsi = { .id = id }; + return ldsi; } /** @@ -68,9 +72,9 @@ int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Al * \param ptr pointer to the data to store */ -int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr) +int LiveDevSetStorageById(LiveDevice *d, LiveDevStorageId id, void *ptr) { - return StorageSetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id, ptr); + return StorageSetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id.id, ptr); } /** @@ -81,9 +85,9 @@ int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr) * */ -void *LiveDevGetStorageById(LiveDevice *d, int id) +void *LiveDevGetStorageById(LiveDevice *d, LiveDevStorageId id) { - return StorageGetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id); + return StorageGetById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id.id); } /** @@ -92,14 +96,15 @@ void *LiveDevGetStorageById(LiveDevice *d, int id) /* Start of "private" function */ -void *LiveDevAllocStorageById(LiveDevice *d, int id) +void *LiveDevAllocStorageById(LiveDevice *d, LiveDevStorageId id) { - return StorageAllocByIdPrealloc((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id); + return StorageAllocByIdPrealloc( + (Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id.id); } -void LiveDevFreeStorageById(LiveDevice *d, int id) +void LiveDevFreeStorageById(LiveDevice *d, LiveDevStorageId id) { - StorageFreeById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id); + StorageFreeById((Storage *)((void *)d + sizeof(LiveDevice)), STORAGE_DEVICE, id.id); } void LiveDevFreeStorage(LiveDevice *d) diff --git a/src/device-storage.h b/src/device-storage.h index 0c671eac46..046c8dd454 100644 --- a/src/device-storage.h +++ b/src/device-storage.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2018 Open Information Security Foundation +/* Copyright (C) 2018-2021 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 @@ -29,17 +29,22 @@ #include "util-storage.h" #include "util-device.h" +typedef struct LiveDevStorageId_ { + int id; +} LiveDevStorageId; + unsigned int LiveDevStorageSize(void); -void *LiveDevGetStorageById(LiveDevice *d, int id); -int LiveDevSetStorageById(LiveDevice *d, int id, void *ptr); -void *LiveDevAllocStorageById(LiveDevice *d, int id); +void *LiveDevGetStorageById(LiveDevice *d, LiveDevStorageId id); +int LiveDevSetStorageById(LiveDevice *d, LiveDevStorageId id, void *ptr); +void *LiveDevAllocStorageById(LiveDevice *d, LiveDevStorageId id); -void LiveDevFreeStorageById(LiveDevice *d, int id); +void LiveDevFreeStorageById(LiveDevice *d, LiveDevStorageId id); void LiveDevFreeStorage(LiveDevice *d); void RegisterLiveDevStorageTests(void); -int LiveDevStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)); +LiveDevStorageId LiveDevStorageRegister(const char *name, const unsigned int size, + void *(*Alloc)(unsigned int), void (*Free)(void *)); #endif /* __DEVICE_STORAGE_H__ */ diff --git a/src/util-device.c b/src/util-device.c index f95696a6c0..a303fa835e 100644 --- a/src/util-device.c +++ b/src/util-device.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2011-2016 Open Information Security Foundation +/* Copyright (C) 2011-2021 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 @@ -25,7 +25,7 @@ #define MAX_DEVNAME 10 -static int g_bypass_storage_id = -1; +static LiveDevStorageId g_bypass_storage_id = { .id = -1 }; /** * \file @@ -590,7 +590,7 @@ void LiveDevAddBypassFail(LiveDevice *dev, uint64_t cnt, int family) } /** - * Increase number of currently succesfully bypassed flows for a protocol family + * Increase number of currently successfully bypassed flows for a protocol family * * \param dev pointer to LiveDevice to set stats for * \param cnt number of flows to add diff --git a/src/util-ebpf.c b/src/util-ebpf.c index b622b6ca00..94236aff5b 100644 --- a/src/util-ebpf.c +++ b/src/util-ebpf.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2018-2019 Open Information Security Foundation +/* Copyright (C) 2018-2021 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 @@ -60,8 +60,8 @@ #define BYPASSED_FLOW_TIMEOUT 60 -static int g_livedev_storage_id = -1; -static int g_flow_storage_id = -1; +static LiveDevStorageId g_livedev_storage_id = { .id = -1 }; +static int g_flow_storage_id = -1; // TODO change this in a different commit struct bpf_map_item { char iface[IFNAMSIZ];