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