Skip to content

Commit f711e57

Browse files
jasonishvictorjulien
authored andcommitted
examples/lib/live: a lib example with live capture
Simple libpcap example for live capture. Allows listening on multiple interfaces to show how multiple threads (workers) can be used. Ticket: OISF#8096
1 parent 8c7172e commit f711e57

8 files changed

Lines changed: 466 additions & 1 deletion

File tree

.github/workflows/builds.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ jobs:
226226
test $(cat eve.json |jq 'select(.stats) | .stats.decoder.pkts') = 110
227227
working-directory: examples/lib/custom
228228

229+
- name: Build live library example
230+
run: make
231+
working-directory: examples/lib/live
232+
229233
- run: python3 scripts/eve-parity.py mapped-fields
230234
- run: python3 scripts/eve-parity.py unmapped-fields
231235
- run: python3 scripts/eve-parity.py unmapped-keywords

Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ EXTRA_DIST = ChangeLog COPYING LICENSE suricata.yaml.in \
1414
examples/plugins
1515
SUBDIRS = rust src plugins qa rules doc etc python ebpf \
1616
$(SURICATA_UPDATE_DIR)
17-
DIST_SUBDIRS = $(SUBDIRS) examples/lib/simple examples/lib/custom
17+
DIST_SUBDIRS = $(SUBDIRS) examples/lib/simple examples/lib/custom \
18+
examples/lib/live
1819

1920
CLEANFILES = stamp-h[0-9]*
2021

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,6 +2582,7 @@ AC_CONFIG_FILES(examples/plugins/ci-capture/Makefile)
25822582
AC_CONFIG_FILES(examples/lib/simple/Makefile examples/lib/simple/Makefile.example)
25832583
AC_CONFIG_FILES(examples/lib/custom/Makefile examples/lib/custom/Makefile.example)
25842584
AC_CONFIG_FILES(examples/lib/cplusplus/Makefile.example)
2585+
AC_CONFIG_FILES(examples/lib/live/Makefile examples/lib/live/Makefile.example)
25852586
AC_CONFIG_FILES(plugins/Makefile)
25862587
AC_CONFIG_FILES(plugins/pfring/Makefile)
25872588
AC_CONFIG_FILES(plugins/napatech/Makefile)

examples/lib/live/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!/Makefile.example.in
2+
Makefile.example
3+
/live

examples/lib/live/Makefile.am

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
bin_PROGRAMS = live
2+
3+
live_SOURCES = main.c
4+
5+
AM_CPPFLAGS = -I$(top_srcdir)/src
6+
7+
live_LDFLAGS = $(all_libraries) $(SECLDFLAGS)
8+
live_LDADD = "-Wl,--start-group,$(top_builddir)/src/libsuricata_c.a,../../$(RUST_SURICATA_LIB),--end-group" $(RUST_LDADD)
9+
live_DEPENDENCIES = $(top_builddir)/src/libsuricata_c.a ../../$(RUST_SURICATA_LIB)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
LIBSURICATA_CONFIG ?= @CONFIGURE_PREFIX@/bin/libsuricata-config
2+
3+
# Define STATIC=1 to request static linking where available
4+
SURICATA_LIBS = `$(LIBSURICATA_CONFIG) --libs $${STATIC:+--static}`
5+
SURICATA_CFLAGS := `$(LIBSURICATA_CONFIG) --cflags`
6+
7+
# Currently the Suricata logging system requires this to be even for
8+
# plugins.
9+
CPPFLAGS += "-D__SCFILENAME__=\"$(*F)\""
10+
11+
all: live
12+
13+
live: main.c
14+
$(CC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(SURICATA_CFLAGS) $(SURICATA_LIBS)
15+
16+
clean:
17+
rm -f live

examples/lib/live/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Live Capture Library Example
2+
3+
This is an example of using the Suricata library to capture live
4+
traffic from a network interface with custom packet handling and
5+
threading.
6+
7+
## Building In Tree
8+
9+
The Suricata build system has created a Makefile that should allow you
10+
to build this application in-tree on most supported platforms. To
11+
build simply run:
12+
13+
```
14+
make
15+
```
16+
17+
## Running
18+
19+
```
20+
./live -i eth0 -l .
21+
```
22+
23+
This example requires at least one `-i` option to specify the network
24+
interface to capture from. You can specify multiple interfaces to
25+
capture from multiple sources simultaneously - a separate worker thread
26+
will be created for each interface:
27+
28+
```
29+
./live -i eth0 -i eth1
30+
```
31+
32+
Any additional arguments are passed directly to Suricata as command
33+
line arguments.
34+
35+
Example with common options:
36+
```
37+
sudo ./live -i eth0 -- -l . -S rules.rules
38+
```
39+
40+
Example capturing from multiple interfaces:
41+
```
42+
sudo ./live -i eth0 -i wlan0 -- -l . -S rules.rules
43+
```
44+
45+
Shutdown: each worker thread may call EngineStop when its capture ends; the
46+
main loop waits for this signal, performs SuricataShutdown concurrently with
47+
per-thread SCTmThreadsSlotPacketLoopFinish, then joins all worker threads
48+
before GlobalsDestroy.
49+
50+
The example supports up to 16 interfaces simultaneously.
51+
52+
## Building Out of Tree
53+
54+
A Makefile.example has also been generated to use as an example on how
55+
to build against the library in a standalone application.
56+
57+
First build and install the Suricata library including:
58+
59+
```
60+
make install-library
61+
make install-headers
62+
```
63+
64+
Then run:
65+
66+
```
67+
make -f Makefile.example
68+
```
69+
70+
If you installed to a non-standard location, you need to ensure that
71+
`libsuricata-config` is in your path, for example:
72+
73+
```
74+
PATH=/opt/suricata/bin:$PATH make -f Makefile.example
75+
```

0 commit comments

Comments
 (0)