/** * \brief Add threshold element into hash table * * \param de_ctx Dectection Context * \param tsh_ptr Threshold element * \param p Packet structure * */ void ThresholdHashAdd(DetectEngineCtx *de_ctx, DetectThresholdEntry *tsh_ptr, Packet *p) { SCEnter(); int ret = 0; switch(tsh_ptr->ipv) { case 4: if (tsh_ptr->track == TRACK_DST) { ret = HashListTableAdd(de_ctx->ths_ctx.threshold_hash_table_dst, tsh_ptr, sizeof(DetectThresholdEntry)); } else if (tsh_ptr->track == TRACK_SRC) { ret = HashListTableAdd(de_ctx->ths_ctx.threshold_hash_table_src, tsh_ptr, sizeof(DetectThresholdEntry)); } break; case 6: if (tsh_ptr->track == TRACK_DST) ret = HashListTableAdd(de_ctx->ths_ctx.threshold_hash_table_dst_ipv6, tsh_ptr, sizeof(DetectThresholdEntry)); else if (tsh_ptr->track == TRACK_SRC) ret = HashListTableAdd(de_ctx->ths_ctx.threshold_hash_table_src_ipv6, tsh_ptr, sizeof(DetectThresholdEntry)); break; } if(ret == -1) { SCLogError(SC_ERR_THRESHOLD_HASH_ADD, "failed to add element into the hash table"); } SCReturn; }
static int HashListTableTestFull02 (void) { int result = 0; HashListTable *ht = HashListTableInit(32, HashListTableGenericHash, NULL, NULL); if (ht == NULL) goto end; int r = HashListTableAdd(ht, "test", 4); if (r != 0) goto end; char *rp = HashListTableLookup(ht, "test", 4); if (rp == NULL) goto end; r = HashListTableRemove(ht, "test2", 5); if (r == 0) goto end; /* all is good! */ result = 1; end: if (ht != NULL) HashListTableFree(ht); return result; }
static int HashListTableTestAdd03 (void) { int result = 0; HashListTable *ht = HashListTableInit(32, HashListTableGenericHash, NULL, NULL); if (ht == NULL) goto end; int r = HashListTableAdd(ht, "test", 0); if (r != 0) goto end; if (ht->listhead == NULL) { printf("ht->listhead == NULL: "); goto end; } if (ht->listtail == NULL) { printf("ht->listtail == NULL: "); goto end; } /* all is good! */ result = 1; end: if (ht != NULL) HashListTableFree(ht); return result; }
static int HashListTableTestAdd02 (void) { int result = 0; HashListTable *ht = HashListTableInit(32, HashListTableGenericHash, NULL, NULL); if (ht == NULL) goto end; int r = HashListTableAdd(ht, NULL, 4); if (r == 0) goto end; /* all is good! */ result = 1; end: if (ht != NULL) HashListTableFree(ht); return result; }
static int HashListTableTestAdd04 (void) { int result = 0; HashListTable *ht = HashListTableInit(32, HashListTableGenericHash, NULL, NULL); if (ht == NULL) goto end; int r = HashListTableAdd(ht, "test", 4); if (r != 0) goto end; char *rp = HashListTableLookup(ht, "test", 4); if (rp == NULL) goto end; HashListTableBucket *htb = HashListTableGetListHead(ht); if (htb == NULL) { printf("htb == NULL: "); goto end; } char *rp2 = HashListTableGetListData(htb); if (rp2 == NULL) { printf("rp2 == NULL: "); goto end; } if (rp != rp2) { printf("rp != rp2: "); goto end; } /* all is good! */ result = 1; end: if (ht != NULL) HashListTableFree(ht); return result; }
/** * \brief Adds a SigGroupHead to the detection engine context SigGroupHead * hash table. * * \param de_ctx Pointer to the detection engine context. * \param sgh Pointer to the SigGroupHead. * * \retval ret 0 on Successfully adding the SigGroupHead; -1 on failure. */ int SigGroupHeadHashAdd(DetectEngineCtx *de_ctx, SigGroupHead *sgh) { int ret = HashListTableAdd(de_ctx->sgh_hash_table, (void *)sgh, 0); return ret; }
static int PrefilterSetupPacketHeaderCommon(SigGroupHead *sgh, int sm_type, void (*Set)(PrefilterPacketHeaderValue *v, void *), _Bool (*Compare)(PrefilterPacketHeaderValue v, void *), void (*Match)(DetectEngineThreadCtx *det_ctx, Packet *p, const void *pectx), _Bool u8hash) { Signature *s = NULL; uint32_t sig = 0; if (sgh == NULL) return 0; /* first count how many engines we will need */ HashListTable *hash_table = HashListTableInit(4096, PrefilterPacketHeaderHashFunc, PrefilterPacketHeaderCompareFunc, PrefilterPacketHeaderFreeFunc); if (hash_table == NULL) return -1; for (sig = 0; sig < sgh->sig_cnt; sig++) { s = sgh->match_array[sig]; if (s == NULL) continue; if (s->prefilter_sm == NULL || s->prefilter_sm->type != sm_type) continue; PrefilterPacketHeaderHashCtx ctx; memset(&ctx, 0, sizeof(ctx)); Set(&ctx.v1, s->prefilter_sm->ctx); GetExtraMatch(s, &ctx.type, &ctx.value); PrefilterPacketHeaderHashCtx *rctx = HashListTableLookup(hash_table, (void *)&ctx, 0); if (rctx != 0) { rctx->cnt++; } else { PrefilterPacketHeaderHashCtx *actx = SCCalloc(1, sizeof(*actx)); if (actx == NULL) goto error; Set(&actx->v1, s->prefilter_sm->ctx); actx->cnt = 1; actx->type = ctx.type; actx->value = ctx.value; int ret = HashListTableAdd(hash_table, actx, 0); if (ret != 0) { SCFree(actx); goto error; } } } if (u8hash == FALSE) { SetupSingle(hash_table, sgh, sm_type, Compare, Match); } else { SetupU8Hash(hash_table, sgh, sm_type, Set, Compare, Match); } HashListTableFree(hash_table); return 0; error: HashListTableFree(hash_table); return -1; }
/** * \brief Adds a MpmStore to the detection engine context MpmStore * * \param de_ctx Pointer to the detection engine context. * \param sgh Pointer to the MpmStore. * * \retval ret 0 on Successfully adding the argument sgh; -1 on failure. */ static int MpmStoreAdd(DetectEngineCtx *de_ctx, MpmStore *s) { int ret = HashListTableAdd(de_ctx->mpm_hash_table, (void *)s, 0); return ret; }