Sockets

socket

Syscall 41 (socket) creates a socket. Arguments: domain (AF_INET = 2), type (SOCK_STREAM = 1), protocol (0 for TCP). The descriptor comes back in RAX:

%define SYS_SOCKET 41 mov rax, SYS_SOCKET mov rdi, 2 ; AF_INET mov rsi, 1 ; SOCK_STREAM xor rdx, rdx ; protocol 0 syscall

bind and listen

Syscall 49 (bind) takes the descriptor, a pointer to a sockaddr_in struct, and its length (16). Syscall 50 (listen) marks the socket as a listening socket with a backlog count:

%define SYS_BIND 49 %define SYS_LISTEN 50 mov rax, SYS_BIND mov rdi, fd mov rsi, addr ; sockaddr_in mov rdx, 16 syscall mov rax, SYS_LISTEN mov rdi, fd mov rsi, 10 ; backlog syscall

accept

Syscall 43 (accept) waits for a connection and returns a new descriptor for it. The sockaddr and length arguments can be NULL. From there, read (0) and write (1) work on the connection descriptor like a file.

Next: A tiny HTTP client

Article author: Arthur Isaev

Banner image