xref: /freebsd-14.2/lib/libc/sys/getsockopt.2 (revision 9b50d902)
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.3 (Berkeley) 4/19/94
33.\"
34.Dd April 19, 1994
35.Dt GETSOCKOPT 2
36.Os BSD 4.3r
37.Sh NAME
38.Nm getsockopt ,
39.Nm setsockopt
40.Nd get and set options on sockets
41.Sh SYNOPSIS
42.Fd #include <sys/types.h>
43.Fd #include <sys/socket.h>
44.Ft int
45.Fn getsockopt "int s" "int level" "int optname" "void *optval" "int *optlen"
46.Ft int
47.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "int optlen"
48.Sh DESCRIPTION
49.Fn Getsockopt
50and
51.Fn setsockopt
52manipulate the
53.Em options
54associated with a socket.  Options may exist at multiple
55protocol levels; they are always present at the uppermost
56.Dq socket
57level.
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.  For example,
68to indicate that an option is to be interpreted by the
69.Tn TCP
70protocol,
71.Fa level
72should be set to the protocol number of
73.Tn TCP ;
74see
75.Xr getprotoent 3 .
76.Pp
77The parameters
78.Fa optval
79and
80.Fa optlen
81are used to access option values for
82.Fn setsockopt .
83For
84.Fn getsockopt
85they identify a buffer in which the value for the
86requested option(s) are to be returned.  For
87.Fn getsockopt ,
88.Fa optlen
89is a value-result parameter, initially containing the
90size of the buffer pointed to by
91.Fa optval ,
92and modified on return to indicate the actual size of
93the value returned.  If no option value is
94to be supplied or returned,
95.Fa optval
96may be NULL.
97.Pp
98.Fa Optname
99and any specified options are passed uninterpreted to the appropriate
100protocol module for interpretation.
101The include file
102.Ao Pa sys/socket.h Ac
103contains definitions for
104socket level options, described below.
105Options at other protocol levels vary in format and
106name; consult the appropriate entries in
107section
1084 of the manual.
109.Pp
110Most socket-level options utilize an
111.Fa int
112parameter for
113.Fa optval .
114For
115.Fn setsockopt ,
116the parameter should be non-zero to enable a boolean option,
117or zero if the option is to be disabled.
118.Dv SO_LINGER
119uses a
120.Fa struct linger
121parameter, defined in
122.Ao Pa sys/socket.h Ac ,
123which specifies the desired state of the option and the
124linger interval (see below).
125.Dv SO_SNDTIMEO
126and
127.Dv SO_RCVTIMEO
128use a
129.Fa struct timeval
130parameter, defined in
131.Ao Pa sys/time.h Ac .
132.Pp
133The following options are recognized at the socket level.
134Except as noted, each may be examined with
135.Fn getsockopt
136and set with
137.Fn setsockopt .
138.Bl -column SO_OOBINLINE -offset indent
139.It Dv SO_DEBUG Ta "enables recording of debugging information"
140.It Dv SO_REUSEADDR Ta "enables local address reuse"
141.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings"
142.It Dv SO_KEEPALIVE Ta "enables keep connections alive"
143.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages"
144.It Dv SO_LINGER  Ta "linger on close if data present"
145.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages"
146.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band"
147.It Dv SO_SNDBUF Ta "set buffer size for output"
148.It Dv SO_RCVBUF Ta "set buffer size for input"
149.It Dv SO_SNDLOWAT Ta "set minimum count for output"
150.It Dv SO_RCVLOWAT Ta "set minimum count for input"
151.It Dv SO_SNDTIMEO Ta "set timeout value for output"
152.It Dv SO_RCVTIMEO Ta "set timeout value for input"
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.El
156.Pp
157.Dv SO_DEBUG
158enables debugging in the underlying protocol modules.
159.Dv SO_REUSEADDR
160indicates that the rules used in validating addresses supplied
161in a
162.Xr bind 2
163call should allow reuse of local addresses.
164.Dv SO_REUSEPORT
165allows completely duplicate bindings by multiple processes
166if they all set
167.Dv SO_REUSEPORT
168before binding the port.
169This option permits multiple instances of a program to each
170receive UDP/IP multicast or broadcast datagrams destined for the bound port.
171.Dv SO_KEEPALIVE
172enables the
173periodic transmission of messages on a connected socket.  Should the
174connected party fail to respond to these messages, the connection is
175considered broken and processes using the socket are notified via a
176.Dv SIGPIPE
177signal when attempting to send data.
178.Dv SO_DONTROUTE
179indicates that outgoing messages should
180bypass the standard routing facilities.  Instead, messages are directed
181to the appropriate network interface according to the network portion
182of the destination address.
183.Pp
184.Dv SO_LINGER
185controls the action taken when unsent messages
186are queued on socket and a
187.Xr close 2
188is performed.
189If the socket promises reliable delivery of data and
190.Dv SO_LINGER is set,
191the system will block the process on the
192.Xr close
193attempt until it is able to transmit the data or until it decides it
194is unable to deliver the information (a timeout period, termed the
195linger interval, is specified in the
196.Fn setsockopt
197call when
198.Dv SO_LINGER
199is requested).
200If
201.Dv SO_LINGER
202is disabled and a
203.Xr close
204is issued, the system will process the close in a manner that allows
205the process to continue as quickly as possible.
206.Pp
207The option
208.Dv SO_BROADCAST
209requests permission to send broadcast datagrams
210on the socket.
211Broadcast was a privileged operation in earlier versions of the system.
212With protocols that support out-of-band data, the
213.Dv SO_OOBINLINE
214option
215requests that out-of-band data be placed in the normal data input queue
216as received; it will then be accessible with
217.Xr recv
218or
219.Xr read
220calls without the
221.Dv MSG_OOB
222flag.
223Some protocols always behave as if this option is set.
224.Dv SO_SNDBUF
225and
226.Dv SO_RCVBUF
227are options to adjust the normal
228buffer sizes allocated for output and input buffers, respectively.
229The buffer size may be increased for high-volume connections,
230or may be decreased to limit the possible backlog of incoming data.
231The system places an absolute limit on these values.
232.Pp
233.Dv SO_SNDLOWAT
234is an option to set the minimum count for output operations.
235Most output operations process all of the data supplied
236by the call, delivering data to the protocol for transmission
237and blocking as necessary for flow control.
238Nonblocking output operations will process as much data as permitted
239subject to flow control without blocking, but will process no data
240if flow control does not allow the smaller of the low water mark value
241or the entire request to be processed.
242A
243.Xr select 2
244operation testing the ability to write to a socket will return true
245only if the low water mark amount could be processed.
246The default value for
247.Dv SO_SNDLOWAT
248is set to a convenient size for network efficiency, often 1024.
249.Dv SO_RCVLOWAT
250is an option to set the minimum count for input operations.
251In general, receive calls will block until any (non-zero) amount of data
252is received, then return with the smaller of the amount available or the amount
253requested.
254The default value for
255.Dv SO_RCVLOWAT
256is 1.
257If
258.Dv SO_RCVLOWAT
259is set to a larger value, blocking receive calls normally
260wait until they have received the smaller of the low water mark value
261or the requested amount.
262Receive calls may still return less than the low water mark if an error
263occurs, a signal is caught, or the type of data next in the receive queue
264is different than that returned.
265.Pp
266.Dv SO_SNDTIMEO
267is an option to set a timeout value for output operations.
268It accepts a
269.Fa struct timeval
270parameter with the number of seconds and microseconds
271used to limit waits for output operations to complete.
272If a send operation has blocked for this much time,
273it returns with a partial count
274or with the error
275.Er EWOULDBLOCK
276if no data were sent.
277In the current implementation, this timer is restarted each time additional
278data are delivered to the protocol,
279implying that the limit applies to output portions ranging in size
280from the low water mark to the high water mark for output.
281.Dv SO_RCVTIMEO
282is an option to set a timeout value for input operations.
283It accepts a
284.Fa struct timeval
285parameter with the number of seconds and microseconds
286used to limit waits for input operations to complete.
287In the current implementation, this timer is restarted each time additional
288data are received by the protocol,
289and thus the limit is in effect an inactivity timer.
290If a receive operation has been blocked for this much time without
291receiving additional data, it returns with a short count
292or with the error
293.Er EWOULDBLOCK
294if no data were received.
295.Pp
296Finally,
297.Dv SO_TYPE
298and
299.Dv SO_ERROR
300are options used only with
301.Fn getsockopt .
302.Dv SO_TYPE
303returns the type of the socket, such as
304.Dv SOCK_STREAM ;
305it is useful for servers that inherit sockets on startup.
306.Dv SO_ERROR
307returns any pending error on the socket and clears
308the error status.
309It may be used to check for asynchronous errors on connected
310datagram sockets or for other asynchronous errors.
311.Sh RETURN VALUES
312A 0 is returned if the call succeeds, -1 if it fails.
313.Sh ERRORS
314The call succeeds unless:
315.Bl -tag -width ENOPROTOOPTAA
316.It Bq Er EBADF
317The argument
318.Fa s
319is not a valid descriptor.
320.It Bq Er ENOTSOCK
321The argument
322.Fa s
323is a file, not a socket.
324.It Bq Er ENOPROTOOPT
325The option is unknown at the level indicated.
326.It Bq Er EFAULT
327The address pointed to by
328.Fa optval
329is not in a valid part of the process address space.
330For
331.Fn getsockopt ,
332this error may also be returned if
333.Fa optlen
334is not in a valid part of the process address space.
335.El
336.Sh SEE ALSO
337.Xr ioctl 2 ,
338.Xr socket 2 ,
339.Xr getprotoent 3
340.Xr protocols 5
341.Sh BUGS
342Several of the socket options should be handled at lower levels of the system.
343.Sh HISTORY
344The
345.Nm
346system call appeared in
347.Bx 4.2 .
348