Expand description
Low-level packet access and construction.
The wire
module deals with the packet representation. It provides two levels
of functionality.
- First, it provides functions to extract fields from sequences of octets,
and to insert fields into sequences of octets. This happens
Packet
family of structures, e.g. EthernetFrame or Ipv4Packet. - Second, in cases where the space of valid field values is much smaller than the space
of possible field values, it provides a compact, high-level representation
of packet data that can be parsed from and emitted into a sequence of octets.
This happens through the
Repr
family of structs and enums, e.g. ArpRepr or Ipv4Repr.
The functions in the wire
module are designed for use together with -Cpanic=abort
.
The Packet
family of data structures guarantees that, if the Packet::check_len()
method
returned Ok(())
, then no accessor or setter method will panic; however, the guarantee
provided by Packet::check_len()
may no longer hold after changing certain fields,
which are listed in the documentation for the specific packet.
The Packet::new_checked
method is a shorthand for a combination of Packet::new_unchecked
and Packet::check_len
.
When parsing untrusted input, it is necessary to use Packet::new_checked()
;
so long as the buffer is not modified, no accessor will fail.
When emitting output, though, it is incorrect to use Packet::new_checked()
;
the length check is likely to succeed on a zeroed buffer, but fail on a buffer
filled with data from a previous packet, such as when reusing buffers, resulting
in nondeterministic panics with some network devices but not others.
The buffer length for emission is not calculated by the Packet
layer.
In the Repr
family of data structures, the Repr::parse()
method never panics
as long as Packet::new_checked()
(or Packet::check_len()
) has succeeded, and
the Repr::emit()
method never panics as long as the underlying buffer is exactly
Repr::buffer_len()
octets long.
§Examples
To emit an IP packet header into an octet buffer, and then parse it back:
use smoltcp::phy::ChecksumCapabilities;
use smoltcp::wire::*;
let repr = Ipv4Repr {
src_addr: Ipv4Address::new(10, 0, 0, 1),
dst_addr: Ipv4Address::new(10, 0, 0, 2),
next_header: IpProtocol::Tcp,
payload_len: 10,
hop_limit: 64,
};
let mut buffer = vec![0; repr.buffer_len() + repr.payload_len];
{ // emission
let mut packet = Ipv4Packet::new_unchecked(&mut buffer);
repr.emit(&mut packet, &ChecksumCapabilities::default());
}
{ // parsing
let packet = Ipv4Packet::new_checked(&buffer)
.expect("truncated packet");
let parsed = Ipv4Repr::parse(&packet, &ChecksumCapabilities::default())
.expect("malformed packet");
assert_eq!(repr, parsed);
}
Re-exports§
pub use self::pretty_print::PrettyPrinter;
Modules§
- pretty_
print - Pretty-printing of packet representation.
Structs§
- ArpPacket
- A read/write wrapper around an Address Resolution Protocol packet buffer.
- Dhcp
Flags - Dhcp
Option - A representation of a single DHCP option.
- Dhcp
Option Writer - A buffer for DHCP options.
- Dhcp
Packet - A read/write wrapper around a Dynamic Host Configuration Protocol packet buffer.
- Dhcp
Repr - A high-level representation of a Dynamic Host Configuration Protocol packet.
- Error
- Parsing a packet failed.
- Ethernet
Address - A six-octet Ethernet II address.
- Ethernet
Frame - A read/write wrapper around an Ethernet II frame buffer.
- Ethernet
Repr - A high-level representation of an Internet Protocol version 4 packet header.
- Icmpv4
Packet - A read/write wrapper around an Internet Control Message Protocol version 4 packet buffer.
- Icmpv6
Packet - A read/write wrapper around an Internet Control Message Protocol version 6 packet buffer.
- Igmp
Packet - A read/write wrapper around an Internet Group Management Protocol v1/v2 packet buffer.
- IpEndpoint
- An internet endpoint address.
- IpListen
Endpoint - An internet endpoint address for listening.
- Ipv4
Address - An IPv4 address.
- Ipv4
Cidr - A specification of an IPv4 CIDR block, containing an address and a variable-length subnet masking prefix length.
- Ipv4
Frag Key - Ipv4
Packet - A read/write wrapper around an Internet Protocol version 4 packet buffer.
- Ipv4
Repr - A high-level representation of an Internet Protocol version 4 packet header.
- Ipv6
Address - An IPv6 address.
- Ipv6
Cidr - A specification of an IPv6 CIDR block, containing an address and a variable-length subnet masking prefix length.
- Ipv6
ExtHeader - A read/write wrapper around an IPv6 Extension Header buffer.
- Ipv6
ExtHeader Repr - Ipv6
Fragment Header - A read/write wrapper around an IPv6 Fragment Header.
- Ipv6
Fragment Repr - A high-level representation of an IPv6 Fragment header.
- Ipv6
HopBy HopHeader - A read/write wrapper around an IPv6 Hop-by-Hop Header buffer.
- Ipv6
HopBy HopRepr - A high-level representation of an IPv6 Hop-by-Hop Header.
- Ipv6
Option - A read/write wrapper around an IPv6 Extension Header Option.
- Ipv6
Options Iterator - A iterator for IPv6 options.
- Ipv6
Packet - A read/write wrapper around an Internet Protocol version 6 packet buffer.
- Ipv6
Repr - A high-level representation of an Internet Protocol version 6 packet header.
- Ipv6
Routing Header - A read/write wrapper around an IPv6 Routing Header buffer.
- MldAddress
Record - A read/write wrapper around an MLDv2 Listener Report Message Address Record.
- MldAddress
Record Repr - A high level representation of an MLDv2 Listener Report Message Address Record.
- Ndisc
Neighbor Flags - Ndisc
Option - A read/write wrapper around an NDISC Option.
- Ndisc
Prefix Info Flags - Ndisc
Prefix Information - Ndisc
Redirected Header - Ndisc
Router Flags - RawHardware
Address - Unparsed hardware address.
- TcpPacket
- A read/write wrapper around a Transmission Control Protocol packet buffer.
- TcpRepr
- A high-level representation of a Transmission Control Protocol packet.
- TcpSeq
Number - A TCP sequence number.
- TcpTimestamp
Repr - UdpPacket
- A read/write wrapper around an User Datagram Protocol packet buffer.
- UdpRepr
- A high-level representation of an User Datagram Protocol packet.
Enums§
- ArpHardware
- ARP hardware type.
- ArpOperation
- ARP operation type.
- ArpRepr
- A high-level representation of an Address Resolution Protocol packet.
- Dhcp
Message Type - The possible message types of a DHCP packet.
- Dhcp
OpCode - The possible opcodes of a DHCP packet.
- Ethernet
Protocol - Ethernet protocol type.
- Hardware
Address - Representation of an hardware address, such as an Ethernet address or an IEEE802.15.4 address.
- Icmp
Repr - Icmpv4
DstUnreachable - Internet protocol control message subtype for type “Destination Unreachable”.
- Icmpv4
Message - Internet protocol control message type.
- Icmpv4
Param Problem - Internet protocol control message subtype for type “Parameter Problem”.
- Icmpv4
Redirect - Internet protocol control message subtype for type “Redirect Message”.
- Icmpv4
Repr - A high-level representation of an Internet Control Message Protocol version 4 packet header.
- Icmpv4
Time Exceeded - Internet protocol control message subtype for type “Time Exceeded”.
- Icmpv6
DstUnreachable - Internet protocol control message subtype for type “Destination Unreachable”.
- Icmpv6
Message - Internet protocol control message type.
- Icmpv6
Param Problem - Internet protocol control message subtype for the type “Parameter Problem”.
- Icmpv6
Repr - A high-level representation of an Internet Control Message Protocol version 6 packet header.
- Icmpv6
Time Exceeded - Internet protocol control message subtype for the type “Time Exceeded”.
- Igmp
Repr - A high-level representation of an Internet Group Management Protocol v1/v2 header.
- Igmp
Version - Type of IGMP membership report version
- IpAddress
- An internetworking address.
- IpCidr
- A specification of a CIDR block, containing an address and a variable-length subnet masking prefix length.
- IpProtocol
- IP datagram encapsulated protocol.
- IpRepr
- An IP packet representation.
- IpVersion
- Internet protocol version.
- Ipv6
Option Failure Type - Action required when parsing the given IPv6 Extension Header Option Type fails
- Ipv6
Option Repr - A high-level representation of an IPv6 Extension Header Option.
- Ipv6
Option Router Alert - A high-level representation of an IPv6 Router Alert Header Option.
- Ipv6
Option Type - IPv6 Extension Header Option Type
- Ipv6
Routing Repr - A high-level representation of an IPv6 Routing Header.
- Ipv6
Routing Type - IPv6 Extension Routing Header Routing Type
- MldRecord
Type - MLDv2 Multicast Listener Report Record Type. See RFC 3810 § 5.2.12 for more details.
- MldRepr
- A high-level representation of an MLDv2 packet header.
- Ndisc
Option Repr - A high-level representation of an NDISC Option.
- Ndisc
Option Type - NDISC Option Type
- Ndisc
Repr - A high-level representation of an Neighbor Discovery packet header.
- TcpControl
- The possible control flags of a Transmission Control Protocol packet.
- TcpOption
- A representation of a single TCP option.
Constants§
- DHCP_
CLIENT_ PORT - DHCP_
MAX_ DNS_ SERVER_ COUNT - DHCP_
SERVER_ PORT - ETHERNET_
HEADER_ LEN - The Ethernet header length
- IPV4_
HEADER_ LEN - IPV4_
MIN_ MTU - Minimum MTU required of all links supporting IPv4. See RFC 791 § 3.1.
- IPV4_
MULTICAST_ ALL_ ROUTERS - All multicast-capable routers
- IPV4_
MULTICAST_ ALL_ SYSTEMS - All multicast-capable nodes
- IPV6_
HEADER_ LEN - Length of an IPv6 header.
- IPV6_
LINK_ LOCAL_ ALL_ MLDV2_ ROUTERS - The link-local all MLVDv2-capable routers multicast address.
- IPV6_
LINK_ LOCAL_ ALL_ NODES - The link-local all nodes multicast address.
- IPV6_
LINK_ LOCAL_ ALL_ ROUTERS - The link-local all routers multicast address.
- IPV6_
LINK_ LOCAL_ ALL_ RPL_ NODES - The link-local all RPL nodes multicast address.
- IPV6_
MIN_ MTU - Minimum MTU required of all links supporting IPv6. See RFC 8200 § 5.
- MAX_
HARDWARE_ ADDRESS_ LEN - TCP_
HEADER_ LEN - UDP_
HEADER_ LEN