JFLib

netpkt/rip.rs#

Source: lib/netpkt/rip.rs

rip -- Routing Information Protocol messages (UDP port 520): version 2 after RFC 2453 and
version 1 after RFC 1058; requests and responses with their route entries, and the
authentication entry of RFC 2453 4.1 (recognised; simple passwords read and written)

  RipView::parse(b)          command, version, must-be-zero field, the complete 20-byte
                             entries (entry(i), entries(), routes() without the
                             authentication entry), auth(), trailing() bytes after them
  Rte { afi, tag, addr, mask, next_hop, metric }   one entry; Rte::v2 / Rte::v1 build route
                             entries, prefix() is the destination with a version 2 mask,
                             mbz_clear() checks the fields version 1 leaves zero
  Auth { kind, data }        the first entry with AFI 0xFFFF: AUTH_PASSWORD (16 bytes of
                             plain text: Auth::password, text()), AUTH_MD5 (RFC 4822: kind and
                             bytes only, not checked)
  Rip { command, version, auth, entries }   read / to_bytes / response / request_all /
                             is_request_all (one entry, address family 0, metric 16)
  CMD_REQUEST, CMD_RESPONSE, AFI_INET, AFI_AUTH, AUTH_*, INFINITY (16), MAX_ENTRIES (25),
  GROUP (224.0.0.9, RIPv2's multicast address), command_name

The view reads whatever is there: more than 25 entries, bytes after the last entry, a version
1 message with a must-be-zero field set or an unknown command are for the receiver to judge
(decode.rs reports them as problems).  Version 1 entries carry no mask; the receiver infers it
from the address class and its own interfaces (RFC 1058 3.7).
/// the header: command, version, two bytes that must be zero
RIP_HDR: usize = 4
/// one entry (route or authentication)
RTE_LEN: usize = 20
/// entries a message holds at most (RFC 2453 3.6); 24 routes with an authentication entry
MAX_ENTRIES: usize = 25
CMD_REQUEST: u8 = 1
CMD_RESPONSE: u8 = 2
/// the address family of IPv4 routes
AFI_INET: u16 = 2
/// the address family that marks the authentication entry (RFC 2453 4.1)
AFI_AUTH: u16 = 0xFFFF
/// a plain-text password of 16 bytes (RFC 2453 4.1)
AUTH_PASSWORD: u16 = 2
/// keyed MD5 (RFC 4822)
AUTH_MD5: u16 = 3
/// the metric of an unreachable destination
INFINITY: u32 = 16
/// where version 2 sends its updates (RFC 2453 4.5)
GROUP: Ipv4 = Ipv4([224, 0, 0, 9])
  • fn command_name(c: u8) -> &'static str the name of a command as capture tools show it

  • struct Rte one 20-byte entry; for the authentication entry tag holds the type and the 16 bytes from addr on hold the authentication data (Auth reads them)

    • afi: u16
    • tag: u16 -- the route tag (version 2; zero in version 1)
    • addr: Ipv4
    • mask: Ipv4 -- the subnet mask (version 2; zero: not given)
    • next_hop: Ipv4 -- where to send packets for the destination (version 2; 0.0.0.0: to the sender)
    • metric: u32

impl Rte

  • fn read(b: &[u8]) -> Rte an entry from its 20 bytes (the caller has checked the length)

  • fn write(&self, b: &mut [u8]) writes the entry into 20 bytes

  • fn v2(prefix: Prefix4, next_hop: Ipv4, metric: u32, tag: u16) -> Rte a version 2 route: the destination with its mask, the next hop (0.0.0.0: the sender itself), the metric and the route tag

  • fn v1(addr: Ipv4, metric: u32) -> Rte a version 1 route: the address alone

  • fn prefix(&self) -> Option<Prefix4> the destination with its version 2 mask (None: no mask, or one with gaps)

  • fn mbz_clear(&self) -> bool the fields version 1 leaves zero are zero (route tag, mask, next hop)

  • struct Auth the authentication entry of a version 2 message

    • kind: u16
    • data: [u8; 16]

impl Auth

  • fn password(pw: &[u8]) -> Auth a plain-text password: left-justified, padded with zero bytes, cut at 16 bytes

  • fn text(&self) -> Option<String> the password of a plain-text entry (up to the first zero byte)

  • struct RipView<'a> a received RIP message

impl<'a> RipView<'a>

  • fn parse(b: &'a [u8]) -> Option<RipView<'a>> at least the header

  • fn command(&self) -> u8

  • fn version(&self) -> u8

  • fn mbz(&self) -> u16 the two header bytes that must be zero

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

  • fn count(&self) -> usize complete entries

  • fn trailing(&self) -> usize bytes after the last complete entry

  • fn entry(&self, i: usize) -> Option<Rte> entry i (authentication entries included)

  • fn entries(&self) -> impl Iterator<Item = Rte> + '_

  • fn auth(&self) -> Option<Auth> the authentication entry: only the first entry may be one (RFC 2453 4.1)

  • fn routes(&self) -> impl Iterator<Item = Rte> + '_ the entries after a leading authentication entry

  • struct Rip a RIP message to write (or one read)

    • command: u8
    • version: u8
    • auth: Option<Auth> -- written as the first entry
    • entries: Vec<Rte>

impl Rip

  • fn response(version: u8, entries: Vec<Rte>) -> Rip
  • fn request_all(version: u8) -> Rip the request for the whole routing table: a single entry with address family 0 and metric 16 (RFC 2453 3.9.1)
  • fn is_request_all(&self) -> bool this is the request for the whole table
  • fn read(v: &RipView) -> Rip
  • fn to_bytes(&self) -> Vec<u8> the message as bytes (no limit on the number of entries: the sender splits at 25)

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