1.\" $OpenBSD: getsockopt.2,v 1.36 2011/12/23 17:00:47 jmc 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: December 23 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.Pp 136.Bl -tag -width SO_OOBINLINE -offset indent -compact 137.It Dv SO_DEBUG 138enables recording of debugging information 139.It Dv SO_REUSEADDR 140enables local address reuse 141.It Dv SO_REUSEPORT 142enables duplicate address and port bindings 143.It Dv SO_KEEPALIVE 144enables keep connections alive 145.It Dv SO_DONTROUTE 146enables routing bypass for outgoing messages 147.It Dv SO_LINGER 148linger on close if data present 149.It Dv SO_BROADCAST 150enables permission to transmit broadcast messages 151.It Dv SO_OOBINLINE 152enables reception of out-of-band data in band 153.It Dv SO_BINDANY 154enables binding to any address 155.It Dv SO_SNDBUF 156set buffer size for output 157.It Dv SO_RCVBUF 158set buffer size for input 159.It Dv SO_SNDLOWAT 160set minimum count for output 161.It Dv SO_RCVLOWAT 162set minimum count for input 163.It Dv SO_SNDTIMEO 164set timeout value for output 165.It Dv SO_RCVTIMEO 166set timeout value for input 167.It Dv SO_TIMESTAMP 168enables reception of a timestamp with datagrams 169.It Dv SO_PEERCRED 170get the credentials from other side of connection 171.It Dv SO_RTABLE 172set the routing domain 173.It Dv SO_SPLICE 174splice two sockets together or get data length 175.It Dv SO_TYPE 176get the type of the socket (get only) 177.It Dv SO_ERROR 178get and clear error on the socket (get only) 179.El 180.Pp 181.Dv SO_DEBUG 182enables debugging in the underlying protocol modules. 183.Dv SO_REUSEADDR 184indicates that the rules used in validating addresses supplied in a 185.Xr bind 2 186call should allow reuse of local addresses. 187.Dv SO_REUSEPORT 188allows completely duplicate bindings by multiple processes if they all set 189.Dv SO_REUSEPORT 190before binding the port. 191This option permits multiple instances of a program to each 192receive UDP/IP multicast or broadcast datagrams destined for the bound port. 193.Dv SO_KEEPALIVE 194enables the periodic transmission of messages on a connected socket. 195Should the connected party fail to respond to these messages, the connection 196is considered broken and processes using the socket are notified via a 197.Dv SIGPIPE 198signal when attempting to send data. 199.Dv SO_DONTROUTE 200indicates that outgoing messages should 201bypass the standard routing facilities. 202Instead, messages are directed to the appropriate network interface 203according to the network portion of the destination address. 204.Pp 205.Dv SO_LINGER 206controls the action taken when unsent messages 207are queued on socket and a 208.Xr close 2 209is performed. 210If the socket promises reliable delivery of data and 211.Dv SO_LINGER 212is set, the system will block the process on the 213.Xr close 2 214attempt until it is able to transmit the data or until it decides it 215is unable to deliver the information (a timeout period measured in seconds, 216termed the linger interval, is specified in the 217.Fn setsockopt 218call when 219.Dv SO_LINGER 220is requested). 221If 222.Dv SO_LINGER 223is disabled and a 224.Xr close 2 225is issued, the system will process the close in a manner that allows 226the process to continue as quickly as possible. 227.Pp 228The option 229.Dv SO_BROADCAST 230requests permission to send broadcast datagrams 231on the socket. 232Broadcast was a privileged operation in earlier versions of the system. 233With protocols that support out-of-band data, the 234.Dv SO_OOBINLINE 235option requests that out-of-band data be placed in the normal data input 236queue as received; it will then be accessible with 237.Xr recv 2 238or 239.Xr read 2 240calls without the 241.Dv MSG_OOB 242flag. 243Some protocols always behave as if this option is set. 244.Pp 245.Dv SO_BINDANY 246allows the socket to be bound to addresses 247which are not local to the machine, so it 248can be used to make a transparent proxy. 249Note that this option is limited to the super-user. 250In order to receive packets for these addresses, 251.Dv SO_BINDANY 252needs to be combined with matching outgoing 253.Xr pf 4 254divert rules. 255For example, with the following rule the socket receives packets 256for 192.168.0.10 even if it is not a local address: 257.Pp 258.Dl pass out inet from 192.168.0.10 divert-reply 259.Pp 260.Dv SO_SNDBUF 261and 262.Dv SO_RCVBUF 263are options to adjust the normal 264buffer sizes allocated for output and input buffers, respectively. 265The buffer size may be increased for high-volume connections, 266or may be decreased to limit the possible backlog of incoming data. 267The system places an absolute limit on these values. 268.Pp 269.Dv SO_SNDLOWAT 270is an option to set the minimum count for output operations. 271Most output operations process all of the data supplied 272by the call, delivering data to the protocol for transmission 273and blocking as necessary for flow control. 274Nonblocking output operations will process as much data as permitted 275subject to flow control without blocking, but will process no data 276if flow control does not allow the smaller of the low water mark value 277or the entire request to be processed. 278A 279.Xr select 2 280or 281.Xr poll 2 282operation testing the ability to write to a socket will return true 283only if the low water mark amount could be processed. 284The default value for 285.Dv SO_SNDLOWAT 286is set to a convenient size for network efficiency, often 1024. 287.Dv SO_RCVLOWAT 288is an option to set the minimum count for input operations. 289In general, receive calls will block until any (non-zero) amount of data 290is received, then return with the smaller of the amount available or the amount 291requested. 292The default value for 293.Dv SO_RCVLOWAT 294is 1. 295If 296.Dv SO_RCVLOWAT 297is set to a larger value, blocking receive calls normally 298wait until they have received the smaller of the low water mark value 299or the requested amount. 300Receive calls may still return less than the low water mark if an error 301occurs, a signal is caught, or the type of data next in the receive queue 302is different than that returned. 303.Pp 304.Dv SO_SNDTIMEO 305is an option to set a timeout value for output operations. 306It accepts a 307.Li struct timeval 308parameter with the number of seconds and microseconds 309used to limit waits for output operations to complete. 310If a send operation has blocked for this much time, 311it returns with a partial count or with the error 312.Er EWOULDBLOCK 313if no data was sent. 314In the current implementation, this timer is restarted each time additional 315data are delivered to the protocol, 316implying that the limit applies to output portions ranging in size 317from the low water mark to the high water mark for output. 318.Dv SO_RCVTIMEO 319is an option to set a timeout value for input operations. 320It accepts a 321.Li struct timeval 322parameter with the number of seconds and microseconds 323used to limit waits for input operations to complete. 324In the current implementation, this timer is restarted each time additional 325data are received by the protocol, 326and thus the limit is in effect an inactivity timer. 327If a receive operation has been blocked for this much time without 328receiving additional data, it returns with a short count 329or with the error 330.Er EWOULDBLOCK 331if no data were received. 332.Pp 333If the 334.Dv SO_TIMESTAMP 335option is enabled on a 336.Dv SOCK_DGRAM 337socket, the 338.Xr recvmsg 2 339call will return a timestamp corresponding to when the datagram was 340received. 341The msg_control field in the msghdr structure points to a buffer 342that contains a cmsghdr structure followed by a struct timeval. 343The cmsghdr fields have the following values: 344.Bd -literal -offset indent 345cmsg_len = CMSG_LEN(sizeof(struct timeval)) 346cmsg_level = SOL_SOCKET 347cmsg_type = SCM_TIMESTAMP 348.Ed 349.Pp 350.Dv SO_PEERCRED 351fetches the 352.Va struct sockpeercred 353credentials from the other side of the connection 354(currently only possible on 355.Dv AF_UNIX 356sockets). 357These credentials are from the time that 358.Xr bind 2 359or 360.Xr connect 2 361were called. 362.Pp 363The 364.Dv SO_RTABLE 365option gets or sets a routing domain. 366If a protocol family of the socket doesn't support routing domains, 367the 368.Er ENOPROTOOPT 369error is returned. 370.Pp 371.Dv SO_SPLICE 372can splice together two connected TCP sockets for zero-copy data 373transfers. 374In the first form, 375.Fn setsockopt 376is called with the source socket 377.Fa s 378and the drain socket's 379.Vt int 380file descriptor as 381.Fa optval . 382In the second form, 383.Fa optval 384is a 385.Vt struct splice 386with the drain socket in 387.Va sp_fd , 388a positive maximum number of bytes or 0 in 389.Va sp_max 390and an idle timeout 391.Va sp_idle 392in the form of a 393.Vt struct timeval . 394If \-1 is given as drain socket, the source socket 395.Fa s 396gets unspliced. 397Otherwise the spliced data transfer continues within the kernel 398until the optional maximum is reached, one of the connections 399terminates, idle timeout expires or an error occurs. 400A successful 401.Xr select 2 , 402.Xr poll 2 , 403or 404.Xr kqueue 2 405operation testing the ability to read from the source socket indicates 406that the splicing has terminated. 407After reaching the maximum, it becomes readable only when more data 408is available. 409The error status can be examined with 410.Dv SO_ERROR 411at the source socket. 412The 413.Er ETIMEDOUT 414error is set if there was no data transferred between two sockets 415during the 416.Va sp_idle 417period of time. 418Note that if a maximum is given, it is only guaranteed that no more 419bytes are transferred. 420A short splice can happen but then a second call to splice will 421transfer the remaining data immediately. 422Also the readability check will not indicate that the maximum has 423been reached but that data after the maximum is available. 424The 425.Dv SO_SPLICE 426option with 427.Fn getsockopt 428and an 429.Vt off_t 430value as 431.Fa optval 432can be used to retrieve the number of bytes transferred so far from the 433source socket 434.Fa s . 435A successful new splice resets this number. 436.Pp 437Finally, 438.Dv SO_TYPE 439and 440.Dv SO_ERROR 441are options used only with 442.Fn getsockopt . 443.Dv SO_TYPE 444returns the type of the socket, such as 445.Dv SOCK_STREAM ; 446it is useful for servers that inherit sockets on startup. 447.Dv SO_ERROR 448returns any pending error on the socket and clears the error status. 449It may be used to check for asynchronous errors on connected 450datagram sockets or for other asynchronous errors. 451.Sh RETURN VALUES 452A 0 is returned if the call succeeds, \-1 if it fails. 453.Sh ERRORS 454The call succeeds unless: 455.Bl -tag -width Er 456.It Bq Er EBADF 457The argument 458.Fa s 459is not a valid descriptor. 460.It Bq Er ENOTSOCK 461The argument 462.Fa s 463is a file, not a socket. 464.It Bq Er ENOPROTOOPT 465The option is unknown at the level indicated. 466.It Bq Er EFAULT 467The address pointed to by 468.Fa optval 469is not in a valid part of the process address space. 470For 471.Fn getsockopt , 472this error may also be returned if 473.Fa optlen 474is not in a valid part of the process address space. 475.El 476.Sh SEE ALSO 477.Xr connect 2 , 478.Xr ioctl 2 , 479.Xr poll 2 , 480.Xr select 2 , 481.Xr socket 2 , 482.Xr getprotoent 3 , 483.Xr pf.conf 5 , 484.Xr protocols 5 , 485.Xr sosplice 9 486.Sh HISTORY 487The 488.Fn getsockopt 489system call appeared in 490.Bx 4.2 . 491.Sh BUGS 492Several of the socket options should be handled at lower levels of the system. 493