netpkt/ipv4.rs#
Source: lib/netpkt/ipv4.rs
ipv4 -- the IPv4 header (RFC 791), fragmentation and reassembly
Ipv4View::parse(b) version 4, IHL >= 5, total length within the buffer (trailing bytes,
e.g. Ethernet padding, are ignored by payload())
Ipv4Hdr { .. } write(out, payload_len) fills length and checksum; push_onto(&mut Pkt)
dec_ttl(hdr) -> u8 TTL - 1 in place with an incremental checksum update (RFC 1624)
fragment(pkt, mtu) splits a packet into fragments of at most `mtu` bytes (None when DF
is set and the packet is too large); options with the copy flag go
into every fragment
Reassembly collects fragments of one datagram (no timers: the caller expires
it after its own timeout)
IPV4_MIN: usize = 20
IPV4_MAX_OPTS: usize = 40
PROTO_ICMP: u8 = 1
PROTO_IGMP: u8 = 2
PROTO_TCP: u8 = 6
PROTO_UDP: u8 = 17
PROTO_IPV6: u8 = 41
PROTO_GRE: u8 = 47
PROTO_ESP: u8 = 50
PROTO_AH: u8 = 51
PROTO_ICMPV6: u8 = 58
PROTO_OSPF: u8 = 89
PROTO_VRRP: u8 = 112
fn proto_name(p: u8) -> Option<&'static str>a name for an IP protocol number ("TCP", "UDP", ...)struct Ipv4View<'a>a received IPv4 packet
impl<'a> Ipv4View<'a>
fn parse(b: &'a [u8]) -> Option<Ipv4View<'a>>fn parse_truncated(b: &'a [u8]) -> Option<Ipv4View<'a>>a packet that may be cut after its header (the quote inside an ICMP error): payload() then ends at the end of the bufferfn is_truncated(&self) -> boolfewer bytes than the total length are presentfn header_len(&self) -> usizefn dscp(&self) -> u8fn ecn(&self) -> u8fn total_len(&self) -> usizefn id(&self) -> u16fn df(&self) -> boolfn mf(&self) -> boolfn frag_off(&self) -> usizethe fragment offset in bytesfn is_fragment(&self) -> boolthis packet is a fragment (MF set or offset > 0)fn ttl(&self) -> u8fn proto(&self) -> u8fn checksum(&self) -> u16fn checksum_ok(&self) -> boolfn src(&self) -> Ipv4fn dst(&self) -> Ipv4fn options(&self) -> &'a [u8]fn header(&self) -> &'a [u8]fn payload(&self) -> &'a [u8]the payload up to the total lengthfn packet(&self) -> &'a [u8]header and payload, without trailing paddingstruct Ipv4Hdran IPv4 header to writedscp: u8ecn: u8id: u16df: boolmf: boolfrag_off: u16-- fragment offset in bytes (a multiple of 8)ttl: u8proto: u8src: Ipv4dst: Ipv4opts_len: u8opts: [u8; IPV4_MAX_OPTS]
impl Ipv4Hdr
fn new(src: Ipv4, dst: Ipv4, proto: u8, ttl: u8) -> Ipv4Hdrfn read(v: &Ipv4View) -> Ipv4Hdrfn set_options(&mut self, o: &[u8]) -> boolsets the options (padded with zeros to a multiple of 4; at most 40 bytes)fn len(&self) -> usizefn write(&self, out: &mut [u8], payload_len: usize)writes the header for a payload ofpayload_lenbytes, with total length and checksumfn push_onto(&self, p: &mut Pkt)prepends the header; the packet's current content is the payloadfn dec_ttl(hdr: &mut [u8]) -> u8decrements the TTL of a header in place and updates the checksum incrementally; returns the new TTL (0 stays 0)fn fragment(packet: &[u8], mtu: usize) -> Option<Vec<Vec<u8>>>splits an IPv4 packet into fragments of at mostmtubytes; the packet itself when it fits, None when it does not fit and DF is set (the sender then gets "fragmentation needed")struct Reassemblythe fragments of one datagram (key: source, destination, protocol, id)fn reassembly_key(v: &Ipv4View) -> (Ipv4, Ipv4, u8, u16)the reassembly key of a fragment
impl Reassembly
const MAX: usize = 65_535the largest datagram acceptedfn new() -> Reassemblyfn add(&mut self, v: &Ipv4View) -> Option<Vec<u8>>adds a fragment; Some(the complete packet) when all parts are there. A fragment that overlaps one already held (a duplicate too) is ignored.fn collected(&self) -> usizebytes collected so far