1 .\" $NetBSD: sockopt.9,v 1.6 2009/08/03 19:57:40 rmind Exp $
3 .\" Copyright (c) 2008 Iain Hibbert
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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.
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.
36 .Nd socket options handling
40 .Fn sockopt_init "struct sockopt *sopt" "int level" "int name" "size_t size"
42 .Fn sockopt_destroy "struct sockopt *sopt"
44 .Fn sockopt_get "struct sockopt *sopt" "void *value" "size_t size"
46 .Fn sockopt_getint "struct sockopt *sopt" "int *value"
48 .Fn sockopt_set "struct sockopt *sopt" "const void *value" "size_t size"
50 .Fn sockopt_setint "struct sockopt *sopt" "int value"
54 structure is used to pass a socket option and associated value:
55 .Bd -literal -offset indent
57 int sopt_level; /* option level */
58 int sopt_name; /* option name */
59 size_t sopt_size; /* data length */
60 void * sopt_data; /* data pointer */
61 uint8_t sopt_buf[sizeof(int)]; /* internal storage */
65 The internal storage is used for the common case of values up to integer
66 size so that memory allocation is not required and sopt_data will point
69 Rather than provide accessor functions, the
71 structure is public and the contents are expected to be internally
72 consistent, but the normal practice would be to use the appropriate methods
73 for storage and retrieval of values where a known datatype is expected,
74 as the size will be verified.
76 Note: a sockopt structure may only be used for a single level/name/size
78 If the structure is to be re-used, it must be destroyed and re-initialized
82 .It Cd "options DIAGNOSTIC"
83 Kernels compiled with the
85 option will perform basic sanity checks on socket options operations.
89 .It Fn sockopt_init "sopt" "level" "name" "size"
90 Initialise sockopt storage.
95 will arrange for sopt_data to point to a buffer of
97 bytes for the sockopt value.
98 Where memory needs to be allocated to satisfy this,
101 .It Fn sockopt_destroy "sopt"
102 Destroy sockopt storage, releasing any allocated memory.
103 .It Fn sockopt_get "sopt" "value" "size"
104 Copy out sockopt value.
107 if an incorrect data size is given.
108 .It Fn sockopt_getint "sopt" "value"
109 Common case of get sockopt integer value.
112 if sockopt does not contain an integer sized value.
113 .It Fn sockopt_set "sopt" "value" "size"
114 Copy in sockopt value.
115 The sockopt structure must contain a data field of
117 bytes or be previously unset, in which case a data buffer may be
127 Note: If you need to use
129 in a context where memory allocation may be required and you do not wish to
130 contemplate failure, the sockopt structure can be initialised in a more suitable
134 .It Fn sockopt_setint "sopt" "value"
135 Common case of set sockopt integer value.
136 The sockpt structure must contain an int sized data field or be previously
137 unset, in which case the data pointer will be set to the internal storage.
140 This section describes places within the
142 source tree where code implementing socket options can be found.
143 All pathnames are relative to
146 The function prototypes and sockopt structure are defined in the
147 .Pa sys/sys/socketvar.h
148 header file, and the socket options implementation is in
149 .Pa sys/kern/uipc_socket.c .
154 The socket options KPI was inspired by a similar KPI in