1-- Internet addresses between `core/net` and the system's records:
2-- `platform/posix` lays a record out from numbers, and this file turns a
3-- `net.address` into those numbers and back, an IPv6 zone included, so
4-- that the system layer names no network concept (D1951).
5
6import core/net
7import core/text
8import platform/posix
9
10fixed if compiler.os <> windows then
11 --- Make the system's record of the internet address `at`, as
12 --- `posix.internet_record` lays one out: C's `struct sockaddr_in` for an
13 --- IPv4 address and `struct sockaddr_in6` for an IPv6 one. The IPv6
14 --- scope is the interface the address's zone names: a zone of decimal
15 --- digits is the interface's number as it stands, and a name is looked up
16 --- with `posix.interface_index` (D581). It fails `enodev` when the zone
17 --- names no interface here, or is a number past 32 bits.
18 public internet_address:
19 (at: net.address) -> (made: posix.socket_address) ! posix.errno =
20 mut endpoint: posix.internet_endpoint =
21 (family: posix.internet, octets: at.octets, port: at.port,
22 scope: 0)
23 if at.family <> net.ipv4 then
24 endpoint.family = posix.internet6
25 endpoint.scope = try scope_of(at)
26 end if
27 made = try posix.internet_record(endpoint)
28 end internet_address
29
30 -- The interface number an IPv6 address's zone names, zero for none.
31 scope_of: (at: net.address) -> (scope: u32) ! posix.errno =
32 scope = 0
33 length: usize = net.zone_length(at)
34 return when length == 0
35 name: []u8 = at.zone[0..<length]
36 scope = text.parse_u32(name) else (problem)
37 _ = problem
38 mut digits: bool = true
39 for index in usize(0)..<length do
40 if name[index] < u8('0') or name[index] > u8('9') then
41 digits = false
42 end if
43 end for
44 fail posix.enodev when digits
45 scope = try posix.interface_index(name)
46 return
47 end
48 end scope_of
49
50 --- The internet address and port the system's record `at` holds. An
51 --- IPv6 scope becomes its zone: the interface's name, or its number in
52 --- decimal when no interface has that number now. It fails
53 --- `eafnosupport` for a Unix-domain address, or a record too short for
54 --- its family.
55 public address_of:
56 (at: posix.socket_address) -> (result: net.address) ! posix.errno =
57 endpoint: posix.internet_endpoint = try posix.endpoint_of(at)
58 if endpoint.family == posix.internet then
59 result = net.ipv4_address(endpoint.octets[0], endpoint.octets[1],
60 endpoint.octets[2], endpoint.octets[3],
61 endpoint.port)
62 else
63 result = net.unspecified(net.ipv6, endpoint.port)
64 result.octets = endpoint.octets
65 if endpoint.scope <> 0 then
66 result.zone = zone_named(endpoint.scope)
67 end if
68 end if
69 end address_of
70
71 -- The zone a received address's scope names: its interface's name, or,
72 -- for an interface that has gone, its number in decimal.
73 zone_named: (scope: u32) -> (zone: [16]u8) =
74 zone = posix.interface_name(scope) else (problem)
75 _ = problem
76 zone = zeroed
77 mut room := text.over(zone[0..<net.zone_room])
78 text.append_u32(room, scope) else none
79 return
80 end
81 end zone_named
82end if