Socket Creation
sys_socket
Create an endpoint for communication.Communication domain:
PF_LOCAL/AF_LOCAL(1): Local/Unix domain socketsPF_INET/AF_INET(2): IPv4 Internet protocolsPF_INET6/AF_INET6(10): IPv6 Internet protocols
Socket type:
SOCK_STREAM(1): Stream socket (TCP)SOCK_DGRAM(2): Datagram socket (UDP)SOCK_RAW(3): Raw socketSOCK_NONBLOCK(0x800): Non-blocking flag (can be OR’d)SOCK_CLOEXEC(0x80000): Close-on-exec flag (can be OR’d)
Protocol to use:
0: Default protocol for typeIPPROTO_TCP: TCP protocolIPPROTO_UDP: UDP protocolIPPROTO_ICMP: ICMP protocolIPPROTO_RAW: Raw IP packets
Returns new socket file descriptor on success, negative errno on error.
EINVAL(-22): Invalid domain, type, or protocolEMFILE(-24): Too many open filesENOMEM(-12): Insufficient memoryEAFNOSUPPORT(-97): Address family not supportedEPROTONOSUPPORT(-93): Protocol not supported
sys_socketpair
Create a pair of connected sockets.Communication domain (typically AF_LOCAL).
Socket type (same as sys_socket).
Protocol (usually 0).
Address to store pair of socket file descriptors.
Returns 0 on success, negative errno on error.
Socket Connection
sys_bind
Bind a socket to an address.Socket file descriptor.
Address of sockaddr structure containing bind address.
Length of sockaddr structure.
Returns 0 on success, negative errno on error.
EBADF(-9): Invalid socket descriptorEINVAL(-22): Socket already bound or invalid addressEADDRINUSE(-98): Address already in useEACCES(-13): Permission deniedEFAULT(-14): Bad address pointer
sys_connect
Initiate a connection on a socket.Socket file descriptor.
Address of sockaddr structure containing peer address.
Length of sockaddr structure.
Returns 0 on success, negative errno on error.
EBADF(-9): Invalid socket descriptorECONNREFUSED(-111): Connection refusedETIMEDOUT(-110): Connection timed outENETUNREACH(-101): Network unreachableEINPROGRESS(-115): Non-blocking socket, connection in progressEISCONN(-106): Socket already connected
sys_listen
Listen for connections on a socket.Socket file descriptor.
Maximum length of pending connection queue.
Returns 0 on success, negative errno on error.
EBADF(-9): Invalid socket descriptorEOPNOTSUPP(-95): Socket doesn’t support listen
sys_accept / sys_accept4
Accept a connection on a socket.Listening socket file descriptor.
Address to store peer address. Can be 0 to ignore.
Address of length variable (input: buffer size, output: actual size).
(accept4 only) Flags:
SOCK_NONBLOCK(0x800): Set O_NONBLOCK on new socketSOCK_CLOEXEC(0x80000): Set close-on-exec on new socket
Returns new connected socket descriptor on success, negative errno on error.
EBADF(-9): Invalid socket descriptorEINVAL(-22): Socket not listeningEINTR(-4): Interrupted by signalEAGAIN(-11): No pending connections (non-blocking socket)
Data Transfer
sys_sendto
Send a message on a socket.Socket file descriptor.
Address of data buffer to send.
Number of bytes to send.
Send flags:
MSG_OOB(0x1): Out-of-band dataMSG_DONTWAIT(0x40): Non-blocking operationMSG_EOR(0x80): End of record
Destination address (for unconnected sockets). Can be 0 for connected sockets.
Length of destination address.
Returns number of bytes sent on success, negative errno on error.
EBADF(-9): Invalid socket descriptorENOTCONN(-107): Socket not connected (and no address provided)EMSGSIZE(-90): Message too largeENOBUFS(-105): No buffer space availableEPIPE(-32): Connection broken
sys_recvfrom
Receive a message from a socket.Socket file descriptor.
Address of buffer to receive data.
Size of receive buffer.
Receive flags:
MSG_PEEK(0x2): Peek at data without removing from queueMSG_WAITALL(0x100): Wait for full request or errorMSG_DONTWAIT(0x40): Non-blocking operation
Address to store sender address. Can be 0 to ignore.
Address of length variable for sender address.
Returns number of bytes received on success, 0 on orderly shutdown, or negative errno on error.
EBADF(-9): Invalid socket descriptorENOTCONN(-107): Socket not connectedEAGAIN(-11): No data available (non-blocking)EINTR(-4): Interrupted by signal
sys_sendmsg / sys_recvmsg
Send/receive message with ancillary data.Socket file descriptor.
Address of msghdr structure containing:
msg_name: Destination/source addressmsg_iov: Scatter/gather arraymsg_control: Ancillary data (e.g., file descriptors via SCM_RIGHTS)
Message flags (same as sendto/recvfrom).
Returns bytes sent/received on success, negative errno on error.
sys_shutdown
Shut down socket send and/or receive operations.Socket file descriptor.
Shutdown mode:
SHUT_RD(0): Disallow further receivesSHUT_WR(1): Disallow further sendsSHUT_RDWR(2): Disallow both sends and receives
Returns 0 on success, negative errno on error.
Socket Options
sys_setsockopt
Set socket options.Socket file descriptor.
Protocol level:
SOL_SOCKET(1): Socket levelIPPROTO_TCP(6): TCP levelIPPROTO_IP(0): IP levelIPPROTO_IPV6(41): IPv6 level
Option name:
- Socket level: SO_REUSEADDR, SO_KEEPALIVE, SO_SNDBUF, SO_RCVBUF, SO_LINGER
- TCP level: TCP_NODELAY, TCP_DEFER_ACCEPT
- IP level: IP_TOS, IP_TTL
Address of option value.
Length of option value.
Returns 0 on success, negative errno on error.
sys_getsockopt
Get socket options.Socket file descriptor.
Protocol level (same as setsockopt).
Option name:
- SO_TYPE, SO_ERROR, SO_DOMAIN, SO_PROTOCOL, SO_PEERCRED
Address to store option value.
Address of length variable (input: buffer size, output: actual size).
Returns 0 on success, negative errno on error.
Socket Information
sys_getsockname
Get local socket address.Socket file descriptor.
Address to store local socket address.
Address of length variable.
Returns 0 on success, negative errno on error.
sys_getpeername
Get peer socket address.Socket file descriptor.
Address to store peer socket address.
Address of length variable.
Returns 0 on success, negative errno on error.