Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: ci

on:
push:
pull_request:

jobs:
build-and-test:
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
pcap: [with-libpcap, without-libpcap]
opt: [-O2, "-O1 -g -fsanitize=address,undefined"]

steps:
- uses: actions/checkout@v4

- name: install deps
run: |
sudo apt-get update -qq
sudo apt-get install -y build-essential libpcap-dev python3 tcpdump iputils-ping
if [ "${{ matrix.pcap }}" = "without-libpcap" ]; then
sudo apt-get remove -y libpcap-dev libpcap0.8-dev || true
fi

- name: build
run: |
make clean
make -j"$(nproc)" OPT="${{ matrix.opt }}" LDFLAGS="${{ contains(matrix.opt,'sanitize') && '-fsanitize=address,undefined' || '' }}"

- name: unit tests
run: make test OPT="${{ matrix.opt }}"

- name: end-to-end tests
run: sudo -E env "PATH=$PATH" bash tests/e2e.sh
if: matrix.opt == '-O2'
# runs in both matrix legs: e2e.sh skips the kernel-filter checks when
# the build has no libpcap

- name: version and capabilities
run: ./bin/pktsniff-server --version
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin/
*.pcap
*.pcap.*
*.o
*.d
core
51 changes: 51 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Contributing

Small C11 project, no dependencies worth arguing about. `make test` and, if you can, `sudo tests/e2e.sh` are the whole gate.

## Get set up

```sh
git clone https://github.com/bas3line/packets
cd packets
sudo apt install build-essential libpcap-dev python3 tcpdump iputils-ping
make && make test
```

The end-to-end suite needs root (it opens a raw socket) and confines itself to loopback on port 8899:

```sh
sudo tests/e2e.sh
```

## What runs in CI

`.github/workflows/ci.yml` builds the tree with and without libpcap, at `-O2` and under ASan/UBSan, runs the unit tests in every combination, and runs `tests/e2e.sh` for the `-O2` build. A change that only works when libpcap is installed will fail there.

## Ground rules

- **Never trust a length.** Anything that reads a packet, a frame or a config string is handed hostile input in `tests/`. `ps_decode` must survive being given the first *n* bytes of any packet, for every *n* — that is what `test_every_truncation` checks, and it is the cheapest bug you will ever find.
- **The stream stays frame-atomic.** If you touch `src/outbuf.c`, keep the invariant: a frame with bytes on the wire is never dropped. `tests/test_outbuf.c` simulates short writes and a stuck reader; make it pass.
- **Server does little, client does the thinking.** Capture, timestamp, forward, optionally record. Dissection, filtering and rendering belong in the client, where a person is watching.
- **No new dependencies** unless they earn their place. libpcap is optional and stays optional.
- **Match the surrounding C**: `ps_` prefixes, `goto done` for cleanup, one header per module in `include/`, comments that explain *why* something is not obvious.
- Warnings are errors in spirit — the tree builds clean with `-Wall -Wextra -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wvla -Wpointer-arith` and `-Werror`.

## Adding something

| you want to | touch |
| --- | --- |
| dissect another protocol | `src/decode.c`, add a case in `tests/test_decode.c` with a hand-built frame |
| add a display filter key | `src/filter.c` (parse + match), `tests/test_filter.c`, the table in `README.md` |
| change the wire format | `include/proto.h`, bump `PS_PROTO_VERSION`, update `docs/PROTOCOL.md` |
| add a server or client flag | `src/main_server.c` / `src/main_client.c`, `--help` text, the option table in `README.md` |
| touch capture behaviour | `src/capture.c`; explain the change in the PR, it is the hard part to test |

## Commits and PRs

- Imperative subject, then a body that says what was wrong and what is now true. "Fix", "improve" and "refactor" on their own say nothing.
- One idea per PR. If it needs "and also", it is two PRs.
- Paste the relevant test output in the description — `make test` summary and, for anything involving capture or the stream, the `tests/e2e.sh` result.

## Reporting a bug

Include the server log lines around the failure, `pktsniff-server --version` (it prints whether libpcap is compiled in), and what `--filter`/`-F` you were using. If the client printed `protocol error on stream`, that is a framing bug and I would like the frames you were seeing: `-v` and the head of the server log usually settle it.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Bas3line

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
103 changes: 103 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# pktsniff - build system
#
# make build bin/pktsniff-server and bin/pktsniff-client
# make test build and run the unit tests (no root needed)
# make asan build with AddressSanitizer/UBSan into bin/asan/
# make install install into $(PREFIX)/bin (default /usr/local)
# make clean
#
# libpcap is optional: with it, the server accepts tcpdump-style BPF filters
# (--filter). Without it the server still works and the client-side --filter is
# unaffected.

BIN := bin
PREFIX ?= /usr/local
CC ?= cc
CSTD := -std=c11
WARN := -Wall -Wextra -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wvla -Wpointer-arith
OPT ?= -O2 -g
CPPFLAGS += -D_GNU_SOURCE -Iinclude
CFLAGS += $(CSTD) $(WARN) $(OPT) -fstack-protector-strong -fno-omit-frame-pointer
LDFLAGS +=
LDLIBS +=

PCAP_CFLAGS := $(shell pkg-config --cflags libpcap 2>/dev/null)
PCAP_LIBS := $(shell pkg-config --libs libpcap 2>/dev/null)
ifeq ($(strip $(PCAP_LIBS)),)
PCAP_LIBS := $(shell echo '-lpcap' | $(CC) -x c - -o /dev/null -lpcap >/dev/null 2>&1 && echo -lpcap)
endif
ifneq ($(strip $(PCAP_LIBS)),)
CPPFLAGS += -DPS_HAVE_PCAP
CFLAGS += $(PCAP_CFLAGS)
SERVER_LIBS += $(PCAP_LIBS)
endif

LIB_SRCS := src/log.c src/util.c src/stats.c src/proto.c src/decode.c src/filter.c \
src/pcap.c src/outbuf.c
SERVER_SRCS := src/main_server.c src/server.c src/capture.c
CLIENT_SRCS := src/main_client.c src/client.c

LIB_OBJS := $(LIB_SRCS:src/%.c=$(BIN)/obj/%.o)
SERVER_OBJS := $(SERVER_SRCS:src/%.c=$(BIN)/obj/%.o)
CLIENT_OBJS := $(CLIENT_SRCS:src/%.c=$(BIN)/obj/%.o)

PS_SERVER_BIN := pktsniff-server
PS_CLIENT_BIN := pktsniff-client
SERVER_BIN := $(BIN)/$(PS_SERVER_BIN)
CLIENT_BIN := $(BIN)/$(PS_CLIENT_BIN)

all: $(SERVER_BIN) $(CLIENT_BIN)

$(BIN)/obj/%.o: src/%.c
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -MMD -MP -c $< -o $@

$(SERVER_BIN): $(LIB_OBJS) $(SERVER_OBJS)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(SERVER_LIBS) $(LDLIBS)

$(CLIENT_BIN): $(LIB_OBJS) $(CLIENT_OBJS)
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

-include $(LIB_OBJS:.o=.d) $(SERVER_OBJS:.o=.d) $(CLIENT_OBJS:.o=.d)

# ---- tests ----
TEST_BIN := $(BIN)/tests
TEST_SRCS := $(wildcard tests/test_*.c)
TEST_PROGS := $(TEST_SRCS:tests/%.c=$(TEST_BIN)/%)

$(TEST_BIN)/%: tests/%.c $(LIB_OBJS)
@mkdir -p $(dir $@)
$(CC) $(CPPFLAGS) $(CFLAGS) -Isrc -o $@ $< $(LIB_OBJS) $(LDLIBS)

test: $(TEST_PROGS)
@fail=0; for t in $(TEST_PROGS); do echo "== $$t"; ./$$t || fail=1; done; \
if [ $$fail -eq 0 ]; then echo "all tests passed"; else echo "TESTS FAILED"; exit 1; fi

# ---- sanitizer build ----
asan:
$(MAKE) OPT='-O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all' \
LDFLAGS='-fsanitize=address,undefined' BIN=bin/asan all test

install: all
install -d $(DESTDIR)$(PREFIX)/bin
install -m 0755 $(SERVER_BIN) $(DESTDIR)$(PREFIX)/bin/$(PS_SERVER_BIN)
install -m 0755 $(CLIENT_BIN) $(DESTDIR)$(PREFIX)/bin/$(PS_CLIENT_BIN)

install-service: all
install -m 0644 packaging/pktsniff.service /etc/systemd/system/pktsniff.service
systemctl daemon-reload
@echo "edit /etc/pktsniff.conf then: systemctl enable --now pktsniff"

clean:
rm -rf $(BIN)

distclean: clean
rm -rf bin

print-config:
@echo "CC = $(CC)"
@echo "libpcap = $(if $(SERVER_LIBS),yes,no)"

.PHONY: all test asan install install-service clean distclean print-config
Loading
Loading