Skip to content

std.net.os

raw socket layer under std.net; for options the higher layers lack

pub sys_wsa_startup :: fn(version: u16, data: *u8) -> i32

os socket seam: Winsock on windows, syscalls on linux (Windows constants translated)

pub sys_wsa_cleanup :: fn() -> i32
pub sys_wsa_lasterr :: fn() -> i32
pub sys_socket :: fn(af: i32, kind: i32, proto: i32) -> usize
pub sys_closesocket :: fn(s: usize) -> i32
pub sys_connect :: fn(s: usize, name: *const u8, namelen: i32) -> i32
pub sys_bind :: fn(s: usize, name: *const u8, namelen: i32) -> i32
pub sys_listen :: fn(s: usize, backlog: i32) -> i32
pub sys_accept :: fn(s: usize, name: ?*u8, namelen: ?*i32) -> usize
pub sys_send :: fn(s: usize, buf: *const u8, len: i32, flags: i32) -> i32
pub sys_recv :: fn(s: usize, buf: *u8, len: i32, flags: i32) -> i32
pub sys_sendto :: fn(s: usize, buf: *const u8, len: i32, flags: i32, to: *const u8, tolen: i32) -> i32
pub sys_recvfrom :: fn(s: usize, buf: *u8, len: i32, flags: i32, from: *u8, fromlen: *i32) -> i32
pub sys_setsockopt :: fn(s: usize, level: i32, opt: i32, val: *const u8, len: i32) -> i32
pub sys_getsockname :: fn(s: usize, name: *u8, namelen: *i32) -> i32
pub sys_ioctlsocket :: fn(s: usize, cmd: u32, argp: *u32) -> i32
pub sys_shutdown :: fn(s: usize, how: i32) -> i32
pub sys_htons :: fn(x: u16) -> u16

host <-> network order for a 16-bit port (x86 little-endian)

pub sys_ntohs :: fn(x: u16) -> u16
pub AF_INET :: 2

protocol constants

pub SOCK_STREAM :: 1
pub SOCK_DGRAM :: 2
pub IPPROTO_TCP :: 6
pub IPPROTO_UDP :: 17
pub INVALID_SOCKET :: 0xFFFFFFFFFFFFFFFFusize
pub SOCKET_ERROR :: -1
pub SOCKADDR_LEN :: 16
pub SOL_SOCKET :: 0xFFFF

setsockopt levels and options

pub SO_REUSEADDR :: 0x0004
pub SO_BROADCAST :: 0x0020
pub SO_RCVTIMEO :: 0x1006
pub SO_SNDTIMEO :: 0x1005
pub TCP_NODELAY :: 0x0001
pub FIONBIO :: 0x8004667Eu32

ioctlsocket non-blocking mode; argp != 0 enables

pub SHUT_RECV :: 0

shutdown() directions

pub SHUT_SEND :: 1
pub SHUT_BOTH :: 2
pub WSAEWOULDBLOCK :: 10035
pub NetError :: errors {
startup_failed,
socket_failed,
connect_failed,
bind_failed,
listen_failed,
accept_failed,
send_failed,
recv_failed,
option_failed,
addr_failed,
would_block,
closed,
unknown_host,
}

shared by all of std.net; the variant names the failed call, last_error() has the code

  • startup_failed: WSAStartup
  • socket_failed: socket()
  • connect_failed: connect()
  • bind_failed: bind()
  • listen_failed: listen()
  • accept_failed: accept()
  • send_failed: send()/sendto()
  • recv_failed: recv()/recvfrom()
  • option_failed: setsockopt()/ioctlsocket()
  • addr_failed: getsockname()
  • would_block: a non-blocking call had no data ready
  • closed: peer closed the connection
  • unknown_host: a name with no address (resolve)
pub RawSockaddr :: struct {
family: u16,
port: u16,
addr: u32,
zero: u64,
}

sockaddr_in, 16 bytes: family, port and addr in network order, 8 zero bytes

pub raw_from :: fn(sa: addr::SocketAddr) -> RawSockaddr

host order -> network order

pub addr_from_raw :: fn(r: RawSockaddr) -> addr::SocketAddr

network order -> host order

pub init :: fn() -> void ! NetError

must be called once per process before any socket call

pub cleanup :: fn()

optional, at process exit

pub last_error :: fn() -> i32

last Winsock error on this thread

pub is_would_block :: fn() -> bool

last error was WSAEWOULDBLOCK