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

ConstantValueDescription
AF_INET2IPv4 address family
SOCK_STREAM1TCP socket type
SOCK_NONBLOCK2048Non-blocking socket flag
SOL_SOCKET1Socket-level options
SO_REUSEADDR2Reuse local address
SO_REUSEPORT15Reuse local port
IPPROTO_TCP6TCP protocol number
TCP_NODELAY1Disable Nagle’s algorithm
O_NONBLOCK2048Non-blocking mode flag
EAGAIN11Resource 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

FunctionSignatureDescription
tcp_close_fd(fd: i32)Close a socket file descriptor
ip_to_u32(s: ref []i8) -> u32Parse dotted-quad IP string to network-order u32
htons(x: u16) -> u16Host to network byte order (16-bit)
ntohs(x: u16) -> u16Network to host byte order (16-bit)
htonl(x: u32) -> u32Host to network byte order (32-bit)
ntohl(x: u32) -> u32Network 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) -> u32

Parse 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 order

htons / ntohs

fn htons(x: u16) -> u16
fn ntohs(x: u16) -> u16

Convert 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: 8080

htonl / ntohl

fn htonl(x: u32) -> u32
fn ntohl(x: u32) -> u32

Convert 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 order

Syscall Numbers

These constants map directly to Linux syscall numbers:

ConstantValueSyscall
SYS_SOCKET41Create a socket
SYS_BIND49Bind socket to address
SYS_LISTEN50Listen for connections
SYS_ACCEPT4288Accept a connection
SYS_CONNECT42Connect to remote address
SYS_SENDTO44Send data on socket
SYS_RECVFROM45Receive data from socket
SYS_CLOSE3Close file descriptor
SYS_SETSOCKOPT54Set socket option
SYS_FCNTL72File control operations