From: Juliana Fajardini Date: Wed, 14 Apr 2021 22:09:02 +0000 (+0100) Subject: ippair/storage: use dedicated 'id' type X-Git-Tag: suricata-7.0.0-beta1~1682 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cf516de587d27300cb5af42ae548f54351f7de5d;p=thirdparty%2Fsuricata.git 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. --- diff --git a/src/app-layer-expectation.c b/src/app-layer-expectation.c index f6efe314e4..fd9ab6edbf 100644 --- a/src/app-layer-expectation.c +++ b/src/app-layer-expectation.c @@ -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); diff --git a/src/app-layer-expectation.h b/src/app-layer-expectation.h index 1bb714ce12..0219e33bcf 100644 --- a/src/app-layer-expectation.h +++ b/src/app-layer-expectation.h @@ -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 diff --git a/src/detect-engine-threshold.c b/src/detect-engine-threshold.c index eff4fe4f6b..1ba59a5090 100644 --- a/src/detect-engine-threshold.c +++ b/src/detect-engine-threshold.c @@ -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"); } diff --git a/src/ippair-bit.c b/src/ippair-bit.c index aacf21fb3f..08e6fe7376 100644 --- a/src/ippair-bit.c +++ b/src/ippair-bit.c @@ -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); } } diff --git a/src/ippair-storage.c b/src/ippair-storage.c index 890b0c9c18..0d1fd4af99 100644 --- a/src/ippair-storage.c +++ b/src/ippair-storage.c @@ -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) diff --git a/src/ippair-storage.h b/src/ippair-storage.h index 0671ac9196..6175084c34 100644 --- a/src/ippair-storage.h +++ b/src/ippair-storage.h @@ -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__ */