]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
datasets: add 'remove' support
authorVictor Julien <victor@inliniac.net>
Mon, 13 Apr 2020 14:31:50 +0000 (16:31 +0200)
committerVictor Julien <victor@inliniac.net>
Mon, 20 Apr 2020 11:57:28 +0000 (13:57 +0200)
src/datasets.c
src/datasets.h
src/util-thash.c

index b2681f02968d9127d0996e0884373af649d7ed68..f68ed2e441dfe320b105e2b2b29dfaf42b16a426 100644 (file)
@@ -1044,3 +1044,84 @@ int DatasetAddSerialized(Dataset *set, const char *string)
     }
     return -1;
 }
+
+/**
+ *  \retval 1 data was removed from the hash
+ *  \retval 0 data not removed (busy)
+ *  \retval -1 data not found
+ */
+static int DatasetRemoveString(Dataset *set, const uint8_t *data, const uint32_t data_len)
+{
+    if (set == NULL)
+        return -1;
+
+    StringType lookup = { .ptr = (uint8_t *)data, .len = data_len,
+        .rep.value = 0 };
+    return THashRemoveFromHash(set->hash, &lookup);
+}
+
+static int DatasetRemoveMd5(Dataset *set, const uint8_t *data, const uint32_t data_len)
+{
+    if (set == NULL)
+        return -1;
+
+    if (data_len != 16)
+        return -2;
+
+    Md5Type lookup = { .rep.value = 0 };
+    memcpy(lookup.md5, data, 16);
+    return THashRemoveFromHash(set->hash, &lookup);
+}
+
+static int DatasetRemoveSha256(Dataset *set, const uint8_t *data, const uint32_t data_len)
+{
+    if (set == NULL)
+        return -1;
+
+    if (data_len != 32)
+        return -2;
+
+    Sha256Type lookup = { .rep.value = 0 };
+    memcpy(lookup.sha256, data, 32);
+    return THashRemoveFromHash(set->hash, &lookup);
+}
+
+/** \brief remove serialized data from set
+ *  \retval int 1 removed
+ *  \retval int 0 found but busy (not removed)
+ *  \retval int -1 API error (not removed)
+ *  \retval int -2 DATA error */
+int DatasetRemoveSerialized(Dataset *set, const char *string)
+{
+    if (set == NULL)
+        return -1;
+
+    switch (set->type) {
+        case DATASET_TYPE_STRING: {
+            uint8_t decoded[strlen(string)];
+            uint32_t len = DecodeBase64(decoded, (const uint8_t *)string, strlen(string), 1);
+            if (len == 0) {
+                return -2;
+            }
+
+            return DatasetRemoveString(set, decoded, len);
+        }
+        case DATASET_TYPE_MD5: {
+            if (strlen(string) != 32)
+                return -2;
+            uint8_t hash[16];
+            if (HexToRaw((const uint8_t *)string, 32, hash, sizeof(hash)) < 0)
+                return -2;
+            return DatasetRemoveMd5(set, hash, 16);
+        }
+        case DATASET_TYPE_SHA256: {
+            if (strlen(string) != 64)
+                return -2;
+            uint8_t hash[32];
+            if (HexToRaw((const uint8_t *)string, 64, hash, sizeof(hash)) < 0)
+                return -2;
+            return DatasetRemoveSha256(set, hash, 32);
+        }
+    }
+    return -1;
+}
index 0b20d34088178627caf13cc58c8eddfc37213b05..16ae562684bb4166757291cf1d5b0e5e30bcb0c8 100644 (file)
@@ -54,6 +54,8 @@ int DatasetAdd(Dataset *set, const uint8_t *data, const uint32_t data_len);
 int DatasetLookup(Dataset *set, const uint8_t *data, const uint32_t data_len);
 DataRepResultType DatasetLookupwRep(Dataset *set, const uint8_t *data, const uint32_t data_len,
         const DataRepType *rep);
+
 int DatasetAddSerialized(Dataset *set, const char *string);
+int DatasetRemoveSerialized(Dataset *set, const char *string);
 
 #endif /* __DATASETS_H__ */
index b9caee68cfc5e52c41b54c3f3c1175c2a2de9eb4..29966689bc73a3fdad532e1127a5dda610e33ea0 100644 (file)
@@ -750,15 +750,8 @@ int THashRemoveFromHash (THashTableContext *ctx, void *data)
     THashHashRow *hb = &ctx->array[key];
 
     HRLOCK_LOCK(hb);
-    if (hb->head == NULL) {
-        HRLOCK_UNLOCK(hb);
-        SCLogDebug("empty hash row");
-        return -1;
-    }
-
-    /* ok, we have data in the bucket. Let's find out if it is our data */
     THashData *h = hb->head;
-    do {
+    while (h != NULL) {
         /* see if this is the data we are looking for */
         if (THashCompare(&ctx->config, h->data, data) == 0) {
             h = h->next;
@@ -789,8 +782,7 @@ int THashRemoveFromHash (THashTableContext *ctx, void *data)
         THashDataFree(ctx, h);
         SCLogDebug("found and removed");
         return 1;
-
-    } while (h != NULL);
+    }
 
     HRLOCK_UNLOCK(hb);
     SCLogDebug("data not found");