/// Whether an IP address is on the public internet: what `fetch` checks /// every address of a host against before it connects (ADR 0079), so a /// URL cannot reach the machine itself, its network or its cloud's /// metadata service (server-side request forgery). /// /// address.classify "8.8.8.8" // Ok(Public) /// address.classify "169.254.169.254" // Ok(Private("link-local")) /// address.classify "::ffff:10.0.0.1" // Ok(Private("IPv4-mapped: private")) /// address.classify "example.com" // Error("not an IP address") /// /// Refused in IPv4: `0.0.0.0/8`, `10.0.0.0/8`, `127.0.0.0/8`, /// `100.64.0.0/10`, `169.254.0.0/16`, `172.16.0.0/12`, `192.168.0.0/16` /// and everything from `224.0.0.0` up (multicast, reserved, broadcast). /// In IPv6: `::`, `::1`, `fe80::/10`, `fec0::/10`, `fc00::/7`, /// `ff00::/8` and the rest of `::/96` (IPv4-compatible). An IPv4-mapped /// address (`::ffff:a.b.c.d`) and a NAT64 one (`64:ff9b::/96`) are judged /// by the IPv4 address they hold. /// /// Only the text `net.resolve` gives is read: dotted decimal without /// leading zeros, and IPv6 without a zone (`%eth0`). Anything else is an /// `Error`, which `fetch` refuses too. import std/int import std/list import std/result import std/string /// Where an address is. @derive Eq Show Debug pub type Reach { Public /// Not on the public internet; `reason` names the range. , Private(reason: String) } /// Where `ip` is, or why it is not an IP address. pub fn classify :: ip: String -> Result { if string.contains ip ":" { ipv6 ip |> result.map v6_reach } else { ipv4 ip |> result.map v4_reach } } /// Whether `ip` is an IP address on the public internet. pub fn is_public :: ip: String -> Bool { classify ip == Ok(Public) } // ---- IPv4 ---------------------------------------------------------------------- /// The four bytes of a dotted-decimal address. fn ipv4 :: text: String -> Result, String> { match string.split text "." { [a, b, c, d] -> result.all (list.map [a, b, c, d] octet) , _ -> Error("not an IP address: {text}") } } fn octet :: text: String -> Result { let digits = string.byte_length text let leading_zero = digits > 1 && string.starts_with text "0" match string.to_int text { Ok ( n ) if digits <= 3 && !leading_zero && n >= 0 && n <= 255 && all_digits text -> Ok(n) , _ -> Error("not an IPv4 byte: {text}") } } fn all_digits :: text: String -> Bool { list.all (string.chars text) fn c { string.contains "0123456789" c } } fn v4_reach :: bytes: List -> Reach { match bytes { [0, _, _, _] -> Private("this network") , [10, _, _, _] -> Private("private") , [127, _, _, _] -> Private("loopback") , [169, 254, _, _] -> Private("link-local") , [192, 168, _, _] -> Private("private") , [100, b, _, _] if b >= 64 && b <= 127 -> Private("shared (carrier-grade NAT)") , [172, b, _, _] if b >= 16 && b <= 31 -> Private("private") , [a, _, _, _] if a >= 224 -> Private("multicast or reserved") , _ -> Public } } // ---- IPv6 ---------------------------------------------------------------------- /// The eight 16-bit groups of an IPv6 address. fn ipv6 :: text: String -> Result, String> { match string.split text "::" { [whole] -> groups whole True |> result.try fn g { exactly 8 g text } , [left, right] -> { use l <- result.try (groups left False) use r <- result.try (groups right True) let missing = 8 - list.length l - list.length r if missing < 1 { Error("not an IPv6 address: {text}") } else { Ok( list.append l (list.append (list.map (list.range 1 missing) fn _ { 0 }) r) ) } } , _ -> Error("not an IPv6 address: {text}") } } fn exactly :: n: Int g: List text: String -> Result, String> { if list.length g == n { Ok(g) } else { Error("not an IPv6 address: {text}") } } /// The groups of one side of `::`. At the end of the address (`at_end`), /// the last may be an IPv4 address: two groups. fn groups :: text: String at_end: Bool -> Result, String> { if text == "" { Ok([]) } else { groups_of (string.split text ":") at_end } } fn groups_of :: parts: List at_end: Bool -> Result, String> { match parts { [] -> Ok([]) , [last] if at_end && string.contains last "." -> { use b <- result.try (ipv4 last) match b { [a, b2, c, d] -> Ok([a * 256 + b2, c * 256 + d]) , _ -> Error("not an IPv4 address: {last}") } } , [part, ..rest] -> { use g <- result.try (hex_group part) use more <- result.try (groups_of rest at_end) Ok([g, ..more]) } } } fn hex_group :: text: String -> Result { let digits = string.chars (string.to_lower text) if digits == [] || list.length digits > 4 { Error("not an IPv6 group: {text}") } else { list.fold digits Ok(0) fn acc d { use n <- result.try acc match string.find "0123456789abcdef" d { Some(v) -> Ok(n * 16 + v) , None -> Error("not an IPv6 group: {text}") } } } } fn v6_reach :: g: List -> Reach { match g { [0, 0, 0, 0, 0, 0, 0, 0] -> Private("unspecified") , [0, 0, 0, 0, 0, 0, 0, 1] -> Private("loopback") , [0, 0, 0, 0, 0, 0xffff, hi, lo] -> embedded "IPv4-mapped" hi lo , [0, 0, 0, 0, 0, 0, _, _] -> Private("IPv4-compatible") , [0x64, 0xff9b, 0, 0, 0, 0, hi, lo] -> embedded "NAT64" hi lo , [first, ..] -> v6_prefix first , [] -> Private("unspecified") } } fn v6_prefix :: first: Int -> Reach { if int.and first 0xffc0 == 0xfe80 { Private("link-local") } else if int.and first 0xffc0 == 0xfec0 { Private("site-local") } else if int.and first 0xfe00 == 0xfc00 { Private("unique local") } else if int.and first 0xff00 == 0xff00 { Private("multicast") } else { Public } } /// The reach of the IPv4 address in the last two groups. fn embedded :: kind: String hi: Int lo: Int -> Reach { match v4_reach [hi / 256, hi % 256, lo / 256, lo % 256] { Public -> Public, Private(reason) -> Private("{kind}: {reason}") } }