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.

This complete program creates one TCP socket, closes it and prints a marker, so it needs no network peer:

default rel %define SYS_SOCKET 41 %define SYS_CLOSE 3 %define SYS_WRITE 1 %define SYS_EXIT 60 %define AF_INET 2 %define SOCK_STREAM 1 section .data msg db "socket", 10 section .text global _start _start: mov rax, SYS_SOCKET mov rdi, AF_INET mov rsi, SOCK_STREAM xor rdx, rdx ; protocol 0 syscall test rax, rax js failed mov rdi, rax mov rax, SYS_CLOSE syscall mov rax, SYS_WRITE mov rdi, 1 mov rsi, msg mov rdx, 7 syscall mov rax, SYS_EXIT xor rdi, rdi syscall failed: mov rax, SYS_EXIT mov rdi, 1 syscall

nasm -f elf64 -o sock.o sock.asm
ld -o sock sock.o
./sock
echo $?

socket
0

The first snippets are fragments: they show the calls and their arguments. The complete program was assembled with NASM 3.01, linked with GNU ld 2.45.1, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which syscall number creates a socket on x86-64 Linux?

    Answer

    41

    socket is 41; connect is 42, accept 43 and bind 49.

  2. Which domain constant is AF_INET?

    Answer

    2

    AF_INET is 2 for IPv4; AF_INET6 is 10.

  3. Which socket type is SOCK_STREAM?

    Answer

    1

    SOCK_STREAM is 1 for TCP; SOCK_DGRAM is 2 for UDP.

  4. What does the protocol argument 0 mean for a TCP socket?

    Answer

    The default protocol for the type

    A stream socket with protocol 0 selects TCP automatically.

  5. What does bind need besides the socket descriptor?

    Answer

    A pointer to a sockaddr and its length

    bind fills in the local address from the sockaddr_in structure.

  6. What does listen take as its second argument?

    Answer

    The backlog of pending connections

    The backlog limits how many connections may wait before accept.

  7. What does this program print? default rel %define SYS_SOCKET 41 %define SYS_CLOSE 3 %define SYS_WRITE 1 %define SYS_EXIT 60 %define AF_INET 2 %define SOCK_STREAM 1 section .data msg db "socket", 10 section .text global _start _start: mov rax, SYS_SOCKET mov rdi, AF_INET mov rsi, SOCK_STREAM xor rdx, rdx syscall test rax, rax js failed mov rdi, rax mov rax, SYS_CLOSE syscall mov rax, SYS_WRITE mov rdi, 1 mov rsi, msg mov rdx, 7 syscall mov rax, SYS_EXIT xor rdi, rdi syscall failed: mov rax, SYS_EXIT mov rdi, 1 syscall

    Answer

    socket

    The program only creates a socket, closes it and prints the marker.

  8. What does accept return?

    Answer

    A new descriptor for the connection

    The listening socket stays open, and each accepted connection has its own descriptor.

  9. How do read and write treat an accepted connection?

    Answer

    Like a file descriptor

    Once connected, the descriptor carries a byte stream that read and write handle.

  10. What exit status does the socket program return when the socket call succeeds?

    Answer

    0

    The success path clears rdi before sys_exit.

Article author: Arthur Isaev