tcp
TCP socket API built on direct Linux syscalls. Provides structs for socket addresses, TCP streams, and TCP listeners, along with byte-order conversion utilities.
Types
SockAddrIn
IPv4 socket address, matching the Linux sockaddr_in layout:
struct SockAddrIn {
sin_family: u16 // AF_INET = 2
sin_port: u16 // port in network byte order (big-endian)
sin_addr: u32 // IPv4 address in network byte order
sin_zero: [8]u8 // padding
}TcpStream
A connected TCP socket:
struct TcpStream {
fd: i32 // socket file descriptor
peer_addr: u32 // peer IPv4 address
peer_port: u16 // peer port number
}TcpListener
A listening TCP socket:
struct TcpListener {
fd: i32 // socket file descriptor
local_port: u16 // port being listened on
}Constants
| Constant | Value | Description |
|---|---|---|
AF_INET | 2 | IPv4 address family |
SOCK_STREAM | 1 | TCP socket type |
SOCK_NONBLOCK | 2048 | Non-blocking socket flag |
SOL_SOCKET | 1 | Socket-level options |
SO_REUSEADDR | 2 | Reuse local address |
SO_REUSEPORT | 15 | Reuse local port |
IPPROTO_TCP | 6 | TCP protocol number |
TCP_NODELAY | 1 | Disable Nagle’s algorithm |
O_NONBLOCK | 2048 | Non-blocking mode flag |
EAGAIN | 11 | Resource temporarily unavailable |
Usage
TCP Server
import tcp
fn main() {
// Create a listening socket
let addr = SockAddrIn {
sin_family: AF_INET,
sin_port: htons(8080),
sin_addr: htonl(0), // INADDR_ANY
sin_zero: [0, 0, 0, 0, 0, 0, 0, 0],
}
let fd = syscall(SYS_SOCKET, AF_INET, SOCK_STREAM, 0)
syscall(SYS_BIND, fd, &addr, 16)
syscall(SYS_LISTEN, fd, 128)
// Accept a connection
let client_fd = syscall(SYS_ACCEPT4, fd, 0, 0, 0)
// Read from client
let buf = [4096]i8
let n = syscall(SYS_RECVFROM, client_fd, buf, 4096, 0, 0)
// Send response
let resp = "HTTP/1.1 200 OK\r\n\r\nHello!"
syscall(SYS_SENDTO, client_fd, resp, 26, 0, 0)
tcp_close_fd(client_fd)
tcp_close_fd(fd)
}TCP Client
import tcp
fn main() {
let fd = syscall(SYS_SOCKET, AF_INET, SOCK_STREAM, 0)
let addr = SockAddrIn {
sin_family: AF_INET,
sin_port: htons(80),
sin_addr: ip_to_u32("93.184.216.34"),
sin_zero: [0, 0, 0, 0, 0, 0, 0, 0],
}
syscall(SYS_CONNECT, fd, &addr, 16)
let req = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n"
syscall(SYS_SENDTO, fd, req, 37, 0, 0)
let buf = [4096]i8
let n = syscall(SYS_RECVFROM, fd, buf, 4096, 0, 0)
tcp_close_fd(fd)
}Function Reference
| Function | Signature | Description |
|---|---|---|
tcp_close_fd | (fd: i32) | Close a socket file descriptor |
ip_to_u32 | (s: ref []i8) -> u32 | Parse dotted-quad IP string to network-order u32 |
htons | (x: u16) -> u16 | Host to network byte order (16-bit) |
ntohs | (x: u16) -> u16 | Network to host byte order (16-bit) |
htonl | (x: u32) -> u32 | Host to network byte order (32-bit) |
ntohl | (x: u32) -> u32 | Network to host byte order (32-bit) |
Detailed API
tcp_close_fd
fn tcp_close_fd(fd: i32)Close a socket file descriptor using the close syscall.
let fd = syscall(SYS_SOCKET, AF_INET, SOCK_STREAM, 0)
// ... use the socket ...
tcp_close_fd(fd)ip_to_u32
fn ip_to_u32(s: ref []i8) -> u32Parse a dotted-quad IPv4 address string (e.g. "192.168.1.1") into a network-byte-order u32.
let addr = ip_to_u32("127.0.0.1")
// addr is the loopback address in network byte orderhtons / ntohs
fn htons(x: u16) -> u16
fn ntohs(x: u16) -> u16Convert a 16-bit value between host byte order and network byte order (big-endian). On little-endian systems (x86-64), this swaps the bytes.
let port = htons(8080) // convert port for sockaddr_in
let local = ntohs(port) // convert back: 8080htonl / ntohl
fn htonl(x: u32) -> u32
fn ntohl(x: u32) -> u32Convert a 32-bit value between host byte order and network byte order.
let any = htonl(0) // INADDR_ANY in network order
let loopback = htonl(0x7F000001) // 127.0.0.1 in network orderSyscall Numbers
These constants map directly to Linux syscall numbers:
| Constant | Value | Syscall |
|---|---|---|
SYS_SOCKET | 41 | Create a socket |
SYS_BIND | 49 | Bind socket to address |
SYS_LISTEN | 50 | Listen for connections |
SYS_ACCEPT4 | 288 | Accept a connection |
SYS_CONNECT | 42 | Connect to remote address |
SYS_SENDTO | 44 | Send data on socket |
SYS_RECVFROM | 45 | Receive data from socket |
SYS_CLOSE | 3 | Close file descriptor |
SYS_SETSOCKOPT | 54 | Set socket option |
SYS_FCNTL | 72 | File control operations |