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