]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
ippair/storage: use dedicated 'id' type
authorJuliana Fajardini <jufajardini@gmail.com>
Wed, 14 Apr 2021 22:09:02 +0000 (23:09 +0100)
committerVictor Julien <vjulien@oisf.net>
Tue, 3 May 2022 11:30:12 +0000 (13:30 +0200)
- Wrap the id in a new IPPairStorageId struct, to avoid id
confusion with other storage API calls.
- Formatting fixes by clang.

(cherry picked from commit cf516de587d27300cb5af42ae548f54351f7de5d)

src/app-layer-expectation.c
src/app-layer-expectation.h
src/detect-engine-threshold.c
src/ippair-bit.c
src/ippair-storage.c
src/ippair-storage.h

index 6078d4d24a13417df5960b6697b7ef84810dc6ce..1f11d13f77950e10bd25205f724d6b8fc88b53f4 100644 (file)
@@ -63,7 +63,7 @@
 
 #include "util-print.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);
index 1bb714ce12c6f0120543ec11df06c2b4ce68bd58..0219e33bcf243cff598306ad8abe2d98f1379e16 100644 (file)
@@ -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
index 1fabcef960d6d561636352b5263e1d27bbb634e7..ad0a6c2ba8dd6cfce5d2e2adb8e0ca2126263158 100644 (file)
@@ -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");
     }
index aacf21fb3ff1a3f06a523246dac9c70223bf7c3d..9570c70128e13d3bb9e126444f5fce6559b2472e 100644 (file)
@@ -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,11 +104,11 @@ 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
+        // bit already set, lets update it's timer
     } else {
         fb->expire = expire;
     }
@@ -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);
     }
 }
 
index 890b0c9c18ac27c8e096ccef920a6360f2ba4d1e..0d1fd4af99d0242c96c4ddf2cd0035fdfc988297 100644 (file)
@@ -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)
index 0671ac9196c9ce588be728db4a335b851dc99e6e..6175084c34268847ef66c7d5e7a52ef0d783023d 100644 (file)
@@ -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
 #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__ */