Skip to content

Add --pcap packet capture export - #181

Merged
lance0 merged 4 commits into
masterfrom
feat/pcap-export
Nov 14, 2026
Merged

lance0 merged 4 commits into
masterfrom
feat/pcap-export

Conversation

@lance0

@lance0 lance0 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Summary

Adds --pcap <FILE>: during a live trace (TUI, batch or streaming), every probe ttl sends and every ICMP packet the receiver reads go into a classic pcap file you can open in Wireshark or read with tcpdump -r. This is the "PCAP export" item from the ROADMAP Medium Effort list.

Design

  • Format: classic pcap with a 24-byte global header and 16-byte record headers. It is little-endian with microsecond timestamps, and the link type is LINKTYPE_RAW (101). Each record is a bare IPv4 or IPv6 packet, so one file can hold both families. The format is a few dozen lines, so it adds no dependency.
  • Hooks: the send hooks sit in the four probe-layer send helpers: send_raw_ipv4, send_icmp, send_udp_probe and send_tcp_probe. None of the engine send paths changed. The receive side gets one line in the receiver loop. engine.rs also records in the Linux IPv6 Echo Reply poll, but only on an unprivileged datagram socket: a raw ICMPv6 receive socket reads Echo Replies too, so recording them in the poll as well would write each one twice.
  • IPv4 probes: recorded exactly as built for IP_HDRINCL. The header is put in wire form: the length comes from the buffer, ip_off is converted to network order on macOS and NetBSD, and the header checksum is filled in. The kernel-assigned IP ID is not visible to ttl and stays 0 in the file.
  • IPv6, and IPv4 ICMP read without its header: the kernel builds these headers, so ttl synthesizes them. The source is the socket's bound address, or the last local address seen for that family, or the address the route lookup picks. The file also records the hop limit and traffic class read from the socket, the next header, and the payload. For UDP over IPv6 the kernel also builds the UDP header, so ttl writes it with the socket's source port and a valid checksum.
  • Hot path: each packet is handed to a bounded sync_channel with room for 4096 packets, using try_send, and a writer thread writes them out. Probing never blocks. If the queue is full the packet is dropped and counted, and the count is printed to stderr at exit.
  • Durability: the writer flushes after every burst, so a Ctrl-C, including a forced second Ctrl-C, still leaves a file Wireshark can read. On a normal exit the capture handle flushes the file and joins the writer thread. The file is written in place rather than written to a temporary file and renamed, because a rename would lose the whole capture on a forced exit.
  • Symlinks: ttl refuses to write through a symlink, following the v0.25.0 export policy. On Unix the file is also opened with O_NOFOLLOW.
  • CLI: --pcap is rejected with --replay and --diff through the existing list of flags that are ignored in those modes.

Tests

  • pcap global header and record header, byte for byte
  • IPv6 header synthesis
  • IPv4 header normalization, including the BSD host-order ip_off case
  • headers for received packets: raw IPv4 is kept as read, IPv4 datagram and IPv6 reads get a synthesized header
  • UDP checksum verified for both families
  • writer end to end into a temporary file, then parsed back
  • symlink refusal
  • CLI: --pcap is rejected with replay and diff, and accepted in the live modes

Gates: cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo clippy --all-targets --no-default-features -- -D warnings and cargo test all pass.

Docs

docs/FEATURES.md gets a Packet Capture section and a CLI reference line, and the ROADMAP item is ticked.

Live check (docker, NET_RAW)

ttl -c 3 --no-tui -p {icmp,udp,tcp} --pcap /out/X.pcap against 1.1.1.1 and 2606:4700:4700::1111, then read back with tshark 4.2 and capinfos in an ubuntu:24.04 container:

  • Every file parses as Raw IP with timestamps matching the run's wall-clock time.
  • Each file holds the sent probes (ICMP Echo, UDP, TCP SYN with TTL or hop limit 1 to N) and the responses (Time Exceeded, Echo Reply, Destination Unreachable).
  • With checksum validation turned on, every IPv4 header and every ICMP, UDP and TCP checksum is valid, including the synthesized IPv6 UDP headers. The only bad checksums are on ICMPv6 that other processes on the host sent to ::1, because a received IPv6 packet gets the local address as its destination.
  • --resolve-all against one.one.one.one writes all four targets to one file. Pointing --pcap at a symlink fails and leaves the target file unchanged.
  • Each IPv6 Echo Reply appears once.

IPv6 TCP sends fail with EINVAL on this host. master does the same (60 of 60 sends), so the failure is not caused by this change, and no IPv6 TCP capture was checked.

Not verified

  • Echo Reply recording on the unprivileged IPv6 datagram path. It needs IPv4 raw sockets without IPv6 raw sockets, which was not set up here.
  • macOS, FreeBSD and Windows builds are left to CI. Received IPv4 ip_off order on BSD is not verified.

A raw ICMPv6 receive socket on Linux reads Echo Replies too, so the
send-socket poll wrote every reply a second time, up to a round late.
The poll now records only on an unprivileged datagram socket, where
the receive socket never sees them.
@lance0
lance0 marked this pull request as ready for review November 14, 2026 20:56
@lance0
lance0 merged commit 69224ee into master Nov 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant