JFLib

netpkt/addrsel.rs#

Source: lib/netpkt/addrsel.rs

addrsel -- destination address selection after RFC 6724: the scope of an address, the
default policy table (precedence and label) and the order in which the addresses a name
resolves to are tried (what getaddrinfo sorts), given the source address each one would use

  scope(a) -> u8            RFC 6724 3.1 and 3.2: the scope of an IPv6 multicast address,
                            2 (link-local) for fe80::/10 and ::1, 5 for the old site-local
                            fec0::/10, 14 (global) for the other unicast addresses (unique
                            local ones included); IPv4: 127/8 and 169.254/16 link-local, the
                            rest global (private addresses too)
  precedence(a) / label(a)  the default policy table of RFC 6724 2.1 (an IPv4 address as
                            ::ffff:a.b.c.d)
  Dest { addr, src, src_len, src_deprecated }   a destination and what source address
                            selection gave it (src None: unusable, no route or no source)
  compare(a, b) / sort(&mut [Dest])   the rules of RFC 6724 6: 1 (unusable last), 2 (matching
                            scope), 3 (no deprecated source), 5 (matching label), 6 (higher
                            precedence), 8 (smaller scope), 9 (longest matching prefix, as
                            glibc only between two IPv6 destinations), 10 (else the order as
                            given: the sort is stable)

Rules 4 (home addresses) and 7 (native transport) decide nothing without Mobile IPv6 and
tunnels and are left out.  The default table makes 2001:db8::/32 and other global IPv6
destinations (precedence 40) come before IPv4 ones (35), and IPv4 before unique local IPv6
addresses (3): a host with a global IPv6 address tries IPv6 first, one with only a link-local
IPv6 address IPv4 first (rule 2).
/// RFC 6724 3.1: link-local scope (fe80::/10, ::1, IPv4 127/8 and 169.254/16)
SCOPE_LINK: u8 = 2
/// the deprecated site-local fec0::/10
SCOPE_SITE: u8 = 5
/// global scope
SCOPE_GLOBAL: u8 = 14
  • fn precedence(a: IpAddr) -> u8 the precedence of the default policy table (higher is preferred)

  • fn label(a: IpAddr) -> u8 the label of the default policy table (a source and a destination with the same label belong together)

  • fn scope(a: IpAddr) -> u8 the scope of an address (RFC 6724 3.1, 3.2)

  • struct Dest a destination to sort and the source address it would leave with

    • addr: IpAddr
    • src: Option<IpAddr> -- the source address selection gave it; None: it cannot be reached (no route, no source)
    • src_len: u8 -- the prefix length of the source (the part before its interface identifier: rule 9 counts common bits up to it)
    • src_deprecated: bool -- the source is deprecated (its preferred lifetime ended)

impl Dest

  • fn new(addr: IpAddr, src: Option<IpAddr>) -> Dest

  • fn compare(a: &Dest, b: &Dest) -> Ordering which of two destinations comes first (Less: a); Equal leaves their order

  • fn sort(d: &mut [Dest]) sorts destinations by RFC 6724 (stable: rule 10 keeps the given order)

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