1.\" $NetBSD: getsockopt.2,v 1.41 2019/05/09 09:09:38 wiz Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)getsockopt.2 8.4 (Berkeley) 5/2/95 31.\" 32.Dd May 8, 2019 33.Dt GETSOCKOPT 2 34.Os 35.Sh NAME 36.Nm getsockopt , 37.Nm setsockopt , 38.Nm getsockopt2 39.Nd get and set options on sockets 40.Sh LIBRARY 41.Lb libc 42.Sh SYNOPSIS 43.In sys/socket.h 44.Ft int 45.Fn getsockopt "int s" "int level" "int optname" "void * restrict optval" "socklen_t * restrict optlen" 46.Ft int 47.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "socklen_t optlen" 48.Ft int 49.Fn getsockopt2 "int s" "int level" "int optname" "void * restrict optval" "socklen_t * restrict optlen" 50.Sh DESCRIPTION 51.Fn getsockopt , 52.Fn setsockopt 53and 54.Fn getsockopt2 55manipulate the 56.Em options 57associated with a socket. 58Options may exist at multiple 59protocol levels; they are always present at the uppermost 60.Dq socket 61level. 62.Pp 63When manipulating socket options the level at which the 64option resides and the name of the option must be specified. 65To manipulate options at the socket level, 66.Fa level 67is specified as 68.Dv SOL_SOCKET . 69To manipulate options at any 70other level the protocol number of the appropriate protocol 71controlling the option is supplied. 72For example, to indicate that an option is to be interpreted by the 73.Tn TCP 74protocol, 75.Fa level 76should be set to the protocol number of 77.Tn TCP ; 78see 79.Xr getprotoent 3 . 80.Pp 81The parameters 82.Fa optval 83and 84.Fa optlen 85are used to access option values for 86.Fn setsockopt . 87For 88.Fn getsockopt 89and 90.Fn getsockopt2 91they identify a buffer in which the value for the 92requested option(s) are to be returned. 93For 94.Fn getsockopt2 95they are also used to provide an extra argument to select which 96value to return. 97For 98.Fn getsockopt 99and 100.Fn getsockopt2 , 101.Fa optlen 102is a value-result parameter, initially containing the 103size of the buffer pointed to by 104.Fa optval , 105and modified on return to indicate the actual size of 106the value returned. 107If the size of the requested option value to be stored in 108.Fa optval 109is greater than the size indicated in 110.Fa optlen 111then only 112.Fa optlen 113bytes will be stored in 114.Fa optval 115and the result will be silently truncated. 116If no option value is to be supplied or returned, 117.Fa optval 118may be 119.Dv NULL . 120.Pp 121.Fa optname 122and any specified options are passed uninterpreted to the appropriate 123protocol module for interpretation. 124The include file 125.Ao Pa sys/socket.h Ac 126contains definitions for 127socket level options, described below. 128Options at other protocol levels vary in format and 129name; consult the appropriate entries in 130section 1314 of the manual, including: 132.Xr faith 4 , 133.Xr icmp6 4 , 134.Xr ip 4 , 135.Xr ip6 4 , 136.Xr ipsec 4 , 137.Xr multicast 4 , 138.Xr pim 4 , 139.Xr route 4 , 140.Xr tcp 4 , 141and 142.Xr unix 4 . 143.Pp 144Most socket-level options use an 145.Fa int 146parameter for 147.Fa optval . 148For 149.Fn setsockopt , 150the parameter should be non-zero to enable a boolean option, 151or zero if the option is to be disabled. 152.Dv SO_LINGER 153uses a 154.Fa struct linger 155parameter, defined in 156.Ao Pa sys/socket.h Ac , 157which specifies the desired state of the option and the 158linger interval (see below). 159.Dv SO_SNDTIMEO 160and 161.Dv SO_RCVTIMEO 162use a 163.Fa struct timeval 164parameter, defined in 165.Ao Pa sys/time.h Ac . 166.Pp 167The following options are recognized at the socket level. 168Except as noted, each may be examined with 169.Fn getsockopt 170or 171.Fn getsockopt2 172and set with 173.Fn setsockopt . 174.Bl -column SO_ACCEPTFILTER data -offset indent 175.It Dv SO_DEBUG Ta "enables recording of debugging information" 176.It Dv SO_REUSEADDR Ta "enables local address reuse" 177.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings" 178.It Dv SO_KEEPALIVE Ta "enables keep connections alive" 179.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages" 180.It Dv SO_LINGER Ta "linger on close if data present" 181.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages" 182.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band" 183.It Dv SO_SNDBUF Ta "set buffer size for output" 184.It Dv SO_RCVBUF Ta "set buffer size for input" 185.It Dv SO_SNDLOWAT Ta "set minimum count for output" 186.It Dv SO_RCVLOWAT Ta "set minimum count for input" 187.It Dv SO_SNDTIMEO Ta "set timeout value for output" 188.It Dv SO_RCVTIMEO Ta "set timeout value for input" 189.It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams" 190.It Dv SO_ACCEPTFILTER Ta "set accept filter on listening socket" 191.It Dv SO_RERROR Ta "enables receive size error reporting" 192.It Dv SO_NOSIGPIPE Ta 193controls generation of 194.Dv SIGPIPE 195for the socket 196.It Dv SO_TYPE Ta "get the type of the socket (get only)" 197.It Dv SO_ERROR Ta "get and clear error on the socket (get only)" 198.El 199.Pp 200.Dv SO_DEBUG 201enables debugging in the underlying protocol modules. 202.Dv SO_REUSEADDR 203indicates that the rules used in validating addresses supplied 204in a 205.Xr bind 2 206call should allow reuse of local addresses. 207.Dv SO_REUSEPORT 208allows completely duplicate bindings by multiple processes 209if they all set 210.Dv SO_REUSEPORT 211before binding the port. 212This option permits multiple instances of a program to each 213receive UDP/IP multicast or broadcast datagrams destined for the bound port. 214.Dv SO_KEEPALIVE 215enables the 216periodic transmission of messages on a connected socket. 217Should the connected party fail to respond to these messages, the connection 218is considered broken and processes using the socket are notified via a 219.Dv SIGPIPE 220signal when attempting to send data. 221.Dv SO_DONTROUTE 222indicates that outgoing messages should 223bypass the standard routing facilities. 224Instead, messages are directed to the appropriate network interface 225according to the network portion of the destination address. 226.Dv SO_RERROR 227indicates that receive buffer overflows should be handled as errors. 228Historically receive buffer overflows have been ignored and programs 229could not tell if they missed messages or messages had been truncated 230because of overflows. 231Since programs historically do not expect to get receive overflow errors, 232this behavior is not the default, but the default can be changed by 233setting the 234.Dv SO_RERROR 235flag using 236.Xr sysctl 8 237and 238.Dv kern.sooptions . 239.Pp 240.Dv SO_LINGER 241controls the action taken when unsent messages 242are queued on socket and a 243.Xr close 2 244is performed. 245If the socket promises reliable delivery of data and 246.Dv SO_LINGER 247is set, the system will block the process on the 248.Xr close 2 249attempt until it is able to transmit the data or until it decides it 250is unable to deliver the information (a timeout period, measured in 251seconds, termed the linger interval, is specified in the 252.Fn setsockopt 253call when 254.Dv SO_LINGER 255is requested). 256If 257.Dv SO_LINGER 258is disabled and a 259.Xr close 2 260is issued, the system will process the close in a manner that allows 261the process to continue as quickly as possible. 262.Pp 263The option 264.Dv SO_BROADCAST 265requests permission to send broadcast datagrams 266on the socket. 267Broadcast was a privileged operation in earlier versions of the system. 268With protocols that support out-of-band data, the 269.Dv SO_OOBINLINE 270option 271requests that out-of-band data be placed in the normal data input queue 272as received; it will then be accessible with 273.Xr recv 2 274or 275.Xr read 2 276calls without the 277.Dv MSG_OOB 278flag. 279Some protocols always behave as if this option is set. 280.Dv SO_SNDBUF 281and 282.Dv SO_RCVBUF 283are options to adjust the normal 284buffer sizes allocated for output and input buffers, respectively. 285The buffer size may be increased for high-volume connections, 286or may be decreased to limit the possible backlog of incoming data. 287The system places an absolute limit on these values. 288.Pp 289.Dv SO_SNDLOWAT 290is an option to set the minimum count for output operations. 291Most output operations process all of the data supplied 292by the call, delivering data to the protocol for transmission 293and blocking as necessary for flow control. 294Nonblocking output operations will process as much data as permitted 295subject to flow control without blocking, but will process no data 296if flow control does not allow the smaller of the low water mark value 297or the entire request to be processed. 298A 299.Xr select 2 300or 301.Xr poll 2 302operation testing the ability to write to a socket will return true 303only if the low water mark amount could be processed. 304The default value for 305.Dv SO_SNDLOWAT 306is set to a convenient size for network efficiency, often 1024. 307.Dv SO_RCVLOWAT 308is an option to set the minimum count for input operations. 309In general, receive calls will block until any (non-zero) amount of data 310is received, then return with the smaller of the amount available or the amount 311requested. 312The default value for 313.Dv SO_RCVLOWAT 314is 1. 315If 316.Dv SO_RCVLOWAT 317is set to a larger value, blocking receive calls normally 318wait until they have received the smaller of the low water mark value 319or the requested amount. 320Receive calls may still return less than the low water mark if an error 321occurs, a signal is caught, or the type of data next in the receive queue 322is different than that returned. 323.Pp 324.Dv SO_SNDTIMEO 325is an option to set a timeout value for output operations. 326It accepts a 327.Fa struct timeval 328parameter with the number of seconds and microseconds 329used to limit waits for output operations to complete. 330If a send operation has blocked for this much time, 331it returns with a partial count 332or with the error 333.Er EAGAIN 334if no data were sent. 335In the current implementation, this timer is restarted each time additional 336data are delivered to the protocol, 337implying that the limit applies to output portions ranging in size 338from the low water mark to the high water mark for output. 339.Dv SO_RCVTIMEO 340is an option to set a timeout value for input operations. 341It accepts a 342.Fa struct timeval 343parameter with the number of seconds and microseconds 344used to limit waits for input operations to complete. 345In the current implementation, this timer is restarted each time additional 346data are received by the protocol, 347and thus the limit is in effect an inactivity timer. 348If a receive operation has been blocked for this much time without 349receiving additional data, it returns with a short count 350or with the error 351.Er EAGAIN 352if no data were received. 353.Pp 354If the 355.Dv SO_TIMESTAMP 356option is enabled on a 357.Dv SOCK_DGRAM 358socket, the 359.Xr recvmsg 2 360call will return a timestamp corresponding to when the datagram was 361received. 362The msg_control field in the msghdr structure points to a buffer 363that contains a cmsghdr structure followed by a struct timeval. 364The cmsghdr fields have the following values: 365.Bd -literal 366cmsg_len = sizeof(struct timeval) 367cmsg_level = SOL_SOCKET 368cmsg_type = SCM_TIMESTAMP 369.Ed 370.Pp 371.Dv SO_ACCEPTFILTER 372places an 373.Xr accept_filter 9 374on the socket, which will filter incoming connections on a listening 375socket before being presented for 376.Xr accept 2 . 377The 378.Fn setsockopt 379system call will fail if the socket already has a filter set, and 380.Xr listen 2 381must be called on the socket before trying to install a filter. 382The 383.Fa optval 384argument 385should point to a 386.Fa struct accept_filter_arg 387that will select and configure the 388.Xr accept_filter 9 , 389defined as follows: 390.Bd -literal 391struct accept_filter_arg { 392 char af_name[16]; 393 char af_arg[256-16]; 394}; 395.Ed 396.Lp 397The 398.Fa af_name 399argument should be filled with the name of the accept filter 400that the application wishes to place on the listening socket. 401The optional argument 402.Fa af_arg 403can be passed to the accept filter specified by 404.Fa af_name 405to provide additional configuration options at attach time. 406Passing in an 407.Fa optval 408of 409.Dv NULL 410will remove the filter. 411.Pp 412Finally, 413.Dv SO_TYPE 414and 415.Dv SO_ERROR 416are options used only with 417.Fn getsockopt 418or 419.Fn getsockopt2 . 420.Dv SO_TYPE 421returns the type of the socket, such as 422.Dv SOCK_STREAM ; 423it is useful for servers that inherit sockets on startup. 424.Dv SO_ERROR 425returns any pending error on the socket and clears 426the error status. 427It may be used to check for asynchronous errors on connected 428datagram sockets or for other asynchronous errors. 429.Sh RETURN VALUES 430A 0 is returned if the call succeeds, \-1 if it fails. 431.Sh ERRORS 432The call succeeds unless: 433.Bl -tag -width Er 434.It Bq Er EBADF 435The argument 436.Fa s 437is not a valid descriptor. 438.It Bq Er EFAULT 439The address pointed to by 440.Fa optval 441is not in a valid part of the process address space. 442For 443.Fn getsockopt , 444this error may also be returned if 445.Fa optlen 446is not in a valid part of the process address space. 447.It Bq Er EINVAL 448The socket 449.Fa s 450was not suitable for installing an 451.Xr accept_filter 9 . 452.It Bq Er ENOPROTOOPT 453The option is unknown at the level indicated. 454.It Bq Er ENOTSOCK 455The argument 456.Fa s 457is a file, not a socket. 458.El 459.Sh SEE ALSO 460.Xr ioctl 2 , 461.Xr poll 2 , 462.Xr select 2 , 463.Xr socket 2 , 464.Xr getprotoent 3 , 465.Xr faith 4 , 466.Xr icmp6 4 , 467.Xr ip 4 , 468.Xr ip6 4 , 469.Xr ipsec 4 , 470.Xr multicast 4 , 471.Xr pim 4 , 472.Xr route 4 , 473.Xr tcp 4 , 474.Xr unix 4 , 475.Xr protocols 5 , 476.Xr accept_filter 9 477.Sh HISTORY 478The 479.Fn getsockopt 480system call appeared in 481.Bx 4.2 . 482.Pp 483The 484.Fn getsockopt2 485system call appeared in 486.Nx 9.0 . 487.Sh BUGS 488Several of the socket options should be handled at lower levels of the system. 489