Skip to main content

Crate crafter

Crate crafter 

Source
Expand description

Public crate for packet-level network interaction.

crafter is the only public crate. It exposes a self-contained API for examples, generated tools, and agent-directed packet workflows:

  • packet construction, decode, checksums, and protocol layers
  • classic pcap file input/output and libpcap-backed wire adapters
  • interface, send, send/receive, batch, and address helpers
  • PacketWire source/writer backends, PacketRecord metadata, Sniffer/Transmitter pipelines, and packet transforms
  • Ethernet, IEEE 802.11/radiotap, LLC/SNAP, IP, IGMP, transport, DHCPv4, Dhcpv6, and selected application protocol primitives

Public modules are organized as crafter::core, crafter::net, crafter::wire, and crafter::prelude. The wire module is the packet-stream API that generated tools should use for backend-neutral capture, transforms, pcap file I/O, and transmission. Most examples should start with use crafter::prelude::*;.

Local examples use dry-run send plans or offline pcaps unless live behavior is explicitly requested.

use crafter::prelude::*;
use std::net::Ipv4Addr;

let packet = Ipv4::new()
    .src(Ipv4Addr::new(192, 0, 2, 10))
    .dst(Ipv4Addr::new(198, 51, 100, 20))
    / Icmpv4::echo_request().id(0x4242).seq(1)
    / Raw::from("hello");

let compiled = packet.compile()?;
println!("{}", packet.summary());
println!("{}", compiled.hexdump());

Wi-Fi stacks use the same offline packet model. IP over IEEE 802.11 is represented with explicit LLC/SNAP, and the example below only compiles, decodes, and inspects synthetic bytes.

use crafter::prelude::*;
use std::net::Ipv4Addr;

let sta = MacAddr::new([0x00, 0x00, 0x5e, 0x00, 0x53, 0x01]);
let ap = MacAddr::new([0x00, 0x00, 0x5e, 0x00, 0x53, 0x02]);
let dot11 = Dot11::data();
let to_ds = dot11.frame_control_value().with_to_ds(true);

let packet = Radiotap::new()
    / dot11
        .frame_control(to_ds)
        .addr1(ap)
        .addr2(sta)
        .addr3(ap)
        .sequence_number(1)
    / LlcSnap::new().ethertype(ETHERTYPE_IPV4)
    / Ipv4::new()
        .src(Ipv4Addr::new(192, 0, 2, 10))
        .dst(Ipv4Addr::new(198, 51, 100, 20))
    / Icmpv4::echo_request().id(0x4242).seq(1)
    / Raw::from("hello");

let compiled = packet.compile()?;
let decoded = Packet::decode_from_link(LinkType::Radiotap, compiled.as_bytes())?;
assert!(decoded.layer::<Radiotap>().is_some());
assert!(decoded.layer::<Dot11>().is_some());
assert!(decoded.layer::<LlcSnap>().is_some());
assert!(decoded.layer::<Ipv4>().is_some());
let _show = decoded.show();
let _hexdump = compiled.hexdump();

Re-exports§

pub use error::CrafterError;
pub use error::Result;
pub use field::Field;
pub use field::FieldState;
pub use mac::MacAddr;
pub use packet::hexdump;
pub use packet::CompiledPacket;
pub use packet::IntoPacket;
pub use packet::Layer;
pub use packet::LayerContext;
pub use packet::LinkType;
pub use packet::NetworkLayer;
pub use packet::Packet;
pub use packet::Raw;
pub use packet::TransportChecksumContext;
pub use registry::EthertypeBindingContext;
pub use registry::Ipv4ProtocolBindingContext;
pub use registry::Ipv6NextHeaderBindingContext;
pub use registry::ProtocolRegistry;
pub use registry::TcpBindingContext;
pub use registry::UdpBindingContext;
pub use net::default_interface;
pub use net::default_interface_in;
pub use net::default_interface_name;
pub use net::find_interface;
pub use net::find_interface_in;
pub use net::get_ip_strings;
pub use net::get_ips;
pub use net::get_my_ip;
pub use net::get_my_ip_in;
pub use net::get_my_ipv6;
pub use net::get_my_ipv6_in;
pub use net::get_my_mac;
pub use net::get_my_mac_in;
pub use net::interface_for;
pub use net::interface_for_in;
pub use net::interfaces;
pub use net::parse_ip_range;
pub use net::parse_numbers;
pub use net::reply_filter;
pub use net::reply_matches;
pub use net::send_packet;
pub use net::send_packets;
pub use net::send_plan;
pub use net::send_recv_packet;
pub use net::send_recv_packets;
pub use net::BatchSend;
pub use net::BatchSendEntry;
pub use net::BatchSendRecv;
pub use net::BatchSendRecvEntry;
pub use net::BatchSendRecvReport;
pub use net::BatchSendReport;
pub use net::InterfaceAddress;
pub use net::InterfaceInfo;
pub use net::Ipv4Range;
pub use net::NetError;
pub use net::PacketBatchSendExt;
pub use net::PacketBatchSendRecvExt;
pub use net::PacketSendExt;
pub use net::PacketSendRecvExt;
pub use net::RawSender;
pub use net::ReplyMatcher;
pub use net::SendMode;
pub use net::SendOptions;
pub use net::SendPlan;
pub use net::SendRecv;
pub use net::SendRecvOptions;
pub use net::SendRecvReport;
pub use net::SendReport;
pub use net::SendTarget;
pub use net::SocketSend;
pub use net::SocketSender;
pub use wire::Sniffer;
pub use wire::derive_pmk;
pub use wire::derive_ptk;
pub use wire::BackendKind;
pub use wire::BluetoothMetadata;
pub use wire::Dot11Metadata;
pub use wire::Dot15d4Metadata;
pub use wire::DropAllTransform;
pub use wire::DuplicateTransform;
pub use wire::IpDefrag;
pub use wire::IpDefragConfig;
pub use wire::IpDefragEvictionReason;
pub use wire::IpDefragMetadata;
pub use wire::IpDefragOverlapPolicy;
pub use wire::IpDefragOverlapStatus;
pub use wire::IpDefragStats;
pub use wire::IpFragment;
pub use wire::IpFragmentConfig;
pub use wire::IpFragmentFamily;
pub use wire::IpFragmentMetadata;
pub use wire::IpFragmentRange;
pub use wire::IpFragmentReason;
pub use wire::IpFragmentStats;
pub use wire::Ipv4DontFragmentPolicy;
pub use wire::Ipv4FragmentIdentificationPolicy;
pub use wire::Ipv6AtomicFragmentPolicy;
pub use wire::Ipv6FragmentIdentificationPolicy;
pub use wire::MediumMetadata;
pub use wire::MemoryPacketWriter;
pub use wire::MemoryWrite;
pub use wire::OpenedPacketSource;
pub use wire::OpenedPacketWriter;
pub use wire::PacketMetadata;
pub use wire::PacketOrigin;
pub use wire::PacketRecord;
pub use wire::PacketSource;
pub use wire::PacketTransform;
pub use wire::PacketWire;
pub use wire::PacketWireBuilder;
pub use wire::PacketWireTarget;
pub use wire::PacketWriter;
pub use wire::PairwiseTransientKey;
pub use wire::PassThroughTransform;
pub use wire::Pmk;
pub use wire::RadioMetadata;
pub use wire::RawSocketWireBuilder;
pub use wire::RawSocketWriter;
pub use wire::SnifferCancel;
pub use wire::SnifferHandle;
pub use wire::TraceAppendTransform;
pub use wire::TransformOutput;
pub use wire::TransformTrace;
pub use wire::Transmitter;
pub use wire::VecPacketSource;
pub use wire::WifiDecryptState;
pub use wire::WifiMetadata;
pub use wire::WifiProtectionStatus;
pub use wire::WireError;
pub use wire::WpaAkm;
pub use wire::WpaCipher;
pub use wire::WpaCredentialStatus;
pub use wire::WpaDecrypt;
pub use wire::WpaDecryptConfig;
pub use wire::WpaDecryptReason;
pub use wire::WpaHandshakeStatus;
pub use wire::WpaKeyKind;
pub use wire::WpaMetadata;
pub use wire::WpaNetwork;
pub use wire::WriteReport;
pub use wire::IP_DEFRAG_DEFAULT_MAX_AGE;
pub use wire::IP_DEFRAG_DEFAULT_MAX_BYTES_PER_DATAGRAM;
pub use wire::IP_DEFRAG_DEFAULT_MAX_DATAGRAMS;
pub use wire::IP_FRAGMENT_MIN_MTU;
pub use protocols::exports::*;

Modules§

checksum
Internet checksum helpers used by IP, ICMP, TCP, and UDP.
core
Core packet and protocol APIs.
endian
Network byte-order read and write helpers.
error
Shared error types for core packet parsing and encoding.
field
Field state tracking for protocol encoders.
mac
MAC address parsing and formatting.
net
Network interface, raw socket, routing, and send/receive helpers.
packet
Packet stack, raw payload layer, and protocol-neutral encode/decode plumbing.
prelude
Common imports for generated packet tools and examples.
protocols
Protocol layer implementations.
registry
Explicit protocol binding and decode dispatch.
wire
Packet-shaped wire I/O abstractions.