Sockets Programming Interfaces
3-18
op
Socket option to get
pbuf
Pointer to memory buffer
pbufsize
Pointer to size of memory buffer
Return Value
If it succeeds, the function returns 0. Otherwise, a value of –1 is returned and
the function fdError() can be called to determine the error:
EBADF
The file descriptor (socket) is invalid.
ENOTSOCK
The file descriptor does not reference a socket.
EINVAL
Buffer arguments are invalid.
Description
The getsockopt() function returns the options associated with a socket. Op-
tions may exist at multiple protocol levels; they are always present at the up-
permost socket level.
When manipulating socket options, the level at which the option resides and
the name of the option must be specified. To manipulate options at the socket
level, level is specified as SOL_SOCKET. To manipulate options at any other
level the protocol number of the appropriate protocol controlling the option is
supplied. In this implementation, only SOL_SOCKET, IPPROTO_IP, and
IPROTO_TCP are supported.
The parameters pbuf and pbufsize identify a buffer in which the value for the
requested option(s) are to be returned. pbufsize is a value-result parameter,
initially containing the size of the buffer pointed to by pbuf, and modified on re-
turn to indicate the actual size of the value returned.
Most socket-level options utilize an int parameter for pbuf. SO_LINGER uses
a struct linger parameter, defined in INC\SOCKET.H, which specifies the de-
sired state of the option and the linger interval (see below). SO_SNDTIMEO
and SO_RCVTIMEO use a struct timeval parameter.
The following options are recognized at the socket level:
SO_REUSEADDR
Specifies that the rules used in validating addresses
supplied in a bind call should allow reuse of local
addresses.
SO_REUSEPORT
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 mul-
ticast or broadcast datagrams destined for the
bound port.
SO_KEEPALIVE
Enables the periodic 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 when attempting to send data.