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