From 8540627b4eb71160c1a40862795a2737391d3b52 Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 28 Apr 2025 11:34:32 -0600 Subject: [PATCH] examples: add simple c++ example For now just used to make sure a C++ variation of our custom example can build. --- configure.ac | 1 + examples/lib/cplusplus/.gitignore | 3 + examples/lib/cplusplus/Makefile.example.in | 9 +++ examples/lib/cplusplus/main.cpp | 70 ++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 examples/lib/cplusplus/.gitignore create mode 100644 examples/lib/cplusplus/Makefile.example.in create mode 100644 examples/lib/cplusplus/main.cpp diff --git a/configure.ac b/configure.ac index ea80157744..fdc23785d0 100644 --- a/configure.ac +++ b/configure.ac @@ -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 index 0000000000..d5cfb2b7d0 --- /dev/null +++ b/examples/lib/cplusplus/.gitignore @@ -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 index 0000000000..0e75ab1453 --- /dev/null +++ b/examples/lib/cplusplus/Makefile.example.in @@ -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 index 0000000000..8606740a2d --- /dev/null +++ b/examples/lib/cplusplus/main.cpp @@ -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; +}