161700Sbostic.\" Copyright (c) 1983, 1991, 1993 261700Sbostic.\" The Regents of the University of California. All rights reserved. 320239Smckusick.\" 443568Strent.\" %sccs.include.redist.man% 520239Smckusick.\" 6*69169Smckusick.\" @(#)getsockopt.2 8.4 (Berkeley) 05/02/95 736759Sbostic.\" 847208Scael.Dd 947208Scael.Dt GETSOCKOPT 2 1047988Skarels.Os BSD 4.3r 1147208Scael.Sh NAME 1247208Scael.Nm getsockopt , 1347208Scael.Nm setsockopt 1447208Scael.Nd get and set options on sockets 1547208Scael.Sh SYNOPSIS 1647208Scael.Fd #include <sys/types.h> 1747208Scael.Fd #include <sys/socket.h> 1847208Scael.Ft int 1948896Ssellgren.Fn getsockopt "int s" "int level" "int optname" "void *optval" "int *optlen" 2047208Scael.Ft int 2148896Ssellgren.Fn setsockopt "int s" "int level" "int optname" "const void *optval" "int optlen" 2247208Scael.Sh DESCRIPTION 2347208Scael.Fn Getsockopt 2420239Smckusickand 2547208Scael.Fn setsockopt 2647208Scaelmanipulate the 2747208Scael.Em options 2820239Smckusickassociated with a socket. Options may exist at multiple 2920239Smckusickprotocol levels; they are always present at the uppermost 3047208Scael.Dq socket 3147208Scaellevel. 3247208Scael.Pp 3320239SmckusickWhen manipulating socket options the level at which the 3420239Smckusickoption resides and the name of the option must be specified. 3547208ScaelTo manipulate options at the socket level, 3647208Scael.Fa level 3747208Scaelis specified as 3847208Scael.Dv SOL_SOCKET . 3947208ScaelTo manipulate options at any 4020239Smckusickother level the protocol number of the appropriate protocol 4120239Smckusickcontrolling the option is supplied. For example, 4247208Scaelto indicate that an option is to be interpreted by the 4347208Scael.Tn TCP 4447208Scaelprotocol, 4547208Scael.Fa level 4647208Scaelshould be set to the protocol number of 4747208Scael.Tn TCP ; 4847208Scaelsee 4947208Scael.Xr getprotoent 3 . 5047208Scael.Pp 5120239SmckusickThe parameters 5247208Scael.Fa optval 5320239Smckusickand 5447208Scael.Fa optlen 5520239Smckusickare used to access option values for 5647208Scael.Fn setsockopt . 5720239SmckusickFor 5847208Scael.Fn getsockopt 5920239Smckusickthey identify a buffer in which the value for the 6020239Smckusickrequested option(s) are to be returned. For 6147208Scael.Fn getsockopt , 6247208Scael.Fa optlen 6320239Smckusickis a value-result parameter, initially containing the 6420239Smckusicksize of the buffer pointed to by 6547208Scael.Fa optval , 6620239Smckusickand modified on return to indicate the actual size of 6720239Smckusickthe value returned. If no option value is 6820239Smckusickto be supplied or returned, 6947208Scael.Fa optval 7047208Scaelmay be NULL. 7147208Scael.Pp 7247208Scael.Fa Optname 7320239Smckusickand any specified options are passed uninterpreted to the appropriate 7420239Smckusickprotocol module for interpretation. 7520239SmckusickThe include file 7647208Scael.Ao Pa sys/socket.h Ac 7747208Scaelcontains definitions for 7847208Scaelsocket level options, described below. 7920239SmckusickOptions at other protocol levels vary in format and 8047208Scaelname; consult the appropriate entries in 8147208Scaelsection 8247208Scael4 of the manual. 8347208Scael.Pp 8447988SkarelsMost socket-level options utilize an 8547208Scael.Fa int 8628139Skarelsparameter for 8747208Scael.Fa optval . 8828139SkarelsFor 8947208Scael.Fn setsockopt , 9047208Scaelthe parameter should be non-zero to enable a boolean option, 9128139Skarelsor zero if the option is to be disabled. 9247208Scael.Dv SO_LINGER 9347208Scaeluses a 9447208Scael.Fa struct linger 9528139Skarelsparameter, defined in 9647208Scael.Ao Pa sys/socket.h Ac , 9728139Skarelswhich specifies the desired state of the option and the 9828139Skarelslinger interval (see below). 9947988Skarels.Dv SO_SNDTIMEO 10047988Skarelsand 10147988Skarels.Dv SO_RCVTIMEO 10247988Skarelsuse a 10347988Skarels.Fa struct timeval 10447988Skarelsparameter, defined in 10547988Skarels.Ao Pa sys/time.h Ac . 10647208Scael.Pp 10728139SkarelsThe following options are recognized at the socket level. 10828139SkarelsExcept as noted, each may be examined with 10947208Scael.Fn getsockopt 11028139Skarelsand set with 11147208Scael.Fn setsockopt . 11247208Scael.Bl -column SO_OOBINLINE -offset indent 11347988Skarels.It Dv SO_DEBUG Ta "enables recording of debugging information" 11447988Skarels.It Dv SO_REUSEADDR Ta "enables local address reuse" 11561699Sbostic.It Dv SO_REUSEPORT Ta "enables duplicate address and port bindings" 11647988Skarels.It Dv SO_KEEPALIVE Ta "enables keep connections alive" 11747988Skarels.It Dv SO_DONTROUTE Ta "enables routing bypass for outgoing messages" 11847988Skarels.It Dv SO_LINGER Ta "linger on close if data present" 11947988Skarels.It Dv SO_BROADCAST Ta "enables permission to transmit broadcast messages" 12047988Skarels.It Dv SO_OOBINLINE Ta "enables reception of out-of-band data in band" 12147988Skarels.It Dv SO_SNDBUF Ta "set buffer size for output" 12247988Skarels.It Dv SO_RCVBUF Ta "set buffer size for input" 12347988Skarels.It Dv SO_SNDLOWAT Ta "set minimum count for output" 12447988Skarels.It Dv SO_RCVLOWAT Ta "set minimum count for input" 12547988Skarels.It Dv SO_SNDTIMEO Ta "set timeout value for output" 12647988Skarels.It Dv SO_RCVTIMEO Ta "set timeout value for input" 12747988Skarels.It Dv SO_TYPE Ta "get the type of the socket (get only)" 12847988Skarels.It Dv SO_ERROR Ta "get and clear error on the socket (get only)" 12947208Scael.El 13047208Scael.Pp 13147208Scael.Dv SO_DEBUG 13247208Scaelenables debugging in the underlying protocol modules. 13347208Scael.Dv SO_REUSEADDR 13447208Scaelindicates that the rules used in validating addresses supplied 13528139Skarelsin a 13647208Scael.Xr bind 2 13747208Scaelcall should allow reuse of local addresses. 13861699Sbostic.Dv SO_REUSEPORT 13961699Sbosticallows completely duplicate bindings by multiple processes 14061699Sbosticif they all set 14161699Sbostic.Dv SO_REUSEPORT 14261699Sbosticbefore binding the port. 14361699SbosticThis option permits multiple instances of a program to each 14461699Sbosticreceive UDP/IP multicast or broadcast datagrams destined for the bound port. 14547208Scael.Dv SO_KEEPALIVE 14647208Scaelenables the 14728139Skarelsperiodic transmission of messages on a connected socket. Should the 14828139Skarelsconnected party fail to respond to these messages, the connection is 14928139Skarelsconsidered broken and processes using the socket are notified via a 15047208Scael.Dv SIGPIPE 15147988Skarelssignal when attempting to send data. 15247208Scael.Dv SO_DONTROUTE 15347208Scaelindicates that outgoing messages should 15428139Skarelsbypass the standard routing facilities. Instead, messages are directed 15528139Skarelsto the appropriate network interface according to the network portion 15628139Skarelsof the destination address. 15747208Scael.Pp 15847208Scael.Dv SO_LINGER 15966930Sbosticcontrols the action taken when unsent messages 16028139Skarelsare queued on socket and a 16147208Scael.Xr close 2 16228139Skarelsis performed. 16347208ScaelIf the socket promises reliable delivery of data and 16447208Scael.Dv SO_LINGER is set, 16528139Skarelsthe system will block the process on the 16647208Scael.Xr close 16728139Skarelsattempt until it is able to transmit the data or until it decides it 16828139Skarelsis unable to deliver the information (a timeout period, termed the 169*69169Smckusicklinger interval, is specified in seconds in the 17047208Scael.Fn setsockopt 17147208Scaelcall when 17247208Scael.Dv SO_LINGER 17347208Scaelis requested). 17447208ScaelIf 17547208Scael.Dv SO_LINGER 17647208Scaelis disabled and a 17747208Scael.Xr close 17828139Skarelsis issued, the system will process the close in a manner that allows 17928139Skarelsthe process to continue as quickly as possible. 18047208Scael.Pp 18147208ScaelThe option 18247208Scael.Dv SO_BROADCAST 18347208Scaelrequests permission to send broadcast datagrams 18428139Skarelson the socket. 18528139SkarelsBroadcast was a privileged operation in earlier versions of the system. 18647208ScaelWith protocols that support out-of-band data, the 18747208Scael.Dv SO_OOBINLINE 18847208Scaeloption 18928139Skarelsrequests that out-of-band data be placed in the normal data input queue 19028139Skarelsas received; it will then be accessible with 19147208Scael.Xr recv 19228139Skarelsor 19347208Scael.Xr read 19447208Scaelcalls without the 19547208Scael.Dv MSG_OOB 19647208Scaelflag. 19747988SkarelsSome protocols always behave as if this option is set. 19847208Scael.Dv SO_SNDBUF 19947208Scaeland 20047208Scael.Dv SO_RCVBUF 20147208Scaelare options to adjust the normal 20228139Skarelsbuffer sizes allocated for output and input buffers, respectively. 20328139SkarelsThe buffer size may be increased for high-volume connections, 20428139Skarelsor may be decreased to limit the possible backlog of incoming data. 20528139SkarelsThe system places an absolute limit on these values. 20647988Skarels.Pp 20747988Skarels.Dv SO_SNDLOWAT 20847988Skarelsis an option to set the minimum count for output operations. 20947988SkarelsMost output operations process all of the data supplied 21047988Skarelsby the call, delivering data to the protocol for transmission 21147988Skarelsand blocking as necessary for flow control. 21247988SkarelsNonblocking output operations will process as much data as permitted 21347988Skarelssubject to flow control without blocking, but will process no data 21447988Skarelsif flow control does not allow the smaller of the low water mark value 21547988Skarelsor the entire request to be processed. 21647988SkarelsA 21747988Skarels.Xr select 2 21847988Skarelsoperation testing the ability to write to a socket will return true 21947988Skarelsonly if the low water mark amount could be processed. 22047988SkarelsThe default value for 22147988Skarels.Dv SO_SNDLOWAT 22247988Skarelsis set to a convenient size for network efficiency, often 1024. 22347988Skarels.Dv SO_RCVLOWAT 22447988Skarelsis an option to set the minimum count for input operations. 22547988SkarelsIn general, receive calls will block until any (non-zero) amount of data 22665100Smckusickis received, then return with the smaller of the amount available or the amount 22747988Skarelsrequested. 22847988SkarelsThe default value for 22960159Smckusick.Dv SO_RCVLOWAT 23047988Skarelsis 1. 23147988SkarelsIf 23260159Smckusick.Dv SO_RCVLOWAT 23347988Skarelsis set to a larger value, blocking receive calls normally 23447988Skarelswait until they have received the smaller of the low water mark value 23547988Skarelsor the requested amount. 23647988SkarelsReceive calls may still return less than the low water mark if an error 23747988Skarelsoccurs, a signal is caught, or the type of data next in the receive queue 23847988Skarelsis different than that returned. 23947988Skarels.Pp 24047988Skarels.Dv SO_SNDTIMEO 24147988Skarelsis an option to set a timeout value for output operations. 24247988SkarelsIt accepts a 24347988Skarels.Fa struct timeval 24447988Skarelsparameter with the number of seconds and microseconds 24547988Skarelsused to limit waits for output operations to complete. 24647988SkarelsIf a send operation has blocked for this much time, 24747988Skarelsit returns with a partial count 24847988Skarelsor with the error 24947988Skarels.Er EWOULDBLOCK 25047988Skarelsif no data were sent. 25147988SkarelsIn the current implementation, this timer is restarted each time additional 25247988Skarelsdata are delivered to the protocol, 25347988Skarelsimplying that the limit applies to output portions ranging in size 25447988Skarelsfrom the low water mark to the high water mark for output. 25547988Skarels.Dv SO_RCVTIMEO 25647988Skarelsis an option to set a timeout value for input operations. 25747988SkarelsIt accepts a 25847988Skarels.Fa struct timeval 25947988Skarelsparameter with the number of seconds and microseconds 26047988Skarelsused to limit waits for input operations to complete. 26147988SkarelsIn the current implementation, this timer is restarted each time additional 26247988Skarelsdata are received by the protocol, 26347988Skarelsand thus the limit is in effect an inactivity timer. 26447988SkarelsIf a receive operation has been blocked for this much time without 26547988Skarelsreceiving additional data, it returns with a short count 26647988Skarelsor with the error 26747988Skarels.Er EWOULDBLOCK 26847988Skarelsif no data were received. 26947988Skarels.Pp 27047208ScaelFinally, 27147208Scael.Dv SO_TYPE 27247208Scaeland 27347208Scael.Dv SO_ERROR 27447208Scaelare options used only with 27553286Sbostic.Fn getsockopt . 27647208Scael.Dv SO_TYPE 27747208Scaelreturns the type of the socket, such as 27847208Scael.Dv SOCK_STREAM ; 27928139Skarelsit is useful for servers that inherit sockets on startup. 28047208Scael.Dv SO_ERROR 28147208Scaelreturns any pending error on the socket and clears 28228139Skarelsthe error status. 28328139SkarelsIt may be used to check for asynchronous errors on connected 28428139Skarelsdatagram sockets or for other asynchronous errors. 28547208Scael.Sh RETURN VALUES 28647208ScaelA 0 is returned if the call succeeds, -1 if it fails. 28747208Scael.Sh ERRORS 28820239SmckusickThe call succeeds unless: 28947208Scael.Bl -tag -width ENOPROTOOPTAA 29047208Scael.It Bq Er EBADF 29147208ScaelThe argument 29247208Scael.Fa s 29347208Scaelis not a valid descriptor. 29447208Scael.It Bq Er ENOTSOCK 29547208ScaelThe argument 29647208Scael.Fa s 29747208Scaelis a file, not a socket. 29847208Scael.It Bq Er ENOPROTOOPT 29928139SkarelsThe option is unknown at the level indicated. 30047208Scael.It Bq Er EFAULT 30124440SmckusickThe address pointed to by 30247208Scael.Fa optval 30324440Smckusickis not in a valid part of the process address space. 30424440SmckusickFor 30547208Scael.Fn getsockopt , 30624440Smckusickthis error may also be returned if 30747208Scael.Fa optlen 30824440Smckusickis not in a valid part of the process address space. 30947208Scael.El 31047208Scael.Sh SEE ALSO 31147208Scael.Xr ioctl 2 , 31247208Scael.Xr socket 2 , 31347208Scael.Xr getprotoent 3 31447208Scael.Xr protocols 5 31547208Scael.Sh BUGS 31628139SkarelsSeveral of the socket options should be handled at lower levels of the system. 31747208Scael.Sh HISTORY 31847208ScaelThe 31947208Scael.Nm 32047208Scaelsystem call appeared in 32147208Scael.Bx 4.2 . 322