Skip to content

Commit

Permalink
detect: split new keyword id from registration
Browse files Browse the repository at this point in the history
Split DetectHelperKeywordRegister into 2 functions, one for acquiring
a new keyword ID, and another to perform the registration.

This makes it easier to do the traditional C keyword initialization
with a dynamic ID.
  • Loading branch information
jasonish committed Jan 17, 2025
1 parent 70984d8 commit b7fa276
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
5 changes: 2 additions & 3 deletions plugins/ndpi/ndpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,7 @@ static void NdpInitRiskKeyword(void)
* support all the fields required to register the nDPI keywords,
* so we'll just register with an empty keyword specifier to get
* the ID, then fill in the ID. */
SCSigTableElmt keyword = {};
ndpi_protocol_keyword_id = DetectHelperKeywordRegister(&keyword);
ndpi_protocol_keyword_id = SCDetectHelperNewKeywordId();
SCLogDebug("Registered new ndpi-protocol keyword with ID %" PRIu32, ndpi_protocol_keyword_id);

sigmatch_table[ndpi_protocol_keyword_id].name = "ndpi-protocol";
Expand All @@ -485,7 +484,7 @@ static void NdpInitRiskKeyword(void)
sigmatch_table[ndpi_protocol_keyword_id].flags =
(SIGMATCH_QUOTES_OPTIONAL | SIGMATCH_HANDLE_NEGATION);

ndpi_risk_keyword_id = DetectHelperKeywordRegister(&keyword);
ndpi_risk_keyword_id = SCDetectHelperNewKeywordId();
SCLogDebug("Registered new ndpi-risk keyword with ID %" PRIu32, ndpi_risk_keyword_id);

sigmatch_table[ndpi_risk_keyword_id].name = "ndpi-risk";
Expand Down
62 changes: 34 additions & 28 deletions src/detect-engine-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ int DetectHelperMultiBufferMpmRegister(const char *name, const char *desc, AppPr
return DetectBufferTypeGetByName(name);
}

int DetectHelperKeywordRegister(const SCSigTableElmt *kw)
int SCDetectHelperNewKeywordId(void)
{
if (DETECT_TBLSIZE_IDX >= DETECT_TBLSIZE) {
void *tmp = SCRealloc(
Expand All @@ -107,45 +107,51 @@ int DetectHelperKeywordRegister(const SCSigTableElmt *kw)
DETECT_TBLSIZE += DETECT_TBLSIZE_STEP;
}

sigmatch_table[DETECT_TBLSIZE_IDX].name = kw->name;
sigmatch_table[DETECT_TBLSIZE_IDX].desc = kw->desc;
sigmatch_table[DETECT_TBLSIZE_IDX].url = kw->url;
sigmatch_table[DETECT_TBLSIZE_IDX].flags = kw->flags;
sigmatch_table[DETECT_TBLSIZE_IDX].AppLayerTxMatch =
DETECT_TBLSIZE_IDX++;
return DETECT_TBLSIZE_IDX - 1;
}

int DetectHelperKeywordRegister(const SCSigTableElmt *kw)
{
int keyword_id = SCDetectHelperNewKeywordId();
if (keyword_id < 0) {
return -1;
}

sigmatch_table[keyword_id].name = kw->name;
sigmatch_table[keyword_id].desc = kw->desc;
sigmatch_table[keyword_id].url = kw->url;
sigmatch_table[keyword_id].flags = kw->flags;
sigmatch_table[keyword_id].AppLayerTxMatch =
(int (*)(DetectEngineThreadCtx * det_ctx, Flow * f, uint8_t flags, void *alstate,
void *txv, const Signature *s, const SigMatchCtx *ctx)) kw->AppLayerTxMatch;
sigmatch_table[DETECT_TBLSIZE_IDX].Setup =
sigmatch_table[keyword_id].Setup =
(int (*)(DetectEngineCtx * de, Signature * s, const char *raw)) kw->Setup;
sigmatch_table[DETECT_TBLSIZE_IDX].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;
DETECT_TBLSIZE_IDX++;
return DETECT_TBLSIZE_IDX - 1;
sigmatch_table[keyword_id].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;

return keyword_id;
}

int DetectHelperTransformRegister(const SCTransformTableElmt *kw)
{
if (DETECT_TBLSIZE_IDX >= DETECT_TBLSIZE) {
void *tmp = SCRealloc(
sigmatch_table, (DETECT_TBLSIZE + DETECT_TBLSIZE_STEP) * sizeof(SigTableElmt));
if (unlikely(tmp == NULL)) {
return -1;
}
sigmatch_table = tmp;
DETECT_TBLSIZE += DETECT_TBLSIZE_STEP;
int transform_id = SCDetectHelperNewKeywordId();
if (transform_id < 0) {
return -1;
}

sigmatch_table[DETECT_TBLSIZE_IDX].name = kw->name;
sigmatch_table[DETECT_TBLSIZE_IDX].desc = kw->desc;
sigmatch_table[DETECT_TBLSIZE_IDX].url = kw->url;
sigmatch_table[DETECT_TBLSIZE_IDX].flags = kw->flags;
sigmatch_table[DETECT_TBLSIZE_IDX].Transform =
sigmatch_table[transform_id].name = kw->name;
sigmatch_table[transform_id].desc = kw->desc;
sigmatch_table[transform_id].url = kw->url;
sigmatch_table[transform_id].flags = kw->flags;
sigmatch_table[transform_id].Transform =
(void (*)(InspectionBuffer * buffer, void *options)) kw->Transform;
sigmatch_table[DETECT_TBLSIZE_IDX].TransformValidate = (bool (*)(
sigmatch_table[transform_id].TransformValidate = (bool (*)(
const uint8_t *content, uint16_t content_len, void *context))kw->TransformValidate;
sigmatch_table[DETECT_TBLSIZE_IDX].Setup =
sigmatch_table[transform_id].Setup =
(int (*)(DetectEngineCtx * de, Signature * s, const char *raw)) kw->Setup;
sigmatch_table[DETECT_TBLSIZE_IDX].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;
DETECT_TBLSIZE_IDX++;
return DETECT_TBLSIZE_IDX - 1;
sigmatch_table[transform_id].Free = (void (*)(DetectEngineCtx * de, void *ptr)) kw->Free;

return transform_id;
}

InspectionBuffer *DetectHelperGetMultiData(struct DetectEngineThreadCtx_ *det_ctx,
Expand Down
2 changes: 2 additions & 0 deletions src/detect-engine-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "detect.h"
#include "rust.h"

int SCDetectHelperNewKeywordId(void);

int DetectHelperKeywordRegister(const SCSigTableElmt *kw);
int DetectHelperBufferRegister(const char *name, AppProto alproto, bool toclient, bool toserver);

Expand Down

0 comments on commit b7fa276

Please sign in to comment.