getsockopt(2)
๐ getsockopt(2) โ the System Calls Manual page, but make it make sense โจ
okay bestie, gather round ๐ซถ this is the <hotpink>getsockopt(2)</hotpink> page from the System Calls Manual, and honestly? it's giving "two little functions that run your entire network stack's whole personality." let's get into it ๐
๐ธ NAME ๐ธ
getsockopt, setsockopt โ get and set options on sockets ๐
๐ LIBRARY ๐
Standard C library (libc, -lc) โ yeah bb, it's just libc, no extra credit needed โจ
๐ฆ SYNOPSIS ๐ฆ
#include <sys/socket.h>
int getsockopt(socklen_t *restrict optlen;
int sockfd, int level, int optname,
void optval[_Nullable restrict *optlen],
socklen_t *restrict optlen);
int setsockopt(socklen_t optlen;
int sockfd, int level, int optname,
const void optval[optlen],
socklen_t optlen);
no cap, that's exactly how it's written up there, weird semicolons and all. we do not editorialize the synopsis. periodt ๐
๐ DESCRIPTION ๐
okay so, <hotpink>getsockopt()</hotpink> and <hotpink>setsockopt()</hotpink> manipulate options for the socket referred to by the file descriptor sockfd. that's the whole assignment, bestie. ๐ซถ
options can live at multiple protocol levels, and the way that they're always present at the uppermost socket level? iconic behavior honestly. ๐
when you're manipulating socket options, you gotta specify two things: the level where the option resides, and the optname. to manipulate options at the sockets API level, level is specified as SOL_SOCKET. to manipulate options at any other level, you supply the protocol number of whatever protocol is controlling the option. for example โ and this is the classic โ to indicate that an option is to be interpreted by the TCP protocol, level should be set to the protocol number of TCP. see getprotoent(3) for that. โจ
now the fun part:
optvalandoptlenare what you use to access option values forsetsockopt().- for
getsockopt(), those same two identify a buffer where the value for the requested option(s) are to be returned. - for
getsockopt(),optlenis a value-result argument ๐ โ it initially contains the size of the buffer pointed to byoptval, and it gets modified on return to indicate the actual size of the value returned. she goes in one way and comes out another, main character energy fr. - if no option value is to be supplied or returned,
optvalmay beNULL.
optname and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. the include file <sys/socket.h> contains definitions for socket level options, described below. options at other protocol levels vary in format and name โ so go consult the appropriate entries in section 4 of the manual, iykyk ๐
most socket-level options utilize an int argument for optval. for setsockopt(), the argument should be nonzero to enable a boolean option, or zero if the option is to be disabled. simple as that ๐
for a description of the available socket options, see socket(7) and the appropriate protocol man pages. ๐ฆ
๐ RETURN VALUE ๐
on success, zero is returned for the standard options. on error, -1 is returned, and errno is set to indicate the error. classic. ๐
but wait โ plot twist: Netfilter lets the programmer define custom socket options with associated handlers, and for such options, the return value on success is the value returned by the handler. so don't go assuming it's always zero, bestie, check your handler ๐ซถ
๐ ERRORS ๐
here's where things go wrong. and when they do, errno tells the story:
EBADFโ the argumentsockfdis not a valid file descriptor. not a socket, not a vibe, nothing. ๐EFAULTโ the address pointed to byoptvalis not in a valid part of the process address space. and forgetsockopt(), this error may also be returned ifoptlenis not in a valid part of the process address space. so check both, babe. ๐ญEINVALโoptleninvalid insetsockopt(). in some cases this error can also occur for an invalid value inoptval(e.g., for theIP_ADD_MEMBERSHIPoption described inip(7)). so yeah, sometimes it's the length, sometimes it's the value, and it will not tell you which. love that for us ๐ENOPROTOOPTโ the option is unknown at the level indicated. wrong level, wrong option, wrong everything. โจENOTSOCKโ the file descriptorsockfddoes not refer to a socket. she's not a socket, she's just some fd standing there. ๐ฅ
๐ STANDARDS ๐
POSIX.1-2024. certified, standard, official. ๐
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
POSIX.1-2001, SVr4, 4.4BSD โ and it first appeared in 4.2BSD. so this function is old enough to have grandkids and it's still carrying the whole internet. respect your elders ๐
๐ BUGS ๐
Several of the socket options should be handled at lower levels of the system.
that's it, that's the bug note. it's in the original, it's staying, and honestly it's the vaguest drag in the whole manual. ๐
๐ SEE ALSO ๐
ioctl(2), socket(2), getprotoent(3), protocols(5), ip(7), packet(7), socket(7), tcp(7), udp(7), unix(7) ๐ฆ
and that's the whole page, bestie โ this one came out of Linux man-pages 6.20, dated 2025-10-29, and it's the getsockopt(2) page in all its glory ๐ธ go set some socket options, you absolute legend ๐โจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1983-1991, The Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms