1.\" $NetBSD: getsockopt.2,v 1.30 2007/12/16 09:49:28 lukem 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 December 16, 2007 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 NULL. 98.Pp 99.Fa optname 100and any specified options are passed uninterpreted to the appropriate 101protocol module for interpretation. 102The include file 103.Ao Pa sys/socket.h Ac 104contains definitions for 105socket level options, described below. 106Options at other protocol levels vary in format and 107name; consult the appropriate entries in 108section 1094 of the manual, including: 110.Xr clnp 4 , 111.Xr faith 4 , 112.Xr icmp6 4 , 113.Xr ip 4 , 114.Xr ip6 4 , 115.Xr ipsec 4 , 116.Xr multicast 4 , 117.Xr pim 4 , 118.Xr route 4 , 119.Xr tcp 4 , 120.Xr tp 4 , 121and 122.Xr unix 4 . 123.Pp 124Most socket-level options use an 125.Fa int 126parameter for 127.Fa optval . 128For 129.Fn setsockopt , 130the parameter should be non-zero to enable a boolean option, 131or zero if the option is to be disabled. 132.Dv SO_LINGER 133uses a 134.Fa struct linger 135parameter, defined in 136.Ao Pa sys/socket.h Ac , 137which specifies the desired state of the option and the 138linger interval (see below). 139.Dv SO_SNDTIMEO 140and 141.Dv SO_RCVTIMEO 142use a 143.Fa struct timeval 144parameter, defined in 145.Ao Pa sys/time.h Ac . 146.Pp 147The following options are recognized at the socket level. 148Except as noted, each may be examined with 149.Fn getsockopt 150and set with 151.Fn setsockopt . 152.Bl -column SO_OOBINLINE -offset indent 153.It Dv SO_DEBUG Ta "enables recording of debugging information" 154.It Dv SO_REUSEADDR Ta "enables local address reuse" 155.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings" 156.It Dv SO_KEEPALIVE Ta "enables keep connections alive" 157.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages" 158.It Dv SO_LINGER Ta "linger on close if data present" 159.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages" 160.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band" 161.It Dv SO_SNDBUF Ta "set buffer size for output" 162.It Dv SO_RCVBUF Ta "set buffer size for input" 163.It Dv SO_SNDLOWAT Ta "set minimum count for output" 164.It Dv SO_RCVLOWAT Ta "set minimum count for input" 165.It Dv SO_SNDTIMEO Ta "set timeout value for output" 166.It Dv SO_RCVTIMEO Ta "set timeout value for input" 167.It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams" 168.It Dv SO_TYPE Ta "get the type of the socket (get only)" 169.It Dv SO_ERROR Ta "get and clear error on the socket (get only)" 170.El 171.Pp 172.Dv SO_DEBUG 173enables debugging in the underlying protocol modules. 174.Dv SO_REUSEADDR 175indicates that the rules used in validating addresses supplied 176in a 177.Xr bind 2 178call should allow reuse of local addresses. 179.Dv SO_REUSEPORT 180allows completely duplicate bindings by multiple processes 181if they all set 182.Dv SO_REUSEPORT 183before binding the port. 184This option permits multiple instances of a program to each 185receive UDP/IP multicast or broadcast datagrams destined for the bound port. 186.Dv SO_KEEPALIVE 187enables the 188periodic transmission of messages on a connected socket. 189Should the connected party fail to respond to these messages, the connection 190is considered broken and processes using the socket are notified via a 191.Dv SIGPIPE 192signal when attempting to send data. 193.Dv SO_DONTROUTE 194indicates that outgoing messages should 195bypass the standard routing facilities. 196Instead, messages are directed to the appropriate network interface 197according to the network portion of the destination address. 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 210seconds, termed 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 230requests that out-of-band data be placed in the normal data input queue 231as received; it will then be accessible with 232.Xr recv 2 233or 234.Xr read 2 235calls without the 236.Dv MSG_OOB 237flag. 238Some protocols always behave as if this option is set. 239.Dv SO_SNDBUF 240and 241.Dv SO_RCVBUF 242are options to adjust the normal 243buffer sizes allocated for output and input buffers, respectively. 244The buffer size may be increased for high-volume connections, 245or may be decreased to limit the possible backlog of incoming data. 246The system places an absolute limit on these values. 247.Pp 248.Dv SO_SNDLOWAT 249is an option to set the minimum count for output operations. 250Most output operations process all of the data supplied 251by the call, delivering data to the protocol for transmission 252and blocking as necessary for flow control. 253Nonblocking output operations will process as much data as permitted 254subject to flow control without blocking, but will process no data 255if flow control does not allow the smaller of the low water mark value 256or the entire request to be processed. 257A 258.Xr select 2 259or 260.Xr poll 2 261operation testing the ability to write to a socket will return true 262only if the low water mark amount could be processed. 263The default value for 264.Dv SO_SNDLOWAT 265is set to a convenient size for network efficiency, often 1024. 266.Dv SO_RCVLOWAT 267is an option to set the minimum count for input operations. 268In general, receive calls will block until any (non-zero) amount of data 269is received, then return with the smaller of the amount available or the amount 270requested. 271The default value for 272.Dv SO_RCVLOWAT 273is 1. 274If 275.Dv SO_RCVLOWAT 276is set to a larger value, blocking receive calls normally 277wait until they have received the smaller of the low water mark value 278or the requested amount. 279Receive calls may still return less than the low water mark if an error 280occurs, a signal is caught, or the type of data next in the receive queue 281is different than that returned. 282.Pp 283.Dv SO_SNDTIMEO 284is an option to set a timeout value for output operations. 285It accepts a 286.Fa struct timeval 287parameter with the number of seconds and microseconds 288used to limit waits for output operations to complete. 289If a send operation has blocked for this much time, 290it returns with a partial count 291or with the error 292.Er EAGAIN 293if no data were sent. 294In the current implementation, this timer is restarted each time additional 295data are delivered to the protocol, 296implying that the limit applies to output portions ranging in size 297from the low water mark to the high water mark for output. 298.Dv SO_RCVTIMEO 299is an option to set a timeout value for input operations. 300It accepts a 301.Fa struct timeval 302parameter with the number of seconds and microseconds 303used to limit waits for input operations to complete. 304In the current implementation, this timer is restarted each time additional 305data are received by the protocol, 306and thus the limit is in effect an inactivity timer. 307If a receive operation has been blocked for this much time without 308receiving additional data, it returns with a short count 309or with the error 310.Er EAGAIN 311if no data were received. 312.Pp 313If the 314.Dv SO_TIMESTAMP 315option is enabled on a 316.Dv SOCK_DGRAM 317socket, the 318.Xr recvmsg 2 319call will return a timestamp corresponding to when the datagram was 320received. 321The msg_control field in the msghdr structure points to a buffer 322that contains a cmsghdr structure followed by a struct timeval. 323The cmsghdr fields have the following values: 324.Bd -literal 325cmsg_len = sizeof(struct timeval) 326cmsg_level = SOL_SOCKET 327cmsg_type = SCM_TIMESTAMP 328.Ed 329.Pp 330Finally, 331.Dv SO_TYPE 332and 333.Dv SO_ERROR 334are options used only with 335.Fn getsockopt . 336.Dv SO_TYPE 337returns the type of the socket, such as 338.Dv SOCK_STREAM ; 339it is useful for servers that inherit sockets on startup. 340.Dv SO_ERROR 341returns any pending error on the socket and clears 342the error status. 343It may be used to check for asynchronous errors on connected 344datagram sockets or for other asynchronous errors. 345.Sh RETURN VALUES 346A 0 is returned if the call succeeds, \-1 if it fails. 347.Sh ERRORS 348The call succeeds unless: 349.Bl -tag -width Er 350.It Bq Er EBADF 351The argument 352.Fa s 353is not a valid descriptor. 354.It Bq Er ENOTSOCK 355The argument 356.Fa s 357is a file, not a socket. 358.It Bq Er ENOPROTOOPT 359The option is unknown at the level indicated. 360.It Bq Er EFAULT 361The address pointed to by 362.Fa optval 363is not in a valid part of the process address space. 364For 365.Fn getsockopt , 366this error may also be returned if 367.Fa optlen 368is not in a valid part of the process address space. 369.El 370.Sh SEE ALSO 371.Xr ioctl 2 , 372.Xr poll 2 , 373.Xr select 2 , 374.Xr socket 2 , 375.Xr getprotoent 3 , 376.Xr clnp 4 , 377.Xr faith 4 , 378.Xr icmp6 4 , 379.Xr ip 4 , 380.Xr ip6 4 , 381.Xr ipsec 4 , 382.Xr multicast 4 , 383.Xr pim 4 , 384.Xr route 4 , 385.Xr tcp 4 , 386.Xr tp 4 , 387.Xr unix 4 , 388.Xr protocols 5 389.Sh HISTORY 390The 391.Fn getsockopt 392system call appeared in 393.Bx 4.2 . 394.Sh BUGS 395Several of the socket options should be handled at lower levels of the system. 396