use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap};
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use crate::clock::Clock;
use crate::netif::Sender;
use crate::netpolicy::SplitMix64;
use crate::scope::SimShared;
pub(crate) const ENV_DIR: &str = "SNARE_PCAPNG_DIR";
pub(crate) const ENV_TESTS: &str = "SNARE_PCAPNG_TESTS";
pub(crate) const ENV_WALL_COMMENT: &str = "SNARE_PCAPNG_WALL_COMMENT";
const BLOCK_SHB: u32 = 0x0A0D_0D0A;
const BLOCK_IDB: u32 = 0x0000_0001;
const BLOCK_EPB: u32 = 0x0000_0006;
const BYTE_ORDER_MAGIC: u32 = 0x1A2B_3C4D;
const LINKTYPE_ETHERNET: u16 = 1;
const OPT_END: u16 = 0;
const OPT_COMMENT: u16 = 1;
const SHB_OS: u16 = 3;
const SHB_USERAPPL: u16 = 4;
const IF_NAME: u16 = 2;
const IF_TSRESOL: u16 = 9;
const EPB_FLAGS: u16 = 2;
const MAX_CAPLEN: usize = 65535;
const ETHERTYPE_IPV4: u16 = 0x0800;
const ETHERTYPE_IPV6: u16 = 0x86dd;
const PROTO_ICMP: u8 = 1;
const PROTO_TCP: u8 = 6;
const PROTO_UDP: u8 = 17;
const PROTO_ICMPV6: u8 = 58;
const FIN: u8 = 0x01;
const SYN: u8 = 0x02;
const RST: u8 = 0x04;
const PSH: u8 = 0x08;
const ACK: u8 = 0x10;
const TTL: u8 = if cfg!(windows) { 128 } else { 64 };
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Dir {
Out,
In,
}
#[derive(Clone, Debug)]
pub(crate) struct Link {
pub(crate) iface: String,
pub(crate) mtu: u32,
}
impl Link {
fn mss(&self, v6: bool) -> usize {
let mtu = self.mtu.min(65535) as usize;
mtu.saturating_sub(if v6 { 60 } else { 40 }).max(1)
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Station {
pub(crate) addr: SocketAddr,
pub(crate) mac: [u8; 6],
}
#[derive(Clone)]
pub(crate) struct TcpTap {
capture: Arc<Capture>,
link: Link,
ends: [Station; 2],
dirs: [Dir; 2],
}
impl TcpTap {
fn key(&self) -> (SocketAddr, SocketAddr) {
(self.ends[0].addr, self.ends[1].addr)
}
pub(crate) fn open(&self, syn_sent: bool, ago: Duration) {
self.capture.tcp_open(self, syn_sent, ago);
}
pub(crate) fn syn(&self) {
self.capture.tcp_syn(self);
}
pub(crate) fn syn_plan(&self, retransmits: &[Duration]) {
self.capture.tcp_syn_plan(self, retransmits);
}
pub(crate) fn resend_syn(&self, ago: Duration) {
self.capture.tcp_resend_syn(self, ago);
}
pub(crate) fn confirm_plan(&self, ago: Duration) {
self.capture.confirm_plan(self, ago);
}
pub(crate) fn failed(&self, ago: Duration) {
self.capture.tcp_failed(self, ago);
}
pub(crate) fn refused(&self, ago: Duration) {
self.capture.tcp_refused(self, ago);
}
#[cfg(any(windows, test))]
pub(crate) fn data(&self, from: usize, bytes: &[u8], arrival: Duration) {
self.capture
.tcp_data(self, from, bytes, arrival, None, true);
}
#[cfg(unix)]
pub(crate) fn data_mss(
&self,
from: usize,
bytes: &[u8],
arrival: Duration,
mss: Option<usize>,
) {
self.capture.tcp_data(self, from, bytes, arrival, mss, true);
}
#[cfg(target_os = "macos")]
pub(crate) fn data_mss_unacknowledged(
&self,
from: usize,
bytes: &[u8],
arrival: Duration,
mss: Option<usize>,
) {
self.capture
.tcp_data(self, from, bytes, arrival, mss, false);
}
#[cfg(target_os = "macos")]
pub(crate) fn watch_probe(&self, probe: std::sync::Weak<dyn crate::sockets::RxProbe>) {
snare_interpose::real(|| {
let mut probes = self.capture.probes.lock().unwrap();
probes.retain(|probe| probe.strong_count() > 0);
probes.push(probe);
});
}
#[cfg(target_os = "macos")]
pub(crate) fn discard_acks_from(&self, from: usize, after: Duration, return_delay: Duration) {
self.capture.with(|writer, now| {
let at = now + crate::clock::nanos(after);
writer.discard_acks(self.key(), Some(from), at);
writer.reserve_reset(self, from, at, return_delay);
});
}
#[cfg(target_os = "macos")]
pub(crate) fn retarget_reset(
&self,
from: usize,
at: crate::readiness::Deadline,
return_delay: Duration,
) {
self.capture.with(|writer, _| {
let at = crate::clock::nanos(
at.timeline_at(self.capture.real_origin, self.capture.clock.as_deref()),
);
writer.retarget_reset(self, from, at, return_delay);
});
}
#[cfg(target_os = "macos")]
pub(crate) fn defer_reset_receipt(&self, to: usize, at: crate::readiness::Deadline) {
self.capture.with(|writer, _| {
let at = crate::clock::nanos(
at.timeline_at(self.capture.real_origin, self.capture.clock.as_deref()),
);
if let Some(flow) = writer.flows.get_mut(&self.key()) {
flow.closed_at[to] = Some(at);
}
});
}
pub(crate) fn fin(&self, from: usize, arrival: Duration) {
self.capture.tcp_fin(self, from, arrival);
}
pub(crate) fn rst(&self, from: usize) {
self.capture.tcp_rst(self, from, Duration::ZERO, None);
}
#[cfg(target_os = "macos")]
pub(crate) fn rst_at(
&self,
from: usize,
at: crate::readiness::Deadline,
return_delay: Duration,
) {
self.capture.with(|writer, _| {
let at = crate::clock::nanos(
at.timeline_at(self.capture.real_origin, self.capture.clock.as_deref()),
);
writer.reset_route(
TcpRoute {
link: &self.link,
ends: &self.ends,
dirs: &self.dirs,
},
from,
at,
Some(return_delay),
);
});
}
}
pub(crate) struct Capture {
path: PathBuf,
seed: u64,
base_realtime: Duration,
clock: Option<Arc<Clock>>,
real_origin: Duration,
#[cfg(target_os = "macos")]
probes: Mutex<Vec<std::sync::Weak<dyn crate::sockets::RxProbe>>>,
inner: Mutex<Writer>,
}
pub(crate) fn wall_comment(asked: bool) -> bool {
asked
|| snare_interpose::real(|| std::env::var_os(ENV_WALL_COMMENT))
.is_some_and(|v| !v.is_empty() && v != "0")
}
fn real_wall() -> Duration {
snare_interpose::real(|| {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
})
}
struct TcpFlow {
isn: [u32; 2],
next_seq: [u32; 2],
syn: [bool; 2],
fin: [bool; 2],
reset: bool,
#[cfg(target_os = "macos")]
closed_at: [Option<u64>; 2],
#[cfg(target_os = "macos")]
pending_closed_at: [Option<u64>; 2],
}
impl TcpFlow {
fn can_send(&self, _from: usize, _at: u64) -> bool {
#[cfg(target_os = "macos")]
{
self.closed_at[_from]
.into_iter()
.chain(self.pending_closed_at[_from])
.all(|closed| _at < closed)
}
#[cfg(not(target_os = "macos"))]
{
!self.reset
}
}
}
struct Frame {
iface: String,
dir: Dir,
bytes: Vec<u8>,
orig_len: usize,
plan: Option<(SocketAddr, SocketAddr)>,
#[cfg(target_os = "macos")]
ack: Option<TcpAck>,
wall: Option<Duration>,
}
#[cfg(target_os = "macos")]
#[derive(Clone, Copy)]
struct TcpAck {
flow: (SocketAddr, SocketAddr),
from: usize,
at: u64,
}
struct TcpRoute<'a> {
link: &'a Link,
ends: &'a [Station; 2],
dirs: &'a [Dir; 2],
}
impl TcpRoute<'_> {
fn key(&self) -> (SocketAddr, SocketAddr) {
(self.ends[0].addr, self.ends[1].addr)
}
}
#[cfg(target_os = "macos")]
struct ResetReservation {
link: Link,
ends: [Station; 2],
dirs: [Dir; 2],
at: [Option<u64>; 2],
return_delay: [Duration; 2],
}
struct Writer {
out: Option<BufWriter<File>>,
pending: BinaryHeap<Reverse<(u64, u64)>>,
frames: HashMap<u64, Frame>,
next_frame: u64,
flows: HashMap<(SocketAddr, SocketAddr), TcpFlow>,
plans: HashMap<(SocketAddr, SocketAddr), u64>,
#[cfg(target_os = "macos")]
resets: HashMap<(SocketAddr, SocketAddr), ResetReservation>,
last: u64,
ip_id: HashMap<IpAddr, u16>,
links: HashMap<String, u32>,
finished: bool,
wall_comment: bool,
now: u64,
}
impl Capture {
pub(crate) fn create(
path: PathBuf,
shared: &SimShared,
wall_comment: bool,
) -> io::Result<Arc<Capture>> {
let file = snare_interpose::real(|| File::create(&path))?;
let base_realtime = match &shared.clock {
Some(clock) => clock.timeline_realtime(),
None => snare_interpose::real(|| {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default();
now.saturating_sub(shared.stamp())
}),
};
let mut writer = Writer {
out: Some(BufWriter::new(file)),
pending: BinaryHeap::new(),
frames: HashMap::new(),
next_frame: 0,
flows: HashMap::new(),
plans: HashMap::new(),
#[cfg(target_os = "macos")]
resets: HashMap::new(),
last: 0,
ip_id: HashMap::new(),
links: HashMap::new(),
finished: false,
wall_comment,
now: 0,
};
snare_interpose::real(|| writer.put(&shb()).and_then(|()| writer.flush_out()))?;
Ok(Arc::new(Capture {
path,
seed: shared.seed,
base_realtime,
clock: shared.clock.clone(),
real_origin: shared.real_origin(),
#[cfg(target_os = "macos")]
probes: Mutex::new(Vec::new()),
inner: Mutex::new(writer),
}))
}
pub(crate) fn from_env(shared: &SimShared, wall_comment: bool) -> Option<Arc<Capture>> {
let (dir, tests) = snare_interpose::real(|| {
(
std::env::var_os(ENV_DIR).filter(|dir| !dir.is_empty()),
std::env::var(ENV_TESTS)
.ok()
.filter(|list| list.split(',').any(|entry| !entry.trim().is_empty())),
)
});
let current = std::thread::current();
let thread = current.name();
let listed = tests.map(|list| test_listed(&list, thread));
let dir = match (dir, listed) {
(_, Some(false)) | (None, None) => return None,
(Some(dir), _) => PathBuf::from(dir),
(None, Some(true)) => snare_interpose::real(std::env::temp_dir).join("snare-pcapng"),
};
let announce = listed.is_some();
let base = sanitize(thread.unwrap_or("sim"));
let name = {
static USED: Mutex<Option<HashMap<PathBuf, u32>>> = Mutex::new(None);
let mut used = snare_interpose::real(|| USED.lock().unwrap_or_else(|e| e.into_inner()));
let n = used
.get_or_insert_with(HashMap::new)
.entry(dir.join(&base))
.or_insert(0);
*n += 1;
if *n == 1 {
format!("{base}.pcapng")
} else {
format!("{base}-{n}.pcapng")
}
};
let path = dir.join(name);
let made = snare_interpose::real(|| std::fs::create_dir_all(&dir))
.and_then(|()| Capture::create(path.clone(), shared, wall_comment));
match made {
Ok(capture) => {
if announce {
eprintln!("snare: pcapng: capturing to {}", path.display());
}
Some(capture)
}
Err(e) => {
eprintln!("snare: pcapng: cannot write {}: {e}", path.display());
None
}
}
}
pub(crate) fn path(&self) -> &Path {
&self.path
}
fn stamp(&self) -> u64 {
crate::clock::nanos(crate::scope::timeline(
self.clock.as_deref(),
self.real_origin,
))
}
fn with(&self, emit: impl FnOnce(&mut Writer, u64)) {
snare_interpose::real(|| {
let mut w = self.inner.lock().unwrap_or_else(|e| e.into_inner());
if w.finished || w.out.is_none() {
return;
}
let now = self.stamp();
w.now = now;
emit(&mut w, now);
w.flush_due(Some(now), self.base_realtime);
});
}
pub(crate) fn finish(&self) {
snare_interpose::real(|| {
#[cfg(target_os = "macos")]
{
if self
.inner
.lock()
.unwrap_or_else(|e| e.into_inner())
.finished
{
return;
}
let probes: Vec<_> = self
.probes
.lock()
.unwrap_or_else(|e| e.into_inner())
.iter()
.filter_map(std::sync::Weak::upgrade)
.collect();
for probe in probes {
probe.land();
}
}
let mut w = self.inner.lock().unwrap_or_else(|e| e.into_inner());
if w.finished {
return;
}
#[cfg(target_os = "macos")]
{
let now = self.stamp();
w.now = now;
w.finish_resets(now);
}
w.flush_due(None, self.base_realtime);
w.finished = true;
w.out = None;
});
}
fn isn(&self, key: (SocketAddr, SocketAddr)) -> [u32; 2] {
let mut h = Fnv::new();
for addr in [key.0, key.1] {
h.addr(addr);
}
let v = SplitMix64(self.seed ^ h.0).next_u64();
[(v >> 32) as u32, v as u32]
}
fn tcp_open(&self, t: &TcpTap, syn_sent: bool, ago: Duration) {
self.with(|w, now| {
let at = now.saturating_sub(crate::clock::nanos(ago));
w.end_plan(t.key(), at);
if !syn_sent || !w.flows.contains_key(&t.key()) {
w.start_flow(t.key(), self.isn(t.key()));
w.segment(t, 0, SYN, &[], at, false);
}
w.segment(t, 1, SYN | ACK, &[], at, false);
w.segment(t, 0, ACK, &[], at, false);
});
}
fn tcp_syn(&self, t: &TcpTap) {
self.with(|w, now| {
w.start_flow(t.key(), self.isn(t.key()));
w.segment(t, 0, SYN, &[], now, false);
});
}
fn tcp_syn_plan(&self, t: &TcpTap, retransmits: &[Duration]) {
self.with(|w, now| {
if !retransmits.is_empty() {
w.plans.insert(t.key(), now);
}
for at in retransmits {
w.segment(t, 0, SYN, &[], now + crate::clock::nanos(*at), true);
}
});
}
fn tcp_resend_syn(&self, t: &TcpTap, ago: Duration) {
self.with(|w, now| {
let at = now.saturating_sub(crate::clock::nanos(ago));
w.segment(t, 0, SYN, &[], at, false);
});
}
fn confirm_plan(&self, t: &TcpTap, ago: Duration) {
self.with(|w, now| {
let at = now.saturating_sub(crate::clock::nanos(ago));
if let Some(sent) = w.plans.get_mut(&t.key()) {
*sent = (*sent).max(at);
}
});
}
fn tcp_failed(&self, t: &TcpTap, ago: Duration) {
self.with(|w, now| {
w.end_plan(t.key(), now.saturating_sub(crate::clock::nanos(ago)));
w.flows.remove(&t.key());
});
}
fn tcp_refused(&self, t: &TcpTap, ago: Duration) {
self.with(|w, now| {
if !w.flows.contains_key(&t.key()) {
w.start_flow(t.key(), self.isn(t.key()));
}
let at = now.saturating_sub(crate::clock::nanos(ago));
w.segment(t, 1, RST | ACK, &[], at, false);
});
}
fn tcp_data(
&self,
t: &TcpTap,
from: usize,
bytes: &[u8],
arrival: Duration,
mss: Option<usize>,
acknowledge: bool,
) {
if bytes.is_empty() {
return;
}
self.with(|w, now| {
if w.flows.get(&t.key()).is_none_or(|f| !f.can_send(from, now)) {
return;
}
let mss = mss.unwrap_or_else(|| {
t.link
.mss(wire_v6(t.ends[0].addr.ip(), t.ends[1].addr.ip()))
});
for chunk in bytes.chunks(mss) {
w.segment(t, from, PSH | ACK, chunk, now, false);
}
if acknowledge {
w.acknowledge(t, 1 - from, now + crate::clock::nanos(arrival));
}
});
}
fn tcp_fin(&self, t: &TcpTap, from: usize, arrival: Duration) {
self.with(|w, now| {
let Some(flow) = w.flows.get_mut(&t.key()) else {
return;
};
if !flow.can_send(from, now) || flow.fin[from] {
return;
}
flow.fin[from] = true;
w.segment(t, from, FIN | ACK, &[], now, false);
w.acknowledge(t, 1 - from, now + crate::clock::nanos(arrival));
});
}
fn tcp_rst(&self, t: &TcpTap, from: usize, ago: Duration, return_delay: Option<Duration>) {
self.with(|w, now| {
w.reset_route(
TcpRoute {
link: &t.link,
ends: &t.ends,
dirs: &t.dirs,
},
from,
now.saturating_sub(crate::clock::nanos(ago)),
return_delay,
);
});
}
pub(crate) fn udp(
&self,
link: &Link,
src: Station,
dst: Station,
dir: Dir,
data: &[u8],
at: Option<Duration>,
) {
self.with(|w, now| {
let at = at.map_or(now, |at| {
crate::clock::nanos(at.saturating_sub(self.base_realtime))
});
let l4 = udp_header(src.addr.port(), dst.addr.port(), data);
w.ip_frame(link, src, dst, dir, PROTO_UDP, l4, at, None);
});
}
pub(crate) fn icmp_port_unreachable(
&self,
link: &Link,
from: Station,
to: Station,
datagram: &[u8],
after: Duration,
) {
self.with(|w, now| {
let (a, b) = wire_pair(to.addr.ip(), from.addr.ip());
let quoted_udp = udp_header(to.addr.port(), from.addr.port(), datagram);
let (proto, l4) = match (a, b) {
(IpAddr::V4(a), IpAddr::V4(b)) => {
let mut quoted = ipv4_header(a, b, PROTO_UDP, quoted_udp.len(), 0);
let mut udp = quoted_udp;
let sum = l4_checksum(a.into(), b.into(), PROTO_UDP, &udp, true);
write_u16(&mut udp, 6, sum);
quoted.extend_from_slice(&udp);
quoted.truncate(576 - 20 - 8);
let mut icmp = vec![3, 3, 0, 0, 0, 0, 0, 0];
icmp.extend_from_slice("ed);
(PROTO_ICMP, icmp)
}
(a, b) => {
let (a6, b6) = (as_v6(a), as_v6(b));
let mut quoted = ipv6_header(a6, b6, PROTO_UDP, quoted_udp.len());
let mut udp = quoted_udp;
let sum = l4_checksum(a6.into(), b6.into(), PROTO_UDP, &udp, true);
write_u16(&mut udp, 6, sum);
quoted.extend_from_slice(&udp);
quoted.truncate(1280 - 40 - 8);
let mut icmp = vec![1, 4, 0, 0, 0, 0, 0, 0];
icmp.extend_from_slice("ed);
(PROTO_ICMPV6, icmp)
}
};
w.ip_frame(
link,
from,
to,
Dir::In,
proto,
l4,
now + crate::clock::nanos(after),
None,
);
});
}
pub(crate) fn l2(&self, iface: &str, frame: &[u8]) {
self.with(|w, now| {
w.push(
now,
Frame {
iface: iface.to_owned(),
dir: Dir::Out,
orig_len: frame.len(),
bytes: frame[..frame.len().min(MAX_CAPLEN)].to_vec(),
plan: None,
#[cfg(target_os = "macos")]
ack: None,
wall: None,
},
);
});
}
}
impl Writer {
#[cfg(target_os = "macos")]
fn reserve_reset(&mut self, tap: &TcpTap, from: usize, at: u64, return_delay: Duration) {
let Some(flow) = self.flows.get_mut(&tap.key()) else {
return;
};
if flow.pending_closed_at[from].is_some_and(|old| old <= at) {
return;
}
self.retarget_reset(tap, from, at, return_delay);
}
#[cfg(target_os = "macos")]
fn retarget_reset(&mut self, tap: &TcpTap, from: usize, at: u64, return_delay: Duration) {
let Some(flow) = self.flows.get_mut(&tap.key()) else {
return;
};
flow.pending_closed_at[from] = Some(at);
if flow.reset {
return;
}
let pending = self
.resets
.entry(tap.key())
.or_insert_with(|| ResetReservation {
link: tap.link.clone(),
ends: tap.ends,
dirs: tap.dirs,
at: [None; 2],
return_delay: [Duration::ZERO; 2],
});
pending.at[from] = Some(at);
pending.return_delay[from] = return_delay;
}
#[cfg(target_os = "macos")]
fn finish_resets(&mut self, now: u64) {
let mut pending: Vec<_> = std::mem::take(&mut self.resets).into_iter().collect();
pending.sort_by_key(|(key, reset)| (reset.at.into_iter().flatten().min(), *key));
for (_, reset) in pending {
let Some((at, from)) = reset
.at
.into_iter()
.enumerate()
.filter_map(|(from, at)| at.map(|at| (at, from)))
.min_by_key(|(at, from)| (*at, 1 - *from))
else {
continue;
};
if at > now {
continue;
}
self.reset_route(
TcpRoute {
link: &reset.link,
ends: &reset.ends,
dirs: &reset.dirs,
},
from,
at,
Some(reset.return_delay[from]),
);
}
}
fn reset_route(
&mut self,
route: TcpRoute<'_>,
from: usize,
at: u64,
_return_delay: Option<Duration>,
) {
let key = route.key();
#[cfg(target_os = "macos")]
self.resets.remove(&key);
let Some(flow) = self.flows.get_mut(&key) else {
return;
};
let emit = !flow.reset;
flow.reset = true;
#[cfg(target_os = "macos")]
{
flow.pending_closed_at[from] = None;
let peer_at = at.saturating_add(crate::clock::nanos(_return_delay.unwrap_or_default()));
for (end, closed) in [(from, at), (1 - from, peer_at)] {
flow.closed_at[end] =
Some(flow.closed_at[end].map_or(closed, |old| old.min(closed)));
}
let closed_at = std::array::from_fn::<_, 2, _>(|end| {
flow.closed_at[end]
.into_iter()
.chain(flow.pending_closed_at[end])
.min()
});
for (end, closed) in closed_at.into_iter().enumerate() {
self.discard_acks(key, Some(end), closed.unwrap());
}
}
if emit {
self.segment_route(route, from, RST | ACK, &[], at, false);
}
}
#[cfg(target_os = "macos")]
fn discard_acks(&mut self, flow: (SocketAddr, SocketAddr), from: Option<usize>, after: u64) {
self.frames.retain(|_, frame| {
frame.ack.is_none_or(|ack| {
ack.flow != flow || from.is_some_and(|from| ack.from != from) || ack.at < after
})
});
}
fn end_plan(&mut self, key: (SocketAddr, SocketAddr), after: u64) {
if self.plans.remove(&key).is_none() {
return;
}
let stale: Vec<u64> = self
.pending
.iter()
.filter(|Reverse((at, id))| {
*at > after && self.frames.get(id).is_some_and(|f| f.plan == Some(key))
})
.map(|Reverse((_, id))| *id)
.collect();
for id in stale {
self.frames.remove(&id);
}
}
fn start_flow(&mut self, key: (SocketAddr, SocketAddr), isn: [u32; 2]) {
#[cfg(target_os = "macos")]
self.resets.remove(&key);
self.flows.insert(
key,
TcpFlow {
isn,
next_seq: isn,
syn: [false; 2],
fin: [false; 2],
reset: false,
#[cfg(target_os = "macos")]
closed_at: [None; 2],
#[cfg(target_os = "macos")]
pending_closed_at: [None; 2],
},
);
}
fn segment(&mut self, t: &TcpTap, from: usize, flags: u8, payload: &[u8], at: u64, plan: bool) {
self.segment_route(
TcpRoute {
link: &t.link,
ends: &t.ends,
dirs: &t.dirs,
},
from,
flags,
payload,
at,
plan,
);
}
fn acknowledge(&mut self, tap: &TcpTap, from: usize, at: u64) {
if self
.flows
.get(&tap.key())
.is_some_and(|flow| flow.can_send(from, at))
{
self.segment(tap, from, ACK, &[], at, false);
}
}
fn segment_route(
&mut self,
t: TcpRoute<'_>,
from: usize,
flags: u8,
payload: &[u8],
at: u64,
plan: bool,
) {
let Some(flow) = self.flows.get_mut(&t.key()) else {
return;
};
let to = 1 - from;
let seq = if flags & SYN != 0 {
flow.isn[from]
} else if flags & RST != 0 && !flow.syn[from] {
0
} else {
flow.next_seq[from]
};
let ack = if flags & ACK != 0 {
flow.next_seq[to]
} else {
0
};
if flags & SYN != 0 && !flow.syn[from] {
flow.syn[from] = true;
flow.next_seq[from] = flow.isn[from].wrapping_add(1);
}
if flags & FIN != 0 {
flow.next_seq[from] = flow.next_seq[from].wrapping_add(1);
}
flow.next_seq[from] = flow.next_seq[from].wrapping_add(payload.len() as u32);
let v6 = wire_v6(t.ends[0].addr.ip(), t.ends[1].addr.ip());
let mss = (flags & SYN != 0).then(|| t.link.mss(v6) as u16);
let (src, dst) = (t.ends[from], t.ends[to]);
let l4 = tcp_header(
src.addr.port(),
dst.addr.port(),
seq,
ack,
flags,
mss,
payload,
);
let key = plan.then(|| t.key());
#[cfg(target_os = "macos")]
let frame_id = self.next_frame;
self.ip_frame(t.link, src, dst, t.dirs[from], PROTO_TCP, l4, at, key);
#[cfg(target_os = "macos")]
if flags == ACK
&& let Some(frame) = self.frames.get_mut(&frame_id)
{
frame.ack = Some(TcpAck {
flow: t.key(),
from,
at: at.max(self.last),
});
}
}
#[allow(clippy::too_many_arguments)]
fn ip_frame(
&mut self,
link: &Link,
src: Station,
dst: Station,
dir: Dir,
proto: u8,
mut l4: Vec<u8>,
at: u64,
plan: Option<(SocketAddr, SocketAddr)>,
) {
let (s, d) = wire_pair(src.addr.ip(), dst.addr.ip());
let csum_at = match proto {
PROTO_TCP => Some(16),
PROTO_UDP => Some(6),
PROTO_ICMP | PROTO_ICMPV6 => Some(2),
_ => None,
};
let mut bytes = Vec::with_capacity(14 + 40 + l4.len());
bytes.extend_from_slice(&dst.mac);
bytes.extend_from_slice(&src.mac);
match (s, d) {
(IpAddr::V4(s4), IpAddr::V4(d4)) => {
if let Some(off) = csum_at {
let sum = if proto == PROTO_ICMP {
checksum(&[&l4])
} else {
l4_checksum(s, d, proto, &l4, proto == PROTO_UDP)
};
write_u16(&mut l4, off, sum);
}
let id = self.ip_id.entry(s).or_insert(0);
let this = *id;
*id = id.wrapping_add(1);
bytes.extend_from_slice(ÐERTYPE_IPV4.to_be_bytes());
bytes.extend_from_slice(&ipv4_header(s4, d4, proto, l4.len(), this));
}
(s, d) => {
let (s6, d6) = (as_v6(s), as_v6(d));
if let Some(off) = csum_at {
let sum = l4_checksum(s6.into(), d6.into(), proto, &l4, proto == PROTO_UDP);
write_u16(&mut l4, off, sum);
}
bytes.extend_from_slice(ÐERTYPE_IPV6.to_be_bytes());
bytes.extend_from_slice(&ipv6_header(s6, d6, proto, l4.len()));
}
}
bytes.extend_from_slice(&l4);
let orig_len = bytes.len();
bytes.truncate(MAX_CAPLEN);
self.push(
at,
Frame {
iface: link.iface.clone(),
dir,
bytes,
orig_len,
plan,
#[cfg(target_os = "macos")]
ack: None,
wall: None,
},
);
}
fn push(&mut self, at: u64, mut frame: Frame) {
if self.wall_comment && at <= self.now {
frame.wall = Some(real_wall());
}
let at = at.max(self.last);
let id = self.next_frame;
self.next_frame += 1;
self.frames.insert(id, frame);
self.pending.push(Reverse((at, id)));
}
fn flush_due(&mut self, upto: Option<u64>, base_realtime: Duration) {
let base = crate::clock::nanos(base_realtime);
let mut result = Ok(());
#[cfg(target_os = "macos")]
let reset_at = self
.resets
.values()
.flat_map(|reset| reset.at.into_iter().flatten())
.min();
while let Some(&Reverse((at, id))) = self.pending.peek() {
if upto.is_some_and(|upto| at > upto) {
break;
}
#[cfg(target_os = "macos")]
if upto.is_some() && reset_at.is_some_and(|reset| at >= reset) {
break;
}
let held = |frame: &Frame| {
frame
.plan
.and_then(|key| self.plans.get(&key))
.is_some_and(|&sent| at > sent)
};
if upto.is_some() && self.frames.get(&id).is_some_and(held) {
break;
}
self.pending.pop();
let Some(frame) = self.frames.remove(&id) else {
continue;
};
self.last = self.last.max(at);
result = self.write_frame(base.saturating_add(at), &frame);
if result.is_err() {
break;
}
}
if result.is_ok() {
result = self.flush_out();
}
if let Err(e) = result {
eprintln!("snare: pcapng: write failed, capture stopped: {e}");
self.out = None;
self.pending.clear();
self.frames.clear();
}
}
fn write_frame(&mut self, ns: u64, frame: &Frame) -> io::Result<()> {
let iface = match self.links.get(&frame.iface) {
Some(&id) => id,
None => {
let id = self.links.len() as u32;
self.put(&idb(&frame.iface))?;
self.links.insert(frame.iface.clone(), id);
id
}
};
let wall = frame
.wall
.or_else(|| self.wall_comment.then(real_wall))
.map(wall_text);
self.put(&epb(iface, ns, frame, wall.as_deref()))
}
fn put(&mut self, block: &[u8]) -> io::Result<()> {
match &mut self.out {
Some(out) => out.write_all(block),
None => Ok(()),
}
}
fn flush_out(&mut self) -> io::Result<()> {
match &mut self.out {
Some(out) => out.flush(),
None => Ok(()),
}
}
}
fn sanitize(name: &str) -> String {
name.chars()
.map(|c| match c {
'a'..='z' | 'A'..='Z' | '0'..='9' | '.' | '_' | '-' => c,
':' => '-',
_ => '_',
})
.collect()
}
fn option(out: &mut Vec<u8>, code: u16, value: &[u8]) {
out.extend_from_slice(&code.to_le_bytes());
out.extend_from_slice(&(value.len() as u16).to_le_bytes());
out.extend_from_slice(value);
out.resize(out.len().next_multiple_of(4), 0);
}
fn block(kind: u32, body: Vec<u8>) -> Vec<u8> {
let total = (12 + body.len()) as u32;
let mut out = Vec::with_capacity(total as usize);
out.extend_from_slice(&kind.to_le_bytes());
out.extend_from_slice(&total.to_le_bytes());
out.extend_from_slice(&body);
out.extend_from_slice(&total.to_le_bytes());
out
}
fn shb() -> Vec<u8> {
let mut body = Vec::new();
body.extend_from_slice(&BYTE_ORDER_MAGIC.to_le_bytes());
body.extend_from_slice(&1u16.to_le_bytes());
body.extend_from_slice(&0u16.to_le_bytes());
body.extend_from_slice(&(-1i64).to_le_bytes());
option(&mut body, SHB_OS, std::env::consts::OS.as_bytes());
option(
&mut body,
SHB_USERAPPL,
concat!("snare ", env!("CARGO_PKG_VERSION")).as_bytes(),
);
option(&mut body, OPT_END, &[]);
block(BLOCK_SHB, body)
}
fn idb(name: &str) -> Vec<u8> {
let mut body = Vec::new();
body.extend_from_slice(&LINKTYPE_ETHERNET.to_le_bytes());
body.extend_from_slice(&0u16.to_le_bytes());
body.extend_from_slice(&0u32.to_le_bytes());
option(&mut body, IF_NAME, name.as_bytes());
option(&mut body, IF_TSRESOL, &[9]);
option(&mut body, OPT_END, &[]);
block(BLOCK_IDB, body)
}
fn epb(iface: u32, ns: u64, frame: &Frame, comment: Option<&str>) -> Vec<u8> {
let mut body = Vec::with_capacity(32 + frame.bytes.len());
body.extend_from_slice(&iface.to_le_bytes());
body.extend_from_slice(&((ns >> 32) as u32).to_le_bytes());
body.extend_from_slice(&(ns as u32).to_le_bytes());
body.extend_from_slice(&(frame.bytes.len() as u32).to_le_bytes());
body.extend_from_slice(&(frame.orig_len as u32).to_le_bytes());
body.extend_from_slice(&frame.bytes);
body.resize(body.len().next_multiple_of(4), 0);
let flags: u32 = match frame.dir {
Dir::In => 1,
Dir::Out => 2,
};
option(&mut body, EPB_FLAGS, &flags.to_le_bytes());
if let Some(comment) = comment {
option(&mut body, OPT_COMMENT, comment.as_bytes());
}
option(&mut body, OPT_END, &[]);
block(BLOCK_EPB, body)
}
fn wall_text(wall: Duration) -> String {
let secs = wall.as_secs();
let (year, month, day) = civil_from_days(secs / 86_400);
let of_day = secs % 86_400;
format!(
"wall {year:04}-{month:02}-{day:02}T{:02}:{:02}:{:02}.{:09}Z",
of_day / 3600,
of_day / 60 % 60,
of_day % 60,
wall.subsec_nanos()
)
}
fn civil_from_days(days: u64) -> (u64, u64, u64) {
let z = days + 719_468;
let era = z / 146_097;
let doe = z - era * 146_097;
let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
let mp = (5 * doy + 2) / 153;
let day = doy - (153 * mp + 2) / 5 + 1;
let month = if mp < 10 { mp + 3 } else { mp - 9 };
let year = yoe + era * 400 + u64::from(month <= 2);
(year, month, day)
}
fn test_listed(list: &str, thread: Option<&str>) -> bool {
let Some(thread) = thread else {
return false;
};
list.split(',')
.map(str::trim)
.filter(|entry| !entry.is_empty())
.any(|entry| {
thread == entry
|| thread
.strip_suffix(entry)
.is_some_and(|head| head.ends_with("::"))
})
}
fn write_u16(buf: &mut [u8], at: usize, v: u16) {
buf[at..at + 2].copy_from_slice(&v.to_be_bytes());
}
fn checksum(parts: &[&[u8]]) -> u16 {
let mut sum: u64 = 0;
let mut odd: Option<u8> = None;
for part in parts {
for &b in *part {
match odd.take() {
Some(hi) => sum += u64::from(u16::from_be_bytes([hi, b])),
None => odd = Some(b),
}
}
}
if let Some(hi) = odd {
sum += u64::from(u16::from_be_bytes([hi, 0]));
}
while sum >> 16 != 0 {
sum = (sum & 0xffff) + (sum >> 16);
}
!(sum as u16)
}
fn l4_checksum(src: IpAddr, dst: IpAddr, proto: u8, l4: &[u8], udp: bool) -> u16 {
let mut pseudo = Vec::with_capacity(40);
match (src, dst) {
(IpAddr::V4(s), IpAddr::V4(d)) => {
pseudo.extend_from_slice(&s.octets());
pseudo.extend_from_slice(&d.octets());
pseudo.extend_from_slice(&[0, proto]);
pseudo.extend_from_slice(&(l4.len() as u16).to_be_bytes());
}
(s, d) => {
pseudo.extend_from_slice(&as_v6(s).octets());
pseudo.extend_from_slice(&as_v6(d).octets());
pseudo.extend_from_slice(&(l4.len() as u32).to_be_bytes());
pseudo.extend_from_slice(&[0, 0, 0, proto]);
}
}
let sum = checksum(&[&pseudo, l4]);
if udp && sum == 0 { 0xffff } else { sum }
}
fn ipv4_header(src: Ipv4Addr, dst: Ipv4Addr, proto: u8, payload: usize, id: u16) -> Vec<u8> {
let total = (20 + payload).min(65535) as u16;
let mut h = vec![0x45, 0];
h.extend_from_slice(&total.to_be_bytes());
h.extend_from_slice(&id.to_be_bytes());
h.extend_from_slice(&0x4000u16.to_be_bytes());
h.extend_from_slice(&[TTL, proto, 0, 0]);
h.extend_from_slice(&src.octets());
h.extend_from_slice(&dst.octets());
let sum = checksum(&[&h]);
write_u16(&mut h, 10, sum);
h
}
fn ipv6_header(src: Ipv6Addr, dst: Ipv6Addr, proto: u8, payload: usize) -> Vec<u8> {
let mut h = vec![0x60, 0, 0, 0];
h.extend_from_slice(&(payload.min(65535) as u16).to_be_bytes());
h.extend_from_slice(&[proto, TTL]);
h.extend_from_slice(&src.octets());
h.extend_from_slice(&dst.octets());
h
}
fn tcp_header(
sport: u16,
dport: u16,
seq: u32,
ack: u32,
flags: u8,
mss: Option<u16>,
payload: &[u8],
) -> Vec<u8> {
let words: u8 = if mss.is_some() { 6 } else { 5 };
let mut h = Vec::with_capacity(usize::from(words) * 4 + payload.len());
h.extend_from_slice(&sport.to_be_bytes());
h.extend_from_slice(&dport.to_be_bytes());
h.extend_from_slice(&seq.to_be_bytes());
h.extend_from_slice(&ack.to_be_bytes());
h.extend_from_slice(&[words << 4, flags]);
h.extend_from_slice(&65535u16.to_be_bytes());
h.extend_from_slice(&[0, 0, 0, 0]);
if let Some(mss) = mss {
h.extend_from_slice(&[2, 4]);
h.extend_from_slice(&mss.to_be_bytes());
}
h.extend_from_slice(payload);
h
}
fn udp_header(sport: u16, dport: u16, data: &[u8]) -> Vec<u8> {
let mut h = Vec::with_capacity(8 + data.len());
h.extend_from_slice(&sport.to_be_bytes());
h.extend_from_slice(&dport.to_be_bytes());
h.extend_from_slice(&((8 + data.len()).min(65535) as u16).to_be_bytes());
h.extend_from_slice(&[0, 0]);
h.extend_from_slice(data);
h
}
fn as_v6(ip: IpAddr) -> Ipv6Addr {
match ip {
IpAddr::V4(v4) => v4.to_ipv6_mapped(),
IpAddr::V6(v6) => v6,
}
}
fn unmapped(ip: IpAddr) -> IpAddr {
match ip {
IpAddr::V6(v6) => v6.to_ipv4_mapped().map_or(ip, IpAddr::V4),
v4 => v4,
}
}
fn wire_pair(a: IpAddr, b: IpAddr) -> (IpAddr, IpAddr) {
match (unmapped(a), unmapped(b)) {
(a @ IpAddr::V4(_), b @ IpAddr::V4(_)) => (a, b),
(a, b) => (IpAddr::V6(as_v6(a)), IpAddr::V6(as_v6(b))),
}
}
fn wire_v6(a: IpAddr, b: IpAddr) -> bool {
wire_pair(a, b).0.is_ipv6()
}
struct Fnv(u64);
impl Fnv {
fn new() -> Self {
Fnv(0xcbf2_9ce4_8422_2325)
}
fn bytes(&mut self, bytes: &[u8]) {
for &b in bytes {
self.0 ^= u64::from(b);
self.0 = self.0.wrapping_mul(0x0100_0000_01b3);
}
}
fn ip(&mut self, ip: IpAddr) {
match unmapped(ip) {
IpAddr::V4(v4) => self.bytes(&v4.octets()),
IpAddr::V6(v6) => self.bytes(&v6.octets()),
}
}
fn addr(&mut self, addr: SocketAddr) {
self.ip(addr.ip());
self.bytes(&addr.port().to_be_bytes());
}
}
fn derived_mac(ip: IpAddr, broadcast: bool) -> [u8; 6] {
match unmapped(ip) {
_ if broadcast => [0xff; 6],
IpAddr::V4(v4) if v4.is_broadcast() => [0xff; 6],
IpAddr::V4(v4) if v4.is_multicast() => {
let o = v4.octets();
[0x01, 0x00, 0x5e, o[1] & 0x7f, o[2], o[3]]
}
IpAddr::V6(v6) if v6.is_multicast() => {
let o = v6.octets();
[0x33, 0x33, o[12], o[13], o[14], o[15]]
}
ip => {
let mut h = Fnv::new();
h.ip(ip);
let b = h.0.to_be_bytes();
[0x02, b[0], b[1], b[2], b[3], b[4]]
}
}
}
impl SimShared {
pub(crate) fn capture(&self) -> Option<&Arc<Capture>> {
self.capture.get()
}
pub(crate) fn wire_ends(
&self,
sender: &Sender,
src: SocketAddr,
dst: SocketAddr,
) -> (Link, Station, Station) {
let topo = self.topo();
let index = match sender {
Sender::Host(path) if path.local => topo.loopback_index(),
Sender::Host(path) => path.egress,
Sender::Station(ip) => topo
.segment_of(*ip)
.or_else(|| topo.segment_of(dst.ip()))
.unwrap_or_else(|| topo.loopback_index()),
};
self.ends_on(&topo, index, src, dst)
}
fn ends_on(
&self,
topo: &crate::netif::Topology,
index: u32,
src: SocketAddr,
dst: SocketAddr,
) -> (Link, Station, Station) {
let (iface, mtu, loopback) = topo
.wire_of(index)
.unwrap_or_else(|| (format!("if{index}"), 1500, false));
let mac = |ip: IpAddr| {
if loopback {
[0; 6]
} else {
topo.mac_of(unmapped(ip))
.unwrap_or_else(|| derived_mac(ip, topo.is_broadcast(unmapped(ip))))
}
};
(
Link { iface, mtu },
Station {
addr: src,
mac: mac(src.ip()),
},
Station {
addr: dst,
mac: mac(dst.ip()),
},
)
}
pub(crate) fn tcp_tap(
&self,
sender: &Sender,
client: SocketAddr,
server: SocketAddr,
server_in: bool,
) -> Option<TcpTap> {
let capture = self.capture()?.clone();
let (link, c, s) = snare_interpose::real(|| self.wire_ends(sender, client, server));
Some(TcpTap {
capture,
link,
ends: [c, s],
dirs: [Dir::Out, if server_in { Dir::In } else { Dir::Out }],
})
}
pub(crate) fn capture_udp(
&self,
sender: &Sender,
src: SocketAddr,
dst: SocketAddr,
dir: Dir,
data: &[u8],
at: Option<Duration>,
) {
if let Some(capture) = self.capture() {
let (link, s, d) = snare_interpose::real(|| self.wire_ends(sender, src, dst));
capture.udp(&link, s, d, dir, data, at);
}
}
pub(crate) fn capture_icmp(
&self,
sender: &Sender,
src: SocketAddr,
unreachable: SocketAddr,
data: &[u8],
after: Duration,
) {
if let Some(capture) = self.capture() {
let (link, s, d) = snare_interpose::real(|| self.wire_ends(sender, src, unreachable));
capture.icmp_port_unreachable(&link, d, s, data, after);
}
}
#[cfg_attr(not(any(target_os = "linux", target_os = "macos")), allow(dead_code))]
pub(crate) fn capture_l2(&self, index: u32, frame: &[u8]) {
if let Some(capture) = self.capture() {
let iface = snare_interpose::real(|| self.topo().name_of(index))
.unwrap_or_else(|| format!("if{index}"));
capture.l2(&iface, frame);
}
}
#[cfg_attr(not(windows), allow(dead_code))]
pub(crate) fn capture_l2_named(&self, iface: &str, frame: &[u8]) {
if let Some(capture) = self.capture() {
capture.l2(iface, frame);
}
}
}
pub(crate) fn attach(shared: &SimShared, path: Option<PathBuf>, wall_comment: bool) {
let wall_comment = self::wall_comment(wall_comment);
let capture = match path {
Some(path) => match Capture::create(path.clone(), shared, wall_comment) {
Ok(capture) => Some(capture),
Err(e) => panic!("cannot create pcapng capture {}: {e}", path.display()),
},
None => Capture::from_env(shared, wall_comment),
};
if let Some(capture) = capture {
let _ = shared.capture.set(capture);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn le32(b: &[u8], at: usize) -> u32 {
u32::from_le_bytes(b[at..at + 4].try_into().unwrap())
}
fn writer() -> Writer {
Writer {
out: None,
pending: BinaryHeap::new(),
frames: HashMap::new(),
next_frame: 0,
flows: HashMap::new(),
plans: HashMap::new(),
#[cfg(target_os = "macos")]
resets: HashMap::new(),
last: 0,
ip_id: HashMap::new(),
links: HashMap::new(),
finished: false,
wall_comment: false,
now: 0,
}
}
fn tap(capture: Arc<Capture>, mtu: u32, client: &str, server: &str) -> TcpTap {
TcpTap {
capture,
link: Link {
iface: "eth0".into(),
mtu,
},
ends: [
Station {
addr: client.parse().unwrap(),
mac: [2, 0, 0, 0, 0, 1],
},
Station {
addr: server.parse().unwrap(),
mac: [2, 0, 0, 0, 0, 2],
},
],
dirs: [Dir::Out, Dir::In],
}
}
fn capture_at(path: PathBuf, seed: u64) -> Arc<Capture> {
let shared = SimShared::new(seed, Some(Arc::new(Clock::new(37))));
Capture::create(path, &shared, false).unwrap()
}
fn scratch(name: &str) -> PathBuf {
std::env::temp_dir().join(format!(
"snare-pcapng-unit-{}-{name}.pcapng",
std::process::id()
))
}
fn blocks(bytes: &[u8]) -> Vec<(u32, Vec<u8>)> {
let mut out = Vec::new();
let mut at = 0;
while at < bytes.len() {
let kind = le32(bytes, at);
let len = le32(bytes, at + 4) as usize;
assert_eq!(le32(bytes, at + len - 4) as usize, len);
out.push((kind, bytes[at + 8..at + len - 4].to_vec()));
at += len;
}
out
}
#[test]
fn shb_idb_epb_layout() {
let shb = shb();
assert_eq!(le32(&shb, 0), BLOCK_SHB);
assert_eq!(le32(&shb, 8), BYTE_ORDER_MAGIC);
assert_eq!(&shb[12..16], &[1, 0, 0, 0]);
assert_eq!(shb.len() % 4, 0);
let idb = idb("eth0");
assert_eq!(le32(&idb, 0), BLOCK_IDB);
assert_eq!(&idb[8..10], &LINKTYPE_ETHERNET.to_le_bytes());
assert_eq!(le32(&idb, 12), 0, "snaplen 0");
assert_eq!(&idb[16..24], &[2, 0, 4, 0, b'e', b't', b'h', b'0']);
assert_eq!(&idb[24..28], &[9, 0, 1, 0]);
assert_eq!(idb[28], 9);
let frame = Frame {
iface: "eth0".into(),
dir: Dir::In,
bytes: vec![1, 2, 3],
orig_len: 70000,
plan: None,
#[cfg(target_os = "macos")]
ack: None,
wall: None,
};
let epb = epb(3, 0x1_0000_0002, &frame, None);
assert_eq!(le32(&epb, 0), BLOCK_EPB);
assert_eq!(le32(&epb, 8), 3);
assert_eq!(le32(&epb, 12), 1);
assert_eq!(le32(&epb, 16), 2);
assert_eq!(le32(&epb, 20), 3);
assert_eq!(le32(&epb, 24), 70000);
assert_eq!(&epb[28..32], &[1, 2, 3, 0]);
assert_eq!(&epb[32..36], &[2, 0, 4, 0]);
assert_eq!(le32(&epb, 36), 1, "inbound");
assert_eq!(le32(&epb, 4) as usize, epb.len());
}
#[test]
fn ipv4_header_checksum_known_value() {
let h = {
let mut h = ipv4_header(
Ipv4Addr::new(192, 168, 0, 1),
Ipv4Addr::new(192, 168, 0, 199),
PROTO_UDP,
0x73 - 20,
0,
);
h[8] = 64;
write_u16(&mut h, 10, 0);
let sum = checksum(&[&h]);
write_u16(&mut h, 10, sum);
h
};
assert_eq!(&h[10..12], &[0xb8, 0x61]);
assert_eq!(checksum(&[&h]), 0);
}
#[test]
fn tcp_and_udp_checksums_verify() {
let (s, d): (IpAddr, IpAddr) = ("10.0.0.1".parse().unwrap(), "10.0.0.2".parse().unwrap());
let mut tcp = tcp_header(1000, 80, 7, 9, PSH | ACK, None, b"hello");
let sum = l4_checksum(s, d, PROTO_TCP, &tcp, false);
write_u16(&mut tcp, 16, sum);
assert_eq!(l4_checksum(s, d, PROTO_TCP, &tcp, false), 0);
let (s6, d6): (IpAddr, IpAddr) = ("fe80::1".parse().unwrap(), "fe80::2".parse().unwrap());
let mut udp = udp_header(5, 6, b"odd");
let sum = l4_checksum(s6, d6, PROTO_UDP, &udp, true);
write_u16(&mut udp, 6, sum);
let check = l4_checksum(s6, d6, PROTO_UDP, &udp, false);
assert!(check == 0 || check == 0xffff);
}
#[test]
fn macs() {
let ip = |s: &str| s.parse::<IpAddr>().unwrap();
assert_eq!(derived_mac(ip("255.255.255.255"), false), [0xff; 6]);
assert_eq!(derived_mac(ip("10.0.0.255"), true), [0xff; 6]);
assert_eq!(
derived_mac(ip("239.129.2.3"), false),
[0x01, 0x00, 0x5e, 0x01, 0x02, 0x03]
);
assert_eq!(
derived_mac(ip("ff02::1:ff00:1234"), false),
[0x33, 0x33, 0xff, 0x00, 0x12, 0x34]
);
let a = derived_mac(ip("10.0.0.7"), false);
assert_eq!(a[0], 0x02);
assert_eq!(a, derived_mac(ip("::ffff:10.0.0.7"), false));
assert_ne!(a, derived_mac(ip("10.0.0.8"), false));
}
#[test]
fn v4_mapped_pair_written_as_ipv4() {
let (a, b) = wire_pair(
"::ffff:10.0.0.1".parse().unwrap(),
"::ffff:10.0.0.2".parse().unwrap(),
);
assert_eq!(a, "10.0.0.1".parse::<IpAddr>().unwrap());
assert_eq!(b, "10.0.0.2".parse::<IpAddr>().unwrap());
let mut w = writer();
let src = Station {
addr: "[::ffff:10.0.0.1]:5".parse().unwrap(),
mac: [0; 6],
};
let dst = Station {
addr: "[::ffff:10.0.0.2]:6".parse().unwrap(),
mac: [0; 6],
};
let link = Link {
iface: "lo".into(),
mtu: 1500,
};
w.ip_frame(
&link,
src,
dst,
Dir::Out,
PROTO_UDP,
udp_header(5, 6, b"x"),
0,
None,
);
let frame = &w.frames[&0];
assert_eq!(&frame.bytes[12..14], ÐERTYPE_IPV4.to_be_bytes());
assert_eq!(&frame.bytes[26..30], &[10, 0, 0, 1]);
}
#[test]
fn pending_heap_keeps_time_order() {
let path = scratch("heap");
let capture = capture_at(path.clone(), 1);
let t = tap(capture.clone(), 1500, "10.0.0.1:40000", "10.0.0.2:80");
t.syn();
t.syn_plan(&[Duration::from_secs(3), Duration::from_secs(1)]);
capture.finish();
let bytes = std::fs::read(&path).unwrap();
let stamps: Vec<u64> = blocks(&bytes)
.into_iter()
.filter(|(k, _)| *k == BLOCK_EPB)
.map(|(_, b)| (u64::from(le32(&b, 4)) << 32) | u64::from(le32(&b, 8)))
.collect();
let base = 1_700_000_000u64 * 1_000_000_000;
assert_eq!(stamps, [base, base + 1_000_000_000, base + 3_000_000_000]);
let _ = std::fs::remove_file(path);
}
#[test]
fn tcp_write_larger_than_mss_is_segmented() {
let path = scratch("mss");
let capture = capture_at(path.clone(), 1);
let t = tap(capture.clone(), 1500, "10.0.0.1:40000", "10.0.0.2:80");
t.open(false, Duration::ZERO);
t.data(0, &vec![7u8; 3000], Duration::ZERO);
capture.finish();
let bytes = std::fs::read(&path).unwrap();
let lens: Vec<usize> = blocks(&bytes)
.into_iter()
.filter(|(k, _)| *k == BLOCK_EPB)
.map(|(_, b)| le32(&b, 12) as usize - 54)
.collect();
assert_eq!(lens, [4, 4, 0, 1460, 1460, 80, 0]);
let _ = std::fs::remove_file(path);
}
#[test]
fn isn_depends_on_seed_and_flow_not_order() {
let a = capture_at(scratch("isn-a"), 7);
let b = capture_at(scratch("isn-b"), 7);
let c = capture_at(scratch("isn-c"), 8);
let f1: (SocketAddr, SocketAddr) =
("10.0.0.1:1".parse().unwrap(), "10.0.0.2:2".parse().unwrap());
let f2: (SocketAddr, SocketAddr) =
("10.0.0.1:3".parse().unwrap(), "10.0.0.2:2".parse().unwrap());
let first = (a.isn(f1), a.isn(f2));
let second = (b.isn(f2), b.isn(f1));
assert_eq!(first, (second.1, second.0));
assert_ne!(a.isn(f1), c.isn(f1));
assert_ne!(a.isn(f1), a.isn(f2));
for name in ["isn-a", "isn-b", "isn-c"] {
let _ = std::fs::remove_file(scratch(name));
}
}
#[test]
fn epb_comment_follows_flags_padded() {
let frame = Frame {
iface: "eth0".into(),
dir: Dir::Out,
bytes: vec![1, 2, 3, 4],
orig_len: 4,
plan: None,
#[cfg(target_os = "macos")]
ack: None,
wall: None,
};
let epb = epb(0, 0, &frame, Some("wall x"));
assert_eq!(&epb[32..36], &[2, 0, 4, 0], "epb_flags");
assert_eq!(le32(&epb, 36), 2, "outbound");
assert_eq!(&epb[40..44], &[1, 0, 6, 0], "opt_comment, 6 bytes");
assert_eq!(&epb[44..52], b"wall x\0\0");
assert_eq!(&epb[52..56], &[0, 0, 0, 0], "opt_endofopt");
assert_eq!(le32(&epb, 4) as usize, epb.len());
}
#[test]
fn wall_text_is_rfc3339_utc() {
assert_eq!(
wall_text(Duration::ZERO),
"wall 1970-01-01T00:00:00.000000000Z"
);
assert_eq!(
wall_text(Duration::new(951_782_400 + 3_723, 5)),
"wall 2000-02-29T01:02:03.000000005Z"
);
assert_eq!(civil_from_days(19_723), (2024, 1, 1));
assert_eq!(civil_from_days(19_782), (2024, 2, 29));
assert_eq!(civil_from_days(20_728), (2026, 10, 2));
assert_eq!(civil_from_days(11_016 + 306), (2000, 12, 31));
}
#[test]
fn test_list_matches_whole_names_and_trailing_segments() {
assert!(test_listed("a, b", Some("b")));
assert!(test_listed("mod::t", Some("mod::t")));
assert!(test_listed("t", Some("mod::t")));
assert!(test_listed("inner::t", Some("outer::inner::t")));
assert!(!test_listed("t", Some("mod::not_t")));
assert!(!test_listed("t", Some("tt")));
assert!(!test_listed(" , ", Some("t")));
assert!(!test_listed("t", None));
}
#[test]
fn io_error_disables_capture_without_panicking() {
let capture = capture_at(scratch("ioerr"), 1);
{
let mut w = capture.inner.lock().unwrap();
let file = File::open(scratch("ioerr")).unwrap();
w.out = Some(BufWriter::with_capacity(0, file));
}
let t = tap(capture.clone(), 1500, "10.0.0.1:40000", "10.0.0.2:80");
t.open(false, Duration::ZERO);
assert!(capture.inner.lock().unwrap().out.is_none());
t.data(0, b"more", Duration::ZERO);
capture.finish();
let _ = std::fs::remove_file(scratch("ioerr"));
}
}