From: Victor Julien Date: Fri, 9 Apr 2021 10:56:01 +0000 (+0200) Subject: flow/storage: use dedicated 'id' type X-Git-Tag: suricata-6.0.6~101 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf98494ae83614d9bbde657f9d9ed8db2211636c;p=thirdparty%2Fsuricata.git flow/storage: use dedicated 'id' type Wrap the id in a new FlowStorageId struct to avoid id confusion with other storage API calls. (cherry picked from commit bc667a4a939c887bc298bbb865eda4338f8cea2f) --- diff --git a/src/app-layer-expectation.c b/src/app-layer-expectation.c index 0aa4797762..6078d4d24a 100644 --- a/src/app-layer-expectation.c +++ b/src/app-layer-expectation.c @@ -64,7 +64,7 @@ #include "util-print.h" static int g_ippair_expectation_id = -1; -static int g_flow_expectation_id = -1; +static FlowStorageId g_flow_expectation_id = { .id = -1 }; SC_ATOMIC_DECLARE(uint32_t, expectation_count); @@ -286,7 +286,7 @@ error: * * \return expectation data identifier */ -int AppLayerExpectationGetFlowId(void) +FlowStorageId AppLayerExpectationGetFlowId(void) { return g_flow_expectation_id; } diff --git a/src/app-layer-expectation.h b/src/app-layer-expectation.h index 6a2ba195b1..1bb714ce12 100644 --- a/src/app-layer-expectation.h +++ b/src/app-layer-expectation.h @@ -24,11 +24,13 @@ #ifndef __APP_LAYER_EXPECTATION__H__ #define __APP_LAYER_EXPECTATION__H__ +#include "flow-storage.h" + void AppLayerExpectationSetup(void); int AppLayerExpectationCreate(Flow *f, int direction, Port src, Port dst, AppProto alproto, void *data); AppProto AppLayerExpectationHandle(Flow *f, uint8_t flags); -int AppLayerExpectationGetFlowId(void); +FlowStorageId AppLayerExpectationGetFlowId(void); void AppLayerExpectationClean(Flow *f); diff --git a/src/detect-engine-tag.c b/src/detect-engine-tag.c index 9f7a5ec4e8..f79548eb61 100644 --- a/src/detect-engine-tag.c +++ b/src/detect-engine-tag.c @@ -46,7 +46,7 @@ SC_ATOMIC_DECLARE(unsigned int, num_tags); /**< Atomic counter, to know if we have tagged hosts/sessions, to avoid locking */ static int host_tag_id = -1; /**< Host storage id for tags */ -static int flow_tag_id = -1; /**< Flow storage id for tags */ +static FlowStorageId flow_tag_id = { .id = -1 }; /**< Flow storage id for tags */ void TagInitCtx(void) { @@ -57,7 +57,7 @@ void TagInitCtx(void) FatalError(SC_ERR_FATAL, "Can't initiate host storage for tag"); } flow_tag_id = FlowStorageRegister("tag", sizeof(void *), NULL, DetectTagDataListFree); - if (flow_tag_id == -1) { + if (flow_tag_id.id == -1) { FatalError(SC_ERR_FATAL, "Can't initiate flow storage for tag"); } } diff --git a/src/flow-storage.c b/src/flow-storage.c index f63052f91d..d4f89e8a2d 100644 --- a/src/flow-storage.c +++ b/src/flow-storage.c @@ -36,24 +36,24 @@ unsigned int FlowStorageSize(void) return StorageGetSize(STORAGE_FLOW); } -void *FlowGetStorageById(Flow *f, int id) +void *FlowGetStorageById(Flow *f, FlowStorageId id) { - return StorageGetById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id); + return StorageGetById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id.id); } -int FlowSetStorageById(Flow *f, int id, void *ptr) +int FlowSetStorageById(Flow *f, FlowStorageId id, void *ptr) { - return StorageSetById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id, ptr); + return StorageSetById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id.id, ptr); } -void *FlowAllocStorageById(Flow *f, int id) +void *FlowAllocStorageById(Flow *f, FlowStorageId id) { - return StorageAllocByIdPrealloc((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id); + return StorageAllocByIdPrealloc((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id.id); } -void FlowFreeStorageById(Flow *f, int id) +void FlowFreeStorageById(Flow *f, FlowStorageId id) { - StorageFreeById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id); + StorageFreeById((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW, id.id); } void FlowFreeStorage(Flow *f) @@ -62,8 +62,12 @@ void FlowFreeStorage(Flow *f) StorageFreeAll((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW); } -int FlowStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)) { - return StorageRegister(STORAGE_FLOW, name, size, Alloc, Free); +FlowStorageId FlowStorageRegister(const char *name, const unsigned int size, + void *(*Alloc)(unsigned int), void (*Free)(void *)) +{ + int id = StorageRegister(STORAGE_FLOW, name, size, Alloc, Free); + FlowStorageId fsi = { .id = id }; + return fsi; } #ifdef UNITTESTS @@ -85,14 +89,15 @@ static int FlowStorageTest01(void) StorageInit(); - int id1 = FlowStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); - if (id1 < 0) + FlowStorageId id1 = FlowStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); + if (id1.id < 0) goto error; - int id2 = FlowStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); - if (id2 < 0) + FlowStorageId id2 = FlowStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); + if (id2.id < 0) goto error; - int id3 = FlowStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree); - if (id3 < 0) + FlowStorageId id3 = + FlowStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree); + if (id3.id < 0) goto error; if (StorageFinalize() < 0) @@ -165,8 +170,8 @@ static int FlowStorageTest02(void) StorageInit(); - int id1 = FlowStorageRegister("test", sizeof(void *), NULL, StorageTestFree); - if (id1 < 0) + FlowStorageId id1 = FlowStorageRegister("test", sizeof(void *), NULL, StorageTestFree); + if (id1.id < 0) goto error; if (StorageFinalize() < 0) @@ -216,14 +221,14 @@ static int FlowStorageTest03(void) StorageInit(); - int id1 = FlowStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); - if (id1 < 0) + FlowStorageId id1 = FlowStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); + if (id1.id < 0) goto error; - int id2 = FlowStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); - if (id2 < 0) + FlowStorageId id2 = FlowStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); + if (id2.id < 0) goto error; - int id3 = FlowStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); - if (id3 < 0) + FlowStorageId id3 = FlowStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); + if (id3.id < 0) goto error; if (StorageFinalize() < 0) diff --git a/src/flow-storage.h b/src/flow-storage.h index 93892d1684..fb4958dc08 100644 --- a/src/flow-storage.h +++ b/src/flow-storage.h @@ -29,17 +29,22 @@ #include "util-storage.h" #include "flow.h" +typedef struct FlowStorageId { + int id; +} FlowStorageId; + unsigned int FlowStorageSize(void); -void *FlowGetStorageById(Flow *h, int id); -int FlowSetStorageById(Flow *h, int id, void *ptr); -void *FlowAllocStorageById(Flow *h, int id); +void *FlowGetStorageById(Flow *h, FlowStorageId id); +int FlowSetStorageById(Flow *h, FlowStorageId id, void *ptr); +void *FlowAllocStorageById(Flow *h, FlowStorageId id); -void FlowFreeStorageById(Flow *h, int id); +void FlowFreeStorageById(Flow *h, FlowStorageId id); void FlowFreeStorage(Flow *h); void RegisterFlowStorageTests(void); -int FlowStorageRegister(const char *name, const unsigned int size, void *(*Alloc)(unsigned int), void (*Free)(void *)); +FlowStorageId FlowStorageRegister(const char *name, const unsigned int size, + void *(*Alloc)(unsigned int), void (*Free)(void *)); #endif /* __FLOW_STORAGE_H__ */ diff --git a/src/flow-util.c b/src/flow-util.c index b16f7c26e4..e48eeca268 100644 --- a/src/flow-util.c +++ b/src/flow-util.c @@ -213,9 +213,9 @@ void FlowInit(Flow *f, const Packet *p) SCReturn; } -int g_bypass_info_id = -1; +FlowStorageId g_bypass_info_id = { .id = -1 }; -int GetFlowBypassInfoID(void) +FlowStorageId GetFlowBypassInfoID(void) { return g_bypass_info_id; } diff --git a/src/flow.h b/src/flow.h index 0a319ffaf2..9adba466df 100644 --- a/src/flow.h +++ b/src/flow.h @@ -24,6 +24,9 @@ #ifndef __FLOW_H__ #define __FLOW_H__ +/* forward declaration for macset include */ +typedef struct FlowStorageId FlowStorageId; + #include "decode.h" #include "util-var.h" #include "util-atomic.h" @@ -578,7 +581,7 @@ int FlowSetMemcap(uint64_t size); uint64_t FlowGetMemcap(void); uint64_t FlowGetMemuse(void); -int GetFlowBypassInfoID(void); +FlowStorageId GetFlowBypassInfoID(void); void RegisterFlowBypassInfo(void); void FlowGetLastTimeAsParts(Flow *flow, uint64_t *secs, uint64_t *usecs); diff --git a/src/util-macset.c b/src/util-macset.c index 51678f27fd..d169711fea 100644 --- a/src/util-macset.c +++ b/src/util-macset.c @@ -56,7 +56,7 @@ struct MacSet_ { last[2]; }; -int g_macset_storage_id = -1; +FlowStorageId g_macset_storage_id = { .id = -1 }; void MacSetRegisterFlowStorage(void) { @@ -83,7 +83,7 @@ void MacSetRegisterFlowStorage(void) bool MacSetFlowStorageEnabled(void) { - return (g_macset_storage_id != -1); + return (g_macset_storage_id.id != -1); } @@ -110,7 +110,7 @@ MacSet *MacSetInit(int size) return ms; } -int MacSetGetFlowStorageID(void) +FlowStorageId MacSetGetFlowStorageID(void) { return g_macset_storage_id; } diff --git a/src/util-macset.h b/src/util-macset.h index 833f9bf8e4..1b17255593 100644 --- a/src/util-macset.h +++ b/src/util-macset.h @@ -41,8 +41,8 @@ int MacSetSize(const MacSet*); void MacSetReset(MacSet*); void MacSetFree(MacSet*); void MacSetRegisterFlowStorage(void); -int MacSetGetFlowStorageID(void); +FlowStorageId MacSetGetFlowStorageID(void); bool MacSetFlowStorageEnabled(void); void MacSetRegisterTests(void); -#endif /* __UTIL_MACSET_H__ */ \ No newline at end of file +#endif /* __UTIL_MACSET_H__ */