JFLib

netpkt/addr.rs#

Source: lib/netpkt/addr.rs

addr -- link and network addresses: MAC, IPv4, IPv6, prefixes

  Mac([u8; 6])        parse "aa:bb:cc:dd:ee:ff" / "aa-bb-.." / "aabb.ccdd.eeff"; Display with
                      colons, cisco() with dots; broadcast / multicast / local bits; EUI-64
  Ipv4([u8; 4])       strict dotted decimal (no leading zeros); masks <-> prefix lengths;
                      class tests (private, loopback, link local, multicast, broadcast)
  Ipv6([u8; 16])      every RFC 4291 text form in, RFC 5952 out; solicited-node and
                      link-local (EUI-64) addresses; multicast MAC mapping; multicast
                      scope, global unicast, the common prefix of two addresses
  IpAddr              V4 | V6
  Prefix4 / Prefix6   an address with a prefix length ("10.0.0.1/24"): network, broadcast,
                      contains, host range (/31 and /32 per RFC 3021); masks and wildcards
  wildcard_match      ACL matching with any (also non-contiguous) wildcard mask

These are value types for packets, not OS socket addresses (those are plat::net's).
  • struct Mac(pub [u8; 6]) an IEEE 802 MAC-48 address

impl Mac

  • const ZERO: Mac = Mac([0; 6])

  • const BROADCAST: Mac = Mac([0xFF; 6])

  • const STP: Mac = Mac([0x01, 0x80, 0xC2, 0, 0, 0]) the destination of IEEE 802.1D bridge PDUs (STP)

  • const fn new(a: u8, b: u8, c: u8, d: u8, e: u8, f: u8) -> Mac

  • fn is_broadcast(&self) -> bool

  • fn is_multicast(&self) -> bool group bit set (broadcast included)

  • fn is_unicast(&self) -> bool

  • fn is_local(&self) -> bool locally administered (not a vendor address)

  • fn is_zero(&self) -> bool

  • fn parse(s: &str) -> Option<Mac> "aa:bb:cc:dd:ee:ff", "aa-bb-cc-dd-ee-ff", "aabb.ccdd.eeff" (Cisco) or 12 hex digits

  • fn cisco(&self) -> String Cisco notation "aabb.ccdd.eeff"

  • fn of_ipv4_multicast(ip: Ipv4) -> Mac the multicast MAC of an IPv4 group (01:00:5e + low 23 bits, RFC 1112)

  • fn of_ipv6_multicast(ip: Ipv6) -> Mac the multicast MAC of an IPv6 group (33:33 + low 32 bits, RFC 2464)

  • fn eui64(&self) -> [u8; 8] the modified EUI-64 interface identifier (RFC 4291 appendix A)

  • fn to_u64(&self) -> u64 the address as a number (for ranges and counters)

  • fn from_u64(v: u64) -> Mac

  • struct Ipv4(pub [u8; 4]) an IPv4 address

impl Ipv4

  • const UNSPECIFIED: Ipv4 = Ipv4([0; 4])

  • const BROADCAST: Ipv4 = Ipv4([255; 4])

  • const LOCALHOST: Ipv4 = Ipv4([127, 0, 0, 1])

  • const fn new(a: u8, b: u8, c: u8, d: u8) -> Ipv4

  • const fn from_u32(v: u32) -> Ipv4

  • const fn to_u32(&self) -> u32

  • fn parse(s: &str) -> Option<Ipv4> strict dotted decimal: four parts 0..255, no leading zeros, nothing else

  • fn is_unspecified(&self) -> bool

  • fn is_loopback(&self) -> bool

  • fn is_private(&self) -> bool 10/8, 172.16/12, 192.168/16 (RFC 1918)

  • fn is_multicast(&self) -> bool 224/4

  • fn is_broadcast(&self) -> bool 255.255.255.255 (the limited broadcast)

  • fn mask_len(&self) -> Option<u8> the prefix length of a contiguous netmask ("255.255.255.0" -> 24), None otherwise

  • fn mask_of(len: u8) -> Ipv4 the netmask of a prefix length (0..=32)

  • fn to_ipv6_mapped(&self) -> Ipv6 IPv4-mapped IPv6 address ::ffff:a.b.c.d

  • fn wildcard_match(ip: Ipv4, addr: Ipv4, wildcard: Ipv4) -> bool ACL-style match: the bits that are 0 in wildcard must be equal (also non-contiguous)

  • struct Ipv6(pub [u8; 16]) an IPv6 address

impl Ipv6

  • const UNSPECIFIED: Ipv6 = Ipv6([0; 16])

  • const LOCALHOST: Ipv6 = Ipv6([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])

  • const ALL_NODES: Ipv6 = Ipv6([0xFF, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) ff02::1, all nodes on the link

  • const ALL_ROUTERS: Ipv6 = Ipv6([0xFF, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2]) ff02::2, all routers on the link

  • fn from_segments(s: [u16; 8]) -> Ipv6

  • fn segments(&self) -> [u16; 8]

  • fn to_u128(&self) -> u128

  • fn from_u128(v: u128) -> Ipv6

  • fn parse(s: &str) -> Option<Ipv6> every RFC 4291 text form: full, "::" compressed, with an embedded IPv4 tail

  • fn is_unspecified(&self) -> bool

  • fn is_loopback(&self) -> bool

  • fn is_multicast(&self) -> bool ff00::/8

  • fn is_unique_local(&self) -> bool fc00::/7

  • fn to_ipv4_mapped(&self) -> Option<Ipv4> ::ffff:a.b.c.d -> a.b.c.d

  • fn solicited_node(&self) -> Ipv6 the solicited-node multicast address ff02::1:ffXX:XXXX (RFC 4291 2.7.1)

  • fn eui64_in(prefix: Ipv6, mac: Mac) -> Ipv6 a /64 prefix plus the modified EUI-64 of a MAC (SLAAC, RFC 4862)

  • fn is_solicited_node(&self) -> bool ff02::1:ff00:0/104, a solicited-node multicast address

  • fn mc_scope(&self) -> u8 the scope of a multicast address (1 interface, 2 link, 5 site, 8 organisation, 14 global; RFC 4291 2.7)

  • fn is_global_unicast(&self) -> bool a global unicast address (not unspecified, loopback, link-local, multicast; unique local addresses count as global in scope, RFC 4193)

  • fn common_prefix(&self, other: &Ipv6) -> u8 the number of leading bits two addresses share (source address selection, RFC 6724)

  • enum IpAddr an IPv4 or IPv6 address

    • V4(Ipv4)
    • V6(Ipv6)

impl IpAddr

  • fn parse(s: &str) -> Option<IpAddr>

  • fn is_v4(&self) -> bool

  • fn v4(&self) -> Option<Ipv4>

  • fn v6(&self) -> Option<Ipv6>

  • fn is_unspecified(&self) -> bool

  • fn is_multicast(&self) -> bool

  • struct Prefix4 an IPv4 address with a prefix length; the address keeps its host bits ("10.0.0.1/24" is an interface address, network() is 10.0.0.0)

    • addr: Ipv4
    • len: u8

impl Prefix4

  • fn new(addr: Ipv4, len: u8) -> Option<Prefix4>

  • fn parse(s: &str) -> Option<Prefix4> "a.b.c.d/len" or "a.b.c.d" (= /32)

  • fn from_mask(addr: Ipv4, mask: Ipv4) -> Option<Prefix4> an address and a dotted netmask ("255.255.255.0"); None for a non-contiguous mask

  • fn from_wildcard(addr: Ipv4, wildcard: Ipv4) -> Option<Prefix4> an address and a contiguous wildcard mask ("0.0.0.255")

  • fn mask(&self) -> Ipv4

  • fn wildcard(&self) -> Ipv4

  • fn network(&self) -> Ipv4 the network address (host bits cleared)

  • fn trunc(&self) -> Prefix4 this prefix with the host bits cleared

  • fn broadcast(&self) -> Ipv4 the directed broadcast address (all host bits set)

  • fn contains(&self, ip: Ipv4) -> bool

  • fn covers(&self, other: &Prefix4) -> bool other lies completely inside this prefix

  • fn overlaps(&self, other: &Prefix4) -> bool two prefixes share addresses

  • fn is_reserved_host(&self, ip: Ipv4) -> bool the address is the network or broadcast address of a /0../30 prefix

  • fn host_count(&self) -> u64 usable host addresses (/31: both, RFC 3021; /32: one)

  • fn first_host(&self) -> Ipv4

  • fn last_host(&self) -> Ipv4

  • struct Prefix6 an IPv6 address with a prefix length

    • addr: Ipv6
    • len: u8

impl Prefix6

  • fn new(addr: Ipv6, len: u8) -> Option<Prefix6>
  • fn parse(s: &str) -> Option<Prefix6> "2001:db8::1/64" or an address alone (= /128)
  • fn network(&self) -> Ipv6
  • fn trunc(&self) -> Prefix6
  • fn contains(&self, ip: Ipv6) -> bool
  • fn covers(&self, other: &Prefix6) -> bool
  • fn overlaps(&self, other: &Prefix6) -> bool two prefixes share addresses

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