1.\" $NetBSD: getsockopt.2,v 1.15 1998/08/29 08:32:38 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. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)getsockopt.2 8.4 (Berkeley) 5/2/95 35.\" 36.Dd May 2, 1995 37.Dt GETSOCKOPT 2 38.Os BSD 4.3r 39.Sh NAME 40.Nm getsockopt , 41.Nm setsockopt 42.Nd get and set options on sockets 43.Sh SYNOPSIS 44.Fd #include <sys/types.h> 45.Fd #include <sys/socket.h> 46.Ft int 47.Fn getsockopt "int s" "int level" "int optname" "void *optval" "int *optlen" 48.Ft int 49.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "int optlen" 50.Sh DESCRIPTION 51.Fn getsockopt 52and 53.Fn setsockopt 54manipulate the 55.Em options 56associated with a socket. Options may exist at multiple 57protocol levels; they are always present at the uppermost 58.Dq socket 59level. 60.Pp 61When manipulating socket options the level at which the 62option resides and the name of the option must be specified. 63To manipulate options at the socket level, 64.Fa level 65is specified as 66.Dv SOL_SOCKET . 67To manipulate options at any 68other level the protocol number of the appropriate protocol 69controlling the option is supplied. For example, 70to indicate that an option is to be interpreted by the 71.Tn TCP 72protocol, 73.Fa level 74should be set to the protocol number of 75.Tn TCP ; 76see 77.Xr getprotoent 3 . 78.Pp 79The parameters 80.Fa optval 81and 82.Fa optlen 83are used to access option values for 84.Fn setsockopt . 85For 86.Fn getsockopt 87they identify a buffer in which the value for the 88requested option(s) are to be returned. For 89.Fn getsockopt , 90.Fa optlen 91is a value-result parameter, initially containing the 92size of the buffer pointed to by 93.Fa optval , 94and modified on return to indicate the actual size of 95the value returned. If no option value is 96to be supplied or returned, 97.Fa optval 98may be 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. 111.Pp 112Most socket-level options utilize an 113.Fa int 114parameter for 115.Fa optval . 116For 117.Fn setsockopt , 118the parameter should be non-zero to enable a boolean option, 119or zero if the option is to be disabled. 120.Dv SO_LINGER 121uses a 122.Fa struct linger 123parameter, defined in 124.Ao Pa sys/socket.h Ac , 125which specifies the desired state of the option and the 126linger interval (see below). 127.Dv SO_SNDTIMEO 128and 129.Dv SO_RCVTIMEO 130use a 131.Fa struct timeval 132parameter, defined in 133.Ao Pa sys/time.h Ac . 134.Pp 135The following options are recognized at the socket level. 136Except as noted, each may be examined with 137.Fn getsockopt 138and set with 139.Fn setsockopt . 140.Bl -column SO_OOBINLINE -offset indent 141.It Dv SO_DEBUG Ta "enables recording of debugging information" 142.It Dv SO_REUSEADDR Ta "enables local address reuse" 143.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings" 144.It Dv SO_KEEPALIVE Ta "enables keep connections alive" 145.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages" 146.It Dv SO_LINGER Ta "linger on close if data present" 147.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages" 148.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band" 149.It Dv SO_SNDBUF Ta "set buffer size for output" 150.It Dv SO_RCVBUF Ta "set buffer size for input" 151.It Dv SO_SNDLOWAT Ta "set minimum count for output" 152.It Dv SO_RCVLOWAT Ta "set minimum count for input" 153.It Dv SO_SNDTIMEO Ta "set timeout value for output" 154.It Dv SO_RCVTIMEO Ta "set timeout value for input" 155.It Dv SO_TIMESTAMP Ta "enables reception of a timestamp with datagrams" 156.It Dv SO_TYPE Ta "get the type of the socket (get only)" 157.It Dv SO_ERROR Ta "get and clear error on the socket (get only)" 158.El 159.Pp 160.Dv SO_DEBUG 161enables debugging in the underlying protocol modules. 162.Dv SO_REUSEADDR 163indicates that the rules used in validating addresses supplied 164in a 165.Xr bind 2 166call should allow reuse of local addresses. 167.Dv SO_REUSEPORT 168allows completely duplicate bindings by multiple processes 169if they all set 170.Dv SO_REUSEPORT 171before binding the port. 172This option permits multiple instances of a program to each 173receive UDP/IP multicast or broadcast datagrams destined for the bound port. 174.Dv SO_KEEPALIVE 175enables the 176periodic transmission of messages on a connected socket. Should the 177connected party fail to respond to these messages, the connection is 178considered broken and processes using the socket are notified via a 179.Dv SIGPIPE 180signal when attempting to send data. 181.Dv SO_DONTROUTE 182indicates that outgoing messages should 183bypass the standard routing facilities. Instead, messages are directed 184to the appropriate network interface according to the network portion 185of the destination address. 186.Pp 187.Dv SO_LINGER 188controls the action taken when unsent messages 189are queued on socket and a 190.Xr close 2 191is performed. 192If the socket promises reliable delivery of data and 193.Dv SO_LINGER is set, 194the system will block the process on the 195.Xr close 2 196attempt until it is able to transmit the data or until it decides it 197is unable to deliver the information (a timeout period, measured in 198seconds, termed the linger interval, is specified in the 199.Fn setsockopt 200call when 201.Dv SO_LINGER 202is requested). 203If 204.Dv SO_LINGER 205is disabled and a 206.Xr close 2 207is issued, the system will process the close in a manner that allows 208the process to continue as quickly as possible. 209.Pp 210The option 211.Dv SO_BROADCAST 212requests permission to send broadcast datagrams 213on the socket. 214Broadcast was a privileged operation in earlier versions of the system. 215With protocols that support out-of-band data, the 216.Dv SO_OOBINLINE 217option 218requests that out-of-band data be placed in the normal data input queue 219as received; it will then be accessible with 220.Xr recv 2 221or 222.Xr read 2 223calls without the 224.Dv MSG_OOB 225flag. 226Some protocols always behave as if this option is set. 227.Dv SO_SNDBUF 228and 229.Dv SO_RCVBUF 230are options to adjust the normal 231buffer sizes allocated for output and input buffers, respectively. 232The buffer size may be increased for high-volume connections, 233or may be decreased to limit the possible backlog of incoming data. 234The system places an absolute limit on these values. 235.Pp 236.Dv SO_SNDLOWAT 237is an option to set the minimum count for output operations. 238Most output operations process all of the data supplied 239by the call, delivering data to the protocol for transmission 240and blocking as necessary for flow control. 241Nonblocking output operations will process as much data as permitted 242subject to flow control without blocking, but will process no data 243if flow control does not allow the smaller of the low water mark value 244or the entire request to be processed. 245A 246.Xr select 2 247or 248.Xr poll 2 249operation testing the ability to write to a socket will return true 250only if the low water mark amount could be processed. 251The default value for 252.Dv SO_SNDLOWAT 253is set to a convenient size for network efficiency, often 1024. 254.Dv SO_RCVLOWAT 255is an option to set the minimum count for input operations. 256In general, receive calls will block until any (non-zero) amount of data 257is received, then return with the smaller of the amount available or the amount 258requested. 259The default value for 260.Dv SO_RCVLOWAT 261is 1. 262If 263.Dv SO_RCVLOWAT 264is set to a larger value, blocking receive calls normally 265wait until they have received the smaller of the low water mark value 266or the requested amount. 267Receive calls may still return less than the low water mark if an error 268occurs, a signal is caught, or the type of data next in the receive queue 269is different than that returned. 270.Pp 271.Dv SO_SNDTIMEO 272is an option to set a timeout value for output operations. 273It accepts a 274.Fa struct timeval 275parameter with the number of seconds and microseconds 276used to limit waits for output operations to complete. 277If a send operation has blocked for this much time, 278it returns with a partial count 279or with the error 280.Er EWOULDBLOCK 281if no data were sent. 282In the current implementation, this timer is restarted each time additional 283data are delivered to the protocol, 284implying that the limit applies to output portions ranging in size 285from the low water mark to the high water mark for output. 286.Dv SO_RCVTIMEO 287is an option to set a timeout value for input operations. 288It accepts a 289.Fa struct timeval 290parameter with the number of seconds and microseconds 291used to limit waits for input operations to complete. 292In the current implementation, this timer is restarted each time additional 293data are received by the protocol, 294and thus the limit is in effect an inactivity timer. 295If a receive operation has been blocked for this much time without 296receiving additional data, it returns with a short count 297or with the error 298.Er EWOULDBLOCK 299if no data were received. 300.Pp 301If the 302.Dv SO_TIMESTAMP 303option is enabled on a 304.Dv SOCK_DGRAM 305socket, the 306.Xr recvmsg 2 307call will return a timestamp corresponding to when the datagram was 308received. 309The msg_control field in the msghdr structure points to a buffer 310that contains a cmsghdr structure followed by a struct timeval. 311The cmsghdr fields have the following values: 312.Bd -literal 313cmsg_len = sizeof(struct timeval) 314cmsg_level = SOL_SOCKET 315cmsg_type = SCM_TIMESTAMP 316.Ed 317.Pp 318Finally, 319.Dv SO_TYPE 320and 321.Dv SO_ERROR 322are options used only with 323.Fn getsockopt . 324.Dv SO_TYPE 325returns the type of the socket, such as 326.Dv SOCK_STREAM ; 327it is useful for servers that inherit sockets on startup. 328.Dv SO_ERROR 329returns any pending error on the socket and clears 330the error status. 331It may be used to check for asynchronous errors on connected 332datagram sockets or for other asynchronous errors. 333.Sh RETURN VALUES 334A 0 is returned if the call succeeds, -1 if it fails. 335.Sh ERRORS 336The call succeeds unless: 337.Bl -tag -width Er 338.It Bq Er EBADF 339The argument 340.Fa s 341is not a valid descriptor. 342.It Bq Er ENOTSOCK 343The argument 344.Fa s 345is a file, not a socket. 346.It Bq Er ENOPROTOOPT 347The option is unknown at the level indicated. 348.It Bq Er EFAULT 349The address pointed to by 350.Fa optval 351is not in a valid part of the process address space. 352For 353.Fn getsockopt , 354this error may also be returned if 355.Fa optlen 356is not in a valid part of the process address space. 357.El 358.Sh SEE ALSO 359.Xr ioctl 2 , 360.Xr socket 2 , 361.Xr select 2 , 362.Xr poll 2 , 363.Xr getprotoent 3 , 364.Xr protocols 5 365.Sh BUGS 366Several of the socket options should be handled at lower levels of the system. 367.Sh HISTORY 368The 369.Fn getsockopt 370system call appeared in 371.Bx 4.2 . 372