Skip to main content
Be Bitwise
x86 AssemblyCapstone: System Calls & I/O

Socket Programming in Assembly

Network sockets extend the file descriptor abstraction to the network. Once you've set one up, reads and writes go through the same sys_read and sys_write you already know. The setup is where the work lives: creating the socket, binding it to an address, accepting connections — each step is its own syscall.

The Socket Lifecycle

A TCP server follows a specific sequence of syscalls:

  1. socket() - create a socket file descriptor
  2. bind() - associate it with a local address and port
  3. listen() - mark it as a passive (server) socket
  4. accept() - wait for and accept an incoming connection
  5. read() / write() - communicate with the client
  6. close() - shut down the connection

Creating a Socket

The socket syscall (number 41) creates a new socket and returns a file descriptor.

nasm
; socket(AF_INET, SOCK_STREAM, 0)
mov rax, 41 ; sys_socket
mov rdi, 2 ; AF_INET (IPv4)
mov rsi, 1 ; SOCK_STREAM (TCP)
mov rdx, 0 ; protocol (0 = default for type)
syscall
test rax, rax
js .socket_error
mov r12, rax ; save socket fd

Key constants:

  • AF_INET = 2 (IPv4)
  • AF_INET6 = 10 (IPv6)
  • SOCK_STREAM = 1 (TCP - reliable, ordered, connection-based)
  • SOCK_DGRAM = 2 (UDP - unreliable, unordered, connectionless)

Building sockaddr_in in Memory

The bind, connect, and accept syscalls all take a pointer to a sockaddr_in structure. In C that's a struct; here in assembly, we lay the bytes out by hand.

nasm
section .bss
sockaddr resb 16 ; struct sockaddr_in is 16 bytes
section .text
; Build sockaddr_in for 127.0.0.1:8080
; struct sockaddr_in {
; sa_family_t sin_family; // 2 bytes: AF_INET = 2
; in_port_t sin_port; // 2 bytes: port in network byte order (big-endian)
; in_addr_t sin_addr; // 4 bytes: IP address in network byte order
; char sin_zero[8]; // 8 bytes: padding
; }
lea rdi, [sockaddr]
mov word [rdi], 2 ; sin_family = AF_INET
mov word [rdi + 2], 0x901F ; sin_port = htons(8080) = 0x1F90 -> stored big-endian as 0x901F
mov dword [rdi + 4], 0x0100007F ; sin_addr = 127.0.0.1 (bytes 7F 00 00 01 in memory)
; bytes 8-15 are padding, already zeroed in .bss

The port has to be in network byte order — big-endian. Port 8080 is 0x1F90. Big-endian wants the high byte (0x1F) first in memory, but x86 stores little-endian, so we write 0x901F and the bytes come out in the right order. The address gets the same treatment: 127.0.0.1 has to sit in memory as 7F 00 00 01, which is the little-endian dword 0x0100007F.

Why 127.0.0.1 and not INADDR_ANY (0.0.0.0)? Binding to 0.0.0.0 accepts connections on every network interface, so this deliberately bare-bones server would be reachable from the rest of your network. Loopback keeps it on your own machine while you experiment.

These are the exact 16 bytes bind receives. Change the port and address, and check that the mov immediates match what you'd write by hand. Then type 0.0.0.0 to see what INADDR_ANY looks like.

sockaddr_in builder

16 bytes, x86-64 Linux
Bytes in memory, offset 0 to 15
02001f907f0000010000000000000000
sin_familyoffset 002 00AF_INET = 2, host byte order
sin_portoffset 21f 90network byte order (htons)
sin_addroffset 47f 00 00 01network byte order
sin_zerooffset 800 00 00 00 00 00 00 00padding, must be zero

Port 8080 is 0x1F90. htons puts the high byte first: 1f 90. Stored without htons, x86 would write 90 1f and the kernel would read port 36895.

mov word  [rdi],     2
mov word  [rdi + 2], 0x901F
mov dword [rdi + 4], 0x0100007F

Change the port or address. Hover a field to highlight its bytes.

Binding to an Address

The bind syscall (number 49) associates the socket with a local address and port.

nasm
; bind(sockfd, &sockaddr, sizeof(sockaddr_in))
mov rax, 49 ; sys_bind
mov rdi, r12 ; socket fd
lea rsi, [sockaddr] ; pointer to sockaddr_in
mov rdx, 16 ; sizeof(struct sockaddr_in)
syscall
test rax, rax
js .bind_error

"Address already in use" (errno 98 / EADDRINUSE) is the classic failure here. It means a previous instance of the server is still hanging around in TIME_WAIT. Production code dodges it by setting SO_REUSEADDR before binding, which costs you one setsockopt syscall (number 54). The skeleton below does exactly that.

Listening for Connections

The listen syscall (number 50) marks the socket as passive and sets the connection backlog.

nasm
; listen(sockfd, backlog)
mov rax, 50 ; sys_listen
mov rdi, r12 ; socket fd
mov rsi, 5 ; backlog: max pending connections
syscall
test rax, rax
js .listen_error

The backlog tells the kernel how many pending connections to queue before it starts dropping new ones on the floor. Five is plenty for poking around locally. Real servers run with hundreds or more.

Accepting Connections

The accept syscall (number 43) parks the process until a client shows up, then hands back a fresh file descriptor for that one connection. The listening socket carries on, ready for the next client.

nasm
section .bss
client_addr resb 16 ; will be filled with client's address
addr_len resd 1 ; will be filled with address length
section .text
; accept(sockfd, &client_addr, &addr_len)
mov dword [addr_len], 16 ; initialize with size of sockaddr_in
mov rax, 43 ; sys_accept
mov rdi, r12 ; listening socket fd
lea rsi, [client_addr] ; where to store client address
lea rdx, [addr_len] ; pointer to address length
syscall
test rax, rax
js .accept_error
mov r13, rax ; save the new client socket fd

Once accept returns, r13 holds a descriptor wired to the client. From here on, read and write against that fd exactly like a file.

Reading and Writing on a Socket

nasm
section .bss
recv_buf resb 1024
section .text
; Read from the client
mov rax, 0 ; sys_read
mov rdi, r13 ; client socket fd
lea rsi, [recv_buf]
mov rdx, 1024
syscall
test rax, rax
jle .client_done ; 0 = client disconnected, negative = error
mov r14, rax ; save byte count
; Echo it back to the client
mov rax, 1 ; sys_write
mov rdi, r13 ; client socket fd
lea rsi, [recv_buf]
mov rdx, r14 ; write exactly what we read
syscall

A Minimal Server Skeleton

Putting it all together:

nasm
section .data
one dd 1 ; option value for setsockopt
section .bss
sock_addr resb 16
cli_addr resb 16
cli_len resd 1
io_buf resb 1024
section .text
global _start
_start:
; 1. Create socket
mov rax, 41
mov rdi, 2 ; AF_INET
mov rsi, 1 ; SOCK_STREAM
xor rdx, rdx
syscall
mov r12, rax ; server socket fd
; 2. setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, 4)
mov rax, 54
mov rdi, r12
mov rsi, 1 ; SOL_SOCKET
mov rdx, 2 ; SO_REUSEADDR
lea r10, [one] ; 4th syscall argument goes in r10
mov r8, 4
syscall
; 3. Build sockaddr_in and bind
lea rdi, [sock_addr]
mov word [rdi], 2 ; AF_INET
mov word [rdi+2], 0x901F ; port 8080 big-endian
mov dword [rdi+4], 0x0100007F ; 127.0.0.1
mov rax, 49
mov rdi, r12
lea rsi, [sock_addr]
mov rdx, 16
syscall
; 4. Listen
mov rax, 50
mov rdi, r12
mov rsi, 5
syscall
.accept_loop:
; 5. Accept a connection
mov dword [cli_len], 16
mov rax, 43
mov rdi, r12
lea rsi, [cli_addr]
lea rdx, [cli_len]
syscall
mov r13, rax ; client fd
; 6. Read and echo
mov rax, 0
mov rdi, r13
lea rsi, [io_buf]
mov rdx, 1024
syscall
mov r14, rax
mov rax, 1
mov rdi, r13
lea rsi, [io_buf]
mov rdx, r14
syscall
; 7. Close client connection
mov rax, 3
mov rdi, r13
syscall
jmp .accept_loop

The skeleton leaves out error checks to keep the shape visible; the capstone adds them. One failure mode deserves a mention now. If the client hangs up before the echo goes out, write on that socket raises SIGPIPE, and the default action for that signal kills the whole server. Sending with sendto (syscall 44) and the MSG_NOSIGNAL flag (0x4000) in r10 turns that into a plain -EPIPE return you can handle.

Input from the Network Is Always Untrusted

Every byte coming off a socket is attacker-controlled. The data in recv_buf could be any sequence of octets — not just well-behaved text. Validate its length before you touch it, check for the format you expect, and never assume there's a null terminator hiding at the end. Our echo server uses read's return value (the actual byte count) rather than guessing at length or termination.

Security Perspective: Input Validation at the Assembly Level

At assembly level there are no string types, no parsing libraries, no automatic sanitisation. When you take data off a socket:

  • The buffer has a fixed size (1024 bytes here). read respects it because we pass that size in rdx. Copy the data anywhere else and you become responsible for tracking the bound yourself.
  • The data isn't null-terminated unless the sender stuck a null in there. Hand it to anything that expects a C string (like strlen via repne scasb) and you'll happily walk off the end of the buffer.
  • Embedded nulls in the middle of the data will confuse string-processing routines. Treat network bytes as raw octets with an explicit length, never as strings.

These aren't theoretical concerns. Most network-service vulnerabilities trace back to mishandled input at exactly this layer.

What is Next

Next lesson pulls everything from this module into a complete echo program. You'll build it piece by piece — from a stdin-only version to a fully network-aware one — with error handling and security thinking woven in.