xref: /netbsd-src/share/man/man9/sockopt.9 (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1.\"	$NetBSD: sockopt.9,v 1.5 2008/10/28 18:42:54 wiz 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 October 28, 2008
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 sockopt structure is used to pass a socket option and associated
53value:
54.Bd -literal -offset indent
55struct sockopt {
56	int		sopt_level;		/* option level */
57	int		sopt_name;		/* option name */
58	size_t		sopt_size;		/* data length */
59	void *		sopt_data;		/* data pointer */
60	uint8_t		sopt_buf[sizeof(int)];	/* internal storage */
61};
62.Ed
63.Pp
64The internal storage is used for the common case of values up to integer
65size so that memory allocation is not required and sopt_data will point
66to this in that case.
67.Pp
68Note: a sockopt structure may only be used for a single level/name/size
69combination.
70If the structure is to be re-used, it must be destroyed and re-initialized
71with the new values.
72.Sh OPTIONS
73.Bl -tag -width xxxx
74.It Cd "options DIAGNOSTIC"
75Kernels compiled with the
76.Dv DIAGNOSTIC
77option will perform basic sanity checks on socket options operations.
78.El
79.Sh FUNCTIONS
80.Bl -tag -width xxxx
81.It Fn sockopt_init "sopt" "level" "name" "size"
82Initialise sockopt storage.
83If
84.Ar size
85is given,
86.Fn sockopt_init
87will arrange for sopt_data to point to a buffer of
88.Ar size
89bytes for the sockopt value.
90Where memory needs to be allocated to satisfy this,
91.Fn sockopt_init
92may sleep.
93.It Fn sockopt_destroy "sopt"
94Destroy sockopt storage, releasing any allocated memory.
95.It Fn sockopt_get "sopt" "value" "size"
96Copy out sockopt value.
97Will return
98.Er EINVAL
99if an incorrect data size is given.
100.It Fn sockopt_getint "sopt" "value"
101Common case of get sockopt integer value.
102Will return
103.Er EINVAL
104if sockopt does not contain an integer sized value.
105.It Fn sockopt_set "sopt" "value" "size"
106Copy in sockopt value.
107The sockopt structure must contain a data field of
108.Ar size
109bytes or be previously unset, in which case a data buffer may be
110allocated using
111.Xr kmem_alloc 9
112with the
113.Dv KM_NOSLEEP
114flag which may cause
115.Fn sockopt_set
116to return
117.Er ENOMEM .
118.Pp
119Note: If you need to use
120.Fn sockopt_set
121in a context where memory allocation may be required and you do not wish to
122contemplate failure, the sockopt structure can be initialised in a more suitable
123context using
124.Fn sockopt_init
125which will not fail.
126.It Fn sockopt_setint "sopt" "value"
127Common case of set sockopt integer value.
128The sockpt structure must contain an int sized data field or be previously
129unset, in which case the data pointer will be set to the internal storage.
130.El
131.Sh CODE REFERENCES
132This section describes places within the
133.Nx
134source tree where code implementing socket options can be found.
135All pathnames are relative to
136.Pa /usr/src .
137.Pp
138The function prototypes and sockopt structure are defined in the
139.Pa sys/sys/socketvar.h
140header file, and the socket options implementation is in
141.Pa sys/kern/uipc_socket.c .
142.Sh SEE ALSO
143.Xr errno 2 ,
144.Xr kmem_alloc 9
145.Sh HISTORY
146The socket options KPI was inspired by a similar KPI in
147.Fx
148and
149first appeared in
150.Nx 5.0 .
151