flow/storage: use dedicated 'id' type

Wrap the id in a new FlowStorageId struct to avoid id confusion with other
storage API calls.
pull/6042/head
Victor Julien 4 years ago
parent d2d0e0adc9
commit bc667a4a93

@ -64,7 +64,7 @@
#include "queue.h" #include "queue.h"
static int g_ippair_expectation_id = -1; 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); SC_ATOMIC_DECLARE(uint32_t, expectation_count);
@ -286,7 +286,7 @@ error:
* *
* \return expectation data identifier * \return expectation data identifier
*/ */
int AppLayerExpectationGetFlowId(void) FlowStorageId AppLayerExpectationGetFlowId(void)
{ {
return g_flow_expectation_id; return g_flow_expectation_id;
} }

@ -24,11 +24,13 @@
#ifndef __APP_LAYER_EXPECTATION__H__ #ifndef __APP_LAYER_EXPECTATION__H__
#define __APP_LAYER_EXPECTATION__H__ #define __APP_LAYER_EXPECTATION__H__
#include "flow-storage.h"
void AppLayerExpectationSetup(void); void AppLayerExpectationSetup(void);
int AppLayerExpectationCreate(Flow *f, int direction, Port src, Port dst, int AppLayerExpectationCreate(Flow *f, int direction, Port src, Port dst,
AppProto alproto, void *data); AppProto alproto, void *data);
AppProto AppLayerExpectationHandle(Flow *f, uint8_t flags); AppProto AppLayerExpectationHandle(Flow *f, uint8_t flags);
int AppLayerExpectationGetFlowId(void); FlowStorageId AppLayerExpectationGetFlowId(void);
void AppLayerExpectationClean(Flow *f); void AppLayerExpectationClean(Flow *f);

@ -46,7 +46,7 @@ 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 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) void TagInitCtx(void)
{ {
@ -57,7 +57,7 @@ void TagInitCtx(void)
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);
if (flow_tag_id == -1) { if (flow_tag_id.id == -1) {
FatalError(SC_ERR_FATAL, "Can't initiate flow storage for tag"); FatalError(SC_ERR_FATAL, "Can't initiate flow storage for tag");
} }
} }

@ -36,24 +36,24 @@ unsigned int FlowStorageSize(void)
return StorageGetSize(STORAGE_FLOW); 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) void FlowFreeStorage(Flow *f)
@ -62,8 +62,12 @@ void FlowFreeStorage(Flow *f)
StorageFreeAll((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW); StorageFreeAll((Storage *)((void *)f + sizeof(Flow)), STORAGE_FLOW);
} }
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,
return StorageRegister(STORAGE_FLOW, name, size, Alloc, Free); void *(*Alloc)(unsigned int), void (*Free)(void *))
{
int id = StorageRegister(STORAGE_FLOW, name, size, Alloc, Free);
FlowStorageId fsi = { .id = id };
return fsi;
} }
#ifdef UNITTESTS #ifdef UNITTESTS
@ -85,14 +89,15 @@ static int FlowStorageTest01(void)
StorageInit(); StorageInit();
int id1 = FlowStorageRegister("test", 8, StorageTestAlloc, StorageTestFree); FlowStorageId id1 = FlowStorageRegister("test", 8, StorageTestAlloc, StorageTestFree);
if (id1 < 0) if (id1.id < 0)
goto error; goto error;
int id2 = FlowStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree); FlowStorageId id2 = FlowStorageRegister("variable", 24, StorageTestAlloc, StorageTestFree);
if (id2 < 0) if (id2.id < 0)
goto error; goto error;
int id3 = FlowStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree); FlowStorageId id3 =
if (id3 < 0) FlowStorageRegister("store", sizeof(void *), StorageTestAlloc, StorageTestFree);
if (id3.id < 0)
goto error; goto error;
if (StorageFinalize() < 0) if (StorageFinalize() < 0)
@ -165,8 +170,8 @@ static int FlowStorageTest02(void)
StorageInit(); StorageInit();
int id1 = FlowStorageRegister("test", sizeof(void *), NULL, StorageTestFree); FlowStorageId id1 = FlowStorageRegister("test", sizeof(void *), NULL, StorageTestFree);
if (id1 < 0) if (id1.id < 0)
goto error; goto error;
if (StorageFinalize() < 0) if (StorageFinalize() < 0)
@ -216,14 +221,14 @@ static int FlowStorageTest03(void)
StorageInit(); StorageInit();
int id1 = FlowStorageRegister("test1", sizeof(void *), NULL, StorageTestFree); FlowStorageId id1 = FlowStorageRegister("test1", sizeof(void *), NULL, StorageTestFree);
if (id1 < 0) if (id1.id < 0)
goto error; goto error;
int id2 = FlowStorageRegister("test2", sizeof(void *), NULL, StorageTestFree); FlowStorageId id2 = FlowStorageRegister("test2", sizeof(void *), NULL, StorageTestFree);
if (id2 < 0) if (id2.id < 0)
goto error; goto error;
int id3 = FlowStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree); FlowStorageId id3 = FlowStorageRegister("test3", 32, StorageTestAlloc, StorageTestFree);
if (id3 < 0) if (id3.id < 0)
goto error; goto error;
if (StorageFinalize() < 0) if (StorageFinalize() < 0)

@ -29,17 +29,22 @@
#include "util-storage.h" #include "util-storage.h"
#include "flow.h" #include "flow.h"
typedef struct FlowStorageId {
int id;
} FlowStorageId;
unsigned int FlowStorageSize(void); unsigned int FlowStorageSize(void);
void *FlowGetStorageById(Flow *h, int id); void *FlowGetStorageById(Flow *h, FlowStorageId id);
int FlowSetStorageById(Flow *h, int id, void *ptr); int FlowSetStorageById(Flow *h, FlowStorageId id, void *ptr);
void *FlowAllocStorageById(Flow *h, int id); void *FlowAllocStorageById(Flow *h, FlowStorageId id);
void FlowFreeStorageById(Flow *h, int id); void FlowFreeStorageById(Flow *h, FlowStorageId id);
void FlowFreeStorage(Flow *h); void FlowFreeStorage(Flow *h);
void RegisterFlowStorageTests(void); 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__ */ #endif /* __FLOW_STORAGE_H__ */

@ -215,9 +215,9 @@ void FlowInit(Flow *f, const Packet *p)
SCReturn; 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; return g_bypass_info_id;
} }

@ -24,6 +24,9 @@
#ifndef __FLOW_H__ #ifndef __FLOW_H__
#define __FLOW_H__ #define __FLOW_H__
/* forward declaration for macset include */
typedef struct FlowStorageId FlowStorageId;
#include "decode.h" #include "decode.h"
#include "util-var.h" #include "util-var.h"
#include "util-atomic.h" #include "util-atomic.h"
@ -579,7 +582,7 @@ int FlowSetMemcap(uint64_t size);
uint64_t FlowGetMemcap(void); uint64_t FlowGetMemcap(void);
uint64_t FlowGetMemuse(void); uint64_t FlowGetMemuse(void);
int GetFlowBypassInfoID(void); FlowStorageId GetFlowBypassInfoID(void);
void RegisterFlowBypassInfo(void); void RegisterFlowBypassInfo(void);
void FlowGetLastTimeAsParts(Flow *flow, uint64_t *secs, uint64_t *usecs); void FlowGetLastTimeAsParts(Flow *flow, uint64_t *secs, uint64_t *usecs);

@ -56,7 +56,7 @@ struct MacSet_ {
last[2]; last[2];
}; };
int g_macset_storage_id = -1; FlowStorageId g_macset_storage_id = { .id = -1 };
void MacSetRegisterFlowStorage(void) void MacSetRegisterFlowStorage(void)
{ {
@ -83,7 +83,7 @@ void MacSetRegisterFlowStorage(void)
bool MacSetFlowStorageEnabled(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; return ms;
} }
int MacSetGetFlowStorageID(void) FlowStorageId MacSetGetFlowStorageID(void)
{ {
return g_macset_storage_id; return g_macset_storage_id;
} }

@ -41,7 +41,7 @@ int MacSetSize(const MacSet*);
void MacSetReset(MacSet*); void MacSetReset(MacSet*);
void MacSetFree(MacSet*); void MacSetFree(MacSet*);
void MacSetRegisterFlowStorage(void); void MacSetRegisterFlowStorage(void);
int MacSetGetFlowStorageID(void); FlowStorageId MacSetGetFlowStorageID(void);
bool MacSetFlowStorageEnabled(void); bool MacSetFlowStorageEnabled(void);
void MacSetRegisterTests(void); void MacSetRegisterTests(void);

Loading…
Cancel
Save