1 .\" $NetBSD: getsockopt.2,v 1.34 2009/06/29 08:38:07 wiz Exp $
3 .\" Copyright (c) 1983, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)getsockopt.2 8.4 (Berkeley) 5/2/95
38 .Nd get and set options on sockets
44 .Fn getsockopt "int s" "int level" "int optname" "void * restrict optval" "socklen_t * restrict optlen"
46 .Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen"
53 associated with a socket.
54 Options may exist at multiple
55 protocol levels; they are always present at the uppermost
59 When manipulating socket options the level at which the
60 option resides and the name of the option must be specified.
61 To manipulate options at the socket level,
65 To manipulate options at any
66 other level the protocol number of the appropriate protocol
67 controlling the option is supplied.
68 For example, to indicate that an option is to be interpreted by the
72 should be set to the protocol number of
81 are used to access option values for
85 they identify a buffer in which the value for the
86 requested option(s) are to be returned.
90 is a value-result parameter, initially containing the
91 size of the buffer pointed to by
93 and modified on return to indicate the actual size of
95 If no option value is to be supplied or returned,
101 and any specified options are passed uninterpreted to the appropriate
102 protocol module for interpretation.
104 .Ao Pa sys/socket.h Ac
105 contains definitions for
106 socket level options, described below.
107 Options at other protocol levels vary in format and
108 name; consult the appropriate entries in
110 4 of the manual, including:
125 Most socket-level options use an
131 the parameter should be non-zero to enable a boolean option,
132 or zero if the option is to be disabled.
136 parameter, defined in
137 .Ao Pa sys/socket.h Ac ,
138 which specifies the desired state of the option and the
139 linger interval (see below).
145 parameter, defined in
146 .Ao Pa sys/time.h Ac .
148 The following options are recognized at the socket level.
149 Except as noted, each may be examined with
153 .Bl -column SO_ACCEPTFILTER data -offset indent
154 .It Dv SO_DEBUG Ta "enables recording of debugging information"
155 .It Dv SO_REUSEADDR Ta "enables local address reuse"
156 .It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings"
157 .It Dv SO_KEEPALIVE Ta "enables keep connections alive"
158 .It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages"
159 .It Dv SO_LINGER Ta "linger on close if data present"
160 .It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages"
161 .It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band"
162 .It Dv SO_SNDBUF Ta "set buffer size for output"
163 .It Dv SO_RCVBUF Ta "set buffer size for input"
164 .It Dv SO_SNDLOWAT Ta "set minimum count for output"
165 .It Dv SO_RCVLOWAT Ta "set minimum count for input"
166 .It Dv SO_SNDTIMEO Ta "set timeout value for output"
167 .It Dv SO_RCVTIMEO Ta "set timeout value for input"
168 .It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams"
169 .It Dv SO_ACCEPTFILTER Ta "set accept filter on listening socket"
170 .It Dv SO_TYPE Ta "get the type of the socket (get only)"
171 .It Dv SO_ERROR Ta "get and clear error on the socket (get only)"
175 enables debugging in the underlying protocol modules.
177 indicates that the rules used in validating addresses supplied
180 call should allow reuse of local addresses.
182 allows completely duplicate bindings by multiple processes
185 before binding the port.
186 This option permits multiple instances of a program to each
187 receive UDP/IP multicast or broadcast datagrams destined for the bound port.
190 periodic transmission of messages on a connected socket.
191 Should the connected party fail to respond to these messages, the connection
192 is considered broken and processes using the socket are notified via a
194 signal when attempting to send data.
196 indicates that outgoing messages should
197 bypass the standard routing facilities.
198 Instead, messages are directed to the appropriate network interface
199 according to the network portion of the destination address.
202 controls the action taken when unsent messages
203 are queued on socket and a
206 If the socket promises reliable delivery of data and
208 is set, the system will block the process on the
210 attempt until it is able to transmit the data or until it decides it
211 is unable to deliver the information (a timeout period, measured in
212 seconds, termed the linger interval, is specified in the
221 is issued, the system will process the close in a manner that allows
222 the process to continue as quickly as possible.
226 requests permission to send broadcast datagrams
228 Broadcast was a privileged operation in earlier versions of the system.
229 With protocols that support out-of-band data, the
232 requests that out-of-band data be placed in the normal data input queue
233 as received; it will then be accessible with
240 Some protocols always behave as if this option is set.
244 are options to adjust the normal
245 buffer sizes allocated for output and input buffers, respectively.
246 The buffer size may be increased for high-volume connections,
247 or may be decreased to limit the possible backlog of incoming data.
248 The system places an absolute limit on these values.
251 is an option to set the minimum count for output operations.
252 Most output operations process all of the data supplied
253 by the call, delivering data to the protocol for transmission
254 and blocking as necessary for flow control.
255 Nonblocking output operations will process as much data as permitted
256 subject to flow control without blocking, but will process no data
257 if flow control does not allow the smaller of the low water mark value
258 or the entire request to be processed.
263 operation testing the ability to write to a socket will return true
264 only if the low water mark amount could be processed.
265 The default value for
267 is set to a convenient size for network efficiency, often 1024.
269 is an option to set the minimum count for input operations.
270 In general, receive calls will block until any (non-zero) amount of data
271 is received, then return with the smaller of the amount available or the amount
273 The default value for
278 is set to a larger value, blocking receive calls normally
279 wait until they have received the smaller of the low water mark value
280 or the requested amount.
281 Receive calls may still return less than the low water mark if an error
282 occurs, a signal is caught, or the type of data next in the receive queue
283 is different than that returned.
286 is an option to set a timeout value for output operations.
289 parameter with the number of seconds and microseconds
290 used to limit waits for output operations to complete.
291 If a send operation has blocked for this much time,
292 it returns with a partial count
295 if no data were sent.
296 In the current implementation, this timer is restarted each time additional
297 data are delivered to the protocol,
298 implying that the limit applies to output portions ranging in size
299 from the low water mark to the high water mark for output.
301 is an option to set a timeout value for input operations.
304 parameter with the number of seconds and microseconds
305 used to limit waits for input operations to complete.
306 In the current implementation, this timer is restarted each time additional
307 data are received by the protocol,
308 and thus the limit is in effect an inactivity timer.
309 If a receive operation has been blocked for this much time without
310 receiving additional data, it returns with a short count
313 if no data were received.
317 option is enabled on a
321 call will return a timestamp corresponding to when the datagram was
323 The msg_control field in the msghdr structure points to a buffer
324 that contains a cmsghdr structure followed by a struct timeval.
325 The cmsghdr fields have the following values:
327 cmsg_len = sizeof(struct timeval)
328 cmsg_level = SOL_SOCKET
329 cmsg_type = SCM_TIMESTAMP
335 on the socket, which will filter incoming connections on a listening
336 socket before being presented for
340 system call will fail if the socket already has a filter set, and
342 must be called on the socket before trying to install a filter.
347 .Fa struct accept_filter_arg
348 that will select and configure the
349 .Xr accept_filter 9 ,
352 struct accept_filter_arg {
360 argument should be filled with the name of the accept filter
361 that the application wishes to place on the listening socket.
362 The optional argument
364 can be passed to the accept filter specified by
366 to provide additional configuration options at attach time.
371 will remove the filter.
377 are options used only with
380 returns the type of the socket, such as
382 it is useful for servers that inherit sockets on startup.
384 returns any pending error on the socket and clears
386 It may be used to check for asynchronous errors on connected
387 datagram sockets or for other asynchronous errors.
389 A 0 is returned if the call succeeds, \-1 if it fails.
391 The call succeeds unless:
396 is not a valid descriptor.
398 The address pointed to by
400 is not in a valid part of the process address space.
403 this error may also be returned if
405 is not in a valid part of the process address space.
409 was not suitable for installing an
410 .Xr accept_filter 9 .
411 .It Bq Er ENOPROTOOPT
412 The option is unknown at the level indicated.
416 is a file, not a socket.
441 system call appeared in
444 Several of the socket options should be handled at lower levels of the system.