Skip to content

Commit 0de9872

Browse files
committed
examples/lib/live: a lib example for live capture
Simple libpcap example for live capture. Allows listening on multiple interfaces.
1 parent 79db7b1 commit 0de9872

File tree

7 files changed

+411
-1
lines changed

7 files changed

+411
-1
lines changed

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+
/custom

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
LIBSURICATA_CONFIG ?= @CONFIGURE_PREFIX@/bin/libsuricata-config
2+
3+
SURICATA_LIBS = `$(LIBSURICATA_CONFIG) --libs --static`
4+
SURICATA_CFLAGS := `$(LIBSURICATA_CONFIG) --cflags`
5+
6+
# Currently the Suricata logging system requires this to be even for
7+
# plugins.
8+
CPPFLAGS += "-D__SCFILENAME__=\"$(*F)\""
9+
10+
all: live
11+
12+
live: main.c
13+
$(CC) -o $@ $^ $(CPPFLAGS) $(CFLAGS) $(SURICATA_CFLAGS) $(SURICATA_LIBS)
14+
15+
clean:
16+
rm -f live

examples/lib/live/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 -- -i eth1 -l . -S rules.rules
46+
```
47+
48+
The example supports up to 16 interfaces simultaneously.
49+
50+
## Building Out of Tree
51+
52+
A Makefile.example has also been generated to use as an example on how
53+
to build against the library in a standalone application.
54+
55+
First build and install the Suricata library including:
56+
57+
```
58+
make install-library
59+
make install-headers
60+
```
61+
62+
Then run:
63+
64+
```
65+
make -f Makefile.example
66+
```
67+
68+
If you installed to a non-standard location, you need to ensure that
69+
`libsuricata-config` is in your path, for example:
70+
71+
```
72+
PATH=/opt/suricata/bin:$PATH make -f Makefile.example
73+
```

0 commit comments

Comments
 (0)