get options associated with a socket
#include <sys/types.h> #include <sys/socket.h> int getsockopt( int s, int level, int optname, void *optval, int *optlen );
The getsockopt() function gets options associated with a socket. Options may exist at multiple protocol levels; they're always present at the uppermost ``socket'' level.
When manipulating a socket option, you must specify the option's name and the level at which the option resides:
The optval and optlen arguments specify buffer in which the value for the requested options are to be returned. The optlen argument is a value-result parameter; you should initialize it to indicate the size of the buffer pointed to by optval. On return, optlen indicates the actual size of the value. If no option value is to be returned, optval may be NULL.
The parameter optname and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. The include file <sys/socket.h> contains definitions for socket-level options, which are described below. Options at other protocol levels vary in format and name.
Most socket-level options use an int parameter for optval. SO_LINGER uses a struct linger parameter, defined in <sys/socket.h>, that specifies the desired state of the option and the linger interval (see below). SO_SNDTIMEO and SO_RCVTIMEO use a struct timeval parameter, defined in <sys/time.h>.
The following options are recognized at the socket level. Except where noted, each may be examined with getsockopt() and set with setsockopt().
Option: | Description: |
---|---|
SO_BROADCAST | Enable permission to transmit broadcast messages. |
SO_DEBUG | Enable recording of debugging information. |
SO_DONTROUTE | Enable routing bypass for outgoing messages. |
SO_ERROR | Get and clear error on the socket (get only). |
SO_KEEPALIVE | Enable keep connections alive. |
SO_LINGER | Linger on close if data present. |
SO_OOBINLINE | Enable reception of out-of-band data in band. |
SO_RCVBUF | Set buffer size for input. |
SO_RCVLOWAT | Set minimum count for input. |
SO_RCVTIMEO | Set timeout value for input. |
SO_REUSEADDR | Enable local address to be reused. |
SO_SNDBUF | Set buffer size for output. |
SO_SNDLOWAT | Set minimum count for output. |
SO_SNDTIMEO | Set timeout value for output. |
SO_TYPE | Get the type of the socket (get only). |
Requests permission to send broadcast datagrams on the socket. ``Broadcast'' was a privileged operation in earlier versions of the system.
Enables debugging in the underlying protocol modules.
Indicates that outgoing messages should bypass the standard routing facilities. The messages are directed to the appropriate network interface according to the network portion of the destination address.
SO_ERROR and SO_TYPE are used only with getsockopt() and setsockopt(). SO_TYPE, which returns the type of the socket (e.g. SOCK_STREAM), is useful for servers that inherit sockets on startup. SO_ERROR, which returns any pending error on the socket, clears the error status. You can use it to check for asynchronous errors on connected datagram sockets or for other asynchronous errors.
Enables the periodic (at least every 2 hours) transmission of messages on a connected socket. Should the connected party fail to respond to these messages, the connection is considered broken and processes using the socket are notified via a SIGPIPE signal when they attempt to send data.
Controls the action taken when unsent messages are queued on socket and a close() is performed. If the socket promises reliable delivery of data and SO_LINGER is set, the system will block the process on the close() attempt until it's able to transmit the data or until it decides it can't deliver the information (a timeout period, termed the linger interval, is specified in the setsockopt() call when SO_LINGER is requested).
If SO_LINGER is disabled and a close() is issued, the system will process the close() in a way that lets the process continue as quickly as possible.
With protocols that support out-of-band data, the SO_OOBINLINE option requests that out-of-band data be placed in the normal data input queue as received; the data will then be accessible with recv() or read() calls without the MSG_OOB flag. Some protocols always behave as if this option is set.
SO_SNDBUF and SO_RCVBUF adjust the normal buffer sizes allocated for output and input buffers, respectively. You can increase the buffer size for high-volume connections, or decrease it to limit the possible backlog of incoming data. The system places an absolute limit on these values and defaults them to at least 8k.
The minimum count for input operations. In general, receive calls will block until any (nonzero) amount of data is received, then return with the amount available or the amount requested, whichever is smaller.
The default value for SO_RCVLOWAT is 1. If SO_RCVLOWAT is set to a larger value, blocking receive calls normally wait until they've received the low-water mark value or the requested amount, whichever is smaller. Receive calls may still return less than the low-water mark if an error occurs, if a signal is caught, or if the type of data next in the receive queue differs from that returned.
A timeout value for input operations. It accepts a struct timeval parameter with the number of seconds and microseconds used to limit waits for input operations to complete.
In the current implementation, this timer is restarted each time additional data is received by the protocol, so the limit is in effect an inactivity timer. If a receive operation has been blocked for this much time without receiving additional data, it returns with a short count or, if no data was received, with the error EWOULDBLOCK.
Indicates that the rules used in validating addresses supplied in a bind() call should allow local addresses to be reused.
The minimum count for output operations. Most output operations process all of the data supplied by the call, delivering data to the protocol for transmission and blocking as necessary for flow control. Nonblocking output operations will process as much data as permitted (subject to flow control without blocking), but will process no data if flow control doesn't allow the smaller of the low-water mark value or the entire request to be processed.
A select() operation testing the ability to write to a socket will return true only if the low-water mark amount could be processed. The default value for SO_SNDLOWAT is set to 2048 bytes.
A timeout value for output operations. It accepts a struct timeval parameter with the number of seconds and microseconds used to limit waits for output operations to complete. If a send operation has blocked for this much time, it returns with a partial count or with the error EWOULDBLOCK if data weren't sent.
This timer is restarted each time additional data is delivered to the protocol, implying that the limit applies to output portions ranging in size from the low-water mark to the high-water mark for output. Timeouts are restricted to 32 seconds or under.
If an error occurred, errno could contain one of the following:
POSIX 1003.1g (draft)
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | No |
Thread | Yes |
ICMP, IP, TCP, and UDP protocols, getprotobyname(), ioctl(), setsockopt(), socket()
/etc/protocols in the TCP/IP User's Guide