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) -> Macfn is_broadcast(&self) -> boolfn is_multicast(&self) -> boolgroup bit set (broadcast included)fn is_unicast(&self) -> boolfn is_local(&self) -> boollocally administered (not a vendor address)fn is_zero(&self) -> boolfn parse(s: &str) -> Option<Mac>"aa:bb:cc:dd:ee:ff", "aa-bb-cc-dd-ee-ff", "aabb.ccdd.eeff" (Cisco) or 12 hex digitsfn cisco(&self) -> StringCisco notation "aabb.ccdd.eeff"fn of_ipv4_multicast(ip: Ipv4) -> Macthe multicast MAC of an IPv4 group (01:00:5e + low 23 bits, RFC 1112)fn of_ipv6_multicast(ip: Ipv6) -> Macthe 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) -> u64the address as a number (for ranges and counters)fn from_u64(v: u64) -> Macstruct 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) -> Ipv4const fn from_u32(v: u32) -> Ipv4const fn to_u32(&self) -> u32fn parse(s: &str) -> Option<Ipv4>strict dotted decimal: four parts 0..255, no leading zeros, nothing elsefn is_unspecified(&self) -> boolfn is_loopback(&self) -> boolfn is_private(&self) -> bool10/8, 172.16/12, 192.168/16 (RFC 1918)fn is_link_local(&self) -> bool169.254/16fn is_multicast(&self) -> bool224/4fn is_broadcast(&self) -> bool255.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 otherwisefn mask_of(len: u8) -> Ipv4the netmask of a prefix length (0..=32)fn to_ipv6_mapped(&self) -> Ipv6IPv4-mapped IPv6 address ::ffff:a.b.c.dfn wildcard_match(ip: Ipv4, addr: Ipv4, wildcard: Ipv4) -> boolACL-style match: the bits that are 0 inwildcardmust 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 linkconst 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 linkfn from_segments(s: [u16; 8]) -> Ipv6fn segments(&self) -> [u16; 8]fn to_u128(&self) -> u128fn from_u128(v: u128) -> Ipv6fn parse(s: &str) -> Option<Ipv6>every RFC 4291 text form: full, "::" compressed, with an embedded IPv4 tailfn is_unspecified(&self) -> boolfn is_loopback(&self) -> boolfn is_multicast(&self) -> boolff00::/8fn is_link_local(&self) -> boolfe80::/10fn is_unique_local(&self) -> boolfc00::/7fn to_ipv4_mapped(&self) -> Option<Ipv4>::ffff:a.b.c.d -> a.b.c.dfn solicited_node(&self) -> Ipv6the solicited-node multicast address ff02::1:ffXX:XXXX (RFC 4291 2.7.1)fn link_local_of(mac: Mac) -> Ipv6fe80::/64 with the modified EUI-64 of a MACfn eui64_in(prefix: Ipv6, mac: Mac) -> Ipv6a /64 prefix plus the modified EUI-64 of a MAC (SLAAC, RFC 4862)fn is_solicited_node(&self) -> boolff02::1:ff00:0/104, a solicited-node multicast addressfn mc_scope(&self) -> u8the scope of a multicast address (1 interface, 2 link, 5 site, 8 organisation, 14 global; RFC 4291 2.7)fn is_global_unicast(&self) -> boola 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) -> u8the number of leading bits two addresses share (source address selection, RFC 6724)enum IpAddran IPv4 or IPv6 addressV4(Ipv4)V6(Ipv6)
impl IpAddr
fn parse(s: &str) -> Option<IpAddr>fn is_v4(&self) -> boolfn v4(&self) -> Option<Ipv4>fn v6(&self) -> Option<Ipv6>fn is_unspecified(&self) -> boolfn is_multicast(&self) -> boolstruct Prefix4an 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: Ipv4len: 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 maskfn from_wildcard(addr: Ipv4, wildcard: Ipv4) -> Option<Prefix4>an address and a contiguous wildcard mask ("0.0.0.255")fn mask(&self) -> Ipv4fn wildcard(&self) -> Ipv4fn network(&self) -> Ipv4the network address (host bits cleared)fn trunc(&self) -> Prefix4this prefix with the host bits clearedfn broadcast(&self) -> Ipv4the directed broadcast address (all host bits set)fn contains(&self, ip: Ipv4) -> boolfn covers(&self, other: &Prefix4) -> boolotherlies completely inside this prefixfn overlaps(&self, other: &Prefix4) -> booltwo prefixes share addressesfn is_reserved_host(&self, ip: Ipv4) -> boolthe address is the network or broadcast address of a /0../30 prefixfn host_count(&self) -> u64usable host addresses (/31: both, RFC 3021; /32: one)fn first_host(&self) -> Ipv4fn last_host(&self) -> Ipv4struct Prefix6an IPv6 address with a prefix lengthaddr: Ipv6len: 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) -> Ipv6fn trunc(&self) -> Prefix6fn contains(&self, ip: Ipv6) -> boolfn covers(&self, other: &Prefix6) -> boolfn overlaps(&self, other: &Prefix6) -> booltwo prefixes share addresses