1.\" $OpenBSD: getsockopt.2,v 1.30 2011/04/22 11:59:26 bluhm 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 $Mdocdate: April 22 2011 $ 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.Aq Pa sys/socket.h 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_BINDANY Ta "enables binding to any address" 145.It Dv SO_SNDBUF Ta "set buffer size for output" 146.It Dv SO_RCVBUF Ta "set buffer size for input" 147.It Dv SO_SNDLOWAT Ta "set minimum count for output" 148.It Dv SO_RCVLOWAT Ta "set minimum count for input" 149.It Dv SO_SNDTIMEO Ta "set timeout value for output" 150.It Dv SO_RCVTIMEO Ta "set timeout value for input" 151.It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams" 152.It Dv SO_PEERCRED Ta "get the credentials from other side of connection" 153.It Dv SO_SPLICE Ta "splice two sockets together or get data length" 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.Pp 222.Dv SO_BINDANY 223allows the socket to be bound to addresses 224which are not local to the machine, so it 225can be used to make a transparent proxy. 226Note that this option is limited to the super-user. 227In order to receive packets for these addresses, 228.Dv SO_BINDANY 229needs to be combined with matching outgoing 230.Xr pf 4 231divert rules. 232For example, with the following rule the socket receives packets 233for 192.168.0.10 even if it is not a local address: 234.Pp 235.Dl pass out inet from 192.168.0.10 divert-reply 236.Pp 237.Dv SO_SNDBUF 238and 239.Dv SO_RCVBUF 240are options to adjust the normal 241buffer sizes allocated for output and input buffers, respectively. 242The buffer size may be increased for high-volume connections, 243or may be decreased to limit the possible backlog of incoming data. 244The system places an absolute limit on these values. 245.Pp 246.Dv SO_SNDLOWAT 247is an option to set the minimum count for output operations. 248Most output operations process all of the data supplied 249by the call, delivering data to the protocol for transmission 250and blocking as necessary for flow control. 251Nonblocking output operations will process as much data as permitted 252subject to flow control without blocking, but will process no data 253if flow control does not allow the smaller of the low water mark value 254or the entire request to be processed. 255A 256.Xr select 2 257or 258.Xr poll 2 259operation testing the ability to write to a socket will return true 260only if the low water mark amount could be processed. 261The default value for 262.Dv SO_SNDLOWAT 263is set to a convenient size for network efficiency, often 1024. 264.Dv SO_RCVLOWAT 265is an option to set the minimum count for input operations. 266In general, receive calls will block until any (non-zero) amount of data 267is received, then return with the smaller of the amount available or the amount 268requested. 269The default value for 270.Dv SO_RCVLOWAT 271is 1. 272If 273.Dv SO_RCVLOWAT 274is set to a larger value, blocking receive calls normally 275wait until they have received the smaller of the low water mark value 276or the requested amount. 277Receive calls may still return less than the low water mark if an error 278occurs, a signal is caught, or the type of data next in the receive queue 279is different than that returned. 280.Pp 281.Dv SO_SNDTIMEO 282is an option to set a timeout value for output operations. 283It accepts a 284.Li struct timeval 285parameter with the number of seconds and microseconds 286used to limit waits for output operations to complete. 287If a send operation has blocked for this much time, 288it returns with a partial count or with the error 289.Er EWOULDBLOCK 290if no data was sent. 291In the current implementation, this timer is restarted each time additional 292data are delivered to the protocol, 293implying that the limit applies to output portions ranging in size 294from the low water mark to the high water mark for output. 295.Dv SO_RCVTIMEO 296is an option to set a timeout value for input operations. 297It accepts a 298.Li struct timeval 299parameter with the number of seconds and microseconds 300used to limit waits for input operations to complete. 301In the current implementation, this timer is restarted each time additional 302data are received by the protocol, 303and thus the limit is in effect an inactivity timer. 304If a receive operation has been blocked for this much time without 305receiving additional data, it returns with a short count 306or with the error 307.Er EWOULDBLOCK 308if no data were received. 309.Pp 310If the 311.Dv SO_TIMESTAMP 312option is enabled on a 313.Dv SOCK_DGRAM 314socket, the 315.Xr recvmsg 2 316call will return a timestamp corresponding to when the datagram was 317received. 318The msg_control field in the msghdr structure points to a buffer 319that contains a cmsghdr structure followed by a struct timeval. 320The cmsghdr fields have the following values: 321.Bd -literal -offset indent 322cmsg_len = CMSG_LEN(sizeof(struct timeval)) 323cmsg_level = SOL_SOCKET 324cmsg_type = SCM_TIMESTAMP 325.Ed 326.Pp 327.Dv SO_PEERCRED 328fetches the 329.Va struct sockpeercred 330credentials from the other side of the connection 331(currently only possible on 332.Dv AF_UNIX 333sockets). 334These credentials are from the time that 335.Xr bind 2 336or 337.Xr connect 2 338were called. 339.Pp 340.Dv SO_SPLICE 341can splice together two connected TCP sockets for zero-copy data 342transfers. 343In the first form, 344.Fn setsockopt 345is called with the source socket 346.Fa s 347and the drain socket's 348.Vt int 349file descriptor as 350.Fa optval . 351In the second form, 352.Fa optval 353is a 354.Vt struct splice 355with the drain socket in 356.Va sp_fd 357and a positive maximum number of bytes or 0 in 358.Va sp_max . 359If \-1 is given as drain socket, the source socket 360.Fa s 361gets unspliced. 362Otherwise the spliced data transfer continues within the kernel 363until the optional maximum is reached, one of the connections 364terminates or an error occurs. 365A successful 366.Xr select 2 , 367.Xr poll 2 , 368or 369.Xr kqueue 2 370operation testing the ability to read from the source socket indicates 371that the splicing has terminated. 372After reaching the maximum, it becomes readable only when more data 373is available. 374The error status can be examined with 375.Dv SO_ERROR 376at the source socket. 377Note that if a maximum is given, it is only guaranteed that no more 378bytes are transferred. 379A short splice can happen but then a second call to splice will 380transfer the remaining data immediately. 381Also the readability check will not indicate that the maximum has 382been reached but that data after the maximum is available. 383The 384.Dv SO_SPLICE 385option with 386.Fn getsockopt 387and an 388.Vt off_t 389value as 390.Fa optval 391can be used to retrieve the number of bytes transferred so far from the 392source socket 393.Fa s . 394A successful new splice resets this number. 395.Pp 396Finally, 397.Dv SO_TYPE 398and 399.Dv SO_ERROR 400are options used only with 401.Fn getsockopt . 402.Dv SO_TYPE 403returns the type of the socket, such as 404.Dv SOCK_STREAM ; 405it is useful for servers that inherit sockets on startup. 406.Dv SO_ERROR 407returns any pending error on the socket and clears the error status. 408It may be used to check for asynchronous errors on connected 409datagram sockets or for other asynchronous errors. 410.Sh RETURN VALUES 411A 0 is returned if the call succeeds, \-1 if it fails. 412.Sh ERRORS 413The call succeeds unless: 414.Bl -tag -width Er 415.It Bq Er EBADF 416The argument 417.Fa s 418is not a valid descriptor. 419.It Bq Er ENOTSOCK 420The argument 421.Fa s 422is a file, not a socket. 423.It Bq Er ENOPROTOOPT 424The option is unknown at the level indicated. 425.It Bq Er EFAULT 426The address pointed to by 427.Fa optval 428is not in a valid part of the process address space. 429For 430.Fn getsockopt , 431this error may also be returned if 432.Fa optlen 433is not in a valid part of the process address space. 434.El 435.Sh SEE ALSO 436.Xr connect 2 , 437.Xr ioctl 2 , 438.Xr poll 2 , 439.Xr select 2 , 440.Xr socket 2 , 441.Xr getprotoent 3 , 442.Xr pf.conf 5 , 443.Xr protocols 5 , 444.Xr sosplice 9 445.Sh HISTORY 446The 447.Fn getsockopt 448system call appeared in 449.Bx 4.2 . 450.Sh BUGS 451Several of the socket options should be handled at lower levels of the system. 452