authorgravatar for git@l4.pmluna <git@l4.pm> 2020-12-15 16:56:42-03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-12-15 14:56:42-05:00
log0c33624a45232848bbde90944338ec03615e6c5f
treeae53873041ee544bfa0d03e6e9e76d1e5d9e1c55
parent5f7352b5b59e69744fc176d7abc7b0a319e7afcb
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

create SendToError (#7417)

* add SendToError * remove error catch * add missing SendToError entries * add mappings to new errors for posix * map windows sendto() errors

2 files changed, 45 insertions(+), 4 deletions(-)

lib/std/fs/file.zig-1
......@@ -698,7 +698,6 @@ pub const File = struct {
698698 error.FastOpenAlreadyInProgress,
699699 error.MessageTooBig,
700700 error.FileDescriptorNotASocket,
701 error.AddressFamilyNotSupported,
702701 => return self.writeFileAllUnseekable(in_file, args),
703702
704703 else => |e| return e,
lib/std/os.zig+45-3
......@@ -4774,9 +4774,34 @@ pub const SendError = error{
47744774 BrokenPipe,
47754775
47764776 FileDescriptorNotASocket,
4777 AddressFamilyNotSupported,
47784777} || UnexpectedError;
47794778
4779pub const SendToError = SendError || error{
4780 /// The passed address didn't have the correct address family in its sa_family field.
4781 AddressFamilyNotSupported,
4782
4783 /// Returned when socket is AF_UNIX and the given path has a symlink loop.
4784 SymLinkLoop,
4785
4786 /// Returned when socket is AF_UNIX and the given path length exceeds `MAX_PATH_BYTES` bytes.
4787 NameTooLong,
4788
4789 /// Returned when socket is AF_UNIX and the given path does not point to an existing file.
4790 FileNotFound,
4791 NotDir,
4792
4793 /// Network is unreachable.
4794 NetworkUnreachable,
4795
4796 /// Insufficient memory was available to fulfill the request.
4797 SystemResources,
4798
4799 /// The socket is not connected (connection-oriented sockets only).
4800 SocketNotConnected,
4801 WouldBlock,
4802 AddressNotAvailable,
4803};
4804
47804805/// Transmit a message to another socket.
47814806///
47824807/// The `sendto` call may be used only when the socket is in a connected state (so that the intended
......@@ -4810,19 +4835,31 @@ pub fn sendto(
48104835 flags: u32,
48114836 dest_addr: ?*const sockaddr,
48124837 addrlen: socklen_t,
4813) SendError!usize {
4838) SendToError!usize {
48144839 while (true) {
48154840 const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen);
48164841 if (builtin.os.tag == .windows) {
48174842 if (rc == windows.ws2_32.SOCKET_ERROR) {
48184843 switch (windows.ws2_32.WSAGetLastError()) {
48194844 .WSAEACCES => return error.AccessDenied,
4845 .WSAEADDRNOTAVAIL => return error.AddressNotAvailable,
48204846 .WSAECONNRESET => return error.ConnectionResetByPeer,
48214847 .WSAEMSGSIZE => return error.MessageTooBig,
48224848 .WSAENOBUFS => return error.SystemResources,
48234849 .WSAENOTSOCK => return error.FileDescriptorNotASocket,
48244850 .WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported,
4825 // TODO: handle more errors
4851 .WSAEDESTADDRREQ => unreachable, // A destination address is required.
4852 .WSAEFAULT => unreachable, // The lpBuffers, lpTo, lpOverlapped, lpNumberOfBytesSent, or lpCompletionRoutine parameters are not part of the user address space, or the lpTo parameter is too small.
4853 .WSAEHOSTUNREACH => return error.NetworkUnreachable,
4854 // TODO: WSAEINPROGRESS, WSAEINTR
4855 .WSAEINVAL => unreachable,
4856 .WSAENETDOWN => return error.NetworkUnreachable,
4857 .WSAENETRESET => return error.ConnectionResetByPeer,
4858 .WSAENETUNREACH => return error.NetworkUnreachable,
4859 .WSAENOTCONN => return error.SocketNotConnected,
4860 .WSAESHUTDOWN => unreachable, // The socket has been shut down; it is not possible to WSASendTo on a socket after shutdown has been invoked with how set to SD_SEND or SD_BOTH.
4861 .WSAEWOULDBLOCK => return error.WouldBlock,
4862 .WSANOTINITIALISED => unreachable, // A successful WSAStartup call must occur before using this function.
48264863 else => |err| return windows.unexpectedWSAError(err),
48274864 }
48284865 } else {
......@@ -4850,6 +4887,11 @@ pub fn sendto(
48504887 EOPNOTSUPP => unreachable, // Some bit in the flags argument is inappropriate for the socket type.
48514888 EPIPE => return error.BrokenPipe,
48524889 EAFNOSUPPORT => return error.AddressFamilyNotSupported,
4890 ELOOP => return error.SymLinkLoop,
4891 ENAMETOOLONG => return error.NameTooLong,
4892 ENOENT => return error.FileNotFound,
4893 ENOTDIR => return error.NotDir,
4894 EHOSTUNREACH => return error.NetworkUnreachable,
48534895 else => |err| return unexpectedErrno(err),
48544896 }
48554897 }