Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

getsockopt(), setsockopt() - get and set socket options

&pagelevel(4)&pagelevel

#include <sys/socket.h> 
#include <netinet/in.h>

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

Description

The getsockopt() and setsockopt() functions read or modify options which are defined for a socket. Options can exist on various protocol levels but they always exist on the highest protocol level.

The name optname of the option and the protocol level level on which the protocol is interpreted must always be specified for accessing a socket option. The user must specify SOL_SOCKET for level to access options on the socket level.

With the setsockopt() function, the user can modify option values via the optval and optlen parameters.

With the getsockopt() function, optval and optlen are the address and lenght of a buffer in which the value of the desired option is to be returned. When the getsockopt() function returns, *optlen contains the actual size of the returned data. *optval contains the value 0 if the option has no value that can be returned.

optname and all specified options are passed without conversion to the relevant protocol module for interpretation. The <sys/socket.h> header file contains definitions for the socket level options.

Options of the protocol level SOL_SOCKET

SO_KEEPALIVE

Indicates whether connections are to be kept open or not. SO_KEEPALIVE causes regular transfer of control messages over a connected socket.

Data type of the option value: int

SO_LINGER

Indicates whether socket closing after a close() call is delayed if data is still pending to be transferred on the socket.

If SO_LINGER is on (structure element l_onoff is not 0), then the structure element l_linger specifies the timeout interval.

Data type of the option value: struct linger

SO_BROADCAST

Indicates whether broadcast messages may be transferred or not.

Since broadcast messages may always be sent in BS2000, this option has no functional significance. However, it must be noted that the reception of broadcast messages can be disallowed (see the BCAM BCOPTION command in the manual "BCAM").

Data type of the option value: int

SO_REUSEADDR

Indicates if the rules for the validity check on the addresses specified for bind() should permit the reuse of local addresses, provided this is supported by the protocol.

Data type of the option value: int

SO_TYPE

Gets the socket type.

SO_TYPE is only allowed with getsockopt(). SO_TYPE returns the type of the socket, i.e. either SOCK_STREAM or SOCK_DGRAM.

Data type of the option value: int

SO_ACCEPTCONN

Queries whether the socket is ready to receive connection requests.

SO_ACCEPTCONN is only allowed with getsockopt().

Data type of the option value: int

SO_SNDBUF

Queries the size of the socket's send buffer.

SO_SNDBUF is only allowed with getsockopt().

Data type of the option value: int

SO_RCVBUF

Queries the size of the socket's receive buffer.

SO_RCVBUF is only allowed with getsockopt().

Data type of the option value: int

The following options are supported for diagnostic purposes and are primarily intended for internal use:

SO_ERROR

Queries the error code (errno) displayed after calling a socket function.

SO_ERROR is only allowed with getsockopt().

Data type of the option value: int

SO_BS2ERROR

Queries the return code after calling the BCAM interface $ICO2000. This is returned in the bcam_rc structure element. In addition, the error code, analogous to the SO_ERROR option, is returned in the bcam_errno structure element.

SO_BS2ERROR is only allowed with getsockopt().

Data type of the option value: struct so_bs2error

Options of the protocol level IPPROTO_TCP

TCP_NODELAY

If not zero, this option disables the delayed sending of data packets by means of the "Nagle algorithm".

Data type of the option value: int

TCP_NODELAY_ACK

If not zero, this option disables the delayed sending of acknowledgements.

Data type of the option value: int

TCP_KEEPALIVE

If not zero, this option enables sending of keep-alive messages on connection-oriented sockets.

Data type of the option value: int

TCP_KEEPIDLE

The time (in seconds) the connection mus have been idle before the transport system starts sending keep-alive messages. Applies only if the socket option SO_KEEPALIVE is active.

Data type of the option value: int

TCP_KEEPINTVL

The time (in seconds) between keep-alive messages. Applies only if the socket option SO_KEEPALIVE is active.

Data type of the option value: int

TCP_KEEPCNT

The maximum number of keep-alive messages the transport system should send before dropping the connection. Applies only if the socket option SO_KEEPALIVE is active.

Data type of the option value: int

Options of the protocol level IPPROTO_IP

IP_MULTICAST_IF

Indicates the local IPv4 address for sending multicast messages.

Data type of the option value: int

IP_MULTICAST_TTL

Indicates the time-to-live (hop limit) value of outgoing multicast messages.

Data type of the option value: int

IP_MULTICAST_LOOP

Indicates whether sent multicast packets should also be sent to local (loopback) sockets.

Data type of the option value: int

IP_ADD_MEMBERSHIP

Activates the delivery of messages to this socket which are sent to the selected IPv4 multicast group. Specifies the local interface address (IPv4 address or INADDR_ANY, not INADDR_LOOPBACK).

IP_ADD_MEMBERSHIP is only allowed with setsockopt().

Data type of the option value: struct ip_mreq

IP_DROP_MEMBERSHIP

Deactivates the delivery of messages to this socket which are sent to the selected IPv4 multicast group. Specifies the local interface address (IPv4 address or INADDR_ANY, not INADDR_LOOPBACK).

IP_DROP_MEMBERSHIP is only allowed with setsockopt().

Data type of the option value: struct ip_mreq

Options of the protocol level IPPROTO_IPV6

IPV6_UNICAST_HOPS

Indicates the unicast hop limit for the socket.

Data type of the option value: int

IPV6_MULTICAST_IF

Indicates the interface number for sending multicast messages.

Data type of the option value: int

IPV6_MULTICAST_LOOP

Indicates whether sent multicast packets should also be sent to local (loopback) sockets.

Data type of the option value: int

IPV6_MULTICAST_HOPS

Indicates the multicast hop limit for the socket.

Data type of the option value: int

IPV6_ADD_MEMBERSHIP

Activates the delivery of messages to this socket which are sent to the selected IPv6 multicast group. Specifies the local interface (interface index).

IPV6_ADD_MEMBERSHIP is only allowed with setsockopt().

Data type of the option value: struct ipv6_mreq

IPV6_DROP_MEMBERSHIP

Deactivates the delivery of messages to this socket which are sent to the selected IPv6 multicast group. Specifies the local interface (interface index).

IPV6_DROP_MEMBERSHIP is only allowed with setsockopt().

Data type of the option value: struct ipv6_mreq

Return value

0:

If successful.

-1:

If errors occur. errno is set to indicate the error.

Errors

EBADF

The s parameter is not a valid descriptor.

EFAULT

optval and optlen do not reference a valid address range.

EINVAL

One of the parameters level , optval or optlen has an illegal value.

ENOPROTOOPT

The option is not known to the specified level.

ENOTSOCK

Descriptor s does not reference a socket.

EOPNOTSUPP

The option is not supported.

See also

socket(), getprotoent()