Module smoltcp::wire

source ·
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),
    protocol:    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-printing of packet representation.

Structs

A read/write wrapper around an Address Resolution Protocol packet buffer.
A six-octet Ethernet II address.
A read/write wrapper around an Ethernet II frame buffer.
A high-level representation of an Internet Protocol version 4 packet header.
A read/write wrapper around an Internet Control Message Protocol version 4 packet buffer.
A read/write wrapper around an Internet Control Message Protocol version 6 packet buffer.
A read/write wrapper around an Internet Group Management Protocol v1/v2 packet buffer.
An internet endpoint address.
A four-octet IPv4 address.
A specification of an IPv4 CIDR block, containing an address and a variable-length subnet masking prefix length.
A read/write wrapper around an Internet Protocol version 4 packet buffer.
A high-level representation of an Internet Protocol version 4 packet header.
A sixteen-octet IPv6 address.
A specification of an IPv6 CIDR block, containing an address and a variable-length subnet masking prefix length.
A read/write wrapper around an IPv6 Fragment Header.
A high-level representation of an IPv6 Fragment header.
A read/write wrapper around an IPv6 Hop-by-Hop Options Header.
A high-level representation of an IPv6 Hop-by-Hop Options header.
A read/write wrapper around an IPv6 Extension Header Option.
A read/write wrapper around an Internet Protocol version 6 packet buffer.
A high-level representation of an Internet Protocol version 6 packet header.
A read/write wrapper around an IPv6 Routing Header buffer.
A read/write wrapper around an MLDv2 Listener Report Message Address Record.
A read/write wrapper around an NDISC Option.
A read/write wrapper around a Transmission Control Protocol packet buffer.
A high-level representation of a Transmission Control Protocol packet.
A TCP sequence number.
A read/write wrapper around an User Datagram Protocol packet buffer.
A high-level representation of an User Datagram Protocol packet.

Enums

ARP hardware type.
ARP operation type.
A high-level representation of an Address Resolution Protocol packet.
Ethernet protocol type.
Internet protocol control message subtype for type “Destination Unreachable”.
Internet protocol control message type.
Internet protocol control message subtype for type “Parameter Problem”.
Internet protocol control message subtype for type “Redirect Message”.
A high-level representation of an Internet Control Message Protocol version 4 packet header.
Internet protocol control message subtype for type “Time Exceeded”.
Internet protocol control message subtype for type “Destination Unreachable”.
Internet protocol control message type.
Internet protocol control message subtype for the type “Parameter Problem”.
A high-level representation of an Internet Control Message Protocol version 6 packet header.
Internet protocol control message subtype for the type “Time Exceeded”.
A high-level representation of an Internet Group Management Protocol v1/v2 header.
Type of IGMP membership report version
An internetworking address.
A specification of a CIDR block, containing an address and a variable-length subnet masking prefix length.
IP datagram encapsulated protocol.
An IP packet representation.
Internet protocol version.
Action required when parsing the given IPv6 Extension Header Option Type fails
A high-level representation of an IPv6 Extension Header Option.
IPv6 Extension Header Option Type
A high-level representation of an IPv6 Routing Header.
A high-level representation of an MLDv2 packet header.
A high-level representation of an NDISC Option.
NDISC Option Type
A high-level representation of an Neighbor Discovery packet header.
The possible control flags of a Transmission Control Protocol packet.
A representation of a single TCP option.

Constants

Minimum MTU required of all links supporting IPv4. See RFC 791 § 3.1.
Minimum MTU required of all links supporting IPv6. See RFC 8200 § 5.