const std = @import("std"); const builtin = @import("builtin"); const windows = @import("windows.zig"); const posix = std.posix; pub const system = posix.system; pub const O = system.O; pub const F = system.F; pub const AF = posix.AF; pub const SO = posix.SO; pub const SOL = posix.SOL; pub const SOCK = posix.SOCK; pub const fd_t = posix.fd_t; pub const socket_t = posix.socket_t; pub const timeval = posix.timeval; pub const IPPROTO = posix.IPPROTO; pub const sockaddr = posix.sockaddr; pub const timespec = posix.timespec; pub const socklen_t = posix.socklen_t; pub const Kevent = system.Kevent; const native_os = builtin.os.tag; /// WARNING: this flag is not supported by windows socket functions directly, /// it is only supported by `socket` below. Be sure that this value does /// not share any bits with any of the `SOCK` values. pub const CLOEXEC = if (native_os == .windows) 0x10000 else SOCK.CLOEXEC; pub const NONBLOCK = if (native_os == .windows) 0x20000 else SOCK.NONBLOCK; pub fn socket(domain: u32, socket_type: u32, protocol: u32) !socket_t { if (native_os == .windows) { // These flags are not actually part of the Windows API, instead they are converted here for compatibility const filtered_sock_type = socket_type & ~@as(u32, NONBLOCK | CLOEXEC); var flags: u32 = windows.ws2_32.WSA_FLAG_OVERLAPPED; if ((socket_type & CLOEXEC) != 0) flags |= windows.ws2_32.WSA_FLAG_NO_HANDLE_INHERIT; const rc = try windows.WSASocketW( @bitCast(domain), @bitCast(filtered_sock_type), @bitCast(protocol), null, 0, flags, ); errdefer windows.closesocket(rc) catch unreachable; if ((socket_type & NONBLOCK) != 0) { var mode: c_ulong = 1; // nonblocking if (windows.ws2_32.SOCKET_ERROR == windows.ws2_32.ioctlsocket(rc, windows.ws2_32.FIONBIO, &mode)) { switch (windows.ws2_32.WSAGetLastError()) { // have not identified any error codes that should be handled yet else => unreachable, } } } return rc; } const have_sock_flags = !builtin.target.os.tag.isDarwin() and native_os != .haiku; const filtered_sock_type = if (!have_sock_flags) socket_type & ~@as(u32, NONBLOCK | CLOEXEC) else socket_type; const rc = posix.system.socket(domain, filtered_sock_type, protocol); switch (posix.errno(rc)) { .SUCCESS => { const fd: fd_t = @intCast(rc); errdefer close(fd); if (!have_sock_flags) { try setSockFlags(fd, socket_type); } return fd; }, .ACCES => return error.AccessDenied, .AFNOSUPPORT => return error.AddressFamilyNotSupported, .INVAL => return error.ProtocolFamilyNotAvailable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .PROTONOSUPPORT => return error.ProtocolNotSupported, .PROTOTYPE => return error.SocketTypeNotSupported, else => return error.Unexpected, } } fn setSockFlags(sock: socket_t, flags: u32) !void { if ((flags & CLOEXEC) != 0) { if (native_os == .windows) { // TODO: Find out if this is supported for sockets } else { var fd_flags = fcntl(sock, F.GETFD, 0) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, error.PermissionDenied => unreachable, error.DeadLock => unreachable, error.LockedRegionLimitExceeded => unreachable, else => |e| return e, }; fd_flags |= system.FD_CLOEXEC; _ = fcntl(sock, F.SETFD, fd_flags) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, error.PermissionDenied => unreachable, error.DeadLock => unreachable, error.LockedRegionLimitExceeded => unreachable, else => |e| return e, }; } } if ((flags & NONBLOCK) != 0) { if (native_os == .windows) { var mode: c_ulong = 1; if (windows.ws2_32.ioctlsocket(sock, windows.ws2_32.FIONBIO, &mode) == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENOTSOCK => return error.FileDescriptorNotASocket, // TODO: handle more errors else => |err| return windows.unexpectedWSAError(err), } } } else { var fl_flags = fcntl(sock, F.GETFL, 0) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, error.PermissionDenied => unreachable, error.DeadLock => unreachable, error.LockedRegionLimitExceeded => unreachable, else => |e| return e, }; fl_flags |= 1 << @bitOffsetOf(O, "NONBLOCK"); _ = fcntl(sock, F.SETFL, fl_flags) catch |err| switch (err) { error.FileBusy => unreachable, error.Locked => unreachable, error.PermissionDenied => unreachable, error.DeadLock => unreachable, error.LockedRegionLimitExceeded => unreachable, else => |e| return e, }; } } } pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) !usize { while (true) { const rc = posix.system.fcntl(fd, cmd, arg); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .INTR => continue, .AGAIN, .ACCES => return error.Locked, .BADF => unreachable, .BUSY => return error.FileBusy, .INVAL => unreachable, // invalid parameters .PERM => return error.PermissionDenied, .MFILE => return error.ProcessFdQuotaExceeded, .NOTDIR => unreachable, // invalid parameter .DEADLK => return error.DeadLock, .NOLCK => return error.LockedRegionLimitExceeded, else => return error.Unexpected, } } } pub fn close(fd: fd_t) void { if (native_os == .windows) { // httpz only ever passes socket handles to close; `closesocket` is the // proper API on Windows (CloseHandle skips Winsock's refcount/cleanup). windows.closesocket(fd) catch {}; return; } switch (posix.errno(system.close(fd))) { .BADF => unreachable, // Always a race condition. .INTR => return, // This is still a success. See https://github.com/ziglang/zig/issues/2425 else => return, } } pub fn setsockopt(fd: socket_t, level: i32, optname: u32, opt: []const u8) !void { if (native_os == .windows) { // Winsock takes SO_RCVTIMEO/SO_SNDTIMEO as a DWORD of milliseconds, // not a struct timeval. Translate so callers can stay portable. var ms_buf: u32 = 0; var opt_ptr: [*]const u8 = opt.ptr; var opt_len: i32 = @intCast(opt.len); if (level == SOL.SOCKET and (optname == SO.RCVTIMEO or optname == SO.SNDTIMEO) and opt.len == @sizeOf(timeval)) { const tv: *const timeval = @ptrCast(@alignCast(opt.ptr)); const total_ms = @as(i64, tv.sec) * 1000 + @divTrunc(@as(i64, tv.usec), 1000); ms_buf = if (total_ms < 0) 0 else @intCast(@min(total_ms, std.math.maxInt(u32))); opt_ptr = @ptrCast(&ms_buf); opt_len = @sizeOf(u32); } const rc = windows.ws2_32.setsockopt(fd, level, @intCast(optname), opt_ptr, opt_len); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAEFAULT => unreachable, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEINVAL => return error.SocketNotBound, else => |err| return windows.unexpectedWSAError(err), } } return; } else { switch (posix.errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(opt.len)))) { .SUCCESS => {}, .BADF => unreachable, // always a race condition .NOTSOCK => unreachable, // always a race condition .INVAL => unreachable, .FAULT => unreachable, .DOM => return error.TimeoutTooBig, .ISCONN => return error.AlreadyConnected, .NOPROTOOPT => return error.InvalidProtocolOption, .NOMEM => return error.SystemResources, .NOBUFS => return error.SystemResources, .PERM => return error.PermissionDenied, .NODEV => return error.NoDevice, .OPNOTSUPP => return error.OperationNotSupported, else => return error.Unexpected, } } } pub fn bind(sock: socket_t, addr: *const sockaddr, len: socklen_t) !void { if (native_os == .windows) { const rc = windows.bind(sock, addr, len); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, // not initialized WSA .WSAEACCES => return error.AccessDenied, .WSAEADDRINUSE => return error.AddressInUse, .WSAEADDRNOTAVAIL => return error.AddressNotAvailable, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEFAULT => unreachable, // invalid pointers .WSAEINVAL => return error.AlreadyBound, .WSAENOBUFS => return error.SystemResources, .WSAENETDOWN => return error.NetworkSubsystemFailed, else => |err| return windows.unexpectedWSAError(err), } unreachable; } return; } else { const rc = system.bind(sock, addr, len); switch (posix.errno(rc)) { .SUCCESS => return, .ACCES, .PERM => return error.AccessDenied, .ADDRINUSE => return error.AddressInUse, .BADF => unreachable, // always a race condition if this error is returned .INVAL => unreachable, // invalid parameters .NOTSOCK => unreachable, // invalid `sockfd` .AFNOSUPPORT => return error.AddressFamilyNotSupported, .ADDRNOTAVAIL => return error.AddressNotAvailable, .FAULT => unreachable, // invalid `addr` pointer .LOOP => return error.SymLinkLoop, .NAMETOOLONG => return error.NameTooLong, .NOENT => return error.FileNotFound, .NOMEM => return error.SystemResources, .NOTDIR => return error.NotDir, .ROFS => return error.ReadOnlyFileSystem, else => return error.Unexpected, } } unreachable; } pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) !void { if (native_os == .windows) { const rc = windows.ws2_32.connect(sock, sock_addr, @intCast(len)); if (rc == 0) return; switch (windows.ws2_32.WSAGetLastError()) { .WSAEADDRINUSE => return error.AddressInUse, .WSAEADDRNOTAVAIL => return error.AddressNotAvailable, .WSAECONNREFUSED => return error.ConnectionRefused, .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAETIMEDOUT => return error.ConnectionTimedOut, .WSAEHOSTUNREACH, .WSAENETUNREACH, => return error.NetworkUnreachable, .WSAEFAULT => unreachable, .WSAEINVAL => unreachable, .WSAEISCONN => unreachable, .WSAENOTSOCK => unreachable, .WSAEWOULDBLOCK => return error.WouldBlock, .WSAEACCES => unreachable, .WSAENOBUFS => return error.SystemResources, .WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported, else => |err| return windows.unexpectedWSAError(err), } return; } while (true) { switch (posix.errno(system.connect(sock, sock_addr, len))) { .SUCCESS => return, .ACCES => return error.AccessDenied, .PERM => return error.PermissionDenied, .ADDRINUSE => return error.AddressInUse, .ADDRNOTAVAIL => return error.AddressNotAvailable, .AFNOSUPPORT => return error.AddressFamilyNotSupported, .AGAIN, .INPROGRESS => return error.WouldBlock, .ALREADY => return error.ConnectionPending, .BADF => unreachable, .CONNREFUSED => return error.ConnectionRefused, .CONNRESET => return error.ConnectionResetByPeer, .FAULT => unreachable, .INTR => continue, .ISCONN => unreachable, .HOSTUNREACH => return error.NetworkUnreachable, .NETUNREACH => return error.NetworkUnreachable, .NOTSOCK => unreachable, .PROTOTYPE => unreachable, .TIMEDOUT => return error.ConnectionTimedOut, .NOENT => return error.FileNotFound, .CONNABORTED => unreachable, else => return error.Unexpected, } } } pub fn getsockname(sock: socket_t, addr: *sockaddr, addrlen: *socklen_t) !void { if (native_os == .windows) { const rc = windows.getsockname(sock, addr, addrlen); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAEFAULT => unreachable, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEINVAL => return error.SocketNotBound, else => |err| return windows.unexpectedWSAError(err), } } return; } else { const rc = system.getsockname(sock, addr, addrlen); switch (posix.errno(rc)) { .SUCCESS => return, .BADF => unreachable, .FAULT => unreachable, .INVAL => unreachable, .NOTSOCK => return error.FileDescriptorNotASocket, .NOBUFS => return error.SystemResources, else => return error.Unexpected, } } } pub const Address = extern union { any: posix.sockaddr, in: posix.sockaddr.in, in6: posix.sockaddr.in6, un: if (@hasDecl(posix.sockaddr, "un")) posix.sockaddr.un else posix.sockaddr, pub fn initUnix(path: []const u8) !Address { var sock_addr = posix.sockaddr.un{ .family = AF.UNIX, .path = undefined, }; // Add 1 to ensure a terminating 0 is present in the path array for maximum portability. if (path.len + 1 > sock_addr.path.len) { return error.NameTooLong; } @memset(&sock_addr.path, 0); @memcpy(sock_addr.path[0..path.len], path); return .{ .un = sock_addr }; } pub fn initIp4(addr: [4]u8, port: u16) !Address { return .{ .in = .{ .port = std.mem.nativeToBig(u16, port), .addr = @as(*align(1) const u32, @ptrCast(&addr)).*, } }; } pub fn initIp6(addr: [16]u8, port: u16, flowinfo: u32, scope_id: u32) !Address { return .{ .in6 = .{ .addr = addr, .port = std.mem.nativeToBig(u16, port), .flowinfo = flowinfo, .scope_id = scope_id, } }; } pub fn getOsSockLen(self: Address) posix.socklen_t { return switch (self.any.family) { posix.AF.INET => @sizeOf(posix.sockaddr.in), posix.AF.INET6 => @sizeOf(posix.sockaddr.in6), posix.AF.UNIX => if (@hasDecl(posix.sockaddr, "un")) @intCast(@offsetOf(posix.sockaddr.un, "path") + std.mem.indexOfScalar(u8, &self.un.path, 0).? + 1) else @sizeOf(posix.sockaddr), else => @sizeOf(posix.sockaddr), }; } pub fn toIOAddress(self: Address) std.Io.net.IpAddress { return switch (self.any.family) { posix.AF.INET => { const bytes: *const [4]u8 = @ptrCast(&self.in.addr); return .{ .ip4 = .{ .bytes = bytes.*, .port = std.mem.bigToNative(u16, self.in.port) } }; }, posix.AF.INET6 => { // @ZIG016 I don't think this is correct const bytes: *const [16]u8 = @ptrCast(&self.in.addr); return .{ .ip6 = .{ .bytes = bytes.*, .port = std.mem.bigToNative(u16, self.in.port) } }; }, else => .{ .ip4 = .unspecified(0) }, }; } pub fn format(self: Address, w: *std.Io.Writer) std.Io.Writer.Error!void { return self.toIOAddress().format(w); } }; pub fn listen(sock: socket_t, backlog: u31) !void { if (native_os == .windows) { const rc = windows.listen(sock, backlog); if (rc == windows.ws2_32.SOCKET_ERROR) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, // not initialized WSA .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAEADDRINUSE => return error.AddressInUse, .WSAEISCONN => return error.AlreadyConnected, .WSAEINVAL => return error.SocketNotBound, .WSAEMFILE, .WSAENOBUFS => return error.SystemResources, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => return error.OperationNotSupported, .WSAEINPROGRESS => unreachable, else => |err| return windows.unexpectedWSAError(err), } } return; } else { const rc = system.listen(sock, backlog); switch (posix.errno(rc)) { .SUCCESS => return, .ADDRINUSE => return error.AddressInUse, .BADF => unreachable, .NOTSOCK => return error.FileDescriptorNotASocket, .OPNOTSUPP => return error.OperationNotSupported, else => return error.Unexpected, } } } pub fn accept( /// This argument is a socket that has been created with `socket`, bound to a local address /// with `bind`, and is listening for connections after a `listen`. sock: socket_t, /// This argument is a pointer to a sockaddr structure. This structure is filled in with the /// address of the peer socket, as known to the communications layer. The exact format of the /// address returned addr is determined by the socket's address family (see `socket` and the /// respective protocol man pages). addr: ?*sockaddr, /// This argument is a value-result argument: the caller must initialize it to contain the /// size (in bytes) of the structure pointed to by addr; on return it will contain the actual size /// of the peer address. /// /// The returned address is truncated if the buffer provided is too small; in this case, `addr_size` /// will return a value greater than was supplied to the call. addr_size: ?*socklen_t, /// The following values can be bitwise ORed in flags to obtain different behavior: /// * `SOCK.NONBLOCK` - Set the `NONBLOCK` file status flag on the open file description (see `open`) /// referred to by the new file descriptor. Using this flag saves extra calls to `fcntl` to achieve /// the same result. /// * `SOCK.CLOEXEC` - Set the close-on-exec (`FD_CLOEXEC`) flag on the new file descriptor. See the /// description of the `CLOEXEC` flag in `open` for reasons why this may be useful. flags: u32, ) !socket_t { const have_accept4 = !(builtin.target.os.tag.isDarwin() or native_os == .windows or native_os == .haiku); std.debug.assert(0 == (flags & ~@as(u32, NONBLOCK | CLOEXEC))); // Unsupported flag(s) const accepted_sock: socket_t = while (true) { const rc = if (have_accept4) system.accept4(sock, addr, addr_size, flags) else if (native_os == .windows) windows.accept(sock, addr, addr_size) else system.accept(sock, addr, addr_size); if (native_os == .windows) { if (rc == windows.ws2_32.INVALID_SOCKET) { switch (windows.ws2_32.WSAGetLastError()) { .WSANOTINITIALISED => unreachable, // not initialized WSA .WSAECONNRESET => return error.ConnectionResetByPeer, .WSAEFAULT => unreachable, .WSAENOTSOCK => return error.FileDescriptorNotASocket, .WSAEINVAL => return error.SocketNotListening, .WSAEMFILE => return error.ProcessFdQuotaExceeded, .WSAENETDOWN => return error.NetworkSubsystemFailed, .WSAENOBUFS => return error.FileDescriptorNotASocket, .WSAEOPNOTSUPP => return error.OperationNotSupported, .WSAEWOULDBLOCK => return error.WouldBlock, else => |err| return windows.unexpectedWSAError(err), } } else { break rc; } } else { switch (posix.errno(rc)) { .SUCCESS => break @intCast(rc), .INTR => continue, .AGAIN => return error.WouldBlock, .BADF => { // ZIG016 This is not right. If we hit this, it's always certainly // an error - we're trying to read from a socket after it's been closed // which is not safe. But, the code around this worked in 0.15 and I // consider this entire 0.16 transition experimental. return error.SocketNotListening; }, .CONNABORTED => return error.ConnectionAborted, .FAULT => unreachable, .INVAL => return error.SocketNotListening, .NOTSOCK => unreachable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .OPNOTSUPP => unreachable, .PROTO => return error.ProtocolFailure, .PERM => return error.BlockedByFirewall, else => return error.Unexpected, } } }; errdefer switch (native_os) { .windows => windows.closesocket(accepted_sock) catch unreachable, else => close(accepted_sock), }; if (!have_accept4) { try setSockFlags(accepted_sock, flags); } return accepted_sock; } pub const iovec_const = posix.iovec_const; /// Union of read errors across platforms. Declared explicitly so callers can /// switch on `error.WouldBlock` portably regardless of OS. pub const ReadError = error{ WouldBlock, ProcessNotFound, Canceled, NotOpenForReading, InputOutput, IsDir, SystemResources, SocketNotConnected, ConnectionResetByPeer, ConnectionTimedOut, NetworkSubsystemFailed, Unexpected, }; pub const WriteError = error{ WouldBlock, InvalidArgument, ProcessNotFound, NotOpenForWriting, DiskQuota, FileTooBig, InputOutput, NoSpaceLeft, AccessDenied, PermissionDenied, BrokenPipe, ConnectionResetByPeer, DeviceBusy, NoDevice, MessageTooBig, SystemResources, SocketNotConnected, NetworkSubsystemFailed, Unexpected, }; pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize { if (native_os == .windows) { // TODO improve this to use WSASend with the full iovec if (iov.len == 0) return 0; const first = iov[0]; return write(fd, first.base[0..first.len]); } while (true) { const rc = system.writev(fd, iov.ptr, @min(iov.len, posix.IOV_MAX)); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .INTR => continue, .INVAL => return error.InvalidArgument, .FAULT => unreachable, .SRCH => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // Can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .PERM => return error.PermissionDenied, .PIPE => return error.BrokenPipe, .CONNRESET => return error.ConnectionResetByPeer, .BUSY => return error.DeviceBusy, else => return error.Unexpected, } } } pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize { if (bytes.len == 0) return 0; if (native_os == .windows) { return windows.send(fd, bytes); } const max_count = switch (native_os) { .linux => 0x7ffff000, .macos, .ios, .watchos, .tvos, .visionos => std.math.maxInt(i32), else => std.math.maxInt(isize), }; while (true) { const rc = system.write(fd, bytes.ptr, @min(bytes.len, max_count)); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .INTR => continue, .INVAL => return error.InvalidArgument, .FAULT => unreachable, .SRCH => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .BADF => return error.NotOpenForWriting, // can be a race condition. .DESTADDRREQ => unreachable, // `connect` was never called. .DQUOT => return error.DiskQuota, .FBIG => return error.FileTooBig, .IO => return error.InputOutput, .NOSPC => return error.NoSpaceLeft, .ACCES => return error.AccessDenied, .PERM => return error.PermissionDenied, .PIPE => return error.BrokenPipe, .CONNRESET => return error.ConnectionResetByPeer, .BUSY => return error.DeviceBusy, .NXIO => return error.NoDevice, .MSGSIZE => return error.MessageTooBig, else => return error.Unexpected, } } } pub fn read(fd: fd_t, buf: []u8) ReadError!usize { if (buf.len == 0) return 0; if (native_os == .windows) { return windows.recv(fd, buf); } // Prevents EINVAL. const max_count = switch (native_os) { .linux => 0x7ffff000, .macos, .ios, .watchos, .tvos, .visionos => std.math.maxInt(i32), else => std.math.maxInt(isize), }; while (true) { const rc = system.read(fd, buf.ptr, @min(buf.len, max_count)); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .INTR => continue, .INVAL => unreachable, .FAULT => unreachable, .SRCH => return error.ProcessNotFound, .AGAIN => return error.WouldBlock, .CANCELED => return error.Canceled, .BADF => return error.NotOpenForReading, // Can be a race condition. .IO => return error.InputOutput, .ISDIR => return error.IsDir, .NOBUFS => return error.SystemResources, .NOMEM => return error.SystemResources, .NOTCONN => return error.SocketNotConnected, .CONNRESET => return error.ConnectionResetByPeer, .TIMEDOUT => return error.ConnectionTimedOut, else => return error.Unexpected, } } } pub const ShutdownHow = enum { recv, send, both }; /// Shutdown socket send/receive operations pub fn shutdown(sock: socket_t, how: ShutdownHow) !void { if (native_os == .windows) { const result = windows.ws2_32.shutdown(sock, switch (how) { .recv => windows.ws2_32.SD_RECEIVE, .send => windows.ws2_32.SD_SEND, .both => windows.ws2_32.SD_BOTH, }); if (0 != result) switch (windows.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), }; } else { const rc = system.shutdown(sock, switch (how) { .recv => posix.SHUT.RD, .send => posix.SHUT.WR, .both => posix.SHUT.RDWR, }); switch (posix.errno(rc)) { .SUCCESS => return, .BADF => unreachable, .INVAL => unreachable, .NOTCONN => return error.SocketNotConnected, .NOTSOCK => unreachable, .NOBUFS => return error.SystemResources, else => return error.Unexpected, } } } pub fn kevent( kq: i32, changelist: []const Kevent, eventlist: []Kevent, timeout: ?*const timespec, ) !usize { while (true) { const rc = system.kevent( kq, changelist.ptr, std.math.cast(c_int, changelist.len) orelse return error.Overflow, eventlist.ptr, std.math.cast(c_int, eventlist.len) orelse return error.Overflow, timeout, ); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .ACCES => return error.AccessDenied, .FAULT => unreachable, .BADF => unreachable, // Always a race condition. .INTR => continue, .INVAL => unreachable, .NOENT => return error.EventNotFound, .NOMEM => return error.SystemResources, .SRCH => return error.ProcessNotFound, else => unreachable, } } } pub fn kqueue() !i32 { const rc = system.kqueue(); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, else => return error.Unexpected, } } pub fn eventfd(initval: u32, flags: u32) !i32 { const rc = system.eventfd(initval, flags); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .INVAL => unreachable, // invalid parameters .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NODEV => return error.SystemResources, .NOMEM => return error.SystemResources, else => return error.Unexpected, } } pub fn epoll_create1(flags: u32) !i32 { const rc = system.epoll_create1(flags); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .INVAL => unreachable, .MFILE => return error.ProcessFdQuotaExceeded, .NFILE => return error.SystemFdQuotaExceeded, .NOMEM => return error.SystemResources, else => return error.Unexpected, } } pub fn epoll_ctl(epfd: i32, op: u32, fd: i32, event: ?*system.epoll_event) !void { const rc = system.epoll_ctl(epfd, op, fd, event); switch (posix.errno(rc)) { .SUCCESS => return, .BADF => unreachable, // always a race condition if this happens .EXIST => return error.FileDescriptorAlreadyPresentInSet, .INVAL => unreachable, .LOOP => return error.OperationCausesCircularLoop, .NOENT => return error.FileDescriptorNotRegistered, .NOMEM => return error.SystemResources, .NOSPC => return error.UserResourceLimitReached, .PERM => return error.FileDescriptorIncompatibleWithEpoll, else => return error.Unexpected, } } /// Waits for an I/O event on an epoll file descriptor. /// Returns the number of file descriptors ready for the requested I/O, /// or zero if no file descriptor became ready during the requested timeout milliseconds. pub fn epoll_wait(epfd: i32, events: []system.epoll_event, timeout: i32) usize { while (true) { // TODO get rid of the @intCast const rc = system.epoll_wait(epfd, events.ptr, @intCast(events.len), timeout); switch (posix.errno(rc)) { .SUCCESS => return @intCast(rc), .INTR => continue, .BADF => unreachable, .FAULT => unreachable, .INVAL => unreachable, else => unreachable, } } }