]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
datasets: add dataset-clear command
authorEric Leblond <eric@regit.org>
Mon, 18 Jan 2021 21:11:15 +0000 (22:11 +0100)
committerVictor Julien <vjulien@oisf.net>
Thu, 27 Oct 2022 07:44:20 +0000 (09:44 +0200)
Ticket: #5184

src/runmode-unix-socket.c
src/runmode-unix-socket.h
src/unix-manager.c

index c302519464f07f1e3c8ce1fb4f3128aa11615e40..c4981e93231e5be041348b5a9186a6bfd9f83b35 100644 (file)
@@ -772,6 +772,42 @@ TmEcode UnixSocketDatasetDump(json_t *cmd, json_t *answer, void *data)
     SCReturnInt(TM_ECODE_OK);
 }
 
+TmEcode UnixSocketDatasetClear(json_t *cmd, json_t *answer, void *data)
+{
+    /* 1 get dataset name */
+    json_t *narg = json_object_get(cmd, "setname");
+    if (!json_is_string(narg)) {
+        json_object_set_new(answer, "message", json_string("setname is not a string"));
+        return TM_ECODE_FAILED;
+    }
+    const char *set_name = json_string_value(narg);
+
+    /* 2 get the data type */
+    json_t *targ = json_object_get(cmd, "settype");
+    if (!json_is_string(targ)) {
+        json_object_set_new(answer, "message", json_string("settype is not a string"));
+        return TM_ECODE_FAILED;
+    }
+    const char *type = json_string_value(targ);
+
+    enum DatasetTypes t = DatasetGetTypeFromString(type);
+    if (t == DATASET_TYPE_NOTSET) {
+        json_object_set_new(answer, "message", json_string("unknown settype"));
+        return TM_ECODE_FAILED;
+    }
+
+    Dataset *set = DatasetFind(set_name, t);
+    if (set == NULL) {
+        json_object_set_new(answer, "message", json_string("set not found or wrong type"));
+        return TM_ECODE_FAILED;
+    }
+
+    THashCleanup(set->hash);
+
+    json_object_set_new(answer, "message", json_string("dataset cleared"));
+    return TM_ECODE_OK;
+}
+
 /**
  * \brief Command to add a tenant handler
  *
index 31c59ee2f5cb3dd9f9781a7b1fa6bd51911c49ae..469dc771c9de61c2ecc10c003c1fe757dcf9b555 100644 (file)
@@ -36,6 +36,7 @@ float MemcapsGetPressure(void);
 TmEcode UnixSocketDatasetAdd(json_t *cmd, json_t* answer, void *data);
 TmEcode UnixSocketDatasetRemove(json_t *cmd, json_t* answer, void *data);
 TmEcode UnixSocketDatasetDump(json_t *cmd, json_t *answer, void *data);
+TmEcode UnixSocketDatasetClear(json_t *cmd, json_t *answer, void *data);
 TmEcode UnixSocketRegisterTenantHandler(json_t *cmd, json_t* answer, void *data);
 TmEcode UnixSocketUnregisterTenantHandler(json_t *cmd, json_t* answer, void *data);
 TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer, void *data);
index 4d0203d9334e22653462b350c1236f72a733c44e..3fdcb73fe54fbc1e46aa339f51af3a1efb3ddb7a 100644 (file)
@@ -1089,6 +1089,8 @@ int UnixManagerInit(void)
     UnixManagerRegisterCommand(
             "get-flow-stats-by-id", UnixSocketGetFlowStatsById, &command, UNIX_CMD_TAKE_ARGS);
     UnixManagerRegisterCommand("dataset-dump", UnixSocketDatasetDump, NULL, 0);
+    UnixManagerRegisterCommand(
+            "dataset-clear", UnixSocketDatasetClear, &command, UNIX_CMD_TAKE_ARGS);
 
     return 0;
 }