1.\" $OpenBSD: getsockopt.2,v 1.46 2014/04/07 10:04:17 mpi 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 7 2014 $ 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/socket.h> 42.Ft int 43.Fn getsockopt "int s" "int level" "int optname" "void *optval" "socklen_t *optlen" 44.Ft int 45.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen" 46.Sh DESCRIPTION 47.Fn getsockopt 48and 49.Fn setsockopt 50manipulate the 51.Em options 52associated with a socket. 53Options may exist at multiple protocol levels; 54they are always present at the uppermost 55.Dq socket 56level. 57.Pp 58When manipulating socket options the level at which the 59option resides and the name of the option must be specified. 60To manipulate options at the socket level, 61.Fa level 62is specified as 63.Dv SOL_SOCKET . 64To manipulate options at any other level the protocol number of the 65appropriate protocol controlling the option is supplied. 66For example, to indicate that an option is to be interpreted by the 67.Tn TCP 68protocol, 69.Fa level 70should be set to the protocol number of 71.Tn TCP ; 72see 73.Xr getprotoent 3 . 74.Pp 75The parameters 76.Fa optval 77and 78.Fa optlen 79are used to access option values for 80.Fn setsockopt . 81For 82.Fn getsockopt 83they identify a buffer in which the value for the 84requested option(s) are to be returned. 85For 86.Fn getsockopt , 87.Fa optlen 88is a value-result parameter, initially containing the 89size of the buffer pointed to by 90.Fa optval , 91and modified on return to indicate the actual size of the value returned. 92If no option value is to be supplied or returned, 93.Fa optval 94may be 95.Dv NULL . 96.Pp 97.Fa optname 98and any specified options are passed uninterpreted to the appropriate 99protocol module for interpretation. 100The include file 101.In sys/socket.h 102contains definitions for socket level options, described below. 103Options at other protocol levels vary in format and name; 104consult the appropriate entries in section 4 of the manual. 105.Pp 106Most socket-level options utilize an 107.Li int 108parameter for 109.Fa optval . 110For 111.Fn setsockopt , 112the parameter should be non-zero to enable a boolean option, 113or zero if the option is to be disabled. 114.Dv SO_LINGER 115uses a 116.Li struct linger 117parameter, defined in 118.In sys/socket.h , 119which specifies the desired state of the option and the 120linger interval (see below). 121.Dv SO_SNDTIMEO 122and 123.Dv SO_RCVTIMEO 124use a 125.Li struct timeval 126parameter, defined in 127.In sys/time.h . 128.Pp 129The following options are recognized at the socket level. 130Except as noted, each may be examined with 131.Fn getsockopt 132and set with 133.Fn setsockopt . 134.Pp 135.Bl -tag -width SO_OOBINLINE -offset indent -compact 136.It Dv SO_DEBUG 137enables recording of debugging information 138.It Dv SO_REUSEADDR 139enables local address reuse 140.It Dv SO_REUSEPORT 141enables duplicate address and port bindings 142.It Dv SO_KEEPALIVE 143enables keep connections alive 144.It Dv SO_DONTROUTE 145enables routing bypass; not supported 146.It Dv SO_LINGER 147linger on close if data present 148.It Dv SO_BROADCAST 149enables permission to transmit broadcast messages 150.It Dv SO_OOBINLINE 151enables reception of out-of-band data in band 152.It Dv SO_BINDANY 153enables binding to any address 154.It Dv SO_SNDBUF 155set buffer size for output 156.It Dv SO_RCVBUF 157set buffer size for input 158.It Dv SO_SNDLOWAT 159set minimum count for output 160.It Dv SO_RCVLOWAT 161set minimum count for input 162.It Dv SO_SNDTIMEO 163set timeout value for output 164.It Dv SO_RCVTIMEO 165set timeout value for input 166.It Dv SO_TIMESTAMP 167enables reception of a timestamp with datagrams 168.It Dv SO_PEERCRED 169get the credentials from other side of connection 170.It Dv SO_RTABLE 171set the routing table used for route lookups 172.It Dv SO_SPLICE 173splice two sockets together or get data length 174.It Dv SO_TYPE 175get the type of the socket (get only) 176.It Dv SO_ERROR 177get and clear error on the socket (get only) 178.El 179.Pp 180.Dv SO_DEBUG 181enables debugging in the underlying protocol modules. 182.Dv SO_REUSEADDR 183indicates that the rules used in validating addresses supplied in a 184.Xr bind 2 185call should allow reuse of local addresses. 186.Dv SO_REUSEPORT 187allows completely duplicate bindings by multiple processes if they all set 188.Dv SO_REUSEPORT 189before binding the port. 190This option permits multiple instances of a program to each 191receive UDP/IP multicast or broadcast datagrams destined for the bound port. 192.Dv SO_KEEPALIVE 193enables the periodic transmission of messages on a connected socket. 194Should the connected party fail to respond to these messages, the connection 195is considered broken and processes using the socket are notified via a 196.Dv SIGPIPE 197signal when attempting to send data. 198.Pp 199.Dv SO_LINGER 200controls the action taken when unsent messages 201are queued on socket and a 202.Xr close 2 203is performed. 204If the socket promises reliable delivery of data and 205.Dv SO_LINGER 206is set, the system will block the process on the 207.Xr close 2 208attempt until it is able to transmit the data or until it decides it 209is unable to deliver the information (a timeout period measured in seconds, 210termed the linger interval, is specified in the 211.Fn setsockopt 212call when 213.Dv SO_LINGER 214is requested). 215If 216.Dv SO_LINGER 217is disabled and a 218.Xr close 2 219is issued, the system will process the close in a manner that allows 220the process to continue as quickly as possible. 221.Pp 222The option 223.Dv SO_BROADCAST 224requests permission to send broadcast datagrams 225on the socket. 226Broadcast was a privileged operation in earlier versions of the system. 227With protocols that support out-of-band data, the 228.Dv SO_OOBINLINE 229option requests that out-of-band data be placed in the normal data input 230queue as received; it will then be accessible with 231.Xr recv 2 232or 233.Xr read 2 234calls without the 235.Dv MSG_OOB 236flag. 237Some protocols always behave as if this option is set. 238.Pp 239.Dv SO_BINDANY 240allows the socket to be bound to addresses 241which are not local to the machine, so it 242can be used to make a transparent proxy. 243Note that this option is limited to the super-user. 244In order to receive packets for these addresses, 245.Dv SO_BINDANY 246needs to be combined with matching outgoing 247.Xr pf 4 248rules with the 249.Ar divert-reply 250parameter. 251For example, with the following rule the socket receives packets 252for 192.168.0.10 even if it is not a local address: 253.Pp 254.Dl pass out inet from 192.168.0.10 divert-reply 255.Pp 256.Dv SO_SNDBUF 257and 258.Dv SO_RCVBUF 259are options to adjust the normal 260buffer sizes allocated for output and input buffers, respectively. 261The buffer size may be increased for high-volume connections, 262or may be decreased to limit the possible backlog of incoming data. 263The system places an absolute limit on these values. 264.Pp 265.Dv SO_SNDLOWAT 266is an option to set the minimum count for output operations. 267Most output operations process all of the data supplied 268by the call, delivering data to the protocol for transmission 269and blocking as necessary for flow control. 270Nonblocking output operations will process as much data as permitted 271subject to flow control without blocking, but will process no data 272if flow control does not allow the smaller of the low water mark value 273or the entire request to be processed. 274A 275.Xr select 2 276or 277.Xr poll 2 278operation testing the ability to write to a socket will return true 279only if the low water mark amount could be processed. 280The default value for 281.Dv SO_SNDLOWAT 282is set to a convenient size for network efficiency, often 1024. 283.Dv SO_RCVLOWAT 284is an option to set the minimum count for input operations. 285In general, receive calls will block until any (non-zero) amount of data 286is received, then return with the smaller of the amount available or the amount 287requested. 288The default value for 289.Dv SO_RCVLOWAT 290is 1. 291If 292.Dv SO_RCVLOWAT 293is set to a larger value, blocking receive calls normally 294wait until they have received the smaller of the low water mark value 295or the requested amount. 296Receive calls may still return less than the low water mark if an error 297occurs, a signal is caught, or the type of data next in the receive queue 298is different than that returned. 299.Pp 300.Dv SO_SNDTIMEO 301is an option to set a timeout value for output operations. 302It accepts a 303.Li struct timeval 304parameter with the number of seconds and microseconds 305used to limit waits for output operations to complete. 306If a send operation has blocked for this much time, 307it returns with a partial count or with the error 308.Er EWOULDBLOCK 309if no data was sent. 310In the current implementation, this timer is restarted each time additional 311data are delivered to the protocol, 312implying that the limit applies to output portions ranging in size 313from the low water mark to the high water mark for output. 314.Dv SO_RCVTIMEO 315is an option to set a timeout value for input operations. 316It accepts a 317.Li struct timeval 318parameter with the number of seconds and microseconds 319used to limit waits for input operations to complete. 320In the current implementation, this timer is restarted each time additional 321data are received by the protocol, 322and thus the limit is in effect an inactivity timer. 323If a receive operation has been blocked for this much time without 324receiving additional data, it returns with a short count 325or with the error 326.Er EWOULDBLOCK 327if no data were received. 328.Pp 329If the 330.Dv SO_TIMESTAMP 331option is enabled on a 332.Dv SOCK_DGRAM 333socket, the 334.Xr recvmsg 2 335call will return a timestamp corresponding to when the datagram was 336received. 337The msg_control field in the msghdr structure points to a buffer 338that contains a cmsghdr structure followed by a struct timeval. 339The cmsghdr fields have the following values: 340.Bd -literal -offset indent 341cmsg_len = CMSG_LEN(sizeof(struct timeval)) 342cmsg_level = SOL_SOCKET 343cmsg_type = SCM_TIMESTAMP 344.Ed 345.Pp 346.Dv SO_PEERCRED 347fetches the 348.Va struct sockpeercred 349credentials from the other side of the connection 350(currently only possible on 351.Dv AF_UNIX 352sockets). 353These credentials are from the time that 354.Xr bind 2 355or 356.Xr connect 2 357were called. 358.Pp 359The 360.Dv SO_RTABLE 361option gets or sets the routing table which will be used by the socket 362for address lookups. 363If a protocol family of the socket doesn't support switching routing tables, 364the 365.Er ENOPROTOOPT 366error is returned. 367Only the superuser is allowed to change the routing table if it is already 368set to a non-zero value. 369A socket's chosen routing table is initialized from the process's configuration, 370previously selected using 371.Xr setrtable 2 . 372.Pp 373.Dv SO_SPLICE 374can splice together two TCP or UDP sockets for zero-copy data transfers. 375Both sockets must be of the same type. 376In the first form, 377.Fn setsockopt 378is called with the source socket 379.Fa s 380and the drain socket's 381.Vt int 382file descriptor as 383.Fa optval . 384In the second form, 385.Fa optval 386is a 387.Vt struct splice 388with the drain socket in 389.Va sp_fd , 390a positive maximum number of bytes or 0 in 391.Va sp_max 392and an idle timeout 393.Va sp_idle 394in the form of a 395.Vt struct timeval . 396If \-1 is given as drain socket, the source socket 397.Fa s 398gets unspliced. 399Otherwise the spliced data transfer continues within the kernel 400until the optional maximum is reached, one of the connections 401terminates, idle timeout expires or an error occurs. 402A successful 403.Xr select 2 , 404.Xr poll 2 , 405or 406.Xr kqueue 2 407operation testing the ability to read from the source socket indicates 408that the splicing has terminated. 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. 418The 419.Er EFBIG 420error is set after exactly 421.Va sp_max 422bytes have been transferred. 423Note that if a maximum is given, it is only guaranteed that no more 424bytes are transferred. 425A short splice can happen, but then a second call to splice will 426transfer the remaining data immediately. 427The 428.Dv SO_SPLICE 429option with 430.Fn getsockopt 431and an 432.Vt off_t 433value as 434.Fa optval 435can be used to retrieve the number of bytes transferred so far from the 436source socket 437.Fa s . 438A successful new splice resets this number. 439.Pp 440Finally, 441.Dv SO_TYPE 442and 443.Dv SO_ERROR 444are options used only with 445.Fn getsockopt . 446.Dv SO_TYPE 447returns the type of the socket, such as 448.Dv SOCK_STREAM ; 449it is useful for servers that inherit sockets on startup. 450.Dv SO_ERROR 451returns any pending error on the socket and clears the error status. 452It may be used to check for asynchronous errors on connected 453datagram sockets or for other asynchronous errors. 454.Sh RETURN VALUES 455A 0 is returned if the call succeeds, \-1 if it fails. 456.Sh ERRORS 457The call succeeds unless: 458.Bl -tag -width Er 459.It Bq Er EBADF 460The argument 461.Fa s 462is not a valid descriptor. 463.It Bq Er ENOTSOCK 464The argument 465.Fa s 466is a file, not a socket. 467.It Bq Er ENOPROTOOPT 468The option is unknown at the level indicated. 469.It Bq Er EOPNOTSUPP 470The option is unsupported. 471.It Bq Er EFAULT 472The address pointed to by 473.Fa optval 474is not in a valid part of the process address space. 475For 476.Fn getsockopt , 477this error may also be returned if 478.Fa optlen 479is not in a valid part of the process address space. 480.El 481.Sh SEE ALSO 482.Xr connect 2 , 483.Xr getrtable 2 , 484.Xr ioctl 2 , 485.Xr poll 2 , 486.Xr select 2 , 487.Xr socket 2 , 488.Xr getprotoent 3 , 489.Xr divert 4 , 490.Xr pf.conf 5 , 491.Xr protocols 5 , 492.Xr sosplice 9 493.Sh STANDARDS 494The 495.Fn getsockopt 496and 497.Fn setsockopt 498functions conform to 499.St -p1003.1-2008 . 500.Sh HISTORY 501The 502.Fn getsockopt 503system call appeared in 504.Bx 4.2 . 505.Sh BUGS 506Several of the socket options should be handled at lower levels of the system. 507