1.1.24.15 setsockopt

C

int setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)

Description

Set options on sockets.

Parameters

ParametersDescription
fdSocket file descriptor.
levelOption level.
optnameOption name.
optvalOption value.
optlenLength of option value.

Returns

  • 0 - Success.

  • -1 - Error, errno set.

Remarks

errnoDescription
EBADFThe argument fd is not a valid file descriptor.
ENOPROTOOPTThe option is unknown at the level indicated.
EINVALoptlen invalid.
ENOMEMInsufficient memory is available.
EBADMSGBad message.

Socket options IP:

IP supports some protocol-specific socket options that can be set with setsockopt. The socket option level for IP is IPPROTO_IP. A boolean integer flag is zero when it is false, otherwise true. Unless otherwise noted, optval is a pointer to an int.

When an invalid socket option is specified, setsockopt fail with the error ENOPROTOOPT.

IP_TOS: Set or receive the Type-Of-Service (TOS) field that is sent with every IP packet originating from this socket. It is used to prioritize packets on the network. TOS is an int. There are some standard TOS flags defined: IPTOS_LOWDELAY to minimize delays for interactive traffic, IPTOS_THROUGHPUT to optimize throughput, IPTOS_RELIABILITY to optimize for reliability. At most one of these TOS values can be specified. Other bits are invalid and shall be cleared.

IP_ADD_MEMBERSHIP:

Join a multicast group. Argument is an ip_mreqn structure.

struct ip_mreqn {
    struct in_addr imr_multiaddr;
    struct in_addr imr_address;
    int            imr_ifindex;
};

imr_multiaddr contains the address of the multicast group the application wants to join or leave. It must be a valid multicast address (or setsockopt fails with the error EINVAL).

IPV6_ADD_MEMBERSHIP: Control membership in multicast groups. Argument is a pointer to a struct ipv6_mreq.

Socket options SOL_SOCKET:

The socket options listed below can be set by using setsockopt with the socket level set to SOL_SOCKET for all sockets.

SO_ASYNC_MODE: Set the asynchronous read mode. The argument is a integer. A value of 0 turns this mode off, 1 enables a simple protocol and 2 enables an acknowledged protocol.

SO_KEEPALIVE: Enable sending of keep-alive messages on connection-oriented sockets. Expects an integer boolean flag.

SO_LINGER: Sets the SO_LINGER option. The argument is a integer.

SO_RCVBUF: Sets the maximum socket receive buffer in bytes.

SO_SNDBUF: Sets the maximum socket send buffer in bytes.

Socket options IPPROTO_TCP:

To set a TCP socket option, call setsockopt to write the option with the option level argument set to IPPROTO_TCP.

TCP_NODELAY: If set, disable the Nagle algorithm. This means that segments are always sent as soon as possible, even if there is only a small amount of data. When not set, data is buffered until there is a sufficient amount to send out, thereby avoiding the frequent sending of small packets, which results in poor utilization of the network.

Socket options IPPROTO_TLS:

To set a TCP(TLS) socket option, call setsockopt to write the option with the option level argument set to IPPROTO_TLS.

TLS_CONF_IDX: Sets the TLS configuration index.