xref: /openbsd-src/lib/libc/sys/getsockopt.2 (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1.\"	$OpenBSD: getsockopt.2,v 1.50 2016/06/13 21:24:43 bluhm Exp $
2.\"	$NetBSD: getsockopt.2,v 1.7 1995/02/27 12:33:29 cgd Exp $
3.\"
4.\" Copyright (c) 1983, 1991, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the University nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.\"     @(#)getsockopt.2	8.3 (Berkeley) 4/19/94
32.\"
33.Dd $Mdocdate: June 13 2016 $
34.Dt GETSOCKOPT 2
35.Os
36.Sh NAME
37.Nm getsockopt ,
38.Nm setsockopt
39.Nd get and set options on sockets
40.Sh SYNOPSIS
41.In sys/socket.h
42.Ft int
43.Fn getsockopt "int s" "int level" "int optname" "void *optval" "socklen_t *optlen"
44.Ft int
45.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen"
46.Sh DESCRIPTION
47.Fn getsockopt
48and
49.Fn setsockopt
50manipulate the
51.Em options
52associated with a socket.
53Options may exist at multiple protocol levels;
54they 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 other level the protocol number of the
65appropriate protocol controlling the option is supplied.
66For example, to indicate that an option is to be interpreted by the
67TCP protocol,
68.Fa level
69should be set to the protocol number of TCP; see
70.Xr getprotoent 3 .
71.Pp
72The parameters
73.Fa optval
74and
75.Fa optlen
76are used to access option values for
77.Fn setsockopt .
78For
79.Fn getsockopt
80they identify a buffer in which the value for the
81requested option(s) are to be returned.
82For
83.Fn getsockopt ,
84.Fa optlen
85is a value-result parameter, initially containing the
86size of the buffer pointed to by
87.Fa optval ,
88and modified on return to indicate the actual size of the value returned.
89If no option value is to be supplied or returned,
90.Fa optval
91may be
92.Dv NULL .
93.Pp
94.Fa optname
95and any specified options are passed uninterpreted to the appropriate
96protocol module for interpretation.
97The include file
98.In sys/socket.h
99contains definitions for socket level options, described below.
100Options at other protocol levels vary in format and name;
101consult the appropriate entries in section 4 of the manual.
102.Pp
103Most socket-level options utilize an
104.Li int
105parameter for
106.Fa optval .
107For
108.Fn setsockopt ,
109the parameter should be non-zero to enable a boolean option,
110or zero if the option is to be disabled.
111.Dv SO_LINGER
112uses a
113.Li struct linger
114parameter, defined in
115.In sys/socket.h ,
116which specifies the desired state of the option and the
117linger interval (see below).
118.Dv SO_SNDTIMEO
119and
120.Dv SO_RCVTIMEO
121use a
122.Li struct timeval
123parameter, defined in
124.In sys/time.h .
125.Pp
126The following options are recognized at the socket level.
127Except as noted, each may be examined with
128.Fn getsockopt
129and set with
130.Fn setsockopt .
131.Pp
132.Bl -tag -width SO_OOBINLINE -offset indent -compact
133.It Dv SO_DEBUG
134enables recording of debugging information
135.It Dv SO_REUSEADDR
136enables local address reuse
137.It Dv SO_REUSEPORT
138enables duplicate address and port bindings
139.It Dv SO_KEEPALIVE
140enables keep connections alive
141.It Dv SO_DONTROUTE
142enables routing bypass; not supported
143.It Dv SO_LINGER
144linger on close if data present
145.It Dv SO_BROADCAST
146enables permission to transmit broadcast messages
147.It Dv SO_OOBINLINE
148enables reception of out-of-band data in band
149.It Dv SO_BINDANY
150enables binding to any address
151.It Dv SO_SNDBUF
152set buffer size for output
153.It Dv SO_RCVBUF
154set buffer size for input
155.It Dv SO_SNDLOWAT
156set minimum count for output
157.It Dv SO_RCVLOWAT
158set minimum count for input
159.It Dv SO_SNDTIMEO
160set timeout value for output
161.It Dv SO_RCVTIMEO
162set timeout value for input
163.It Dv SO_TIMESTAMP
164enables reception of a timestamp with datagrams
165.It Dv SO_PEERCRED
166get the credentials from other side of connection
167.It Dv SO_RTABLE
168set the routing table used for route lookups
169.It Dv SO_SPLICE
170splice two sockets together or get data length
171.It Dv SO_TYPE
172get the type of the socket (get only)
173.It Dv SO_ERROR
174get and clear error on the socket (get only)
175.El
176.Pp
177.Dv SO_DEBUG
178enables debugging in the underlying protocol modules.
179.Dv SO_REUSEADDR
180indicates that the rules used in validating addresses supplied in a
181.Xr bind 2
182call should allow reuse of local addresses.
183.Dv SO_REUSEPORT
184allows completely duplicate bindings by multiple processes if they all set
185.Dv SO_REUSEPORT
186before binding the port.
187This option permits multiple instances of a program to each
188receive UDP/IP multicast or broadcast datagrams destined for the bound port.
189.Dv SO_KEEPALIVE
190enables the periodic transmission of messages on a connected socket.
191Should the connected party fail to respond to these messages, the connection
192is considered broken and processes using the socket are notified via a
193.Dv SIGPIPE
194signal when attempting to send data.
195.Pp
196.Dv SO_LINGER
197controls the action taken when unsent messages
198are queued on socket and a
199.Xr close 2
200is performed.
201If the socket promises reliable delivery of data and
202.Dv SO_LINGER
203is set, the system will block the process on the
204.Xr close 2
205attempt until it is able to transmit the data or until it decides it
206is unable to deliver the information (a timeout period measured in seconds,
207termed the linger interval, is specified in the
208.Fn setsockopt
209call when
210.Dv SO_LINGER
211is requested).
212If
213.Dv SO_LINGER
214is disabled and a
215.Xr close 2
216is issued, the system will process the close in a manner that allows
217the process to continue as quickly as possible.
218.Pp
219The option
220.Dv SO_BROADCAST
221requests permission to send broadcast datagrams
222on the socket.
223Broadcast was a privileged operation in earlier versions of the system.
224With protocols that support out-of-band data, the
225.Dv SO_OOBINLINE
226option requests that out-of-band data be placed in the normal data input
227queue as received; it will then be accessible with
228.Xr recv 2
229or
230.Xr read 2
231calls without the
232.Dv MSG_OOB
233flag.
234Some protocols always behave as if this option is set.
235.Pp
236.Dv SO_BINDANY
237allows the socket to be bound to addresses
238which are not local to the machine, so it
239can be used to make a transparent proxy.
240Note that this option is limited to the super-user.
241In order to receive packets for these addresses,
242.Dv SO_BINDANY
243needs to be combined with matching outgoing
244.Xr pf 4
245rules with the
246.Ar divert-reply
247parameter.
248For example, with the following rule the socket receives packets
249for 192.168.0.10 even if it is not a local address:
250.Pp
251.Dl pass out inet from 192.168.0.10 divert-reply
252.Pp
253.Dv SO_SNDBUF
254and
255.Dv SO_RCVBUF
256are options to adjust the normal
257buffer sizes allocated for output and input buffers, respectively.
258The buffer size may be increased for high-volume connections,
259or may be decreased to limit the possible backlog of incoming data.
260The system places an absolute limit on these values.
261.Pp
262.Dv SO_SNDLOWAT
263is an option to set the minimum count for output operations.
264Most output operations process all of the data supplied
265by the call, delivering data to the protocol for transmission
266and blocking as necessary for flow control.
267Nonblocking output operations will process as much data as permitted
268subject to flow control without blocking, but will process no data
269if flow control does not allow the smaller of the low water mark value
270or the entire request to be processed.
271A
272.Xr select 2
273or
274.Xr poll 2
275operation testing the ability to write to a socket will return true
276only if the low water mark amount could be processed.
277The default value for
278.Dv SO_SNDLOWAT
279is set to a convenient size for network efficiency, often 1024.
280.Dv SO_RCVLOWAT
281is an option to set the minimum count for input operations.
282In general, receive calls will block until any (non-zero) amount of data
283is received, then return with the smaller of the amount available or the amount
284requested.
285The default value for
286.Dv SO_RCVLOWAT
287is 1.
288If
289.Dv SO_RCVLOWAT
290is set to a larger value, blocking receive calls normally
291wait until they have received the smaller of the low water mark value
292or the requested amount.
293Receive calls may still return less than the low water mark if an error
294occurs, a signal is caught, or the type of data next in the receive queue
295is different than that returned.
296.Pp
297.Dv SO_SNDTIMEO
298is an option to set a timeout value for output operations.
299It accepts a
300.Li struct timeval
301parameter with the number of seconds and microseconds
302used to limit waits for output operations to complete.
303If a send operation has blocked for this much time,
304it returns with a partial count or with the error
305.Er EWOULDBLOCK
306if no data was sent.
307In the current implementation, this timer is restarted each time additional
308data are delivered to the protocol,
309implying that the limit applies to output portions ranging in size
310from the low water mark to the high water mark for output.
311.Dv SO_RCVTIMEO
312is an option to set a timeout value for input operations.
313It accepts a
314.Li struct timeval
315parameter with the number of seconds and microseconds
316used to limit waits for input operations to complete.
317In the current implementation, this timer is restarted each time additional
318data are received by the protocol,
319and thus the limit is in effect an inactivity timer.
320If a receive operation has been blocked for this much time without
321receiving additional data, it returns with a short count
322or with the error
323.Er EWOULDBLOCK
324if no data were received.
325.Pp
326If the
327.Dv SO_TIMESTAMP
328option is enabled on a
329.Dv SOCK_DGRAM
330socket, the
331.Xr recvmsg 2
332call will return a timestamp corresponding to when the datagram was
333received.
334The msg_control field in the msghdr structure points to a buffer
335that contains a cmsghdr structure followed by a struct timeval.
336The cmsghdr fields have the following values:
337.Bd -literal -offset indent
338cmsg_len = CMSG_LEN(sizeof(struct timeval))
339cmsg_level = SOL_SOCKET
340cmsg_type = SCM_TIMESTAMP
341.Ed
342.Pp
343.Dv SO_PEERCRED
344fetches the
345.Va struct sockpeercred
346credentials from the other side of the connection
347(currently only possible on
348.Dv AF_UNIX
349sockets).
350These credentials are from the time that
351.Xr bind 2
352or
353.Xr connect 2
354were called.
355.Pp
356The
357.Dv SO_RTABLE
358option gets or sets the routing table which will be used by the socket
359for address lookups.
360If a protocol family of the socket doesn't support switching routing tables,
361the
362.Er ENOPROTOOPT
363error is returned.
364Only the superuser is allowed to change the routing table if it is already
365set to a non-zero value.
366A socket's chosen routing table is initialized from the process's configuration,
367previously selected using
368.Xr setrtable 2 .
369.Pp
370.Dv SO_SPLICE
371can splice together two TCP or UDP sockets for unidirectional
372zero-copy data transfers.
373Splice also the other way around to get bidirectional data flow.
374Both sockets must be of the same type.
375In the first form,
376.Fn setsockopt
377is called with the source socket
378.Fa s
379and the drain socket's
380.Vt int
381file descriptor as
382.Fa optval .
383In the second form,
384.Fa optval
385is a
386.Vt struct splice
387with the drain socket in
388.Va sp_fd ,
389a positive maximum number of bytes or 0 in
390.Va sp_max
391and an idle timeout
392.Va sp_idle
393in the form of a
394.Vt struct timeval .
395If \-1 is given as drain socket, the source socket
396.Fa s
397gets unspliced.
398Otherwise the spliced data transfer continues within the kernel
399until the optional maximum is reached, one of the connections
400terminates, idle timeout expires or an error occurs.
401A successful
402.Xr select 2 ,
403.Xr poll 2 ,
404or
405.Xr kqueue 2
406operation testing the ability to read from the source socket indicates
407that the splicing has terminated.
408When one of the sockets gets closed, splicing ends.
409The error status can be examined with
410.Dv SO_ERROR
411at the source socket.
412The
413.Er ELOOP
414error is set if userland created a loop by splicing sockets connected
415to localhost.
416The
417.Er ETIMEDOUT
418error is set if there was no data transferred between two sockets
419during the
420.Va sp_idle
421period of time.
422The
423.Er EFBIG
424error is set after exactly
425.Va sp_max
426bytes have been transferred.
427Note that if a maximum is given, it is only guaranteed that no more
428bytes are transferred.
429A short splice can happen, but then a second call to splice will
430transfer the remaining data immediately.
431The
432.Dv SO_SPLICE
433option with
434.Fn getsockopt
435and an
436.Vt off_t
437value as
438.Fa optval
439can be used to retrieve the number of bytes transferred so far from the
440source socket
441.Fa s .
442A successful new splice resets this number.
443.Pp
444Finally,
445.Dv SO_TYPE
446and
447.Dv SO_ERROR
448are options used only with
449.Fn getsockopt .
450.Dv SO_TYPE
451returns the type of the socket, such as
452.Dv SOCK_STREAM ;
453it is useful for servers that inherit sockets on startup.
454.Dv SO_ERROR
455returns any pending error on the socket and clears the error status.
456It may be used to check for asynchronous errors on connected
457datagram sockets or for other asynchronous errors.
458.Sh RETURN VALUES
459.Rv -std
460.Sh ERRORS
461The call succeeds unless:
462.Bl -tag -width Er
463.It Bq Er EBADF
464The argument
465.Fa s
466is not a valid descriptor.
467.It Bq Er ENOTSOCK
468The argument
469.Fa s
470is a file, not a socket.
471.It Bq Er ENOPROTOOPT
472The option is unknown at the level indicated.
473.It Bq Er EOPNOTSUPP
474The option is unsupported.
475.It Bq Er EFAULT
476The address pointed to by
477.Fa optval
478is not in a valid part of the process address space.
479For
480.Fn getsockopt ,
481this error may also be returned if
482.Fa optlen
483is not in a valid part of the process address space.
484.El
485.Sh SEE ALSO
486.Xr connect 2 ,
487.Xr getrtable 2 ,
488.Xr ioctl 2 ,
489.Xr poll 2 ,
490.Xr select 2 ,
491.Xr socket 2 ,
492.Xr getprotoent 3 ,
493.Xr divert 4 ,
494.Xr pf.conf 5 ,
495.Xr protocols 5 ,
496.Xr sosplice 9
497.Sh STANDARDS
498The
499.Fn getsockopt
500and
501.Fn setsockopt
502functions conform to
503.St -p1003.1-2008 .
504.Sh HISTORY
505The
506.Fn getsockopt
507system call appeared in
508.Bx 4.2 .
509.Sh BUGS
510Several of the socket options should be handled at lower levels of the system.
511