authorgravatar for BarabasGitHub@users.noreply.github.comBas van den Berg <BarabasGitHub@users.noreply.github.com> 2020-09-09 18:55:45+02:00
committergravatar for BarabasGitHub@users.noreply.github.comBas van den Berg <BarabasGitHub@users.noreply.github.com> 2020-09-09 18:55:45+02:00
logf5b9e445aa9d2b344b87d4868f9fbb8021577f55
tree126838ce428230cea6cde16349515a7bd1317952
parent44f877d18bef6c82d727efeb5ff78ea97d3307bf

Handle some WSA errors


1 files changed, 108 insertions(+), 8 deletions(-)

lib/std/os.zig+108-8
......@@ -2750,6 +2750,13 @@ pub const BindError = error{
27502750
27512751 /// The socket inode would reside on a read-only filesystem.
27522752 ReadOnlyFileSystem,
2753
2754 /// The network subsystem has failed.
2755 NetworkSubsystemFailed,
2756
2757 FileDescriptorNotASocket,
2758
2759 AlreadyBound,
27532760} || UnexpectedError;
27542761
27552762/// addr is `*const T` where T is one of the sockaddr
......@@ -2758,7 +2765,15 @@ pub fn bind(sock: socket_t, addr: *const sockaddr, len: socklen_t) BindError!voi
27582765 if (builtin.os.tag == .windows) {
27592766 if (rc == windows.ws2_32.SOCKET_ERROR) {
27602767 switch (windows.ws2_32.WSAGetLastError()) {
2761 // TODO: handle errors
2768 .WSANOTINITIALISED => unreachable, // not initialized WSA
2769 .WSAEACCES => return error.AccessDenied,
2770 .WSAEADDRINUSE => return error.AddressInUse,
2771 .WSAEADDRNOTAVAIL => return error.AddressNotAvailable,
2772 .WSAENOTSOCK => return error.FileDescriptorNotASocket,
2773 .WSAEFAULT => unreachable, // invalid pointers
2774 .WSAEINVAL => return error.AlreadyBound,
2775 .WSAENOBUFS => return error.SystemResources,
2776 .WSAENETDOWN => return error.NetworkSubsystemFailed,
27622777 else => |err| return windows.unexpectedWSAError(err),
27632778 }
27642779 unreachable;
......@@ -2799,6 +2814,19 @@ const ListenError = error{
27992814
28002815 /// The socket is not of a type that supports the listen() operation.
28012816 OperationNotSupported,
2817
2818 /// The network subsystem has failed.
2819 NetworkSubsystemFailed,
2820
2821 /// Ran out of system resources
2822 /// On Windows it can either run out of socket descriptors or buffer space
2823 SystemResources,
2824
2825 /// Already connected
2826 AlreadyConnected,
2827
2828 /// Socket has not been bound yet
2829 SocketNotBound,
28022830} || UnexpectedError;
28032831
28042832pub fn listen(sock: socket_t, backlog: u31) ListenError!void {
......@@ -2806,7 +2834,15 @@ pub fn listen(sock: socket_t, backlog: u31) ListenError!void {
28062834 if (builtin.os.tag == .windows) {
28072835 if (rc == windows.ws2_32.SOCKET_ERROR) {
28082836 switch (windows.ws2_32.WSAGetLastError()) {
2809 // TODO: handle errors
2837 .WSANOTINITIALISED => unreachable, // not initialized WSA
2838 .WSAENETDOWN => return error.NetworkSubsystemFailed,
2839 .WSAEADDRINUSE => return error.AddressInUse,
2840 .WSAEISCONN => return error.AlreadyConnected,
2841 .WSAEINVAL => return error.SocketNotBound,
2842 .WSAEMFILE, .WSAENOBUFS => return error.SystemResources,
2843 .WSAENOTSOCK => return error.FileDescriptorNotASocket,
2844 .WSAEOPNOTSUPP => return error.OperationNotSupported,
2845 .WSAEINPROGRESS => unreachable,
28102846 else => |err| return windows.unexpectedWSAError(err),
28112847 }
28122848 }
......@@ -2826,6 +2862,9 @@ pub fn listen(sock: socket_t, backlog: u31) ListenError!void {
28262862pub const AcceptError = error{
28272863 ConnectionAborted,
28282864
2865 /// The file descriptor sockfd does not refer to a socket.
2866 FileDescriptorNotASocket,
2867
28292868 /// The per-process limit on the number of open file descriptors has been reached.
28302869 ProcessFdQuotaExceeded,
28312870
......@@ -2851,6 +2890,16 @@ pub const AcceptError = error{
28512890 /// Permission to create a socket of the specified type and/or
28522891 /// protocol is denied.
28532892 PermissionDenied,
2893
2894 /// An incoming connection was indicated, but was subsequently terminated by the
2895 /// remote peer prior to accepting the call.
2896 ConnectionResetByPeer,
2897
2898 /// The network subsystem has failed.
2899 NetworkSubsystemFailed,
2900
2901 /// The referenced socket is not a type that supports connection-oriented service.
2902 OperationNotSupported,
28542903} || UnexpectedError;
28552904
28562905/// Accept a connection on a socket.
......@@ -2892,7 +2941,15 @@ pub fn accept(
28922941 if (builtin.os.tag == .windows) {
28932942 if (rc == windows.ws2_32.INVALID_SOCKET) {
28942943 switch (windows.ws2_32.WSAGetLastError()) {
2895 // TODO: handle errors
2944 .WSANOTINITIALISED => unreachable, // not initialized WSA
2945 .WSAECONNRESET => return error.ConnectionResetByPeer,
2946 .WSAEFAULT => unreachable,
2947 .WSAEINVAL => return error.SocketNotListening,
2948 .WSAEMFILE => return error.ProcessFdQuotaExceeded,
2949 .WSAENETDOWN => return error.NetworkSubsystemFailed,
2950 .WSAENOBUFS => return error.FileDescriptorNotASocket,
2951 .WSAEOPNOTSUPP => return error.OperationNotSupported,
2952 .WSAEWOULDBLOCK => return error.WouldBlock,
28962953 else => |err| return windows.unexpectedWSAError(err),
28972954 }
28982955 } else {
......@@ -3044,6 +3101,14 @@ pub fn eventfd(initval: u32, flags: u32) EventFdError!i32 {
30443101pub const GetSockNameError = error{
30453102 /// Insufficient resources were available in the system to perform the operation.
30463103 SystemResources,
3104
3105 /// The network subsystem has failed.
3106 NetworkSubsystemFailed,
3107
3108 /// Socket hasn't been bound yet
3109 SocketNotBound,
3110
3111 FileDescriptorNotASocket,
30473112} || UnexpectedError;
30483113
30493114pub fn getsockname(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSockNameError!void {
......@@ -3051,7 +3116,11 @@ pub fn getsockname(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSock
30513116 if (builtin.os.tag == .windows) {
30523117 if (rc == windows.ws2_32.SOCKET_ERROR) {
30533118 switch (windows.ws2_32.WSAGetLastError()) {
3054 // TODO: handle errors
3119 .WSANOTINITIALISED => unreachable,
3120 .WSAENETDOWN => return error.NetworkSubsystemFailed,
3121 .WSAEFAULT => unreachable, // addr or addrlen have invalid pointers or addrlen points to an incorrect value
3122 .WSAENOTSOCK => return error.FileDescriptorNotASocket,
3123 .WSAEINVAL => return error.SocketNotBound,
30553124 else => |err| return windows.unexpectedWSAError(err),
30563125 }
30573126 }
......@@ -3064,7 +3133,7 @@ pub fn getsockname(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) GetSock
30643133 EBADF => unreachable, // always a race condition
30653134 EFAULT => unreachable,
30663135 EINVAL => unreachable, // invalid parameters
3067 ENOTSOCK => unreachable,
3136 ENOTSOCK => return error.FileDescriptorNotASocket,
30683137 ENOBUFS => return error.SystemResources,
30693138 }
30703139 }
......@@ -4011,7 +4080,10 @@ fn setSockFlags(sock: socket_t, flags: u32) !void {
40114080 var mode: c_ulong = 1;
40124081 if (windows.ws2_32.ioctlsocket(sock, windows.ws2_32.FIONBIO, &mode) == windows.ws2_32.SOCKET_ERROR) {
40134082 switch (windows.ws2_32.WSAGetLastError()) {
4014 // TODO: handle errors
4083 .WSANOTINITIALISED => unreachable,
4084 .WSAENETDOWN => return error.NetworkSubsystemFailed,
4085 .WSAENOTSOCK => return error.FileDescriptorNotASocket,
4086 // TODO: handle more errors
40154087 else => |err| return windows.unexpectedWSAError(err),
40164088 }
40174089 }
......@@ -4625,6 +4697,8 @@ pub const SendError = error{
46254697 /// The local end has been shut down on a connection oriented socket. In this case, the
46264698 /// process will also receive a SIGPIPE unless MSG_NOSIGNAL is set.
46274699 BrokenPipe,
4700
4701 FileDescriptorNotASocket,
46284702} || UnexpectedError;
46294703
46304704/// Transmit a message to another socket.
......@@ -4666,7 +4740,12 @@ pub fn sendto(
46664740 if (builtin.os.tag == .windows) {
46674741 if (rc == windows.ws2_32.SOCKET_ERROR) {
46684742 switch (windows.ws2_32.WSAGetLastError()) {
4669 // TODO: handle errors
4743 .WSAEACCES => return error.AccessDenied,
4744 .WSAECONNRESET => return error.ConnectionResetByPeer,
4745 .WSAEMSGSIZE => return error.MessageTooBig,
4746 .WSAENOBUFS => return error.SystemResources,
4747 .WSAENOTSOCK => return error.FileDescriptorNotASocket,
4748 // TODO: handle more errors
46704749 else => |err| return windows.unexpectedWSAError(err),
46714750 }
46724751 } else {
......@@ -5158,6 +5237,20 @@ pub const RecvFromError = error{
51585237
51595238 /// Could not allocate kernel memory.
51605239 SystemResources,
5240
5241 ConnectionResetByPeer,
5242
5243 /// The socket has not been bound.
5244 SocketNotBound,
5245
5246 /// The UDP message was too big for the buffer and part of it has been discarded
5247 MessageTooBig,
5248
5249 /// The network subsystem has failed.
5250 NetworkSubsystemFailed,
5251
5252 /// The socket is not connected (connection-oriented sockets only).
5253 SocketNotConnected,
51615254} || UnexpectedError;
51625255
51635256pub fn recv(sock: socket_t, buf: []u8, flags: u32) RecvFromError!usize {
......@@ -5176,7 +5269,14 @@ pub fn recvfrom(
51765269 if (builtin.os.tag == .windows) {
51775270 if (rc == windows.ws2_32.SOCKET_ERROR) {
51785271 switch (windows.ws2_32.WSAGetLastError()) {
5179 // TODO: handle errors
5272 .WSANOTINITIALISED => unreachable,
5273 .WSAECONNRESET => return error.ConnectionResetByPeer,
5274 .WSAEINVAL => return error.SocketNotBound,
5275 .WSAEMSGSIZE => return error.MessageTooBig,
5276 .WSAENETDOWN => return error.NetworkSubsystemFailed,
5277 .WSAENOTCONN => return error.SocketNotConnected,
5278 .WSAEWOULDBLOCK => return error.WouldBlock,
5279 // TODO: handle more errors
51805280 else => |err| return windows.unexpectedWSAError(err),
51815281 }
51825282 } else {