setsockopt

Name

setsockopt -- set socket options

Synopsis

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

int setsockopt(int socket, int level, int option_name, const void * option_value, socklen_t option_len);

Description

The setsockopt() function shall behave as specified in POSIX 1003.1-2001 (ISO/IEC 9945-2003), with the following extensions.

IP Protocol Level Options

If the level parameter is IPPROTO_IP, the following values shall be supported for option_name (see RFC 791:Internet Protocol for further details):

IP_OPTIONS 

Set the Internet Protocol options sent with every packet from this socket. The option_value shall point to a memory buffer containing the options and option_len shall contain the size in bytes of that buffer. For IPv4, the maximum length of options is 40 bytes.

IP_TOS 

Set the Type of Service flags to use when sending packets with this socket. The option_value shall point to a value containing the type of service value. The least significant two bits of the value shall contain the new Type of Service indicator. Use of other bits in the value is unspecified. The option_len parameter shall hold the size, in bytes, of the buffer referred to by option_value.

IP_TTL 

Set the current unicast Internet Protocol Time To Live value used when sending packets with this socket. The option_value shall point to a value containing the time to live value, which shall be between 1 and 255. The option_len parameter shall hold the size, in bytes, of the buffer referred to by option_value.

IP_MULTICAST_TTL 

Sets the Time To Live value of outgoing multicast packets for this socket. optval shall point to an integer which contains the new TTL value. If the new TTL value is -1, the implementation should use an unspecified default TTL value. If the new TTL value is out of the range of acceptable values (0-255), setsockopt() shall return -1 and set errno to indicate the error.

IP_MULTICAST_LOOP 

Sets a boolean flag indicating whether multicast packets originating locally should be looped back to the local sockets. optval shall point to an integer which contains the new flag value.

IP_ADD_MEMBERSHIP 

Join a multicast group. optval shall point to a ip_mreq structure. Before calling, the caller should fill in the imr_multiaddr field with the multicast group address and the imr_address field with the address of the local interface. If imr_address is set to INADDR_ANY, then an appropriate interface is chosen by the system.

IP_DROP_MEMBERSHIP 

Leave a multicast group. optval shall point to a ip_mreq structure containing the same values as were used with IP_ADD_MEMBERSHIP.

IP_MULTICAST_IF 

Set the local device for a multicast socket. optval shall point to either an ip_mreqn structure or an in_addr structure. If using the ip_mreqn structure, the imr_multiaddr field should be set to multicast group address, the imr_address field to the address of the local interface, and the imr_index field to the interface index. If using the in_addr structure, the address of the local interface shall be specified. If in_addr or imr_address is set to INADDR_ANY, then an appropriate interface is chosen by the system. If imr_index is zero, then an appropriate interface index is chosen by the implementation.

The ip_mreq structure contains two struct in_addr fields: imr_multiaddr and imr_address.

Return Value

On success, 0 is returned. On error, -1 is returned and the global variable errno is set appropriately.

Errors

As defined in POSIX 1003.1-2001 (ISO/IEC 9945-2003).