p The internal storage is used for the common case of values up to integer size so that memory allocation is not required and sopt_data will point to this in that case.
p Rather than provide accessor functions, the .Ft sockopt structure is public and the 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, as the size will be verified.
p Note: a sockopt structure may only be used for a single level/name/size combination. If the structure is to be re-used, it must be destroyed and re-initialized with the new values. .Sh OPTIONS l -tag -width xxxx t Cd "options DIAGNOSTIC" Kernels compiled with the .Dv DIAGNOSTIC option will perform basic sanity checks on socket options operations. .El .Sh FUNCTIONS l -tag -width xxxx t Fn sockopt_init "sopt" "level" "name" "size" Initialise sockopt storage. If .Ar size is given, .Fn sockopt_init will arrange for sopt_data to point to a buffer of .Ar size bytes for the sockopt value. Where memory needs to be allocated to satisfy this, .Fn sockopt_init may sleep. t Fn sockopt_destroy "sopt" Destroy sockopt storage, releasing any allocated memory. t Fn sockopt_get "sopt" "value" "size" Copy out sockopt value. Will return .Er EINVAL if an incorrect data size is given. t Fn sockopt_getint "sopt" "value" Common case of get sockopt integer value. Will return .Er EINVAL if sockopt does not contain an integer sized value. t Fn sockopt_set "sopt" "value" "size" Copy in sockopt value. The sockopt structure must contain a data field of .Ar size bytes or be previously unset, in which case a data buffer may be allocated using .Xr kmem_alloc 9 with the .Dv KM_NOSLEEP flag which may cause .Fn sockopt_set to return .Er ENOMEM .
p Note: If you need to use .Fn sockopt_set in a context where memory allocation may be required and you do not wish to contemplate failure, the sockopt structure can be initialised in a more suitable context using .Fn sockopt_init which will not fail. t Fn sockopt_setint "sopt" "value" Common case of set sockopt integer value. The sockopt structure must contain an int sized data field or be previously unset, in which case the data pointer will be set to the internal storage. .El .Sh CODE REFERENCES The function prototypes and sockopt structure are defined in the
a sys/sys/socketvar.h header file, and the socket options implementation is in
a sys/kern/uipc_socket.c . .Sh SEE ALSO .Xr errno 2 , .Xr kmem 9 .Sh HISTORY The socket options KPI was inspired by a similar KPI in .Fx and first appeared in .Nx 5.0 .