1.\" $NetBSD: sockopt.9,v 1.11 2018/01/04 01:42:25 christos Exp $ 2.\" 3.\" Copyright (c) 2008 Iain Hibbert 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25.\" 26.Dd January 3, 2018 27.Dt SOCKOPT 9 28.Os 29.Sh NAME 30.Nm sockopt_init , 31.Nm sockopt_destroy , 32.Nm sockopt_get , 33.Nm sockopt_getint , 34.Nm sockopt_set , 35.Nm sockopt_setint 36.Nd socket options handling 37.Sh SYNOPSIS 38.In sys/socketvar.h 39.Ft void 40.Fn sockopt_init "struct sockopt *sopt" "int level" "int name" "size_t size" 41.Ft void 42.Fn sockopt_destroy "struct sockopt *sopt" 43.Ft int 44.Fn sockopt_get "struct sockopt *sopt" "void *value" "size_t size" 45.Ft int 46.Fn sockopt_getint "struct sockopt *sopt" "int *value" 47.Ft int 48.Fn sockopt_set "struct sockopt *sopt" "const void *value" "size_t size" 49.Ft int 50.Fn sockopt_setint "struct sockopt *sopt" "int value" 51.Sh DESCRIPTION 52The 53.Ft sockopt 54structure is used to pass a socket option and associated value: 55.Bd -literal -offset indent 56struct sockopt { 57 int sopt_level; /* option level */ 58 int sopt_name; /* option name */ 59 size_t sopt_size; /* data length */ 60 size_t sopt_retsize; /* returned data length */ 61 void * sopt_data; /* data pointer */ 62 uint8_t sopt_buf[sizeof(int)]; /* internal storage */ 63}; 64.Ed 65.Pp 66The internal storage is used for the common case of values up to integer 67size so that memory allocation is not required and sopt_data will point 68to this in that case. 69.Pp 70Rather than provide accessor functions, the 71.Ft sockopt 72structure is public and the contents are expected to be internally 73consistent, but the normal practice would be to use the appropriate methods 74for storage and retrieval of values where a known datatype is expected, 75as the size will be verified. 76.Pp 77Note: a sockopt structure may only be used for a single level/name/size 78combination. 79If the structure is to be re-used, it must be destroyed and re-initialized 80with the new values. 81.Sh OPTIONS 82.Bl -tag -width xxxx 83.It Cd "options DIAGNOSTIC" 84Kernels compiled with the 85.Dv DIAGNOSTIC 86option will perform basic sanity checks on socket options operations. 87.El 88.Sh FUNCTIONS 89.Bl -tag -width xxxx 90.It Fn sockopt_init "sopt" "level" "name" "size" 91Initialise sockopt storage. 92If 93.Ar size 94is given, 95.Fn sockopt_init 96will arrange for sopt_data to point to a buffer of 97.Ar size 98bytes for the sockopt value. 99Where memory needs to be allocated to satisfy this, 100.Fn sockopt_init 101may sleep. 102.It Fn sockopt_destroy "sopt" 103Destroy sockopt storage, releasing any allocated memory. 104.It Fn sockopt_get "sopt" "value" "size" 105Copy out sockopt value. 106Will return 107.Er EINVAL 108if an incorrect data size is given. 109.It Fn sockopt_getint "sopt" "value" 110Common case of get sockopt integer value. 111Will return 112.Er EINVAL 113if sockopt does not contain an integer sized value. 114.It Fn sockopt_set "sopt" "value" "size" 115Copy in sockopt value. 116The sockopt structure must contain a data field of 117.Ar size 118bytes or be previously unset, in which case a data buffer may be 119allocated using 120.Xr kmem_alloc 9 121with the 122.Dv KM_NOSLEEP 123flag which may cause 124.Fn sockopt_set 125to return 126.Er ENOMEM . 127.Pp 128Note: If you need to use 129.Fn sockopt_set 130in a context where memory allocation may be required and you do not wish to 131contemplate failure, the sockopt structure can be initialised in a more suitable 132context using 133.Fn sockopt_init 134which will not fail. 135.It Fn sockopt_setint "sopt" "value" 136Common case of set sockopt integer value. 137The sockopt structure must contain an int sized data field or be previously 138unset, in which case the data pointer will be set to the internal storage. 139.El 140.Sh CODE REFERENCES 141The function prototypes and sockopt structure are defined in the 142.Pa sys/sys/socketvar.h 143header file, and the socket options implementation is in 144.Pa sys/kern/uipc_socket.c . 145.Sh SEE ALSO 146.Xr errno 2 , 147.Xr kmem 9 148.Sh HISTORY 149The socket options KPI was inspired by a similar KPI in 150.Fx 151and 152first appeared in 153.Nx 5.0 . 154