SOCKOPT(9)
β¨ SOCKOPT(9) β the socket options glow-up guide β¨
okay so this is the sockopt(9) page from the Kernel Developer's Manual, and bestie, it's literally all about handling socket options in kernel land. no cap, it's giving "small struct, big responsibilities" energy. let's get into it π
πΈ NAME πΈ
sockopt_init, sockopt_destroy, sockopt_get, sockopt_getint, sockopt_set, sockopt_setint β socket options handling. yes ma'am, the whole friend group π«Ά
π SYNOPSIS π
#include <sys/socketvar.h>
void
sockopt_init(struct sockopt *sopt, int level, int name, size_t size);
void
sockopt_destroy(struct sockopt *sopt);
int
sockopt_get(struct sockopt *sopt, void *value, size_t size);
int
sockopt_getint(struct sockopt *sopt, int *value);
int
sockopt_set(struct sockopt *sopt, const void *value, size_t size);
int
sockopt_setint(struct sockopt *sopt, int value);
like, copy that exactly, bestie. character for character. no typos, periodt π
π¦ DESCRIPTION π¦
so the sockopt structure is what passes a socket option and its associated value around. it's giving "carry the whole vibe in one little struct":
struct sockopt {
int sopt_level; /* option level */
int sopt_name; /* option name */
size_t sopt_size; /* data length */
size_t sopt_retsize; /* returned data length */
void * sopt_data; /* data pointer */
uint8_t sopt_buf[sizeof(int)]; /* internal storage */
};
the internal storage exists for the common case of values up to integer size, so that memory allocation is literally not required β and in that case sopt_data will point to this internal storage. iconic efficiency, honestly π
so here's the tea: instead of providing accessor functions, the sockopt structure is public and its contents are expected to be internally consistent. but the normal practice would be to use the appropriate methods for storage and retrieval of values where a known datatype is expected, because the size will be verified. iykyk π₯
Note: asockoptstructure may only be used for a singlelevel/name/sizecombination. if the structure is to be re-used, it must be destroyed and re-initialized with the new values. no recycling your ex, bestie π
β¨ OPTIONS β¨
options DIAGNOSTICβ kernels compiled with theDIAGNOSTICoption will perform basic sanity checks on socket options operations. it's giving "paranoid but make it fashion" π΅οΈββοΈ
π FUNCTIONS π
sockopt_init(sopt, level, name, size)β initialisesockoptstorage. ifsizeis given,sockopt_init()will arrange forsopt_datato point to a buffer ofsizebytes for thesockoptvalue. where memory needs to be allocated to satisfy this,sockopt_init()may sleep. so like, don't call it somewhere you can't nap π΄sockopt_destroy(sopt)β destroysockoptstorage, releasing any allocated memory. it's the cleanup era, we love to see it π§Ήsockopt_get(sopt, value, size)β copy outsockoptvalue. will returnEINVALif an incorrect data size is given. size mismatch? blocked and reported π«sockopt_getint(sopt, value)β common case of getsockoptinteger value. will returnEINVALifsockoptdoes not contain an integer sized value. int-sized or it's not invited to the party πsockopt_set(sopt, value, size)β copy insockoptvalue. thesockoptstructure must contain a data field ofsizebytes or be previously unset, in which case a data buffer may be allocated usingkmem_alloc(9)with theKM_NOSLEEPflag which may causesockopt_set()to returnENOMEM. allocation said "not today" and now you're dealing withENOMEM, oof π
Note: if you need to usesockopt_set()in a context where memory allocation may be required and you do not wish to contemplate failure, thesockoptstructure can be initialised in a more suitable context usingsockopt_init()which will not fail. plan ahead and slay π
sockopt_setint(sopt, value)β common case of setsockoptinteger value. thesockoptstructure must contain anintsized data field or be previously unset, in which case the data pointer will be set to the internal storage. tiny value, no allocation drama, we love a low-maintenance icon π
π CODE REFERENCES π
the function prototypes and sockopt structure are defined in the sys/sys/socketvar.h header file, and the socket options implementation is in sys/kern/uipc_socket.c. go read the source, bestie, it's giving main character energy π
πΈ SEE ALSO πΈ
like, go look at those too. cross-referencing is self-care π«Ά
π HISTORY π
the socket options KPI was inspired by a similar KPI in FreeBSD and first appeared in NetBSD 5.0. a little throwback moment for the lore keepers π
and that's the whole page, bestie β no details dropped, no flags forgotten, no cap. this gorgeous little doc is from the Debian edition, dated January 3, 2018, and it lives its best life as SOCKOPT(9). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 2008 Iain Hibbert
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms