1.\" $OpenBSD: getsockopt.2,v 1.20 2003/06/02 20:18:39 millert 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. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)getsockopt.2 8.3 (Berkeley) 4/19/94 32.\" 33.Dd February 15, 1999 34.Dt GETSOCKOPT 2 35.Os 36.Sh NAME 37.Nm getsockopt , 38.Nm setsockopt 39.Nd get and set options on sockets 40.Sh SYNOPSIS 41.Fd #include <sys/types.h> 42.Fd #include <sys/socket.h> 43.Ft int 44.Fn getsockopt "int s" "int level" "int optname" "void *optval" "socklen_t *optlen" 45.Ft int 46.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen" 47.Sh DESCRIPTION 48.Fn getsockopt 49and 50.Fn setsockopt 51manipulate the 52.Em options 53associated with a socket. 54Options may exist at multiple protocol levels; 55they 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 other level the protocol number of the 66appropriate protocol controlling the option is supplied. 67For example, to indicate that an option is to be interpreted by the 68.Tn TCP 69protocol, 70.Fa level 71should be set to the protocol number of 72.Tn TCP ; 73see 74.Xr getprotoent 3 . 75.Pp 76The parameters 77.Fa optval 78and 79.Fa optlen 80are used to access option values for 81.Fn setsockopt . 82For 83.Fn getsockopt 84they identify a buffer in which the value for the 85requested option(s) are to be returned. 86For 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 the value returned. 93If no option value is to be supplied or returned, 94.Fa optval 95may be 96.Dv 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 socket level options, described below. 104Options at other protocol levels vary in format and name; 105consult the appropriate entries in section 4 of the manual. 106.Pp 107Most socket-level options utilize an 108.Li int 109parameter for 110.Fa optval . 111For 112.Fn setsockopt , 113the parameter should be non-zero to enable a boolean option, 114or zero if the option is to be disabled. 115.Dv SO_LINGER 116uses a 117.Li struct linger 118parameter, defined in 119.Aq Pa sys/socket.h , 120which specifies the desired state of the option and the 121linger interval (see below). 122.Dv SO_SNDTIMEO 123and 124.Dv SO_RCVTIMEO 125use a 126.Li struct timeval 127parameter, defined in 128.Aq Pa sys/time.h . 129.Pp 130The following options are recognized at the socket level. 131Except as noted, each may be examined with 132.Fn getsockopt 133and set with 134.Fn setsockopt . 135.Bl -column SO_OOBINLINE -offset indent 136.It Dv SO_DEBUG Ta "enables recording of debugging information" 137.It Dv SO_REUSEADDR Ta "enables local address reuse" 138.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings" 139.It Dv SO_KEEPALIVE Ta "enables keep connections alive" 140.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages" 141.It Dv SO_LINGER Ta "linger on close if data present" 142.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages" 143.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band" 144.It Dv SO_SNDBUF Ta "set buffer size for output" 145.It Dv SO_RCVBUF Ta "set buffer size for input" 146.It Dv SO_SNDLOWAT Ta "set minimum count for output" 147.It Dv SO_RCVLOWAT Ta "set minimum count for input" 148.It Dv SO_SNDTIMEO Ta "set timeout value for output" 149.It Dv SO_RCVTIMEO Ta "set timeout value for input" 150.It Dv SO_TYPE Ta "get the type of the socket (get only)" 151.It Dv SO_ERROR Ta "get and clear error on the socket (get only)" 152.El 153.Pp 154.Dv SO_DEBUG 155enables debugging in the underlying protocol modules. 156.Dv SO_REUSEADDR 157indicates that the rules used in validating addresses supplied in a 158.Xr bind 2 159call should allow reuse of local addresses. 160.Dv SO_REUSEPORT 161allows completely duplicate bindings by multiple processes if they all set 162.Dv SO_REUSEPORT 163before binding the port. 164This option permits multiple instances of a program to each 165receive UDP/IP multicast or broadcast datagrams destined for the bound port. 166.Dv SO_KEEPALIVE 167enables the periodic transmission of messages on a connected socket. 168Should the connected party fail to respond to these messages, the connection 169is considered broken and processes using the socket are notified via a 170.Dv SIGPIPE 171signal when attempting to send data. 172.Dv SO_DONTROUTE 173indicates that outgoing messages should 174bypass the standard routing facilities. 175Instead, messages are directed to the appropriate network interface 176according to the network portion of the destination address. 177.Pp 178.Dv SO_LINGER 179controls the action taken when unsent messages 180are queued on socket and a 181.Xr close 2 182is performed. 183If the socket promises reliable delivery of data and 184.Dv SO_LINGER 185is set, the system will block the process on the 186.Xr close 2 187attempt until it is able to transmit the data or until it decides it 188is unable to deliver the information (a timeout period measured in seconds, 189termed the linger interval, is specified in the 190.Fn setsockopt 191call when 192.Dv SO_LINGER 193is requested). 194If 195.Dv SO_LINGER 196is disabled and a 197.Xr close 2 198is issued, the system will process the close in a manner that allows 199the process to continue as quickly as possible. 200.Pp 201The option 202.Dv SO_BROADCAST 203requests permission to send broadcast datagrams 204on the socket. 205Broadcast was a privileged operation in earlier versions of the system. 206With protocols that support out-of-band data, the 207.Dv SO_OOBINLINE 208option requests that out-of-band data be placed in the normal data input 209queue as received; it will then be accessible with 210.Xr recv 2 211or 212.Xr read 2 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 237or 238.Xr poll 2 239operation testing the ability to write to a socket will return true 240only if the low water mark amount could be processed. 241The default value for 242.Dv SO_SNDLOWAT 243is set to a convenient size for network efficiency, often 1024. 244.Dv SO_RCVLOWAT 245is an option to set the minimum count for input operations. 246In general, receive calls will block until any (non-zero) amount of data 247is received, then return with the smaller of the amount available or the amount 248requested. 249The default value for 250.Dv SO_RCVLOWAT 251is 1. 252If 253.Dv SO_RCVLOWAT 254is set to a larger value, blocking receive calls normally 255wait until they have received the smaller of the low water mark value 256or the requested amount. 257Receive calls may still return less than the low water mark if an error 258occurs, a signal is caught, or the type of data next in the receive queue 259is different than that returned. 260.Pp 261.Dv SO_SNDTIMEO 262is an option to set a timeout value for output operations. 263It accepts a 264.Li struct timeval 265parameter with the number of seconds and microseconds 266used to limit waits for output operations to complete. 267If a send operation has blocked for this much time, 268it returns with a partial count or with the error 269.Er EWOULDBLOCK 270if no data was sent. 271In the current implementation, this timer is restarted each time additional 272data are delivered to the protocol, 273implying that the limit applies to output portions ranging in size 274from the low water mark to the high water mark for output. 275.Dv SO_RCVTIMEO 276is an option to set a timeout value for input operations. 277It accepts a 278.Li struct timeval 279parameter with the number of seconds and microseconds 280used to limit waits for input operations to complete. 281In the current implementation, this timer is restarted each time additional 282data are received by the protocol, 283and thus the limit is in effect an inactivity timer. 284If a receive operation has been blocked for this much time without 285receiving additional data, it returns with a short count 286or with the error 287.Er EWOULDBLOCK 288if no data were received. 289.Pp 290Finally, 291.Dv SO_TYPE 292and 293.Dv SO_ERROR 294are options used only with 295.Fn getsockopt . 296.Dv SO_TYPE 297returns the type of the socket, such as 298.Dv SOCK_STREAM ; 299it is useful for servers that inherit sockets on startup. 300.Dv SO_ERROR 301returns any pending error on the socket and clears the 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 connect 2 , 331.Xr ioctl 2 , 332.Xr poll 2 , 333.Xr select 2 , 334.Xr socket 2 , 335.Xr getprotoent 3 , 336.Xr protocols 5 337.Sh STANDARDS 338.Dv SO_PEERCRED 339is not supported, see 340.Xr getpeereid 2 341instead. 342.Sh HISTORY 343The 344.Fn getsockopt 345system call appeared in 346.Bx 4.2 . 347.Sh BUGS 348Several of the socket options should be handled at lower levels of the system. 349