JFLib

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 buffer

  • fn is_truncated(&self) -> bool fewer bytes than the total length are present

  • fn header_len(&self) -> usize

  • fn dscp(&self) -> u8

  • fn ecn(&self) -> u8

  • fn total_len(&self) -> usize

  • fn id(&self) -> u16

  • fn df(&self) -> bool

  • fn mf(&self) -> bool

  • fn frag_off(&self) -> usize the fragment offset in bytes

  • fn is_fragment(&self) -> bool this packet is a fragment (MF set or offset > 0)

  • fn ttl(&self) -> u8

  • fn proto(&self) -> u8

  • fn checksum(&self) -> u16

  • fn checksum_ok(&self) -> bool

  • fn src(&self) -> Ipv4

  • fn dst(&self) -> Ipv4

  • fn options(&self) -> &'a [u8]

  • fn header(&self) -> &'a [u8]

  • fn payload(&self) -> &'a [u8] the payload up to the total length

  • fn packet(&self) -> &'a [u8] header and payload, without trailing padding

  • struct Ipv4Hdr an IPv4 header to write

    • dscp: u8
    • ecn: u8
    • id: u16
    • df: bool
    • mf: bool
    • frag_off: u16 -- fragment offset in bytes (a multiple of 8)
    • ttl: u8
    • proto: u8
    • src: Ipv4
    • dst: Ipv4
    • opts_len: u8
    • opts: [u8; IPV4_MAX_OPTS]

impl Ipv4Hdr

  • fn new(src: Ipv4, dst: Ipv4, proto: u8, ttl: u8) -> Ipv4Hdr

  • fn read(v: &Ipv4View) -> Ipv4Hdr

  • fn set_options(&mut self, o: &[u8]) -> bool sets the options (padded with zeros to a multiple of 4; at most 40 bytes)

  • fn len(&self) -> usize

  • fn write(&self, out: &mut [u8], payload_len: usize) writes the header for a payload of payload_len bytes, with total length and checksum

  • fn push_onto(&self, p: &mut Pkt) prepends the header; the packet's current content is the payload

  • fn dec_ttl(hdr: &mut [u8]) -> u8 decrements 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 most mtu bytes; the packet itself when it fits, None when it does not fit and DF is set (the sender then gets "fragmentation needed")

  • struct Reassembly the 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_535 the largest datagram accepted
  • fn new() -> Reassembly
  • fn 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) -> usize bytes collected so far

Source: docs/API.md in the JFLib repository.