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