1.\" $NetBSD: getsockopt.2,v 1.34 2009/06/29 08:38:07 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 June 28, 2009 33.Dt GETSOCKOPT 2 34.Os 35.Sh NAME 36.Nm getsockopt , 37.Nm setsockopt 38.Nd get and set options on sockets 39.Sh LIBRARY 40.Lb libc 41.Sh SYNOPSIS 42.In sys/socket.h 43.Ft int 44.Fn getsockopt "int s" "int level" "int optname" "void * restrict optval" "socklen_t * restrict 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 55protocol levels; they 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 66other level the protocol number of the appropriate protocol 67controlling the option is supplied. 68For example, to indicate that an option is to be interpreted by the 69.Tn TCP 70protocol, 71.Fa level 72should be set to the protocol number of 73.Tn TCP ; 74see 75.Xr getprotoent 3 . 76.Pp 77The parameters 78.Fa optval 79and 80.Fa optlen 81are used to access option values for 82.Fn setsockopt . 83For 84.Fn getsockopt 85they identify a buffer in which the value for the 86requested option(s) are to be returned. 87For 88.Fn getsockopt , 89.Fa optlen 90is a value-result parameter, initially containing the 91size of the buffer pointed to by 92.Fa optval , 93and modified on return to indicate the actual size of 94the value returned. 95If no option value is to be supplied or returned, 96.Fa optval 97may be 98.Dv NULL . 99.Pp 100.Fa optname 101and any specified options are passed uninterpreted to the appropriate 102protocol module for interpretation. 103The include file 104.Ao Pa sys/socket.h Ac 105contains definitions for 106socket level options, described below. 107Options at other protocol levels vary in format and 108name; consult the appropriate entries in 109section 1104 of the manual, including: 111.Xr clnp 4 , 112.Xr faith 4 , 113.Xr icmp6 4 , 114.Xr ip 4 , 115.Xr ip6 4 , 116.Xr ipsec 4 , 117.Xr multicast 4 , 118.Xr pim 4 , 119.Xr route 4 , 120.Xr tcp 4 , 121.Xr tp 4 , 122and 123.Xr unix 4 . 124.Pp 125Most socket-level options use an 126.Fa int 127parameter for 128.Fa optval . 129For 130.Fn setsockopt , 131the parameter should be non-zero to enable a boolean option, 132or zero if the option is to be disabled. 133.Dv SO_LINGER 134uses a 135.Fa struct linger 136parameter, defined in 137.Ao Pa sys/socket.h Ac , 138which specifies the desired state of the option and the 139linger interval (see below). 140.Dv SO_SNDTIMEO 141and 142.Dv SO_RCVTIMEO 143use a 144.Fa struct timeval 145parameter, defined in 146.Ao Pa sys/time.h Ac . 147.Pp 148The following options are recognized at the socket level. 149Except as noted, each may be examined with 150.Fn getsockopt 151and set with 152.Fn setsockopt . 153.Bl -column SO_ACCEPTFILTER data -offset indent 154.It Dv SO_DEBUG Ta "enables recording of debugging information" 155.It Dv SO_REUSEADDR Ta "enables local address reuse" 156.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings" 157.It Dv SO_KEEPALIVE Ta "enables keep connections alive" 158.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages" 159.It Dv SO_LINGER Ta "linger on close if data present" 160.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages" 161.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band" 162.It Dv SO_SNDBUF Ta "set buffer size for output" 163.It Dv SO_RCVBUF Ta "set buffer size for input" 164.It Dv SO_SNDLOWAT Ta "set minimum count for output" 165.It Dv SO_RCVLOWAT Ta "set minimum count for input" 166.It Dv SO_SNDTIMEO Ta "set timeout value for output" 167.It Dv SO_RCVTIMEO Ta "set timeout value for input" 168.It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams" 169.It Dv SO_ACCEPTFILTER Ta "set accept filter on listening socket" 170.It Dv SO_TYPE Ta "get the type of the socket (get only)" 171.It Dv SO_ERROR Ta "get and clear error on the socket (get only)" 172.El 173.Pp 174.Dv SO_DEBUG 175enables debugging in the underlying protocol modules. 176.Dv SO_REUSEADDR 177indicates that the rules used in validating addresses supplied 178in a 179.Xr bind 2 180call should allow reuse of local addresses. 181.Dv SO_REUSEPORT 182allows completely duplicate bindings by multiple processes 183if they all set 184.Dv SO_REUSEPORT 185before binding the port. 186This option permits multiple instances of a program to each 187receive UDP/IP multicast or broadcast datagrams destined for the bound port. 188.Dv SO_KEEPALIVE 189enables the 190periodic transmission of messages on a connected socket. 191Should the connected party fail to respond to these messages, the connection 192is considered broken and processes using the socket are notified via a 193.Dv SIGPIPE 194signal when attempting to send data. 195.Dv SO_DONTROUTE 196indicates that outgoing messages should 197bypass the standard routing facilities. 198Instead, messages are directed to the appropriate network interface 199according to the network portion of the destination address. 200.Pp 201.Dv SO_LINGER 202controls the action taken when unsent messages 203are queued on socket and a 204.Xr close 2 205is performed. 206If the socket promises reliable delivery of data and 207.Dv SO_LINGER 208is set, the system will block the process on the 209.Xr close 2 210attempt until it is able to transmit the data or until it decides it 211is unable to deliver the information (a timeout period, measured in 212seconds, termed the linger interval, is specified in the 213.Fn setsockopt 214call when 215.Dv SO_LINGER 216is requested). 217If 218.Dv SO_LINGER 219is disabled and a 220.Xr close 2 221is issued, the system will process the close in a manner that allows 222the process to continue as quickly as possible. 223.Pp 224The option 225.Dv SO_BROADCAST 226requests permission to send broadcast datagrams 227on the socket. 228Broadcast was a privileged operation in earlier versions of the system. 229With protocols that support out-of-band data, the 230.Dv SO_OOBINLINE 231option 232requests that out-of-band data be placed in the normal data input queue 233as received; it will then be accessible with 234.Xr recv 2 235or 236.Xr read 2 237calls without the 238.Dv MSG_OOB 239flag. 240Some protocols always behave as if this option is set. 241.Dv SO_SNDBUF 242and 243.Dv SO_RCVBUF 244are options to adjust the normal 245buffer sizes allocated for output and input buffers, respectively. 246The buffer size may be increased for high-volume connections, 247or may be decreased to limit the possible backlog of incoming data. 248The system places an absolute limit on these values. 249.Pp 250.Dv SO_SNDLOWAT 251is an option to set the minimum count for output operations. 252Most output operations process all of the data supplied 253by the call, delivering data to the protocol for transmission 254and blocking as necessary for flow control. 255Nonblocking output operations will process as much data as permitted 256subject to flow control without blocking, but will process no data 257if flow control does not allow the smaller of the low water mark value 258or the entire request to be processed. 259A 260.Xr select 2 261or 262.Xr poll 2 263operation testing the ability to write to a socket will return true 264only if the low water mark amount could be processed. 265The default value for 266.Dv SO_SNDLOWAT 267is set to a convenient size for network efficiency, often 1024. 268.Dv SO_RCVLOWAT 269is an option to set the minimum count for input operations. 270In general, receive calls will block until any (non-zero) amount of data 271is received, then return with the smaller of the amount available or the amount 272requested. 273The default value for 274.Dv SO_RCVLOWAT 275is 1. 276If 277.Dv SO_RCVLOWAT 278is set to a larger value, blocking receive calls normally 279wait until they have received the smaller of the low water mark value 280or the requested amount. 281Receive calls may still return less than the low water mark if an error 282occurs, a signal is caught, or the type of data next in the receive queue 283is different than that returned. 284.Pp 285.Dv SO_SNDTIMEO 286is an option to set a timeout value for output operations. 287It accepts a 288.Fa struct timeval 289parameter with the number of seconds and microseconds 290used to limit waits for output operations to complete. 291If a send operation has blocked for this much time, 292it returns with a partial count 293or with the error 294.Er EAGAIN 295if no data were sent. 296In the current implementation, this timer is restarted each time additional 297data are delivered to the protocol, 298implying that the limit applies to output portions ranging in size 299from the low water mark to the high water mark for output. 300.Dv SO_RCVTIMEO 301is an option to set a timeout value for input operations. 302It accepts a 303.Fa struct timeval 304parameter with the number of seconds and microseconds 305used to limit waits for input operations to complete. 306In the current implementation, this timer is restarted each time additional 307data are received by the protocol, 308and thus the limit is in effect an inactivity timer. 309If a receive operation has been blocked for this much time without 310receiving additional data, it returns with a short count 311or with the error 312.Er EAGAIN 313if no data were received. 314.Pp 315If the 316.Dv SO_TIMESTAMP 317option is enabled on a 318.Dv SOCK_DGRAM 319socket, the 320.Xr recvmsg 2 321call will return a timestamp corresponding to when the datagram was 322received. 323The msg_control field in the msghdr structure points to a buffer 324that contains a cmsghdr structure followed by a struct timeval. 325The cmsghdr fields have the following values: 326.Bd -literal 327cmsg_len = sizeof(struct timeval) 328cmsg_level = SOL_SOCKET 329cmsg_type = SCM_TIMESTAMP 330.Ed 331.Pp 332.Dv SO_ACCEPTFILTER 333places an 334.Xr accept_filter 9 335on the socket, which will filter incoming connections on a listening 336socket before being presented for 337.Xr accept 2 . 338The 339.Fn setsockopt 340system call will fail if the socket already has a filter set, and 341.Xr listen 2 342must be called on the socket before trying to install a filter. 343The 344.Fa optval 345argument 346should point to a 347.Fa struct accept_filter_arg 348that will select and configure the 349.Xr accept_filter 9 , 350defined as follows: 351.Bd -literal 352struct accept_filter_arg { 353 char af_name[16]; 354 char af_arg[256-16]; 355}; 356.Ed 357.Lp 358The 359.Fa af_name 360argument should be filled with the name of the accept filter 361that the application wishes to place on the listening socket. 362The optional argument 363.Fa af_arg 364can be passed to the accept filter specified by 365.Fa af_name 366to provide additional configuration options at attach time. 367Passing in an 368.Fa optval 369of 370.Dv NULL 371will remove the filter. 372.Pp 373Finally, 374.Dv SO_TYPE 375and 376.Dv SO_ERROR 377are options used only with 378.Fn getsockopt . 379.Dv SO_TYPE 380returns the type of the socket, such as 381.Dv SOCK_STREAM ; 382it is useful for servers that inherit sockets on startup. 383.Dv SO_ERROR 384returns any pending error on the socket and clears 385the error status. 386It may be used to check for asynchronous errors on connected 387datagram sockets or for other asynchronous errors. 388.Sh RETURN VALUES 389A 0 is returned if the call succeeds, \-1 if it fails. 390.Sh ERRORS 391The call succeeds unless: 392.Bl -tag -width Er 393.It Bq Er EBADF 394The argument 395.Fa s 396is not a valid descriptor. 397.It Bq Er EFAULT 398The address pointed to by 399.Fa optval 400is not in a valid part of the process address space. 401For 402.Fn getsockopt , 403this error may also be returned if 404.Fa optlen 405is not in a valid part of the process address space. 406.It Bq Er EINVAL 407The socket 408.Fa s 409was not suitable for installing an 410.Xr accept_filter 9 . 411.It Bq Er ENOPROTOOPT 412The option is unknown at the level indicated. 413.It Bq Er ENOTSOCK 414The argument 415.Fa s 416is a file, not a socket. 417.El 418.Sh SEE ALSO 419.Xr ioctl 2 , 420.Xr poll 2 , 421.Xr select 2 , 422.Xr socket 2 , 423.Xr getprotoent 3 , 424.Xr clnp 4 , 425.Xr faith 4 , 426.Xr icmp6 4 , 427.Xr ip 4 , 428.Xr ip6 4 , 429.Xr ipsec 4 , 430.Xr multicast 4 , 431.Xr pim 4 , 432.Xr route 4 , 433.Xr tcp 4 , 434.Xr tp 4 , 435.Xr unix 4 , 436.Xr protocols 5 , 437.Xr accept_filter 9 438.Sh HISTORY 439The 440.Fn getsockopt 441system call appeared in 442.Bx 4.2 . 443.Sh BUGS 444Several of the socket options should be handled at lower levels of the system. 445