ippair/storage: use dedicated 'id' type

- Wrap the id in a new IPPairStorageId struct, to avoid id
confusion with other storage API calls.
- Formatting fixes by clang.
pull/6058/head
Juliana Fajardini 5 years ago committed by Victor Julien
parent aa9ad56a5b
commit cf516de587

@ -63,7 +63,7 @@
#include "util-print.h"
#include "queue.h"
static int g_ippair_expectation_id = -1;
static IPPairStorageId g_ippair_expectation_id = { .id = -1 };
static FlowStorageId g_flow_expectation_id = { .id = -1 };
SC_ATOMIC_DECLARE(uint32_t, expectation_count);

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Open Information Security Foundation
/* Copyright (C) 2017-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

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2015 Open Information Security Foundation
/* Copyright (C) 2007-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
@ -70,7 +70,7 @@
#include "tm-threads.h"
static int host_threshold_id = -1; /**< host storage id for thresholds */
static int ippair_threshold_id = -1; /**< ip pair storage id for thresholds */
static IPPairStorageId ippair_threshold_id = { .id = -1 }; /**< ip pair storage id for thresholds */
int ThresholdHostStorageId(void)
{
@ -85,7 +85,7 @@ void ThresholdInit(void)
"Can't initiate host storage for thresholding");
}
ippair_threshold_id = IPPairStorageRegister("threshold", sizeof(void *), NULL, ThresholdListFree);
if (ippair_threshold_id == -1) {
if (ippair_threshold_id.id == -1) {
FatalError(SC_ERR_FATAL,
"Can't initiate IP pair storage for thresholding");
}

@ -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
* the GNU General Public License version 2 as published by the Free
@ -38,7 +38,7 @@
#include "util-unittest.h"
#include "ippair-storage.h"
static int ippair_bit_id = -1; /**< IPPair storage id for bits */
static IPPairStorageId g_ippair_bit_storage_id = { .id = -1 }; /**< IPPair storage id for bits */
static void XBitFreeAll(void *store)
{
@ -48,8 +48,8 @@ static void XBitFreeAll(void *store)
void IPPairBitInitCtx(void)
{
ippair_bit_id = IPPairStorageRegister("bit", sizeof(void *), NULL, XBitFreeAll);
if (ippair_bit_id == -1) {
g_ippair_bit_storage_id = IPPairStorageRegister("bit", sizeof(void *), NULL, XBitFreeAll);
if (g_ippair_bit_storage_id.id == -1) {
FatalError(SC_ERR_FATAL, "Can't initiate ippair storage for bits");
}
}
@ -59,14 +59,14 @@ int IPPairHasBits(IPPair *ippair)
{
if (ippair == NULL)
return 0;
return IPPairGetStorageById(ippair, ippair_bit_id) ? 1 : 0;
return IPPairGetStorageById(ippair, g_ippair_bit_storage_id) ? 1 : 0;
}
/** \retval 1 ippair timed out wrt xbits
* \retval 0 ippair still has active (non-expired) xbits */
int IPPairBitsTimedoutCheck(IPPair *h, struct timeval *ts)
{
GenericVar *gv = IPPairGetStorageById(h, ippair_bit_id);
GenericVar *gv = IPPairGetStorageById(h, g_ippair_bit_storage_id);
for ( ; gv != NULL; gv = gv->next) {
if (gv->type == DETECT_XBITS) {
XBit *xb = (XBit *)gv;
@ -80,7 +80,7 @@ int IPPairBitsTimedoutCheck(IPPair *h, struct timeval *ts)
/* get the bit with idx from the ippair */
static XBit *IPPairBitGet(IPPair *h, uint32_t idx)
{
GenericVar *gv = IPPairGetStorageById(h, ippair_bit_id);
GenericVar *gv = IPPairGetStorageById(h, g_ippair_bit_storage_id);
for ( ; gv != NULL; gv = gv->next) {
if (gv->type == DETECT_XBITS && gv->idx == idx) {
return (XBit *)gv;
@ -104,9 +104,9 @@ static void IPPairBitAdd(IPPair *h, uint32_t idx, uint32_t expire)
fb->next = NULL;
fb->expire = expire;
GenericVar *gv = IPPairGetStorageById(h, ippair_bit_id);
GenericVar *gv = IPPairGetStorageById(h, g_ippair_bit_storage_id);
GenericVarAppend(&gv, (GenericVar *)fb);
IPPairSetStorageById(h, ippair_bit_id, gv);
IPPairSetStorageById(h, g_ippair_bit_storage_id, gv);
// bit already set, lets update it's timer
} else {
@ -120,11 +120,11 @@ static void IPPairBitRemove(IPPair *h, uint32_t idx)
if (fb == NULL)
return;
GenericVar *gv = IPPairGetStorageById(h, ippair_bit_id);
GenericVar *gv = IPPairGetStorageById(h, g_ippair_bit_storage_id);
if (gv) {
GenericVarRemove(&gv, (GenericVar *)fb);
XBitFree(fb);
IPPairSetStorageById(h, ippair_bit_id, gv);
IPPairSetStorageById(h, g_ippair_bit_storage_id, gv);
}
}

@ -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
* the GNU General Public License version 2 as published by the Free
@ -32,24 +32,24 @@ unsigned int IPPairStorageSize(void)
return StorageGetSize(STORAGE_IPPAIR);
}
void *IPPairGetStorageById(IPPair *h, int id)
void *IPPairGetStorageById(IPPair *h, IPPairStorageId id)
{
return StorageGetById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id);
return StorageGetById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id.id);
}
int IPPairSetStorageById(IPPair *h, int id, void *ptr)
int IPPairSetStorageById(IPPair *h, IPPairStorageId id, void *ptr)
{
return StorageSetById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id, ptr);
return StorageSetById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id.id, ptr);
}
void *IPPairAllocStorageById(IPPair *h, int id)
void *IPPairAllocStorageById(IPPair *h, IPPairStorageId id)
{
return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id);
return StorageAllocByIdPrealloc((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id.id);
}
void IPPairFreeStorageById(IPPair *h, int id)
void IPPairFreeStorageById(IPPair *h, IPPairStorageId id)
{
StorageFreeById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id);
StorageFreeById((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR, id.id);
}
void IPPairFreeStorage(IPPair *h)
@ -58,8 +58,12 @@ void IPPairFreeStorage(IPPair *h)
StorageFreeAll((Storage *)((void *)h + sizeof(IPPair)), STORAGE_IPPAIR);
}
int IPPairStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) {
return StorageRegister(STORAGE_IPPAIR, name, size, Alloc, Free);
IPPairStorageId IPPairStorageRegister(const char *name, const unsigned int size,
void *(*Alloc)(unsigned int), void (*Free)(void *))
{
int id = StorageRegister(STORAGE_IPPAIR, name, size, Alloc, Free);
IPPairStorageId ippsi = { .id = id };
return ippsi;
}
#ifdef UNITTESTS
@ -79,14 +83,15 @@ static int IPPairStorageTest01(void)
{
StorageInit();
int id1 = IPPairStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1 < 0)
IPPairStorageId id1 = IPPairStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1.id < 0)
goto error;
int id2 = IPPairStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2 < 0)
IPPairStorageId id2 = IPPairStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2.id < 0)
goto error;
int id3 = IPPairStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3 < 0)
IPPairStorageId id3 =
IPPairStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3.id < 0)
goto error;
if (StorageFinalize() < 0)
@ -161,8 +166,8 @@ static int IPPairStorageTest02(void)
{
StorageInit();
int id1 = IPPairStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
if (id1 < 0)
IPPairStorageId id1 = IPPairStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
if (id1.id < 0)
goto error;
if (StorageFinalize() < 0)
@ -214,14 +219,14 @@ static int IPPairStorageTest03(void)
{
StorageInit();
int id1 = IPPairStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
if (id1 < 0)
IPPairStorageId id1 = IPPairStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
if (id1.id < 0)
goto error;
int id2 = IPPairStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2 < 0)
IPPairStorageId id2 = IPPairStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2.id < 0)
goto error;
int id3 = IPPairStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3 < 0)
IPPairStorageId id3 = IPPairStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3.id < 0)
goto error;
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
* the GNU General Public License version 2 as published by the Free
@ -29,17 +29,22 @@
#include "util-storage.h"
#include "ippair.h"
typedef struct IPPairStorageId {
int id;
} IPPairStorageId;
unsigned int IPPairStorageSize(void);
void *IPPairGetStorageById(IPPair *h, int id);
int IPPairSetStorageById(IPPair *h, int id, void *ptr);
void *IPPairAllocStorageById(IPPair *h, int id);
void *IPPairGetStorageById(IPPair *h, IPPairStorageId id);
int IPPairSetStorageById(IPPair *h, IPPairStorageId id, void *ptr);
void *IPPairAllocStorageById(IPPair *h, IPPairStorageId id);
void IPPairFreeStorageById(IPPair *h, int id);
void IPPairFreeStorageById(IPPair *h, IPPairStorageId id);
void IPPairFreeStorage(IPPair *h);
void RegisterIPPairStorageTests(void);
int IPPairStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *));
IPPairStorageId IPPairStorageRegister(const char *name, const unsigned int size,
void *(*Alloc)(unsigned int), void (*Free)(void *));
#endif /* __IPPAIR_STORAGE_H__ */

Loading…
Cancel
Save