1.\" $OpenBSD: getsockopt.2,v 1.25 2008/05/09 11:58:16 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: May 9 2008 $ 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_TYPE Ta "get the type of the socket (get only)" 153.It Dv SO_ERROR Ta "get and clear error on the socket (get only)" 154.El 155.Pp 156.Dv SO_DEBUG 157enables debugging in the underlying protocol modules. 158.Dv SO_REUSEADDR 159indicates that the rules used in validating addresses supplied in a 160.Xr bind 2 161call should allow reuse of local addresses. 162.Dv SO_REUSEPORT 163allows completely duplicate bindings by multiple processes if they all set 164.Dv SO_REUSEPORT 165before binding the port. 166This option permits multiple instances of a program to each 167receive UDP/IP multicast or broadcast datagrams destined for the bound port. 168.Dv SO_KEEPALIVE 169enables the periodic transmission of messages on a connected socket. 170Should the connected party fail to respond to these messages, the connection 171is considered broken and processes using the socket are notified via a 172.Dv SIGPIPE 173signal when attempting to send data. 174.Dv SO_DONTROUTE 175indicates that outgoing messages should 176bypass the standard routing facilities. 177Instead, messages are directed to the appropriate network interface 178according to the network portion of the destination address. 179.Pp 180.Dv SO_LINGER 181controls the action taken when unsent messages 182are queued on socket and a 183.Xr close 2 184is performed. 185If the socket promises reliable delivery of data and 186.Dv SO_LINGER 187is set, the system will block the process on the 188.Xr close 2 189attempt until it is able to transmit the data or until it decides it 190is unable to deliver the information (a timeout period measured in seconds, 191termed the linger interval, is specified in the 192.Fn setsockopt 193call when 194.Dv SO_LINGER 195is requested). 196If 197.Dv SO_LINGER 198is disabled and a 199.Xr close 2 200is issued, the system will process the close in a manner that allows 201the process to continue as quickly as possible. 202.Pp 203The option 204.Dv SO_BROADCAST 205requests permission to send broadcast datagrams 206on the socket. 207Broadcast was a privileged operation in earlier versions of the system. 208With protocols that support out-of-band data, the 209.Dv SO_OOBINLINE 210option requests that out-of-band data be placed in the normal data input 211queue as received; it will then be accessible with 212.Xr recv 2 213or 214.Xr read 2 215calls without the 216.Dv MSG_OOB 217flag. 218Some protocols always behave as if this option is set. 219.Pp 220.Dv SO_BINDANY 221allows the socket to be bound to addresses 222which are not local to the machine, so it 223can be used to make a transparent proxy. 224Note that this option is limited to the super-user. 225In order to receive packets for these addresses, 226.Dv SO_BINDANY 227needs to be combined with matching outgoing 228.Xr pf 4 229divert rules. 230For example, with the following rule the socket receives packets 231for 192.168.0.10 even if it is not a local address: 232.Pp 233.Dl pass out inet from 192.168.0.10 divert-reply 234.Pp 235.Dv SO_SNDBUF 236and 237.Dv SO_RCVBUF 238are options to adjust the normal 239buffer sizes allocated for output and input buffers, respectively. 240The buffer size may be increased for high-volume connections, 241or may be decreased to limit the possible backlog of incoming data. 242The system places an absolute limit on these values. 243.Pp 244.Dv SO_SNDLOWAT 245is an option to set the minimum count for output operations. 246Most output operations process all of the data supplied 247by the call, delivering data to the protocol for transmission 248and blocking as necessary for flow control. 249Nonblocking output operations will process as much data as permitted 250subject to flow control without blocking, but will process no data 251if flow control does not allow the smaller of the low water mark value 252or the entire request to be processed. 253A 254.Xr select 2 255or 256.Xr poll 2 257operation testing the ability to write to a socket will return true 258only if the low water mark amount could be processed. 259The default value for 260.Dv SO_SNDLOWAT 261is set to a convenient size for network efficiency, often 1024. 262.Dv SO_RCVLOWAT 263is an option to set the minimum count for input operations. 264In general, receive calls will block until any (non-zero) amount of data 265is received, then return with the smaller of the amount available or the amount 266requested. 267The default value for 268.Dv SO_RCVLOWAT 269is 1. 270If 271.Dv SO_RCVLOWAT 272is set to a larger value, blocking receive calls normally 273wait until they have received the smaller of the low water mark value 274or the requested amount. 275Receive calls may still return less than the low water mark if an error 276occurs, a signal is caught, or the type of data next in the receive queue 277is different than that returned. 278.Pp 279.Dv SO_SNDTIMEO 280is an option to set a timeout value for output operations. 281It accepts a 282.Li struct timeval 283parameter with the number of seconds and microseconds 284used to limit waits for output operations to complete. 285If a send operation has blocked for this much time, 286it returns with a partial count or with the error 287.Er EWOULDBLOCK 288if no data was sent. 289In the current implementation, this timer is restarted each time additional 290data are delivered to the protocol, 291implying that the limit applies to output portions ranging in size 292from the low water mark to the high water mark for output. 293.Dv SO_RCVTIMEO 294is an option to set a timeout value for input operations. 295It accepts a 296.Li struct timeval 297parameter with the number of seconds and microseconds 298used to limit waits for input operations to complete. 299In the current implementation, this timer is restarted each time additional 300data are received by the protocol, 301and thus the limit is in effect an inactivity timer. 302If a receive operation has been blocked for this much time without 303receiving additional data, it returns with a short count 304or with the error 305.Er EWOULDBLOCK 306if no data were received. 307.Pp 308If the 309.Dv SO_TIMESTAMP 310option is enabled on a 311.Dv SOCK_DGRAM 312socket, the 313.Xr recvmsg 2 314call will return a timestamp corresponding to when the datagram was 315received. 316The msg_control field in the msghdr structure points to a buffer 317that contains a cmsghdr structure followed by a struct timeval. 318The cmsghdr fields have the following values: 319.Bd -literal -offset indent 320cmsg_len = CMSG_LEN(sizeof(struct timeval)) 321cmsg_level = SOL_SOCKET 322cmsg_type = SCM_TIMESTAMP 323.Ed 324.Pp 325Finally, 326.Dv SO_TYPE 327and 328.Dv SO_ERROR 329are options used only with 330.Fn getsockopt . 331.Dv SO_TYPE 332returns the type of the socket, such as 333.Dv SOCK_STREAM ; 334it is useful for servers that inherit sockets on startup. 335.Dv SO_ERROR 336returns any pending error on the socket and clears the error status. 337It may be used to check for asynchronous errors on connected 338datagram sockets or for other asynchronous errors. 339.Sh RETURN VALUES 340A 0 is returned if the call succeeds, \-1 if it fails. 341.Sh ERRORS 342The call succeeds unless: 343.Bl -tag -width Er 344.It Bq Er EBADF 345The argument 346.Fa s 347is not a valid descriptor. 348.It Bq Er ENOTSOCK 349The argument 350.Fa s 351is a file, not a socket. 352.It Bq Er ENOPROTOOPT 353The option is unknown at the level indicated. 354.It Bq Er EFAULT 355The address pointed to by 356.Fa optval 357is not in a valid part of the process address space. 358For 359.Fn getsockopt , 360this error may also be returned if 361.Fa optlen 362is not in a valid part of the process address space. 363.El 364.Sh SEE ALSO 365.Xr connect 2 , 366.Xr ioctl 2 , 367.Xr poll 2 , 368.Xr select 2 , 369.Xr socket 2 , 370.Xr getprotoent 3 , 371.Xr pf.conf 5 , 372.Xr protocols 5 373.Sh STANDARDS 374.Dv SO_PEERCRED 375is not supported, see 376.Xr getpeereid 2 377instead. 378.Sh HISTORY 379The 380.Fn getsockopt 381system call appeared in 382.Bx 4.2 . 383.Sh BUGS 384Several of the socket options should be handled at lower levels of the system. 385