aboutsummaryrefslogtreecommitdiff
path: root/lib/std/x/os/socket_windows.zig
blob: dc6d27c050163f9c808246e477293eb16166100c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
const std = @import("../../std.zig");
const net = @import("net.zig");

const os = std.os;
const mem = std.mem;

const windows = std.os.windows;
const ws2_32 = windows.ws2_32;

pub fn Mixin(comptime Socket: type) type {
    return struct {
        /// Open a new socket.
        pub fn init(domain: u32, socket_type: u32, protocol: u32, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket {
            var raw_flags: u32 = ws2_32.WSA_FLAG_OVERLAPPED;
            const set = std.EnumSet(Socket.InitFlags).init(flags);
            if (set.contains(.close_on_exec)) raw_flags |= ws2_32.WSA_FLAG_NO_HANDLE_INHERIT;

            const fd = ws2_32.WSASocketW(
                @intCast(i32, domain),
                @intCast(i32, socket_type),
                @intCast(i32, protocol),
                null,
                0,
                raw_flags,
            );
            if (fd == ws2_32.INVALID_SOCKET) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSANOTINITIALISED => {
                        _ = try windows.WSAStartup(2, 2);
                        return Socket.init(domain, socket_type, protocol, flags);
                    },
                    .WSAEAFNOSUPPORT => error.AddressFamilyNotSupported,
                    .WSAEMFILE => error.ProcessFdQuotaExceeded,
                    .WSAENOBUFS => error.SystemResources,
                    .WSAEPROTONOSUPPORT => error.ProtocolNotSupported,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }

            if (set.contains(.nonblocking)) {
                var enabled: c_ulong = 1;
                const rc = ws2_32.ioctlsocket(fd, ws2_32.FIONBIO, &enabled);
                if (rc == ws2_32.SOCKET_ERROR) {
                    return windows.unexpectedWSAError(ws2_32.WSAGetLastError());
                }
            }

            return Socket{ .fd = fd };
        }

        /// Closes the socket.
        pub fn deinit(self: Socket) void {
            _ = ws2_32.closesocket(self.fd);
        }

        /// Shutdown either the read side, write side, or all side of the socket.
        pub fn shutdown(self: Socket, how: os.ShutdownHow) !void {
            const rc = ws2_32.shutdown(self.fd, switch (how) {
                .recv => ws2_32.SD_RECEIVE,
                .send => ws2_32.SD_SEND,
                .both => ws2_32.SD_BOTH,
            });
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSAECONNABORTED => return error.ConnectionAborted,
                    .WSAECONNRESET => return error.ConnectionResetByPeer,
                    .WSAEINPROGRESS => return error.BlockingOperationInProgress,
                    .WSAEINVAL => unreachable,
                    .WSAENETDOWN => return error.NetworkSubsystemFailed,
                    .WSAENOTCONN => return error.SocketNotConnected,
                    .WSAENOTSOCK => unreachable,
                    .WSANOTINITIALISED => unreachable,
                    else => |err| return windows.unexpectedWSAError(err),
                };
            }
        }

        /// Binds the socket to an address.
        pub fn bind(self: Socket, address: Socket.Address) !void {
            const rc = ws2_32.bind(self.fd, @ptrCast(*const ws2_32.sockaddr, &address.toNative()), @intCast(c_int, address.getNativeSize()));
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAEACCES => error.AccessDenied,
                    .WSAEADDRINUSE => error.AddressInUse,
                    .WSAEADDRNOTAVAIL => error.AddressNotAvailable,
                    .WSAEFAULT => error.BadAddress,
                    .WSAEINPROGRESS => error.WouldBlock,
                    .WSAEINVAL => error.AlreadyBound,
                    .WSAENOBUFS => error.NoEphemeralPortsAvailable,
                    .WSAENOTSOCK => error.NotASocket,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }
        }

        /// Start listening for incoming connections on the socket.
        pub fn listen(self: Socket, max_backlog_size: u31) !void {
            const rc = ws2_32.listen(self.fd, max_backlog_size);
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAEADDRINUSE => error.AddressInUse,
                    .WSAEISCONN => error.AlreadyConnected,
                    .WSAEINVAL => error.SocketNotBound,
                    .WSAEMFILE, .WSAENOBUFS => error.SystemResources,
                    .WSAENOTSOCK => error.FileDescriptorNotASocket,
                    .WSAEOPNOTSUPP => error.OperationNotSupported,
                    .WSAEINPROGRESS => error.WouldBlock,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }
        }

        /// Have the socket attempt to the connect to an address.
        pub fn connect(self: Socket, address: Socket.Address) !void {
            const rc = ws2_32.connect(self.fd, @ptrCast(*const ws2_32.sockaddr, &address.toNative()), @intCast(c_int, address.getNativeSize()));
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSAEADDRINUSE => error.AddressInUse,
                    .WSAEADDRNOTAVAIL => error.AddressNotAvailable,
                    .WSAECONNREFUSED => error.ConnectionRefused,
                    .WSAETIMEDOUT => error.ConnectionTimedOut,
                    .WSAEFAULT => error.BadAddress,
                    .WSAEINVAL => error.ListeningSocket,
                    .WSAEISCONN => error.AlreadyConnected,
                    .WSAENOTSOCK => error.NotASocket,
                    .WSAEACCES => error.BroadcastNotEnabled,
                    .WSAENOBUFS => error.SystemResources,
                    .WSAEAFNOSUPPORT => error.AddressFamilyNotSupported,
                    .WSAEINPROGRESS, .WSAEWOULDBLOCK => error.WouldBlock,
                    .WSAEHOSTUNREACH, .WSAENETUNREACH => error.NetworkUnreachable,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }
        }

        /// Accept a pending incoming connection queued to the kernel backlog
        /// of the socket.
        pub fn accept(self: Socket, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket.Connection {
            var address: Socket.Address.Native.Storage = undefined;
            var address_len: c_int = @sizeOf(Socket.Address.Native.Storage);

            const fd = ws2_32.accept(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len);
            if (fd == ws2_32.INVALID_SOCKET) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSANOTINITIALISED => unreachable,
                    .WSAECONNRESET => error.ConnectionResetByPeer,
                    .WSAEFAULT => unreachable,
                    .WSAEINVAL => error.SocketNotListening,
                    .WSAEMFILE => error.ProcessFdQuotaExceeded,
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAENOBUFS => error.FileDescriptorNotASocket,
                    .WSAEOPNOTSUPP => error.OperationNotSupported,
                    .WSAEWOULDBLOCK => error.WouldBlock,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }

            const socket = Socket.from(fd);
            errdefer socket.deinit();

            const socket_address = Socket.Address.fromNative(@ptrCast(*ws2_32.sockaddr, &address));

            const set = std.EnumSet(Socket.InitFlags).init(flags);
            if (set.contains(.nonblocking)) {
                var enabled: c_ulong = 1;
                const rc = ws2_32.ioctlsocket(fd, ws2_32.FIONBIO, &enabled);
                if (rc == ws2_32.SOCKET_ERROR) {
                    return windows.unexpectedWSAError(ws2_32.WSAGetLastError());
                }
            }

            return Socket.Connection.from(socket, socket_address);
        }

        /// Read data from the socket into the buffer provided with a set of flags
        /// specified. It returns the number of bytes read into the buffer provided.
        pub fn read(self: Socket, buf: []u8, flags: u32) !usize {
            var bufs = &[_]ws2_32.WSABUF{.{ .len = @intCast(u32, buf.len), .buf = buf.ptr }};
            var num_bytes: u32 = undefined;
            var flags_ = flags;

            const rc = ws2_32.WSARecv(self.fd, bufs, 1, &num_bytes, &flags_, null, null);
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSAECONNABORTED => error.ConnectionAborted,
                    .WSAECONNRESET => error.ConnectionResetByPeer,
                    .WSAEDISCON => error.ConnectionClosedByPeer,
                    .WSAEFAULT => error.BadBuffer,
                    .WSAEINPROGRESS,
                    .WSAEWOULDBLOCK,
                    .WSA_IO_PENDING,
                    .WSAETIMEDOUT,
                    => error.WouldBlock,
                    .WSAEINTR => error.Cancelled,
                    .WSAEINVAL => error.SocketNotBound,
                    .WSAEMSGSIZE => error.MessageTooLarge,
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAENETRESET => error.NetworkReset,
                    .WSAENOTCONN => error.SocketNotConnected,
                    .WSAENOTSOCK => error.FileDescriptorNotASocket,
                    .WSAEOPNOTSUPP => error.OperationNotSupported,
                    .WSAESHUTDOWN => error.AlreadyShutdown,
                    .WSA_OPERATION_ABORTED => error.OperationAborted,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }

            return @intCast(usize, num_bytes);
        }

        /// Write a buffer of data provided to the socket with a set of flags specified.
        /// It returns the number of bytes that are written to the socket.
        pub fn write(self: Socket, buf: []const u8, flags: u32) !usize {
            var bufs = &[_]ws2_32.WSABUF{.{ .len = @intCast(u32, buf.len), .buf = @intToPtr([*]u8, @ptrToInt(buf.ptr)) }};
            var num_bytes: u32 = undefined;

            const rc = ws2_32.WSASend(self.fd, bufs, 1, &num_bytes, flags, null, null);
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSAECONNABORTED => error.ConnectionAborted,
                    .WSAECONNRESET => error.ConnectionResetByPeer,
                    .WSAEFAULT => error.BadBuffer,
                    .WSAEINPROGRESS,
                    .WSAEWOULDBLOCK,
                    .WSA_IO_PENDING,
                    .WSAETIMEDOUT,
                    => error.WouldBlock,
                    .WSAEINTR => error.Cancelled,
                    .WSAEINVAL => error.SocketNotBound,
                    .WSAEMSGSIZE => error.MessageTooLarge,
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAENETRESET => error.NetworkReset,
                    .WSAENOBUFS => error.BufferDeadlock,
                    .WSAENOTCONN => error.SocketNotConnected,
                    .WSAENOTSOCK => error.FileDescriptorNotASocket,
                    .WSAEOPNOTSUPP => error.OperationNotSupported,
                    .WSAESHUTDOWN => error.AlreadyShutdown,
                    .WSA_OPERATION_ABORTED => error.OperationAborted,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }

            return @intCast(usize, num_bytes);
        }

        /// Writes multiple I/O vectors with a prepended message header to the socket
        /// with a set of flags specified. It returns the number of bytes that are
        /// written to the socket.
        pub fn writeMessage(self: Socket, msg: Socket.Message, flags: u32) !usize {
            const call = try windows.loadWinsockExtensionFunction(ws2_32.LPFN_WSASENDMSG, self.fd, ws2_32.WSAID_WSASENDMSG);

            var num_bytes: u32 = undefined;

            const rc = call(self.fd, &msg, flags, &num_bytes, null, null);
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSAECONNABORTED => error.ConnectionAborted,
                    .WSAECONNRESET => error.ConnectionResetByPeer,
                    .WSAEFAULT => error.BadBuffer,
                    .WSAEINPROGRESS,
                    .WSAEWOULDBLOCK,
                    .WSA_IO_PENDING,
                    .WSAETIMEDOUT,
                    => error.WouldBlock,
                    .WSAEINTR => error.Cancelled,
                    .WSAEINVAL => error.SocketNotBound,
                    .WSAEMSGSIZE => error.MessageTooLarge,
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAENETRESET => error.NetworkReset,
                    .WSAENOBUFS => error.BufferDeadlock,
                    .WSAENOTCONN => error.SocketNotConnected,
                    .WSAENOTSOCK => error.FileDescriptorNotASocket,
                    .WSAEOPNOTSUPP => error.OperationNotSupported,
                    .WSAESHUTDOWN => error.AlreadyShutdown,
                    .WSA_OPERATION_ABORTED => error.OperationAborted,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }

            return @intCast(usize, num_bytes);
        }

        /// Read multiple I/O vectors with a prepended message header from the socket
        /// with a set of flags specified. It returns the number of bytes that were
        /// read into the buffer provided.
        pub fn readMessage(self: Socket, msg: *Socket.Message, flags: u32) !usize {
            _ = flags;
            const call = try windows.loadWinsockExtensionFunction(ws2_32.LPFN_WSARECVMSG, self.fd, ws2_32.WSAID_WSARECVMSG);

            var num_bytes: u32 = undefined;

            const rc = call(self.fd, msg, &num_bytes, null, null);
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSAECONNABORTED => error.ConnectionAborted,
                    .WSAECONNRESET => error.ConnectionResetByPeer,
                    .WSAEDISCON => error.ConnectionClosedByPeer,
                    .WSAEFAULT => error.BadBuffer,
                    .WSAEINPROGRESS,
                    .WSAEWOULDBLOCK,
                    .WSA_IO_PENDING,
                    .WSAETIMEDOUT,
                    => error.WouldBlock,
                    .WSAEINTR => error.Cancelled,
                    .WSAEINVAL => error.SocketNotBound,
                    .WSAEMSGSIZE => error.MessageTooLarge,
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAENETRESET => error.NetworkReset,
                    .WSAENOTCONN => error.SocketNotConnected,
                    .WSAENOTSOCK => error.FileDescriptorNotASocket,
                    .WSAEOPNOTSUPP => error.OperationNotSupported,
                    .WSAESHUTDOWN => error.AlreadyShutdown,
                    .WSA_OPERATION_ABORTED => error.OperationAborted,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }

            return @intCast(usize, num_bytes);
        }

        /// Query the address that the socket is locally bounded to.
        pub fn getLocalAddress(self: Socket) !Socket.Address {
            var address: Socket.Address.Native.Storage = undefined;
            var address_len: c_int = @sizeOf(Socket.Address.Native.Storage);

            const rc = ws2_32.getsockname(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len);
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSANOTINITIALISED => unreachable,
                    .WSAEFAULT => unreachable,
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAENOTSOCK => error.FileDescriptorNotASocket,
                    .WSAEINVAL => error.SocketNotBound,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }

            return Socket.Address.fromNative(@ptrCast(*ws2_32.sockaddr, &address));
        }

        /// Query the address that the socket is connected to.
        pub fn getRemoteAddress(self: Socket) !Socket.Address {
            var address: Socket.Address.Native.Storage = undefined;
            var address_len: c_int = @sizeOf(Socket.Address.Native.Storage);

            const rc = ws2_32.getpeername(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len);
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSANOTINITIALISED => unreachable,
                    .WSAEFAULT => unreachable,
                    .WSAENETDOWN => error.NetworkSubsystemFailed,
                    .WSAENOTSOCK => error.FileDescriptorNotASocket,
                    .WSAEINVAL => error.SocketNotBound,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }

            return Socket.Address.fromNative(@ptrCast(*ws2_32.sockaddr, &address));
        }

        /// Query and return the latest cached error on the socket.
        pub fn getError(self: Socket) !void {
            _ = self;
            return {};
        }

        /// Query the read buffer size of the socket.
        pub fn getReadBufferSize(self: Socket) !u32 {
            _ = self;
            return 0;
        }

        /// Query the write buffer size of the socket.
        pub fn getWriteBufferSize(self: Socket) !u32 {
            _ = self;
            return 0;
        }

        /// Set a socket option.
        pub fn setOption(self: Socket, level: u32, code: u32, value: []const u8) !void {
            const rc = ws2_32.setsockopt(self.fd, @intCast(i32, level), @intCast(i32, code), value.ptr, @intCast(i32, value.len));
            if (rc == ws2_32.SOCKET_ERROR) {
                return switch (ws2_32.WSAGetLastError()) {
                    .WSANOTINITIALISED => unreachable,
                    .WSAENETDOWN => return error.NetworkSubsystemFailed,
                    .WSAEFAULT => unreachable,
                    .WSAENOTSOCK => return error.FileDescriptorNotASocket,
                    .WSAEINVAL => return error.SocketNotBound,
                    else => |err| windows.unexpectedWSAError(err),
                };
            }
        }

        /// Have close() or shutdown() syscalls block until all queued messages in the socket have been successfully
        /// sent, or if the timeout specified in seconds has been reached. It returns `error.UnsupportedSocketOption`
        /// if the host does not support the option for a socket to linger around up until a timeout specified in
        /// seconds.
        pub fn setLinger(self: Socket, timeout_seconds: ?u16) !void {
            const settings = Socket.Linger.init(timeout_seconds);
            return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.LINGER, mem.asBytes(&settings));
        }

        /// On connection-oriented sockets, have keep-alive messages be sent periodically. The timing in which keep-alive
        /// messages are sent are dependant on operating system settings. It returns `error.UnsupportedSocketOption` if
        /// the host does not support periodically sending keep-alive messages on connection-oriented sockets.
        pub fn setKeepAlive(self: Socket, enabled: bool) !void {
            return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.KEEPALIVE, mem.asBytes(&@as(u32, @boolToInt(enabled))));
        }

        /// Allow multiple sockets on the same host to listen on the same address. It returns `error.UnsupportedSocketOption` if
        /// the host does not support sockets listening the same address.
        pub fn setReuseAddress(self: Socket, enabled: bool) !void {
            return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.REUSEADDR, mem.asBytes(&@as(u32, @boolToInt(enabled))));
        }

        /// Allow multiple sockets on the same host to listen on the same port. It returns `error.UnsupportedSocketOption` if
        /// the host does not supports sockets listening on the same port.
        ///
        /// TODO: verify if this truly mimicks SO.REUSEPORT behavior, or if SO.REUSE_UNICASTPORT provides the correct behavior
        pub fn setReusePort(self: Socket, enabled: bool) !void {
            try self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.BROADCAST, mem.asBytes(&@as(u32, @boolToInt(enabled))));
            try self.setReuseAddress(enabled);
        }

        /// Set the write buffer size of the socket.
        pub fn setWriteBufferSize(self: Socket, size: u32) !void {
            return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.SNDBUF, mem.asBytes(&size));
        }

        /// Set the read buffer size of the socket.
        pub fn setReadBufferSize(self: Socket, size: u32) !void {
            return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.RCVBUF, mem.asBytes(&size));
        }

        /// WARNING: Timeouts only affect blocking sockets. It is undefined behavior if a timeout is
        /// set on a non-blocking socket.
        ///
        /// Set a timeout on the socket that is to occur if no messages are successfully written
        /// to its bound destination after a specified number of milliseconds. A subsequent write
        /// to the socket will thereafter return `error.WouldBlock` should the timeout be exceeded.
        pub fn setWriteTimeout(self: Socket, milliseconds: u32) !void {
            return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.SNDTIMEO, mem.asBytes(&milliseconds));
        }

        /// WARNING: Timeouts only affect blocking sockets. It is undefined behavior if a timeout is
        /// set on a non-blocking socket.
        ///
        /// Set a timeout on the socket that is to occur if no messages are successfully read
        /// from its bound destination after a specified number of milliseconds. A subsequent
        /// read from the socket will thereafter return `error.WouldBlock` should the timeout be
        /// exceeded.
        pub fn setReadTimeout(self: Socket, milliseconds: u32) !void {
            return self.setOption(ws2_32.SOL.SOCKET, ws2_32.SO.RCVTIMEO, mem.asBytes(&milliseconds));
        }
    };
}