Nata is a Rust toolkit for parsing, inspecting, constructing, and writing network packets. It takes inspiration from Scapy while providing strongly typed protocol layers and symmetric binary serialization.
Capabilities
- Parse raw bytes into typed protocol layers and serialize them back to bytes.
- Compose packets from independently configurable layers.
- Finalize dependent fields such as IPv4 lengths, transport lengths, header checksums, and TCP/UDP checksums.
- Parse complete protocol stacks with built-in layer bindings, or register custom bindings for application protocols.
- Add custom protocols by implementing the
LayerandLayerExttraits. - Read and write live packets and offline PCAP files.
- Compile the core packet and layer APIs without
std.
Usage
[]
= "0.1"
See Cargo features for optional integrations and no_std
configuration.
Quick start
The example below creates an Ethernet/IPv4/UDP packet containing a raw payload.
finalize fills in the dependent length and checksum fields before the packet
is serialized. The resulting bytes are then parsed back into the same four
layers.
use ;
The same program is available as examples/build_packet.rs.
See the API documentation and examples for custom layers, offline PCAP processing, and live packet capture and injection.
Cargo features
Nata enables std by default. This provides libpnet interfaces and offline PCAP
file I/O. Live libpcap support is opt-in.
| Feature | Default | Description |
|---|---|---|
std |
Yes | Live interfaces through libpnet and offline PCAP file I/O |
libpcap |
No | Live capture and injection through libpcap |
Enable live libpcap support with:
[]
= { = "0.1", = false, = ["std", "libpcap"] }
For no_std applications, disable the default features:
[]
= { = "0.1", = false }
Packet types use allocation, so no_std applications must provide an
allocator. See the example_no_std fixture.
libpcap requires its development files.
Built-in layers
- Ethernet II (
Ether,EtherType,MacAddress) - IPv4 (
Ipv4,IpProtocol,Ipv4Option) - IPv6 (
Ipv6,IpProtocol) - ICMPv4 (
Icmp4,IcmpType) - TCP (
Tcp,TcpFlags,TcpOption) - UDP (
Udp) - Raw payload data (
Raw)
PacketParser::new() includes bindings for Ethernet to IPv4/IPv6, IP to
ICMPv4/TCP/UDP, and TCP/UDP to raw payload data. Unknown protocols fall back to
Raw. Additional bindings can be registered with PacketParser::bind_layer,
allowing application protocols to participate in the same parsing pipeline.
I/O integrations
Packet parsing and construction work directly with byte slices and do not require a network interface.
See examples/read_write_pcap.rs for portable,
offline packet I/O. For live packet I/O, see
examples/spoof_http_server.rs, a minimal HTTP
server built with packet capture and Ethernet/IPv4/TCP/Raw packet injection.
Development
Run the same checks as CI inside the cached Docker environment:
List the available build, test, lint, and coverage commands with:
License
Licensed under either the MIT license or Apache License 2.0.