mirror of https://github.com/OISF/suricata
examples: add simple c++ example
For now just used to make sure a C++ variation of our custom example can build.pull/13125/head
parent
9d5158594f
commit
8540627b4e
@ -0,0 +1,3 @@
|
||||
!/Makefile.example.in
|
||||
/Makefile.example
|
||||
/main
|
||||
@ -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)
|
||||
@ -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;
|
||||
}
|
||||
Loading…
Reference in New Issue