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