// Bounded, cancellation-aware TCP client sockets for native Linux programs. // The caller supplies a literal IPv4 address; this module performs no DNS, // HTTP, TLS, or plaintext-protocol fallback. libc :: #system_library "libc"; LinuxSocket :: struct { fd: s32 valid: bool error: s32 } LinuxSocketEvent :: struct { ready: s32 error: s32 } LinuxSocketAddress :: struct { family: u16 port: u16 address: u32 zero: [8]u8 } LinuxPollFd :: struct { fd: s32 events: s16 revents: s16 } SocketRaw :: (domain: s32, type: s32, protocol: s32) -> s32 #foreign libc "socket"; ConnectRaw :: (fd: s32, address: *LinuxSocketAddress, length: u32) -> s32 #foreign libc "connect"; // nfds_t is unsigned long on Linux, which is pointer-sized. PollRaw :: (fds: *LinuxPollFd, count: usize, timeout_ms: s32) -> s32 #foreign libc "poll"; GetSocketOptionRaw :: (fd: s32, level: s32, option: s32, value: *s32, length: *u32) -> s32 #foreign libc "getsockopt"; SendRaw :: (fd: s32, output: *u8, count: usize, flags: s32) -> isize #foreign libc "send"; ReceiveRaw :: (fd: s32, output: *u8, count: usize, flags: s32) -> isize #foreign libc "recv"; ShutdownRaw :: (fd: s32, how: s32) -> s32 #foreign libc "shutdown"; CloseRaw :: (fd: s32) -> s32 #foreign libc "close"; ErrnoLocation :: () -> *s32 #foreign libc "__errno_location"; LinuxErrno :: () -> s32 { return ErrnoLocation().* } SocketWaitForEvents :: (socket: LinuxSocket, events: s32, timeout_ms: s32) -> LinuxSocketEvent { result: LinuxSocketEvent waiter: [1]LinuxPollFd waiter[0].fd = socket.fd waiter[0].events = cast(s16)events ready: s32 = PollRaw(*waiter[0], 1, timeout_ms) if ready == 0 { result.error = 110 return result } if ready < 0 { result.error = LinuxErrno() return result } result.ready = cast(s32)waiter[0].revents return result } SocketConnectError :: (socket: LinuxSocket) -> s32 { value: [1]s32 length: [1]u32 length[0] = 4 if GetSocketOptionRaw(socket.fd, 1, 4, *value[0], *length[0]) != 0 { return LinuxErrno() } return value[0] } #program_export OpenTCPv4 :: (address: [4]u8, port: u16, timeout_ms: s32) -> LinuxSocket { result: LinuxSocket fd: s32 = SocketRaw(2, 526337, 0) if fd < 0 { result.error = LinuxErrno() return result } endpoint: [1]LinuxSocketAddress endpoint[0].family = cast(u16)2 endpoint[0].port = cast(u16)(((port & 255) << 8) | (port >> 8)) endpoint[0].address = cast(u32)address[0] | (cast(u32)address[1] << 8) | (cast(u32)address[2] << 16) | (cast(u32)address[3] << 24) result.fd = fd status: s32 = ConnectRaw(fd, *endpoint[0], 16) if status < 0 { errno: s32 = LinuxErrno() if errno != 115 { result.error = errno CloseRaw(fd) result.fd = -1 return result } writable: LinuxSocketEvent = SocketWaitForEvents(result, 4 | 8, timeout_ms) if writable.error != 0 { result.error = writable.error CloseRaw(fd) result.fd = -1 return result } connect_error: s32 = SocketConnectError(result) if connect_error != 0 { result.error = connect_error CloseRaw(fd) result.fd = -1 return result } } result.valid = true return result } // Signal write EOF while keeping this owned descriptor readable. Repeating a // successful write shutdown is harmless; SocketClose still releases ownership. #program_export SocketCloseWrite :: (socket: LinuxSocket) -> bool { if !socket.valid { return false } return ShutdownRaw(socket.fd, 1) == 0 } #program_export SocketCancel :: (socket: LinuxSocket) -> bool { if !socket.valid { return false } return ShutdownRaw(socket.fd, 2) == 0 } #program_export SocketSendAll :: (socket: LinuxSocket, input: []u8, timeout_ms: s32) -> s64 { if !socket.valid { return -1 } sent: s64 = 0 while sent < cast(s64)input.count { writable: LinuxSocketEvent = SocketWaitForEvents(socket, 4 | 8, timeout_ms) if writable.error != 0 { return -1 } count: isize = SendRaw(socket.fd, *input[sent], cast(usize)(cast(s64)input.count - sent), 16384) if count > cast(isize)0 { sent += cast(s64)count } else if count == cast(isize)0 { return -1 } else if count < cast(isize)0 { errno: s32 = LinuxErrno() if errno != 11 && errno != 4 { return -1 } } } return sent } #program_export SocketReceive :: (socket: LinuxSocket, output: []u8, timeout_ms: s32) -> s64 { if !socket.valid { return -1 } if output.count == 0 { return 0 } readable: LinuxSocketEvent = SocketWaitForEvents(socket, 1 | 8, timeout_ms) if readable.error != 0 { return -1 } count: isize = ReceiveRaw(socket.fd, *output[0], cast(usize)output.count, 0) if count < cast(isize)0 { return -1 } return cast(s64)count } #program_export SocketClose :: (socket: *LinuxSocket) -> bool { if !socket.valid { return false } closed: bool = CloseRaw(socket.fd) == 0 if closed { socket.fd = -1 socket.valid = false } return closed }