[Previous] [Contents] [Index] [Next]

getsockopt()

Get options associated with a socket

Synopsis:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>

int getsockopt( int s, 
                int level, 
                int optname,
                void * optval, 
                size_t * optlen );

Library:

socket3r.lib, socket3s.lib

Description:

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>.

Options recognized at socket level

The following options are recognized at the socket level. Except where noted, each may be examined with getsockopt() and set with setsockopt().

IP_HDRINCL

Include a custom IP header with the data. Indicates that a custom IP header is included with the data and may be used only with the SOCK_RAW type.

IP_TOS

Set the type-of-service field in the IP header for SOCK_STREAM and SOCK_DGRAM sockets.

SO_BROADCAST

Enable permission to transmit broadcast messages. Requests permission to send broadcast datagrams on the socket. "Broadcast" was a privileged operation in earlier versions of the system.

SO_DEBUG

Enable recording of debugging in the underlying protocol modules.

SO_DONTROUTE

Enable routing bypass for outgoing messages. 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

Get and clear error on the socket (get only). 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.

SO_KEEPALIVE

Enable 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.

SO_LINGER

Linger on close if data present. 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.

SO_OOBINLINE

Enable reception of out-of-band data in band. 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_RCVBUF and SO_SNDBUF

Set buffer size for input/output. 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.

SO_RCVLOWAT

Set 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.

SO_RCVTIMEO

Set 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.

SO_REUSEADDR

Enable duplicate address and port bindings to be reused. Indicates that the rules used in validating addresses supplied in a bind() call should allow local addresses to be reused.

SO_REUSEPORT

Enable duplicate address and port bindings. Allows completely duplicate bindings by multiple processes if they all set SO_REUSEPORT before binding the port. This option permits multiple instances of a program to each receive UDP/IP multicast or broadcast datagrams destined for the bound port.

SO_SNDLOWAT

Set 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.

SO_SNDTIMEO

Set 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.

SO_TIMESTAMP

Enable reception of a timestamp with datagrams. If the SO_TIMESTAMP option is enabled on a SOCK_DGRAM socket, the recvmsg() call returns a timestamp corresponding to when the datagram was received. The msg_control field in the msghdr structure points to a buffer that contains a cmsghdr structure followed by a struct timeval. The cmsghdr fields have the following values:

cmsg_len = sizeof(struct cmsghdr) + sizeof(struct timeval)
cmsg_level = SOL_SOCKET
cmsg_type = SCM_TIMESTAMP

TCP_KEEPALIVE

Adjust the amount of time in seconds between keepalive probes. The default value is 2 hours. It accepts a struct timeval parameter with the number of seconds to wait between the keepalive probes.

TCP_NODELAY

Don't delay send to coalesce packets. Under most circumstances, TCP sends data when it's presented. When outstanding data hasn't yet been acknowledged, TCP gathers small amounts of output to be sent in a single packet once an acknowledgment is received.

For a few clients (such as windowing systems that send a stream of mouse events that receive no replies), this packetization may cause significant delays. Therefore, TCP provides a boolean option, TCP_NODELAY, to defeat this algorithm.

Returns:

0
Success.
-1
An error occurred (errno is set).

Errors:

EBADF
Invalid descriptor s.
EDOM
Value was set out of range.
EFAULT
The address pointed to by optval isn't in a valid part of the process address space. For getsockopt(), this error may also be returned if optlen isn't in a valid part of the process address space.
EINVAL
The optval argument can't be NULL; optlen can't be 0.
ENOPROTOOPT
The option is unknown at the level indicated.

Classification:

Standard Unix, POSIX 1003.1g (draft)

Safety:
Interrupt handler No
Signal handler No
Thread Yes

See also:

ICMP, IP, TCP, and UDP protocols

getprotobyname(), ioctl(), setsockopt(), socket()

/etc/protocols in the TCP/IP User's Guide

close(), read(), select() in the C Library Reference


[Previous] [Contents] [Index] [Next]