Skip to main content

parse_host_ip

Function parse_host_ip 

Source
pub fn parse_host_ip(host: &str) -> Option<ParsedHostIp>
Expand description

Parse a host string as an IP address the way inet_aton and system resolvers do, and say how it was spelled. The input is a host as it appears in a URL: ASCII case does not matter, IPv6 may be bracketed, and one trailing dot is allowed (and makes an IPv4 spelling non-canonical).

Accepted, with the result:

Inputaddrkindis_canonical
1.1.1.11.1.1.1Ipv4DottedQuadtrue
127.1127.0.0.1Ipv4Legacyfalse
10.1.210.1.0.2Ipv4Legacyfalse
0177.0.0.1 (octal parts)127.0.0.1Ipv4Legacyfalse
0x7f.0.0.1, 0x7f000001 (hex)127.0.0.1Ipv4Legacyfalse
2130706433 (one 32-bit part)127.0.0.1Ipv4Legacyfalse
1.1.1.1. (trailing dot)1.1.1.1Ipv4Legacyfalse
::1, [::1], ::ffff:127.0.0.1as writtenIpv6true

inet_aton rules: one to four parts; each part is decimal, octal (a leading 0) or hex (0x); every part but the last is at most 255; the last part fills the remaining bytes (so a.b.c has a 16-bit last part and a.b a 24-bit one). Returns None for hostnames, for more than four parts, for out-of-range parts, for a digit outside the part’s radix (08.1), and for empty parts.