net is a module — import net — and tcp, udp, dns and http all use
it, so a program that reads who connected, who sent a datagram, what a name
resolves to or where a request went holds the same type whichever it asked.
An address is a value. An IpAddr is plain bytes inside whatever holds it:
four octets in one u32 for IPv4, sixteen in two u64 for IPv6. Holding one,
copying one, comparing one and using one as a Map key allocate nothing.
enum IpAddr {
V4 { bits: u32 }
V6 { high: u64, low: u64, scope: u32 }
}bits holds the four octets with the first most significant, so 127.0.0.1 is
0x7F000001. high holds the first four groups of an IPv6 address and low
the last four, the first of each most significant, so ::1 is high: 0, low: 1.
Making and reading one
import net
fn main() int {
ip := net.IpAddr.v4(10, 0, 0, 1)
same := net.IpAddr.V4 { bits: 0x0A000001 }
if ip != same {
return 1
}
if "${ip}" != "10.0.0.1" {
return 2
}
parsed := match net.IpAddr.parse("2001:db8::1") {
Ok(p) => p
Err(e) => return 3
}
if "${parsed}" != "2001:db8::1" {
return 4
}
if parsed.is_loopback() || parsed.is_multicast() {
return 5
}
return 0
}IpAddr.v4(a, b, c, d) and IpAddr.v6(high, low) build one;
loopback_v4(), loopback_v6(), unspecified_v4() and unspecified_v6() are
the four everyday ones. is_v4(), is_v6(), is_loopback(),
is_unspecified() and is_multicast() ask about it, as Rust’s IpAddr does.
Equality, hashing and ordering come with the type and look at every word: V4
orders before V6, an IPv4 address by its number, and an IPv6 address by its
sixteen octets as one big-endian number and then by scope, which is Rust’s
IpAddr order with the scope as the last key.
Text
IpAddr.parse(text) reads dotted decimal and the colon-hex forms of RFC 4291,
and answers Result[IpAddr, ParseAddrError]. It accepts exactly the texts
Rust’s str::parse::<IpAddr> accepts: four decimal octets without leading
zeros; or eight hex groups of up to four digits, either case, with one ::
standing for the zero groups and the last two groups optionally written as a
dotted IPv4 address. It reads in one pass and allocates nothing.
Display writes what Rust’s Ipv6Addr writes: lower-case hex, the leftmost
longest run of two or more zero groups as ::, and ::ffff:a.b.c.d for an
IPv4-mapped address.
import net
fn main() int {
// The groups are 1, 0, 2, 0, 0, 0, 0, 3: the longer run of zeros is the `::`.
if "${net.IpAddr.v6(0x0001000000020000, 0x0000000000000003)}" != "1:0:2::3" {
return 1
}
match net.IpAddr.parse("1.2.3.256") {
Ok(ip) => return 2
Err(e) => {
// What broke, and where in the text.
if e.at != 6 {
return 3
}
}
}
return 0
}A ParseAddrError carries the rule that was broken (kind, an AddrFault)
and the byte offset in the text where it was (at); its Display says both.
The interface a link-local address is on
An IPv6 address that names a link needs to say which one: a datagram from
fe80::1 can only be answered on the interface it arrived on. The scope of a
V6 is that interface’s index, 0 when the address names none, and it is part
of the value: fe80::1%2 and fe80::1%3 are two addresses, and so are
fe80::1 and fe80::1%2.
write_to writes a scoped address with % and the index, and IpAddr.parse
reads it back. That is the one place parse accepts more than Rust’s
IpAddr does (Rust reads a scope only in a socket address); a scope that is a
name, %eth0, is refused, because naming an interface is a lookup this module
does not do.
IPv4 inside IPv6
A dual-stack socket reports an IPv4 peer as ::ffff:a.b.c.d. The hosts report
the IPv4 address it is, so an allow-list written on one machine means the same
thing on another; the exception is an address with a scope, which stays IPv6.
to_v4() is the IPv4 address an address names (itself, or the one inside a
mapped IPv6 address, None for any other IPv6 address), to_v6() is its
IPv6 form, and to_canonical() turns a mapped address into the IPv4 address it
carries and leaves every other address alone. As Rust’s is_loopback does,
is_loopback() is false for ::ffff:127.0.0.1: ask to_canonical() first when
an address came from somewhere that maps.
An address and a port
SocketAddr { ip, port } is an address with a port, held by value the same way.
Its Display is 1.2.3.4:80 for IPv4 and [::1]:80 for IPv6, with the scope
inside the brackets ([fe80::1%3]:9000), and SocketAddr.parse(text) reads
exactly what Rust’s str::parse::<SocketAddr> reads, scope included.
import net
fn main() int {
at := match net.SocketAddr.parse("[fe80::1%3]:9000") {
Ok(a) => a
Err(e) => return 1
}
if at.port != 9000 {
return 2
}
if "${at}" != "[fe80::1%3]:9000" {
return 3
}
return 0
}Where addresses come from
| Call | Answers |
|---|---|
tcp.Conn.peer_ip(), local_ip(); tcp.Listener.ip() |
IpAddr? (None for a unix: socket, which has a path) |
tcp.Conn.peer_addr(), local_addr(); tcp.Listener.addr() |
SocketAddr?, the address and the port together |
udp.Datagram.from |
SocketAddr, with the sender’s scope |
dns.Record.address(), dns.Answer.first_address() |
IpAddr? of an A or AAAA record |
dns.Query.peer_ip() |
IpAddr? the question came from |
http.Conn.peer_ip() |
IpAddr? the connection came from |
http.BodyStream.peer() |
SocketAddr? the request reached |
Each is the host writing the address’s words into the answer: the same cost as any other small value a host answers, with no text rendered, no block carved and nothing parsed.
Where addresses go
| Call | Takes |
|---|---|
udp.Socket.send_to(buf, to) |
SocketAddr; send_to_host(buf, "host:port") takes a name |
dns.reverse(ip) |
IpAddr |
dns.Reply.a(name, ttl, ip), aaaa(name, ttl, ip) |
IpAddr, of the record’s family: an A record holds an IPv4 address and an AAAA an IPv6 one, and the other family is refused as InvalidName |
tcp.connect_from(addr, source, timeout_ms) |
SocketAddr, the local address to leave from |
An address a program passes to a host crosses as its words, IpAddr.words():
the family, the two halves of an IPv6 address and its scope, each in a lane of
its own, so the host reads them where the call left them. A name is still
text, because a name is something a host looks up: tcp.connect,
udp.connect and send_to_host take host:port.