]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
examples: add simple c++ example
authorJason Ish <jason.ish@oisf.net>
Mon, 28 Apr 2025 17:34:32 +0000 (11:34 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 30 Apr 2025 20:22:25 +0000 (22:22 +0200)
For now just used to make sure a C++ variation of our custom example
can build.

configure.ac
examples/lib/cplusplus/.gitignore [new file with mode: 0644]
examples/lib/cplusplus/Makefile.example.in [new file with mode: 0644]
examples/lib/cplusplus/main.cpp [new file with mode: 0644]

index ea80157744ece59f2f8bbcf104c2eaeee6faddcf..fdc23785d0bdb773616e19d76646cbac05cbd4e5 100644 (file)
@@ -2498,6 +2498,7 @@ AC_CONFIG_FILES(examples/plugins/c-custom-loggers/Makefile)
 AC_CONFIG_FILES(examples/plugins/ci-capture/Makefile)
 AC_CONFIG_FILES(examples/lib/simple/Makefile examples/lib/simple/Makefile.example)
 AC_CONFIG_FILES(examples/lib/custom/Makefile examples/lib/custom/Makefile.example)
+AC_CONFIG_FILES(examples/lib/cplusplus/Makefile.example)
 AC_CONFIG_FILES(plugins/Makefile)
 AC_CONFIG_FILES(plugins/pfring/Makefile)
 AC_CONFIG_FILES(plugins/napatech/Makefile)
diff --git a/examples/lib/cplusplus/.gitignore b/examples/lib/cplusplus/.gitignore
new file mode 100644 (file)
index 0000000..d5cfb2b
--- /dev/null
@@ -0,0 +1,3 @@
+!/Makefile.example.in
+/Makefile.example
+/main
diff --git a/examples/lib/cplusplus/Makefile.example.in b/examples/lib/cplusplus/Makefile.example.in
new file mode 100644 (file)
index 0000000..0e75ab1
--- /dev/null
@@ -0,0 +1,9 @@
+LIBSURICATA_CONFIG ?=  @CONFIGURE_PREFIX@/bin/libsuricata-config
+
+SURICATA_LIBS =                `$(LIBSURICATA_CONFIG) --libs`
+SURICATA_CFLAGS :=     `$(LIBSURICATA_CONFIG) --cflags`
+
+all: main
+
+main: main.cpp
+       $(CXX) -o $@ $^ $(SURICATA_CFLAGS) $(SURICATA_LIBS)
diff --git a/examples/lib/cplusplus/main.cpp b/examples/lib/cplusplus/main.cpp
new file mode 100644 (file)
index 0000000..8606740
--- /dev/null
@@ -0,0 +1,70 @@
+#include "suricata-common.h"
+#include "suricata.h"
+#include "conf.h"
+#include "util-device.h"
+
+int main(int argc, char **argv)
+{
+    SuricataPreInit(argv[0]);
+
+    /* Parse command line options. This is optional, you could
+     * directly configure Suricata through the Conf API. */
+    SCParseCommandLine(argc, argv);
+
+    /* Find our list of pcap files, after the "--". */
+    while (argc) {
+        bool end = strncmp(argv[0], "--", 2) == 0;
+        argv++;
+        argc--;
+        if (end) {
+            break;
+        }
+    }
+    if (argc == 0) {
+        fprintf(stderr, "ERROR: No PCAP files provided\n");
+        return 1;
+    }
+
+    /* Set the runmode to library mode. Perhaps in the future this
+     * should be done in some library bootstrap function. */
+    SCRunmodeSet(RUNMODE_LIB);
+
+    /* Validate/finalize the runmode. */
+    if (SCFinalizeRunMode() != TM_ECODE_OK) {
+        exit(EXIT_FAILURE);
+    }
+
+    /* Handle internal runmodes. Typically you wouldn't do this as a
+     * library user, however this example is showing how to replicate
+     * the Suricata application with the library. */
+    switch (SCStartInternalRunMode(argc, argv)) {
+        case TM_ECODE_DONE:
+            exit(EXIT_SUCCESS);
+        case TM_ECODE_FAILED:
+            exit(EXIT_FAILURE);
+    }
+
+    /* Load configuration file, could be done earlier but must be done
+     * before SuricataInit, but even then its still optional as you
+     * may be programmatically configuration Suricata. */
+    if (SCLoadYamlConfig() != TM_ECODE_OK) {
+        exit(EXIT_FAILURE);
+    }
+
+    /* Set "offline" runmode to replay a pcap in library mode. */
+    if (!SCConfSetFromString("runmode=offline", 1)) {
+        exit(EXIT_FAILURE);
+    }
+
+    /* Force logging to the current directory. */
+    SCConfSetFromString("default-log-dir=.", 1);
+
+    if (LiveRegisterDevice("lib0") < 0) {
+        fprintf(stderr, "LiveRegisterDevice failed");
+        exit(1);
+    }
+
+    SuricataInit();
+
+    return 0;
+}