Skip to main content

capfile/
lib.rs

1//! Capfile - Pure Rust crate for reading/writing pcap/pcapng files
2//!
3//! This crate provides zero-copy parsing of pcap and pcapng files with
4//! support for packet dissection and writing capture files.
5//!
6//! # Features
7//!
8//! - **Zero-copy parsing**: Data is parsed without copying where possible,
9//!   making it efficient for large capture files
10//! - **Both formats**: Supports legacy PCAP and modern PCAPNG formats
11//! - **Packet dissection**: Built-in support for dissecting Ethernet, IPv4,
12//!   IPv6, TCP, UDP, ICMP, and DNS protocols
13//! - **Write support**: Create new PCAP and PCAPNG files
14//!
15//! # Quick Start
16//!
17//! ## Reading a PCAP file
18//!
19//! ```ignore
20//! use capfile::PcapReader;
21//!
22//! // Open a pcap file (requires std feature)
23//! let mut reader = PcapReader::open("capture.pcap")?;
24//!
25//! // Iterate over packets
26//! while let Some(pkt) = reader.next_packet()? {
27//!     println!("{} bytes at {}", pkt.len(), pkt.timestamp_ns());
28//! }
29//! ```
30//!
31//! ## Reading from bytes (no_std/WASM)
32//!
33//! ```ignore
34//! use capfile::PcapReader;
35//! use std::io::Cursor;
36//!
37//! let data = include_bytes!("capture.pcap");
38//! let mut reader = PcapReader::from_reader(Cursor::new(data))?;
39//!
40//! while let Some(pkt) = reader.next_packet()? {
41//!     // Process packet
42//! }
43//! ```
44//!
45//! ## Reading a PCAPNG file
46//!
47//! ```ignore
48//! use capfile::PcapngReader;
49//! use capfile::format::pcapng::Block;
50//!
51//! let mut reader = PcapngReader::open("capture.pcapng")?;
52//!
53//! // Get interface information
54//! for (i, iface) in reader.interfaces().iter().enumerate() {
55//!     println!("Interface {}: link_type={}", i, iface.link_type);
56//! }
57//!
58//! // Iterate over blocks
59//! while let Some(block) = reader.next_block()? {
60//!     match block {
61//!         Block::EnhancedPacket(epb) => {
62//!             println!("Packet: {} bytes", epb.captured_length);
63//!         }
64//!         Block::InterfaceStatistics(stats) => {
65//!             println!("Stats: {} packets", stats.packets_received);
66//!         }
67//!         _ => {}
68//!     }
69//! }
70//! ```
71//!
72//! ## Packet Dissection
73//!
74//! ```ignore
75//! use capfile::{PcapReader, dissect::{Ethernet, Ipv4, Tcp}};
76//!
77//! let mut reader = PcapReader::open("capture.pcap")?;
78//!
79//! if let Some(pkt) = reader.next_packet()? {
80//!     // Dissect Ethernet
81//!     let eth = Ethernet::new(pkt.data())?;
82//!     println!("{} -> {}", format_mac(eth.src()), format_mac(eth.dst()));
83//!
84//!     // Dissect IPv4
85//!     let ip = Ipv4::new(eth.payload())?;
86//!     println!("{} -> {}", ip.src_str(), ip.dst_str());
87//!
88//!     // Dissect TCP
89//!     if let Ok(tcp) = Tcp::new(ip.payload()) {
90//!         println!("{} -> {}", tcp.src_port(), tcp.dst_port());
91//!     }
92//! }
93//! ```
94//!
95//! ## Writing a PCAP file
96//!
97//! ```ignore
98//! use capfile::PcapWriter;
99//!
100//! let mut writer = PcapWriter::create("output.pcap", 1)?; // Ethernet
101//!
102//! // Write a packet
103//! writer.write_packet(b"packet data", 1000000000, 100)?;
104//! ```
105//!
106//! # Modules
107//!
108//! - [`error`] - Error types for operations
109//! - [`format`] - File format parsing (PCAP and PCAPNG)
110//! - [`reader`] - PCAP and PCAPNG readers
111//! - [`writer`] - PCAP and PCAPNG writers
112//! - `dissect` - Packet dissection for various protocols
113
114pub mod dissect;
115pub mod error;
116pub mod format;
117pub mod reader;
118pub mod writer;
119
120pub use error::Error;
121#[cfg(feature = "std")]
122pub use reader::mmap::PcapReaderMmap;
123pub use reader::{Interface, PacketRef, PcapReader, PcapngReader};
124pub use writer::{PcapWriter, PcapngWriter};