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