host/storage: use dedicated 'id' type

- Wrap the id in a HostStorageId struct to avoid id confusion
with other storage API calls.
- Fix formatting with clang script.
pull/6058/head
Juliana Fajardini 5 years ago committed by Victor Julien
parent cf516de587
commit b807059c34

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation /* Copyright (C) 2007-2021 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -45,7 +45,7 @@
SC_ATOMIC_DECLARE(unsigned int, num_tags); /**< Atomic counter, to know if we SC_ATOMIC_DECLARE(unsigned int, num_tags); /**< Atomic counter, to know if we
have tagged hosts/sessions, have tagged hosts/sessions,
to avoid locking */ to avoid locking */
static int host_tag_id = -1; /**< Host storage id for tags */ static HostStorageId host_tag_id = { .id = -1 }; /**< Host storage id for tags */
static FlowStorageId flow_tag_id = { .id = -1 }; /**< Flow storage id for tags */ static FlowStorageId flow_tag_id = { .id = -1 }; /**< Flow storage id for tags */
void TagInitCtx(void) void TagInitCtx(void)
@ -53,7 +53,7 @@ void TagInitCtx(void)
SC_ATOMIC_INIT(num_tags); SC_ATOMIC_INIT(num_tags);
host_tag_id = HostStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree); host_tag_id = HostStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree);
if (host_tag_id == -1) { if (host_tag_id.id == -1) {
FatalError(SC_ERR_FATAL, "Can't initiate host storage for tag"); FatalError(SC_ERR_FATAL, "Can't initiate host storage for tag");
} }
flow_tag_id = FlowStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree); flow_tag_id = FlowStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree);

@ -69,10 +69,10 @@
#include "util-var-name.h" #include "util-var-name.h"
#include "tm-threads.h" #include "tm-threads.h"
static int host_threshold_id = -1; /**< host storage id for thresholds */ static HostStorageId host_threshold_id = { .id = -1 }; /**< host storage id for thresholds */
static IPPairStorageId ippair_threshold_id = { .id = -1 }; /**< ip pair storage id for thresholds */ static IPPairStorageId ippair_threshold_id = { .id = -1 }; /**< ip pair storage id for thresholds */
int ThresholdHostStorageId(void) HostStorageId ThresholdHostStorageId(void)
{ {
return host_threshold_id; return host_threshold_id;
} }
@ -80,7 +80,7 @@ int ThresholdHostStorageId(void)
void ThresholdInit(void) void ThresholdInit(void)
{ {
host_threshold_id = HostStorageRegister("threshold", sizeof(void *), NULL, ThresholdListFree); host_threshold_id = HostStorageRegister("threshold", sizeof(void *), NULL, ThresholdListFree);
if (host_threshold_id == -1) { if (host_threshold_id.id == -1) {
FatalError(SC_ERR_FATAL, FatalError(SC_ERR_FATAL,
"Can't initiate host storage for thresholding"); "Can't initiate host storage for thresholding");
} }

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2010 Open Information Security Foundation /* Copyright (C) 2007-2021 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -28,10 +28,11 @@
#include "detect.h" #include "detect.h"
#include "host.h" #include "host.h"
#include "ippair.h" #include "ippair.h"
#include "host-storage.h"
void ThresholdInit(void); void ThresholdInit(void);
int ThresholdHostStorageId(void); HostStorageId ThresholdHostStorageId(void);
int ThresholdHostHasThreshold(Host *); int ThresholdHostHasThreshold(Host *);
int ThresholdIPPairHasThreshold(IPPair *pair); int ThresholdIPPairHasThreshold(IPPair *pair);

@ -1,4 +1,4 @@
/* Copyright (C) 2014 Open Information Security Foundation /* Copyright (C) 2014-2021 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -38,7 +38,7 @@
#include "util-unittest.h" #include "util-unittest.h"
#include "host-storage.h" #include "host-storage.h"
static int host_bit_id = -1; /**< Host storage id for bits */ static HostStorageId host_bit_id = { .id = -1 }; /**< Host storage id for bits */
static void HostBitFreeAll(void *store) static void HostBitFreeAll(void *store)
{ {
@ -49,7 +49,7 @@ static void HostBitFreeAll(void *store)
void HostBitInitCtx(void) void HostBitInitCtx(void)
{ {
host_bit_id = HostStorageRegister("bit", sizeof(void *), NULL, HostBitFreeAll); host_bit_id = HostStorageRegister("bit", sizeof(void *), NULL, HostBitFreeAll);
if (host_bit_id == -1) { if (host_bit_id.id == -1) {
FatalError(SC_ERR_FATAL, "Can't initiate host storage for bits"); FatalError(SC_ERR_FATAL, "Can't initiate host storage for bits");
} }
} }

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation /* Copyright (C) 2007-2021 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -56,8 +56,12 @@ unsigned int HostStorageSize(void)
* It has to be called once during the init of the sub system * It has to be called once during the init of the sub system
*/ */
int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) { HostStorageId HostStorageRegister(const char *name, const unsigned int size,
return StorageRegister(STORAGE_HOST, name, size, Alloc, Free); void *(*Alloc)(unsigned int), void (*Free)(void *))
{
int id = StorageRegister(STORAGE_HOST, name, size, Alloc, Free);
HostStorageId hsi = { .id = id };
return hsi;
} }
/** /**
@ -68,9 +72,9 @@ int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc
* \param ptr pointer to the data to store * \param ptr pointer to the data to store
*/ */
int HostSetStorageById(Host *h, int id, void *ptr) int HostSetStorageById(Host *h, HostStorageId id, void *ptr)
{ {
return StorageSetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id, ptr); return StorageSetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id, ptr);
} }
/** /**
@ -81,9 +85,9 @@ int HostSetStorageById(Host *h, int id, void *ptr)
* *
*/ */
void *HostGetStorageById(Host *h, int id) void *HostGetStorageById(Host *h, HostStorageId id)
{ {
return StorageGetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id); return StorageGetById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
} }
/** /**
@ -92,14 +96,14 @@ void *HostGetStorageById(Host *h, int id)
/* Start of "private" function */ /* Start of "private" function */
void *HostAllocStorageById(Host *h, int id) void *HostAllocStorageById(Host *h, HostStorageId id)
{ {
return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id); return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
} }
void HostFreeStorageById(Host *h, int id) void HostFreeStorageById(Host *h, HostStorageId id)
{ {
StorageFreeById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id); StorageFreeById((Storage *)((void *)h + sizeof(Host)), STORAGE_HOST, id.id);
} }
void HostFreeStorage(Host *h) void HostFreeStorage(Host *h)
@ -126,14 +130,15 @@ static int HostStorageTest01(void)
{ {
StorageInit(); StorageInit();
int id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); HostStorageId id1 = HostStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1 < 0) if (id1.id < 0)
goto error; goto error;
int id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); HostStorageId id2 = HostStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2 < 0) if (id2.id < 0)
goto error; goto error;
int id3 = HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree); HostStorageId id3 =
if (id3 < 0) HostStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3.id < 0)
goto error; goto error;
if (StorageFinalize() < 0) if (StorageFinalize() < 0)
@ -205,8 +210,8 @@ static int HostStorageTest02(void)
{ {
StorageInit(); StorageInit();
int id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree); HostStorageId id1 = HostStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
if (id1 < 0) if (id1.id < 0)
goto error; goto error;
if (StorageFinalize() < 0) if (StorageFinalize() < 0)
@ -255,14 +260,14 @@ static int HostStorageTest03(void)
{ {
StorageInit(); StorageInit();
int id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); HostStorageId id1 = HostStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
if (id1 < 0) if (id1.id < 0)
goto error; goto error;
int id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); HostStorageId id2 = HostStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2 < 0) if (id2.id < 0)
goto error; goto error;
int id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); HostStorageId id3 = HostStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3 < 0) if (id3.id < 0)
goto error; goto error;
if (StorageFinalize() < 0) if (StorageFinalize() < 0)

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2013 Open Information Security Foundation /* Copyright (C) 2007-2021 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -29,17 +29,22 @@
#include "util-storage.h" #include "util-storage.h"
#include "host.h" #include "host.h"
typedef struct HostStorageId_ {
int id;
} HostStorageId;
unsigned int HostStorageSize(void); unsigned int HostStorageSize(void);
void *HostGetStorageById(Host *h, int id); void *HostGetStorageById(Host *h, HostStorageId id);
int HostSetStorageById(Host *h, int id, void *ptr); int HostSetStorageById(Host *h, HostStorageId id, void *ptr);
void *HostAllocStorageById(Host *h, int id); void *HostAllocStorageById(Host *h, HostStorageId id);
void HostFreeStorageById(Host *h, int id); void HostFreeStorageById(Host *h, HostStorageId id);
void HostFreeStorage(Host *h); void HostFreeStorage(Host *h);
void RegisterHostStorageTests(void); void RegisterHostStorageTests(void);
int HostStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)); HostStorageId HostStorageRegister(const char *name, const unsigned int size,
void *(*Alloc)(unsigned int), void (*Free)(void *));
#endif /* __HOST_STORAGE_H__ */ #endif /* __HOST_STORAGE_H__ */

Loading…
Cancel
Save