/** * \test Check if the reference info from the reference.config file have * been loaded into the hash table. */ int SCRConfTest06(void) { DetectEngineCtx *de_ctx = DetectEngineCtxInit(); SCRConfReference *ref = NULL; int result = 1; if (de_ctx == NULL) return 0; SCRConfGenerateInValidDummyReferenceConfigFD02(); SCRConfLoadReferenceConfigFile(de_ctx); SCRConfDeleteDummyReferenceConfigFD(); if (de_ctx->reference_conf_ht == NULL) goto end; result = (de_ctx->reference_conf_ht->count == 1); ref = SCRConfAllocSCRConfReference("one", "one"); result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) != NULL); SCRConfDeAllocSCRConfReference(ref); ref = SCRConfAllocSCRConfReference("two", "two"); result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL); SCRConfDeAllocSCRConfReference(ref); ref = SCRConfAllocSCRConfReference("three", "three"); result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL); SCRConfDeAllocSCRConfReference(ref); ref = SCRConfAllocSCRConfReference("four", "four"); result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL); SCRConfDeAllocSCRConfReference(ref); ref = SCRConfAllocSCRConfReference("five", "five"); result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL); SCRConfDeAllocSCRConfReference(ref); end: if (de_ctx != NULL) DetectEngineCtxFree(de_ctx); return result; }
/** * \brief Parses a line from the reference config file and adds it to Reference * Config hash table DetectEngineCtx->reference_conf_ht. * * \param rawstr Pointer to the string to be parsed. * \param de_ctx Pointer to the Detection Engine Context. * * \retval 0 On success. * \retval -1 On failure. */ static int SCRConfAddReference(char *rawstr, DetectEngineCtx *de_ctx) { char system[64]; char url[1024]; SCRConfReference *ref_new = NULL; SCRConfReference *ref_lookup = NULL; #define MAX_SUBSTRINGS 30 int ret = 0; int ov[MAX_SUBSTRINGS]; ret = pcre_exec(regex, regex_study, rawstr, strlen(rawstr), 0, 0, ov, 30); if (ret < 0) { SCLogError(SC_ERR_REFERENCE_CONFIG, "Invalid Reference Config in " "reference.config file"); goto error; } /* retrieve the reference system */ ret = pcre_copy_substring((char *)rawstr, ov, 30, 1, system, sizeof(system)); if (ret < 0) { SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring() failed"); goto error; } /* retrieve the reference url */ ret = pcre_copy_substring((char *)rawstr, ov, 30, 2, url, sizeof(url)); if (ret < 0) { SCLogError(SC_ERR_PCRE_GET_SUBSTRING, "pcre_copy_substring() failed"); goto error; } /* Create a new instance of the parsed Reference string */ ref_new = SCRConfAllocSCRConfReference(system, url); if (ref_new == NULL) goto error; /* Check if the Reference is present in the HashTable. In case it's present * ignore it, as it's a duplicate. If not present, add it to the table */ ref_lookup = HashTableLookup(de_ctx->reference_conf_ht, ref_new, 0); if (ref_lookup == NULL) { if (HashTableAdd(de_ctx->reference_conf_ht, ref_new, 0) < 0) { SCLogDebug("HashTable Add failed"); } } else { SCLogDebug("Duplicate reference found inside reference.config"); SCRConfDeAllocSCRConfReference(ref_new); } return 0; error: return -1; }
/** * \brief Gets the refernce config from the corresponding hash table stored * in the Detection Engine Context's reference conf ht, given the * reference name. * * \param ct_name Pointer to the reference name that has to be looked up. * \param de_ctx Pointer to the Detection Engine Context. * * \retval lookup_rconf_info Pointer to the SCRConfReference instance from * the hash table on success; NULL on failure. */ SCRConfReference *SCRConfGetReference(const char *rconf_name, DetectEngineCtx *de_ctx) { SCRConfReference *ref_conf = SCRConfAllocSCRConfReference(rconf_name, NULL); if (ref_conf == NULL) return NULL; SCRConfReference *lookup_ref_conf = HashTableLookup(de_ctx->reference_conf_ht, ref_conf, 0); SCRConfDeAllocSCRConfReference(ref_conf); return lookup_ref_conf; }
/** * \brief Used to free the Reference Config Hash Data that was stored in * DetectEngineCtx->reference_conf_ht Hashtable. * * \param data Pointer to the data that has to be freed. */ void SCRConfReferenceHashFree(void *data) { SCRConfDeAllocSCRConfReference(data); return; }