xref: /netbsd-src/lib/libc/sys/getsockopt.2 (revision 213aa76def5f7e6d8b6925a59d5bc8221df842d9)
1.\"	$NetBSD: getsockopt.2,v 1.42 2021/08/08 20:54:48 nia Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  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.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.\"     @(#)getsockopt.2	8.4 (Berkeley) 5/2/95
31.\"
32.Dd August 7, 2021
33.Dt GETSOCKOPT 2
34.Os
35.Sh NAME
36.Nm getsockopt ,
37.Nm setsockopt ,
38.Nm getsockopt2
39.Nd get and set options on sockets
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In sys/socket.h
44.Ft int
45.Fn getsockopt "int s" "int level" "int optname" "void * restrict optval" "socklen_t * restrict optlen"
46.Ft int
47.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen"
48.Ft int
49.Fn getsockopt2 "int s" "int level" "int optname" "void * restrict optval" "socklen_t * restrict optlen"
50.Sh DESCRIPTION
51.Fn getsockopt ,
52.Fn setsockopt
53and
54.Fn getsockopt2
55manipulate the
56.Em options
57associated with a socket.
58.Pp
59When manipulating socket options the level at which the
60option resides and the name of the option must be specified.
61To manipulate options at the socket level,
62.Fa level
63is specified as
64.Dv SOL_SOCKET .
65To manipulate options at any
66other level the protocol number of the appropriate protocol
67controlling the option is supplied.
68Options may exist at multiple protocol levels.
69For example, to indicate that an option is to be interpreted by the
70.Tn TCP
71protocol,
72.Fa level
73should be set to the protocol number of
74.Tn TCP ;
75see
76.Xr getprotoent 3 .
77.Pp
78The parameters
79.Fa optval
80and
81.Fa optlen
82are used to access option values for
83.Fn setsockopt .
84For
85.Fn getsockopt
86and
87.Fn getsockopt2
88they identify a buffer in which the value for the
89requested option(s) are to be returned.
90For
91.Fn getsockopt2
92they are also used to provide an extra argument to select which
93value to return.
94For
95.Fn getsockopt
96and
97.Fn getsockopt2 ,
98.Fa optlen
99is a value-result parameter, initially containing the
100size of the buffer pointed to by
101.Fa optval ,
102and modified on return to indicate the actual size of
103the value returned.
104If the size of the requested option value to be stored in
105.Fa optval
106is greater than the size indicated in
107.Fa optlen
108then only
109.Fa optlen
110bytes will be stored in
111.Fa optval
112and the result will be silently truncated.
113If no option value is to be supplied or returned,
114.Fa optval
115may be
116.Dv NULL .
117.Pp
118.Fa optname
119and any specified options are passed uninterpreted to the appropriate
120protocol module for interpretation.
121The include file
122.Ao Pa sys/socket.h Ac
123contains definitions for
124socket level options, described below.
125Options at other protocol levels vary in format and
126name; consult the appropriate entries in
127section
1284 of the manual, including:
129.Xr faith 4 ,
130.Xr icmp6 4 ,
131.Xr ip 4 ,
132.Xr ip6 4 ,
133.Xr ipsec 4 ,
134.Xr multicast 4 ,
135.Xr pim 4 ,
136.Xr route 4 ,
137.Xr tcp 4 ,
138and
139.Xr unix 4 .
140.Pp
141Most socket-level options use an
142.Fa int
143parameter for
144.Fa optval .
145For
146.Fn setsockopt ,
147the parameter should be non-zero to enable a boolean option,
148or zero if the option is to be disabled.
149.Dv SO_LINGER
150uses a
151.Fa struct linger
152parameter, defined in
153.Ao Pa sys/socket.h Ac ,
154which specifies the desired state of the option and the
155linger interval (see below).
156.Dv SO_SNDTIMEO
157and
158.Dv SO_RCVTIMEO
159use a
160.Fa struct timeval
161parameter, defined in
162.Ao Pa sys/time.h Ac .
163.Pp
164The following options are recognized at the socket level.
165Except as noted, each may be examined with
166.Fn getsockopt
167or
168.Fn getsockopt2
169and set with
170.Fn setsockopt .
171.Bl -column SO_ACCEPTFILTER data -offset indent
172.It Dv SO_DEBUG Ta "enables recording of debugging information"
173.It Dv SO_REUSEADDR Ta "enables local address reuse"
174.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings"
175.It Dv SO_KEEPALIVE Ta "enables keep connections alive"
176.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages"
177.It Dv SO_LINGER  Ta "linger on close if data present"
178.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages"
179.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band"
180.It Dv SO_SNDBUF Ta "set buffer size for output"
181.It Dv SO_RCVBUF Ta "set buffer size for input"
182.It Dv SO_SNDLOWAT Ta "set minimum count for output"
183.It Dv SO_RCVLOWAT Ta "set minimum count for input"
184.It Dv SO_SNDTIMEO Ta "set timeout value for output"
185.It Dv SO_RCVTIMEO Ta "set timeout value for input"
186.It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams"
187.It Dv SO_ACCEPTFILTER Ta "set accept filter on listening socket"
188.It Dv SO_RERROR Ta "enables receive size error reporting"
189.It Dv SO_NOSIGPIPE Ta
190controls generation of
191.Dv SIGPIPE
192for the socket
193.It Dv SO_TYPE Ta "get the type of the socket (get only)"
194.It Dv SO_ERROR Ta "get and clear error on the socket (get only)"
195.El
196.Pp
197.Dv SO_DEBUG
198enables debugging in the underlying protocol modules.
199.Dv SO_REUSEADDR
200indicates that the rules used in validating addresses supplied
201in a
202.Xr bind 2
203call should allow reuse of local addresses.
204.Dv SO_REUSEPORT
205allows completely duplicate bindings by multiple processes
206if they all set
207.Dv SO_REUSEPORT
208before binding the port.
209This option permits multiple instances of a program to each
210receive UDP/IP multicast or broadcast datagrams destined for the bound port.
211.Dv SO_KEEPALIVE
212enables the
213periodic transmission of messages on a connected socket.
214Should the connected party fail to respond to these messages, the connection
215is considered broken and processes using the socket are notified via a
216.Dv SIGPIPE
217signal when attempting to send data.
218.Dv SO_DONTROUTE
219indicates that outgoing messages should
220bypass the standard routing facilities.
221Instead, messages are directed to the appropriate network interface
222according to the network portion of the destination address.
223.Dv SO_RERROR
224indicates that receive buffer overflows should be handled as errors.
225Historically receive buffer overflows have been ignored and programs
226could not tell if they missed messages or messages had been truncated
227because of overflows.
228Since programs historically do not expect to get receive overflow errors,
229this behavior is not the default, but the default can be changed by
230setting the
231.Dv SO_RERROR
232flag using
233.Xr sysctl 8
234and
235.Dv kern.sooptions .
236.Pp
237.Dv SO_LINGER
238controls the action taken when unsent messages
239are queued on socket and a
240.Xr close 2
241is performed.
242If the socket promises reliable delivery of data and
243.Dv SO_LINGER
244is set, the system will block the process on the
245.Xr close 2
246attempt until it is able to transmit the data or until it decides it
247is unable to deliver the information (a timeout period, measured in
248seconds, termed the linger interval, is specified in the
249.Fn setsockopt
250call when
251.Dv SO_LINGER
252is requested).
253If
254.Dv SO_LINGER
255is disabled and a
256.Xr close 2
257is issued, the system will process the close in a manner that allows
258the process to continue as quickly as possible.
259.Pp
260The option
261.Dv SO_BROADCAST
262requests permission to send broadcast datagrams
263on the socket.
264Broadcast was a privileged operation in earlier versions of the system.
265With protocols that support out-of-band data, the
266.Dv SO_OOBINLINE
267option
268requests that out-of-band data be placed in the normal data input queue
269as received; it will then be accessible with
270.Xr recv 2
271or
272.Xr read 2
273calls without the
274.Dv MSG_OOB
275flag.
276Some protocols always behave as if this option is set.
277.Dv SO_SNDBUF
278and
279.Dv SO_RCVBUF
280are options to adjust the normal
281buffer sizes allocated for output and input buffers, respectively.
282The buffer size may be increased for high-volume connections,
283or may be decreased to limit the possible backlog of incoming data.
284The system places an absolute limit on these values.
285.Pp
286.Dv SO_SNDLOWAT
287is an option to set the minimum count for output operations.
288Most output operations process all of the data supplied
289by the call, delivering data to the protocol for transmission
290and blocking as necessary for flow control.
291Nonblocking output operations will process as much data as permitted
292subject to flow control without blocking, but will process no data
293if flow control does not allow the smaller of the low water mark value
294or the entire request to be processed.
295A
296.Xr select 2
297or
298.Xr poll 2
299operation testing the ability to write to a socket will return true
300only if the low water mark amount could be processed.
301The default value for
302.Dv SO_SNDLOWAT
303is set to a convenient size for network efficiency, often 1024.
304.Dv SO_RCVLOWAT
305is an option to set the minimum count for input operations.
306In general, receive calls will block until any (non-zero) amount of data
307is received, then return with the smaller of the amount available or the amount
308requested.
309The default value for
310.Dv SO_RCVLOWAT
311is 1.
312If
313.Dv SO_RCVLOWAT
314is set to a larger value, blocking receive calls normally
315wait until they have received the smaller of the low water mark value
316or the requested amount.
317Receive calls may still return less than the low water mark if an error
318occurs, a signal is caught, or the type of data next in the receive queue
319is different than that returned.
320.Pp
321.Dv SO_SNDTIMEO
322is an option to set a timeout value for output operations.
323It accepts a
324.Fa struct timeval
325parameter with the number of seconds and microseconds
326used to limit waits for output operations to complete.
327If a send operation has blocked for this much time,
328it returns with a partial count
329or with the error
330.Er EAGAIN
331if no data were sent.
332In the current implementation, this timer is restarted each time additional
333data are delivered to the protocol,
334implying that the limit applies to output portions ranging in size
335from the low water mark to the high water mark for output.
336.Dv SO_RCVTIMEO
337is an option to set a timeout value for input operations.
338It accepts a
339.Fa struct timeval
340parameter with the number of seconds and microseconds
341used to limit waits for input operations to complete.
342In the current implementation, this timer is restarted each time additional
343data are received by the protocol,
344and thus the limit is in effect an inactivity timer.
345If a receive operation has been blocked for this much time without
346receiving additional data, it returns with a short count
347or with the error
348.Er EAGAIN
349if no data were received.
350.Pp
351If the
352.Dv SO_TIMESTAMP
353option is enabled on a
354.Dv SOCK_DGRAM
355socket, the
356.Xr recvmsg 2
357call will return a timestamp corresponding to when the datagram was
358received.
359The msg_control field in the msghdr structure points to a buffer
360that contains a cmsghdr structure followed by a struct timeval.
361The cmsghdr fields have the following values:
362.Bd -literal
363cmsg_len = sizeof(struct timeval)
364cmsg_level = SOL_SOCKET
365cmsg_type = SCM_TIMESTAMP
366.Ed
367.Pp
368.Dv SO_ACCEPTFILTER
369places an
370.Xr accept_filter 9
371on the socket, which will filter incoming connections on a listening
372socket before being presented for
373.Xr accept 2 .
374The
375.Fn setsockopt
376system call will fail if the socket already has a filter set, and
377.Xr listen 2
378must be called on the socket before trying to install a filter.
379The
380.Fa optval
381argument
382should point to a
383.Fa struct accept_filter_arg
384that will select and configure the
385.Xr accept_filter 9 ,
386defined as follows:
387.Bd -literal
388struct  accept_filter_arg {
389        char    af_name[16];
390        char    af_arg[256-16];
391};
392.Ed
393.Lp
394The
395.Fa af_name
396argument should be filled with the name of the accept filter
397that the application wishes to place on the listening socket.
398The optional argument
399.Fa af_arg
400can be passed to the accept filter specified by
401.Fa af_name
402to provide additional configuration options at attach time.
403Passing in an
404.Fa optval
405of
406.Dv NULL
407will remove the filter.
408.Pp
409Finally,
410.Dv SO_TYPE
411and
412.Dv SO_ERROR
413are options used only with
414.Fn getsockopt
415or
416.Fn getsockopt2 .
417.Dv SO_TYPE
418returns the type of the socket, such as
419.Dv SOCK_STREAM ;
420it is useful for servers that inherit sockets on startup.
421.Dv SO_ERROR
422returns any pending error on the socket and clears
423the error status.
424It may be used to check for asynchronous errors on connected
425datagram sockets or for other asynchronous errors.
426.Sh RETURN VALUES
427A 0 is returned if the call succeeds, \-1 if it fails.
428.Sh ERRORS
429The call succeeds unless:
430.Bl -tag -width Er
431.It Bq Er EBADF
432The argument
433.Fa s
434is not a valid descriptor.
435.It Bq Er EFAULT
436The address pointed to by
437.Fa optval
438is not in a valid part of the process address space.
439For
440.Fn getsockopt ,
441this error may also be returned if
442.Fa optlen
443is not in a valid part of the process address space.
444.It Bq Er EINVAL
445The socket
446.Fa s
447was not suitable for installing an
448.Xr accept_filter 9 .
449.It Bq Er ENOPROTOOPT
450The option is unknown at the level indicated.
451.It Bq Er ENOTSOCK
452The argument
453.Fa s
454is a file, not a socket.
455.El
456.Sh SEE ALSO
457.Xr ioctl 2 ,
458.Xr poll 2 ,
459.Xr select 2 ,
460.Xr socket 2 ,
461.Xr getprotoent 3 ,
462.Xr faith 4 ,
463.Xr icmp6 4 ,
464.Xr ip 4 ,
465.Xr ip6 4 ,
466.Xr ipsec 4 ,
467.Xr multicast 4 ,
468.Xr pim 4 ,
469.Xr route 4 ,
470.Xr tcp 4 ,
471.Xr unix 4 ,
472.Xr protocols 5 ,
473.Xr accept_filter 9
474.Sh HISTORY
475The
476.Fn getsockopt
477system call appeared in
478.Bx 4.2 .
479.Pp
480The
481.Fn getsockopt2
482system call appeared in
483.Nx 9.0 .
484.Sh BUGS
485Several of the socket options should be handled at lower levels of the system.
486