LISTEN(2)

okay so bestie, sit down, grab a boba πŸ§‹ and let me tell you about LISTEN(2) β€” that's the page from the System Calls Manual, and it's literally the glow-up your sockets need before they can make friends. we're doing this. πŸ’–


πŸŽ€ NAME πŸŽ€

listen β€” listen for connections on a socket πŸ¦‹

(yes that's the whole vibe: your socket is out here waiting for people to slide into its DMs, no cap)

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) πŸ’…

✨ SYNOPSIS ✨

#include <sys/socket.h>

int
listen(int s, int backlog);

🌸 DESCRIPTION 🌸

so here's the whole plot, bestie. to accept connections, you first create a socket with socket(2), then you declare a willingness to accept incoming connections and set a queue limit for incoming connections using listen(), and then you actually accept the connections with accept(2). it's a whole three-step era and you cannot skip a step, i don't make the rules. πŸ’…

the listen() system call applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET. like, if your socket is a different type, this is simply not its moment. πŸ™ˆ

the backlog argument defines the maximum length the queue of pending connections may grow to. plot twist though β€” the real maximum queue length will be 1.5 times more than the value specified in the backlog argument. math is math. ✨

a subsequent listen() system call on the listening socket allows the caller to change the maximum queue length using a new backlog argument. glow-up on demand, honestly.

if a connection request arrives with the queue full, the client may receive an error with an indication of ECONNREFUSED, or, in the case of TCP, the connection will be silently dropped. silently. dropped. ghosted, literally ghosted, no explanation, no text back. πŸ‘»

current queue lengths of listening sockets can be queried using the netstat(1) command. πŸ‘‘

note that before FreeBSD 4.5 and the introduction of the syncache, the backlog argument also determined the length of the incomplete connection queue, which held TCP sockets in the process of completing TCP's 3-way handshake. these incomplete connections are now held entirely in the syncache, which is unaffected by queue lengths. so inflating backlog values to help handle denial of service attacks? no longer necessary. we love an upgrade. 🫢

the sysctl(3) MIB variable kern.ipc.soacceptqueue specifies a hard limit on backlog; if a value greater than kern.ipc.soacceptqueue or less than zero is specified, backlog is silently forced to kern.ipc.soacceptqueue. the kernel really said "no thoughts, just the limit." πŸ’€


πŸŽ€ INTERACTION WITH ACCEPT FILTERS πŸŽ€

when accept filtering is used on a socket, a second queue will be used to hold sockets that have connected, but have not yet met their accept filtering criteria. once the criteria has been met, these sockets will be moved over into the completed connection queue to be accept(2)ed. moving up in the world, we love to see it. 🌈

but β€” if this secondary queue is full and a new connection comes in, the oldest socket which has not yet met its accept filter criteria will be terminated. terminated. first in, first out, and out means out. 😭

this secondary queue, like the primary listen queue, is sized according to the backlog argument. everything is backlog, it's giving main character energy fr fr. πŸ’…


🌈 RETURN VALUES 🌈

the listen() function returns the value 0 if successful; otherwise, the value -1 is returned and the global variable errno is set to indicate the error.

so: 0 = slay ✨, -1 = go read your errno bestie 🫢


πŸ’€ ERRORS πŸ’€

the listen() system call will fail if:

  • [EBADF] β€” the argument s is not a valid descriptor. (that's not a real fd, babes πŸ’€)
  • [EDESTADDRREQ] β€” the socket is not bound to a local address, and the protocol does not support listening on an unbound socket. (nowhere to be found, no address, no service 😭)
  • [EINVAL] β€” the socket is already connected, or in the process of being connected. (it's already taken, sorry πŸ’…)
  • [ENOTSOCK] β€” the argument s is not a socket. (you brought the wrong thing to the party πŸ™ˆ)
  • [EOPNOTSUPP] β€” the socket is not of a type that supports the operation listen(). (this type simply does not do listen(), and that's that πŸŽ€)

πŸ‘‘ SEE ALSO πŸ‘‘

netstat(1), accept(2), connect(2), socket(2), sysctl(3), sysctl(8), accept_filter(9)

go read them, they're all connected, iykyk. πŸ”₯


πŸ•°οΈ HISTORY πŸ•°οΈ

the listen() system call appeared in 4.2BSD. the ability to configure the maximum backlog at run-time, and to use a negative backlog to request the maximum allowable value, was introduced in FreeBSD 2.2. the kern.ipc.somaxconn sysctl(3) has been replaced with kern.ipc.soacceptqueue in FreeBSD 10.0 to prevent confusion about its actual functionality. and the original sysctl(3) kern.ipc.somaxconn is still available but hidden from a sysctl(3) -a output so that existing applications and scripts continue to work. backwards compatible and lowkey iconic. 🫢


and that's the whole page, bestie!! this one came out of Debian, dated August 18, 2016 β€” so go forth, listen() responsibly, and remember: if the queue is full, somebody's getting ghosted. mwah πŸ’‹πŸŽ€

πŸ“¦ where this page comes from