1-- Windows Sockets, ws2_32's calls, and iphlpapi's interface names, raw.
2-- A socket is a handle the system gives as a word, `SOCKET`; a call that
3-- fails returns `socket_error` or `invalid_socket` and leaves the reason
4-- for `socket_last_error`. `started` makes the one `WSAStartup` call a
5-- process needs before its first socket.
6--
7-- It is a module of its own, beneath platform/windows, which refine links
8-- into every Windows program: Windows Sockets names its calls `send`,
9-- `connect` and `bind`, C symbols a program may declare for its own C
10-- code, so only a program that imports this module, as the hosted
11-- network providers do, has them declared (D2281). Every hosted system
12-- may import it, and off Windows it declares nothing.
13
14import core/atomic
15import platform/windows
16
17fixed if compiler.os == windows then
18 linker.library("ws2_32")
19 linker.library("iphlpapi")
20
21 --- What a failing call returns: `SOCKET_ERROR`, and `INVALID_SOCKET`,
22 --- all ones.
23 public socket_error: i32 = -1
24 --- `INVALID_SOCKET`: what a call that makes a socket gives when it fails,
25 --- all ones.
26 public invalid_socket: windows.handle = 0xFFFFFFFFFFFFFFFF
27
28 --- The address families, `AF_INET` and `AF_INET6`.
29 public af_inet: i32 = 2
30 --- `AF_INET6`: IPv6.
31 public af_inet6: i32 = 23
32
33 --- The kinds, `SOCK_STREAM` and `SOCK_DGRAM`.
34 public sock_stream: i32 = 1
35 --- `SOCK_DGRAM`: datagrams, UDP.
36 public sock_dgram: i32 = 2
37
38 --- `WSASocketW`'s flags: the socket may be used with overlapped calls,
39 --- and a child does not inherit it.
40 public wsa_flag_overlapped: u32 = 0x01
41 --- `WSA_FLAG_NO_HANDLE_INHERIT`: a child does not inherit the socket.
42 public wsa_flag_no_handle_inherit: u32 = 0x80
43
44 --- The Windows Sockets error codes the providers tell apart: `WSAE*`.
45 public wsa_interrupted: u32 = 10004
46 --- `WSAEACCES`.
47 public wsa_access: u32 = 10013
48 --- `WSAEWOULDBLOCK`.
49 public wsa_would_block: u32 = 10035
50 --- `WSAEMSGSIZE`.
51 public wsa_message_size: u32 = 10040
52 --- `WSAEAFNOSUPPORT`.
53 public wsa_family_unsupported: u32 = 10047
54 --- `WSAEADDRINUSE`.
55 public wsa_address_in_use: u32 = 10048
56 --- `WSAEADDRNOTAVAIL`.
57 public wsa_address_unavailable: u32 = 10049
58 --- `WSAENETUNREACH`.
59 public wsa_network_unreachable: u32 = 10051
60 --- `WSAENETRESET`.
61 public wsa_network_reset: u32 = 10052
62 --- `WSAECONNABORTED`.
63 public wsa_connection_aborted: u32 = 10053
64 --- `WSAECONNRESET`.
65 public wsa_connection_reset: u32 = 10054
66 --- `WSAENOTCONN`.
67 public wsa_not_connected: u32 = 10057
68 --- `WSAESHUTDOWN`.
69 public wsa_shut_down: u32 = 10058
70 --- `WSAETIMEDOUT`.
71 public wsa_timed_out: u32 = 10060
72 --- `WSAECONNREFUSED`.
73 public wsa_connection_refused: u32 = 10061
74 --- `WSAEHOSTUNREACH`.
75 public wsa_host_unreachable: u32 = 10065
76
77 -- `WSADATA`, which `WSAStartup` fills and nothing here reads: room for
78 -- the largest it is.
79 wsa_data: type = struct
80 words: [52]usize
81 end wsa_data
82
83 extern(c) link(symbol: "WSAStartup") wsa_startup:
84 (version: u16, into: ptr mut wsa_data) -> (status: i32)
85
86 -- Whether `WSAStartup` has been called, which several threads may do
87 -- at once: the system counts each call, so a second is harmless.
88 mut initialised: atomic.cell(u32) = zeroed
89
90 --- Make the process's one `WSAStartup` call, asking for Windows Sockets
91 --- 2.2, before its first socket: zero, or the reason it failed.
92 public started: () -> (status: u32) =
93 status = 0
94 return when atomic.load(item: u32, at: addr initialised) <> 0
95 mut found: wsa_data = zeroed
96 given: i32 = wsa_startup(0x0202, addr found)
97 if given <> 0 then
98 status = u32(given)
99 return
100 end if
101 atomic.store(item: u32, at: addr initialised, value: 1)
102 end started
103
104 --- The calling thread's last Windows Sockets error, `WSAGetLastError`.
105 public extern(c) link(symbol: "WSAGetLastError") socket_last_error:
106 () -> (code: u32)
107
108 --- Make a socket, `WSASocketW` with no protocol information: its handle,
109 --- or `invalid_socket`.
110 public extern(c) link(symbol: "WSASocketW") make_socket:
111 (family: i32, kind: i32, protocol: i32, information: usize,
112 group: u32, flags: u32) -> (socket: windows.handle)
113
114 --- Close a socket, `closesocket`: zero or `socket_error`.
115 public extern(c) link(symbol: "closesocket") close_socket:
116 (socket: windows.handle) -> (status: i32)
117
118 --- An address of either family as the system lays it out: room for a
119 --- `SOCKADDR_IN6`, which is the larger, and its length in bytes.
120 public socket_address: type = struct
121 bytes: [28]u8
122 length: i32
123 end socket_address
124
125 --- Bind, connect, listen and accept, `bind`, `connect`, `listen` and
126 --- `accept`: zero or `socket_error`; accept's new socket or
127 --- `invalid_socket`, its peer's address in `peer` when it is not zero.
128 public extern(c) link(symbol: "bind") bind:
129 (socket: windows.handle, at: ptr u8, length: i32) -> (status: i32)
130 --- Connect `socket` to the address at `to`, `connect`: zero or
131 --- `socket_error`.
132 public extern(c) link(symbol: "connect") connect:
133 (socket: windows.handle, to: ptr u8, length: i32) -> (status: i32)
134 --- Make `socket` a listener, `listen`: zero or `socket_error`.
135 public extern(c) link(symbol: "listen") listen:
136 (socket: windows.handle, backlog: i32) -> (status: i32)
137 --- Take the next connection made to the listener `socket`, `accept`: its
138 --- socket or `invalid_socket`; the peer's address goes to `peer` when it
139 --- is not zero.
140 public extern(c) link(symbol: "accept") accept:
141 (socket: windows.handle, peer: usize, length: usize) -> (result: windows.handle)
142
143 --- Send and receive on a connected socket, `send` and `recv`: the
144 --- bytes moved, zero from `receive` once the peer has finished sending,
145 --- or `socket_error`.
146 public extern(c) link(symbol: "send") send:
147 (socket: windows.handle, bytes: ptr u8, count: i32, flags: i32) -> (sent: i32)
148 --- Receive on a connected socket, `recv`: the bytes taken, zero once the
149 --- peer has finished sending, or `socket_error`.
150 public extern(c) link(symbol: "recv") receive:
151 (socket: windows.handle, into: ptr mut u8, count: i32, flags: i32)
152 -> (got: i32)
153
154 --- Send a datagram to `to` and receive one with its sender's address,
155 --- `sendto` and `recvfrom`.
156 public extern(c) link(symbol: "sendto") send_to:
157 (socket: windows.handle, bytes: ptr u8, count: i32, flags: i32, to: ptr u8,
158 length: i32) -> (sent: i32)
159 --- Receive a datagram and its sender's address, `recvfrom`.
160 public extern(c) link(symbol: "recvfrom") receive_from:
161 (socket: windows.handle, into: ptr mut u8, count: i32, flags: i32,
162 sender: ptr mut u8, length: ptr mut i32) -> (got: i32)
163
164 --- Stop receiving, sending or both, `shutdown`.
165 public sd_receive: i32 = 0
166 --- `SD_SEND`: stop sending.
167 public sd_send: i32 = 1
168 --- `SD_BOTH`: stop receiving and sending.
169 public sd_both: i32 = 2
170 --- Stop receiving, sending or both, `shutdown`.
171 public extern(c) link(symbol: "shutdown") shutdown:
172 (socket: windows.handle, how: i32) -> (status: i32)
173
174 --- The address of a socket's own end and its peer's, `getsockname` and
175 --- `getpeername`.
176 public extern(c) link(symbol: "getsockname") socket_name:
177 (socket: windows.handle, into: ptr mut u8, length: ptr mut i32) -> (status: i32)
178 --- The address of a connection's peer, `getpeername`.
179 public extern(c) link(symbol: "getpeername") peer_name:
180 (socket: windows.handle, into: ptr mut u8, length: ptr mut i32) -> (status: i32)
181
182 --- Socket options, `setsockopt` and `getsockopt`: the level, the option
183 --- and its value's bytes.
184 public sol_socket: i32 = 0xFFFF
185 --- `SO_ERROR`: the outcome a connection begun without waiting ended with.
186 public so_error: i32 = 0x1007
187 --- `SO_SNDTIMEO`: how long a send may wait, in milliseconds.
188 public so_send_timeout: i32 = 0x1005
189 --- `SO_RCVTIMEO`: how long a receive may wait, in milliseconds.
190 public so_receive_timeout: i32 = 0x1006
191 --- `IPPROTO_TCP`: the level of TCP's options.
192 public ipproto_tcp: i32 = 6
193 --- `TCP_NODELAY`: send small writes at once.
194 public tcp_no_delay: i32 = 1
195 --- `IPPROTO_IPV6`: the level of IPv6's options.
196 public ipproto_ipv6: i32 = 41
197 --- `IPV6_V6ONLY`: an IPv6 socket takes IPv6 alone.
198 public ipv6_only: i32 = 27
199 --- Set a socket option, `setsockopt`: zero or `socket_error`.
200 public extern(c) link(symbol: "setsockopt") set_option:
201 (socket: windows.handle, level: i32, option: i32, value: ptr u8, length: i32)
202 -> (status: i32)
203 --- Read a socket option, `getsockopt`: zero or `socket_error`.
204 public extern(c) link(symbol: "getsockopt") get_option:
205 (socket: windows.handle, level: i32, option: i32, value: ptr mut u8,
206 length: ptr mut i32) -> (status: i32)
207
208 --- Set a socket to block or not, `ioctlsocket` with `FIONBIO`.
209 public fionbio: i32 = -2147195266
210 --- Set a socket to block, with `value` zero, or not, `ioctlsocket` with
211 --- `FIONBIO`.
212 public extern(c) link(symbol: "ioctlsocket") control:
213 (socket: windows.handle, command: i32, value: ptr mut u32) -> (status: i32)
214
215 --- `fd_set` as Windows Sockets lays it out: a count and the sockets,
216 --- room for one here.
217 public socket_set: type = struct
218 count: u32
219 sockets: [1]windows.handle
220 end socket_set
221
222 --- `timeval`: seconds and microseconds, 32 bits each.
223 public time_limit: type = struct
224 seconds: i32
225 microseconds: i32
226 end time_limit
227
228 --- Wait until a socket of the set at `reading` can be read, one of
229 --- `writing` written or one of `failing` has failed, up to the
230 --- `time_limit` at `limit`, `select`: how many are ready, zero when the
231 --- time ran out, or `socket_error`. Each set's address may be zero for
232 --- none, and each keeps only its ready sockets.
233 public extern(c) link(symbol: "select") select:
234 (ignored: i32, reading: usize, writing: usize, failing: usize,
235 limit: usize) -> (count: i32)
236
237 --- The interface number a name gives and the name of a number,
238 --- `if_nametoindex` and `if_indextoname`: zero, or no name, when there
239 --- is none.
240 public extern(c) link(symbol: "if_nametoindex") interface_index:
241 (name: ptr u8) -> (index: u32)
242 --- The name of the interface `index` into `into`, `if_indextoname`: zero
243 --- when there is none.
244 public extern(c) link(symbol: "if_indextoname") interface_name:
245 (index: u32, into: ptr mut u8) -> (name: usize)
246end if