]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
clean reference config API
authorAnoop Saldanha <poonaatsoc@gmail.com>
Fri, 22 Jun 2012 10:28:03 +0000 (15:58 +0530)
committerVictor Julien <victor@inliniac.net>
Tue, 26 Jun 2012 07:36:10 +0000 (09:36 +0200)
src/detect-engine.c
src/detect-reference.c
src/util-reference-config.c
src/util-reference-config.h

index 8a55e201f8518a6e53510eb33ac5c240de070784..a35eb14482658d7dd655146ece870db7f9c77131 100644 (file)
@@ -48,6 +48,7 @@
 
 //#include "util-mpm.h"
 #include "util-classification-config.h"
+#include "util-reference-config.h"
 #include "util-error.h"
 #include "util-hash.h"
 #include "util-byte.h"
@@ -165,6 +166,7 @@ void DetectEngineCtxFree(DetectEngineCtx *de_ctx) {
         SCFree(de_ctx->sig_array);
 
     SCClassConfDeInitContext(de_ctx);
+    SCRConfDeInitContext(de_ctx);
 
     SCFree(de_ctx);
     //DetectAddressGroupPrintMemory();
index 381e8a43932f914c245f02786aa43169d4dc18dd..284324118809dc553932dc2ae42398092f77651b 100644 (file)
@@ -148,9 +148,7 @@ static DetectReference *DetectReferenceParse(char *rawstr, DetectEngineCtx *de_c
     if (key == NULL || content == NULL)
         goto error;
 
-    SCRConfReference *ref_conf = SCRConfAllocSCRConfReference(key, NULL);
-    SCRConfReference *lookup_ref_conf = HashTableLookup(de_ctx->reference_conf_ht,
-                                                        ref_conf, 0);
+    SCRConfReference *lookup_ref_conf = SCRConfGetReference(key, de_ctx);
     if (lookup_ref_conf != NULL) {
         ref->key = lookup_ref_conf->url;
     } else {
@@ -159,7 +157,6 @@ static DetectReference *DetectReferenceParse(char *rawstr, DetectEngineCtx *de_c
                    "have a look at the conf param \"reference-config-file\"", key);
         goto error;
     }
-    SCRConfDeAllocSCRConfReference(ref_conf);
 
     /* make a copy so we can free pcre's substring */
     ref->reference = SCStrdup((char *)content);
index 86a17baaab905a412893ab9ef5e84e3e5814da10..2caf4ac7e0446eacf7abe07aaa6fb6ecadb91718 100644 (file)
@@ -69,7 +69,7 @@ static char *SCRConfGetConfFilename(void);
  * \retval  0 On success.
  * \retval -1 On failure.
  */
-static int SCRConfInitContext(DetectEngineCtx *de_ctx)
+static int SCRConfInitContextAndLocalResources(DetectEngineCtx *de_ctx)
 {
     char *filename = NULL;
     const char *eb = NULL;
@@ -83,7 +83,7 @@ static int SCRConfInitContext(DetectEngineCtx *de_ctx)
     if (de_ctx->reference_conf_ht == NULL) {
         SCLogError(SC_ERR_HASH_TABLE_INIT, "Error initializing the hash "
                    "table");
-        return -1;
+        goto error;
     }
 
     /* if it is not NULL, use the file descriptor.  The hack so that we can
@@ -124,6 +124,9 @@ static int SCRConfInitContext(DetectEngineCtx *de_ctx)
         fd = NULL;
     }
 
+    regex = NULL;
+    regex_study = NULL;
+
     return -1;
 }
 
@@ -145,14 +148,29 @@ static char *SCRConfGetConfFilename(void)
 }
 
 /**
- * \brief Releases resources used by the Reference Config API.
+ * \brief Releases local resources used by the Reference Config API.
  */
-static void SCRConfDeInitContext(DetectEngineCtx *de_ctx)
+static void SCRConfDeInitLocalResources(DetectEngineCtx *de_ctx)
 {
     if (fd != NULL)
         fclose(fd);
     file_path = SC_RCONF_DEFAULT_FILE_PATH;
     fd = NULL;
+    regex = NULL;
+    regex_study = NULL;
+
+    return;
+}
+
+/**
+ * \brief Releases de_ctx resources related to Reference Config API.
+ */
+void SCRConfDeInitContext(DetectEngineCtx *de_ctx)
+{
+    if (de_ctx->reference_conf_ht != NULL)
+        HashTableFree(de_ctx->reference_conf_ht);
+
+    de_ctx->reference_conf_ht = NULL;
 
     return;
 }
@@ -458,18 +476,40 @@ void SCRConfReferenceHashFree(void *data)
  */
 int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx)
 {
-    if (SCRConfInitContext(de_ctx) == -1) {
-        printf("\nPlease check the \"reference-config-file\" option in your suricata.yaml file.\n");
+    if (SCRConfInitContextAndLocalResources(de_ctx) == -1) {
+        SCLogInfo("Please check the \"reference-config-file\" option in your suricata.yaml file");
         exit(EXIT_FAILURE);
-        return -1;
     }
 
     SCRConfParseFile(de_ctx);
-    SCRConfDeInitContext(de_ctx);
+    SCRConfDeInitLocalResources(de_ctx);
 
     return 0;
 }
 
+/**
+ * \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)
+        exit(EXIT_FAILURE);
+    SCRConfReference *lookup_ref_conf = HashTableLookup(de_ctx->reference_conf_ht,
+                                                        ref_conf, 0);
+
+    SCRConfDeAllocSCRConfReference(ref_conf);
+    return lookup_ref_conf;
+}
 
 /*----------------------------------Unittests---------------------------------*/
 
@@ -639,7 +679,6 @@ int SCRConfTest03(void)
 int SCRConfTest04(void)
 {
     DetectEngineCtx *de_ctx = DetectEngineCtxInit();
-    SCRConfReference *ref = NULL;
     int result = 1;
 
     if (de_ctx == NULL)
@@ -654,21 +693,10 @@ int SCRConfTest04(void)
 
     result = (de_ctx->reference_conf_ht->count == 3);
 
-    ref = SCRConfAllocSCRConfReference("one", "http://www.one.com");
-    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) != NULL);
-    SCRConfDeAllocSCRConfReference(ref);
-
-    ref = SCRConfAllocSCRConfReference("two", "http://www.two.com");
-    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) != NULL);
-    SCRConfDeAllocSCRConfReference(ref);
-
-    ref = SCRConfAllocSCRConfReference("three", "http://www.three.com");
-    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) != NULL);
-    SCRConfDeAllocSCRConfReference(ref);
-
-    ref = SCRConfAllocSCRConfReference("four", "http://www.four.com");
-    result &= (HashTableLookup(de_ctx->reference_conf_ht, ref, 0) == NULL);
-    SCRConfDeAllocSCRConfReference(ref);
+    result &= (SCRConfGetReference("one", de_ctx) != NULL);
+    result &= (SCRConfGetReference("two", de_ctx) != NULL);
+    result &= (SCRConfGetReference("three", de_ctx) != NULL);
+    result &= (SCRConfGetReference("four", de_ctx) == NULL);
 
  end:
     if (de_ctx != NULL)
@@ -684,7 +712,6 @@ int SCRConfTest04(void)
 int SCRConfTest05(void)
 {
     DetectEngineCtx *de_ctx = DetectEngineCtxInit();
-    SCRConfReference *ref = NULL;
     int result = 1;
 
     if (de_ctx == NULL)
@@ -699,25 +726,11 @@ int SCRConfTest05(void)
 
     result = (de_ctx->reference_conf_ht->count == 0);
 
-    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);
+    result &= (SCRConfGetReference("one", de_ctx) == NULL);
+    result &= (SCRConfGetReference("two", de_ctx) == NULL);
+    result &= (SCRConfGetReference("three", de_ctx) == NULL);
+    result &= (SCRConfGetReference("four", de_ctx) == NULL);
+    result &= (SCRConfGetReference("five", de_ctx) == NULL);
 
  end:
     if (de_ctx != NULL)
@@ -732,7 +745,6 @@ int SCRConfTest05(void)
 int SCRConfTest06(void)
 {
     DetectEngineCtx *de_ctx = DetectEngineCtxInit();
-    SCRConfReference *ref = NULL;
     int result = 1;
 
     if (de_ctx == NULL)
@@ -747,25 +759,11 @@ int SCRConfTest06(void)
 
     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);
+    result &= (SCRConfGetReference("one", de_ctx) != NULL);
+    result &= (SCRConfGetReference("two", de_ctx) == NULL);
+    result &= (SCRConfGetReference("three", de_ctx) == NULL);
+    result &= (SCRConfGetReference("four", de_ctx) == NULL);
+    result &= (SCRConfGetReference("five", de_ctx) == NULL);
 
  end:
     if (de_ctx != NULL)
index 061bacb1365996be38751be1dcc5b665b25d7867..f5b7ddb296b5fd037ae149fad977d8099cfdb306 100644 (file)
@@ -37,6 +37,9 @@ typedef struct SCRConfReference_ {
 SCRConfReference *SCRConfAllocSCRConfReference(const char *, const char *);
 void SCRConfDeAllocSCRConfReference(SCRConfReference *);
 int SCRConfLoadReferenceConfigFile(DetectEngineCtx *);
+void SCRConfDeInitContext(DetectEngineCtx *);
+SCRConfReference *SCRConfGetReference(const char *,
+                                      DetectEngineCtx *);
 void SCRConfRegisterTests(void);
 
 /* these below functions are only used by unittests */