Skip to content

Commit e095047

Browse files
committed
examples/lib/live: a lib example for 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 dcc5d34 commit e095047

8 files changed

Lines changed: 441 additions & 1 deletion

File tree

.github/workflows/builds.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ jobs:
223223
- run: python3 scripts/eve-parity.py unmapped-fields
224224
- run: python3 scripts/eve-parity.py unmapped-keywords
225225

226+
- name: Build live library example
227+
run: |
228+
make
229+
working-directory: examples/lib/live
230+
226231
- name: Cleaning source directory for standalone plugin test.
227232
run: make clean
228233

Makefile.am

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ 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) \
18+
examples/lib/simple \
19+
examples/lib/custom \
20+
examples/lib/live
1821

1922
CLEANFILES = stamp-h[0-9]*
2023

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,6 +2574,7 @@ AC_CONFIG_FILES(examples/plugins/c-custom-loggers/Makefile)
25742574
AC_CONFIG_FILES(examples/plugins/ci-capture/Makefile)
25752575
AC_CONFIG_FILES(examples/lib/simple/Makefile examples/lib/simple/Makefile.example)
25762576
AC_CONFIG_FILES(examples/lib/custom/Makefile examples/lib/custom/Makefile.example)
2577+
AC_CONFIG_FILES(examples/lib/live/Makefile examples/lib/live/Makefile.example)
25772578
AC_CONFIG_FILES(examples/lib/cplusplus/Makefile.example)
25782579
AC_CONFIG_FILES(plugins/Makefile)
25792580
AC_CONFIG_FILES(plugins/pfring/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)