Landin library reference discuss source

hosted/system/windows_addresses.ldn

1--  Internet addresses between `core/net` and Windows Sockets' records, as
2--  addresses.ldn turns them into platform/posix's: a `SOCKADDR_IN` or
3--  `SOCKADDR_IN6` laid out from a `net.address`, an IPv6 zone included,
4--  and back.
5
6import core/net
7import core/text
8import platform/windows
9import platform/windows/sockets
10
11fixed if compiler.os == windows then
12    --  Put a 16-bit or 32-bit value into `into` at `at`, the network's
13    --  order or the machine's.
14    put_network_u16: (inout into: [28]u8, at: usize, value: u16) -> none =
15        into[at] = u8(value >> 8)
16        into[at + 1] = u8(value & 0xFF)
17    end put_network_u16
18
19    put_u16: (inout into: [28]u8, at: usize, value: u16) -> none =
20        into[at] = u8(value & 0xFF)
21        into[at + 1] = u8(value >> 8)
22    end put_u16
23
24    put_u32: (inout into: [28]u8, at: usize, value: u32) -> none =
25        for index in usize(0)..<4 do
26            into[at + index] = u8((value >> u32(8 * index)) & 0xFF)
27        end for
28    end put_u32
29
30    get_u16: (from_bytes: [28]u8, at: usize) -> (value: u16) =
31        value = u16(from_bytes[at]) | (u16(from_bytes[at + 1]) << 8)
32    end get_u16
33
34    get_u32: (from_bytes: [28]u8, at: usize) -> (value: u32) =
35        value = 0
36        for index in usize(0)..<4 do
37            value = value | (u32(from_bytes[at + index]) << u32(8 * index))
38        end for
39    end get_u32
40
41    --- The zone of `at` names no interface here, or is a number past 32
42    --- bits.
43    public no_interface: atom
44
45    --- Make Windows Sockets' record of the internet address `at`: a
46    --- `SOCKADDR_IN` for an IPv4 address and a `SOCKADDR_IN6` for an IPv6
47    --- one, whose scope is the interface the zone names, decimal digits as
48    --- the interface's number and a name looked up with `if_nametoindex`
49    --- (D581).  It fails `no_interface` when the zone names none.
50    public internet_address: (at: net.address)
51                             -> (made: sockets.socket_address) ! no_interface =
52        made = (bytes: zeroed, length: 16)
53        if at.family == net.ipv4 then
54            put_u16(made.bytes, 0, u16(sockets.af_inet))
55            put_network_u16(made.bytes, 2, at.port)
56            for index in usize(0)..<4 do
57                made.bytes[4 + index] = at.octets[index]
58            end for
59            return
60        end if
61        made.length = 28
62        put_u16(made.bytes, 0, u16(sockets.af_inet6))
63        put_network_u16(made.bytes, 2, at.port)
64        for index in usize(0)..<16 do
65            made.bytes[8 + index] = at.octets[index]
66        end for
67        put_u32(made.bytes, 24, try scope_of(at))
68    end internet_address
69
70    --  The interface number an IPv6 address's zone names, zero for none.
71    scope_of: (at: net.address) -> (scope: u32) ! no_interface =
72        scope = 0
73        length: usize = net.zone_length(at)
74        return when length == 0
75        name: []u8 = at.zone[0..<length]
76        scope = text.parse_u32(name) else (problem)
77            _ = problem
78            mut digits: bool = true
79            for index in usize(0)..<length do
80                if name[index] < u8('0') or name[index] > u8('9') then
81                    digits = false
82                end if
83            end for
84            fail no_interface when digits
85            mut terminated: [16]u8 = zeroed
86            for index in usize(0)..<length do
87                terminated[index] = name[index]
88            end for
89            found: u32 = sockets.interface_index(addr terminated[0])
90            fail no_interface when found == 0
91            scope = found
92            return
93        end
94    end scope_of
95
96    --- The internet address and port Windows Sockets' record `at` holds.
97    --- An IPv6 scope becomes its zone: the interface's name, or its number
98    --- in decimal when no interface has that number now.  It fails
99    --- `no_interface` for a record of another family.
100    public address_of: (at: sockets.socket_address)
101                       -> (result: net.address) ! no_interface =
102        family: u16 = get_u16(at.bytes, 0)
103        port: u16 = (u16(at.bytes[2]) << 8) | u16(at.bytes[3])
104        if family == u16(sockets.af_inet) then
105            result = net.ipv4_address(at.bytes[4], at.bytes[5], at.bytes[6],
106                                      at.bytes[7], port)
107            return
108        end if
109        fail no_interface when family <> u16(sockets.af_inet6)
110        result = net.unspecified(net.ipv6, port)
111        for index in usize(0)..<16 do
112            result.octets[index] = at.bytes[8 + index]
113        end for
114        scope: u32 = get_u32(at.bytes, 24)
115        if scope <> 0 then
116            result.zone = zone_named(scope)
117        end if
118    end address_of
119
120    --  The zone a received address's scope names: its interface's name, or
121    --  for an interface that has gone its number in decimal.
122    zone_named: (scope: u32) -> (zone: [16]u8) =
123        zone = zeroed
124        mut room: [256]u8 = zeroed
125        if sockets.interface_name(scope, addr room[0]) <> 0 then
126            mut length: usize = 0
127            while length < lenof room and room[length] <> 0 do inc length end while
128            if length <= net.zone_room then
129                for index in usize(0)..<length do
130                    zone[index] = room[index]
131                end for
132                return
133            end if
134        end if
135        mut digits := text.over(zone[0..<net.zone_room])
136        text.append_u32(digits, scope) else none
137    end zone_named
138end if