Skip to content

Commit 348845c

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: #8096
1 parent 79db7b1 commit 348845c

File tree

10 files changed

+450
-2
lines changed

10 files changed

+450
-2
lines changed

.github/workflows/builds.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,11 @@ jobs:
219219
test $(cat eve.json |jq 'select(.stats) | .stats.decoder.pkts') = 110
220220
working-directory: examples/lib/custom
221221

222+
- name: Build live library example
223+
run: |
224+
make
225+
working-directory: examples/lib/live
226+
222227
- name: Cleaning source directory for standalone plugin test.
223228
run: make clean
224229

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/custom/Makefile.example.in

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
LIBSURICATA_CONFIG ?= @CONFIGURE_PREFIX@/bin/libsuricata-config
22

3-
SURICATA_LIBS = `$(LIBSURICATA_CONFIG) --libs --static`
3+
# Define STATIC=1 to request static linking where available
4+
SURICATA_LIBS = `$(LIBSURICATA_CONFIG) --libs $${STATIC:+--static}`
45
SURICATA_CFLAGS := `$(LIBSURICATA_CONFIG) --cflags`
56

67
# Currently the Suricata logging system requires this to be even for

examples/lib/custom/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ int main(int argc, char **argv)
256256
* to be run concurrently at this time. */
257257
SuricataShutdown();
258258

259+
/* Ensure worker thread exits cleanly before teardown. */
260+
pthread_join(worker, NULL);
261+
259262
GlobalsDestroy();
260263

261264
return EXIT_SUCCESS;

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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 -l .
30+
```
31+
32+
Any additional arguments are passed directly to Suricata as command
33+
line arguments.
34+
35+
**Note:** Live packet capture typically requires root privileges or
36+
appropriate capabilities (e.g., `CAP_NET_RAW` on Linux).
37+
38+
Example with common options:
39+
```
40+
sudo ./live -i eth0 -- -l . -S rules.rules
41+
```
42+
43+
Example capturing from multiple interfaces:
44+
```
45+
sudo ./live -i eth0 -i wlan0 -- -l . -S rules.rules
46+
```
47+
48+
To apply a BPF filter (e.g. only TCP traffic) pass it after Suricata options:
49+
```
50+
sudo ./live -i eth0 -- -l . -S rules.rules tcp
51+
```
52+
Multiple interface example with BPF:
53+
```
54+
sudo ./live -i eth0 -i wlan0 -- -l . -S rules.rules tcp port 80
55+
```
56+
Note: Only specify `-i` interfaces before `--`. Options after `--` are passed
57+
to Suricata (e.g., `-l`, `-S`). BPF terms at the end are combined into a filter
58+
string.
59+
60+
Shutdown: each worker thread may call EngineStop when its capture ends; the
61+
main loop waits for this signal, performs SuricataShutdown concurrently with
62+
per-thread SCTmThreadsSlotPacketLoopFinish, then joins all worker threads
63+
before GlobalsDestroy.
64+
65+
The example supports up to 16 interfaces simultaneously.
66+
67+
## Building Out of Tree
68+
69+
A Makefile.example has also been generated to use as an example on how
70+
to build against the library in a standalone application.
71+
72+
First build and install the Suricata library including:
73+
74+
```
75+
make install-library
76+
make install-headers
77+
```
78+
79+
Then run:
80+
81+
```
82+
make -f Makefile.example
83+
```
84+
85+
If you installed to a non-standard location, you need to ensure that
86+
`libsuricata-config` is in your path, for example:
87+
88+
```
89+
PATH=/opt/suricata/bin:$PATH make -f Makefile.example
90+
```

0 commit comments

Comments
 (0)