#include <sys/types.h> #include <sys/socket.h> int setsockopt(int socket_descriptor, int level, int option_name, char *option_value, int option_length)
#define _XOPEN_SOURCE 520 #include <sys/socket.h> int setsockopt(int socket_descriptor, int level, int option_name, const void *option_value, socklen_t option_length)
The setsockopt() function is used to set socket options.
There are two versions of the API, as shown above. The base i5/OS API uses BSD 4.3 structures and syntax. The other uses syntax and structures compatible with the UNIX 98 programming interface specifications. You can select the UNIX 98 compatible interface with the _XOPEN_SOURCE macro.
IPPROTO_IP | Request applies to IP protocol layer. |
IPPROTO_TCP | Request applies to TCP protocol layer. |
SOL_SOCKET | Request applies to socket layer. |
IPPROTO_IPV6 | Request applies to IPv6 protocol layer. |
IPPROTO_ICMPV6 | Request applies to ICMPv6 protocol layer. |
Note: Options directed to a specific protocol level are only supported by that protocol. An option that is directed to the SOL_SOCKET level always completes successfully. This provides compatibility with Berkeley Software Distributions implementations that also shield the application from protocols that do not support an option.
Socket Options That Apply to the IP Layer (IPPROTO_IP)
Option | Description |
---|---|
IP_OPTIONS | Set IP header options. This is only supported for sockets with an address family of AF_INET. |
IP_TOS | Set Type Of Service (TOS) and Precedence in the IP header. This option is only supported for sockets with an address family of AF_INET. |
IP_TTL | Set Time To Live (TTL) in the IP header. This option is only supported for sockets with an address family AF_INET. |
IP_MULTICAST_IF | Set interface over which outgoing multicast datagrams should be sent. An option_value parameter of type in_addr is used to specify the local IP address that is associated with the interface over which outgoing multicast datagrams should be sent. An address of INADDR_ANY removes the previous selection. This option is only supported for sockets with an address family of AF_INET and type of SOCK_DGRAM or SOCK_RAW. |
IP_MULTICAST_TTL | Set Time To Live (TTL) in the IP header for outgoing multicast datagrams. An option_value parameter of type char is used to set this value between 0 and 255. This option is only supported for sockets with an address family of AF_INET and type of SOCK_DGRAM or SOCK_RAW. |
IP_MULTICAST_LOOP | Specify that a copy of an outgoing multicast datagram should be delivered to the sending host as long as it is a member of the multicast group. If this option is not set, a copy of the datagram will not be delivered to the sending host. An option_value parameter of type char is used to control loopback being on or off. This option is only supported for sockets with an address family of AF_INET and type of SOCK_DGRAM or SOCK_RAW. |
IP_ADD_MEMBERSHIP | Joins a multicast group as specified in the option_value parameter of type struct ip_mreq. A maximum of IP_MAX_MEMBERSHIPS groups may be joined per socket. This option is only supported for sockets with an address family of AF_INET and type of SOCK_DGRAM or SOCK_RAW. |
IP_DROP_MEMBERSHIP | Leaves a multicast group as specified in the option_value parameter of type struct ip_mreq. This option is only supported for sockets with an address family of AF_INET and type of SOCK_DGRAM or SOCK_RAW. |
IP_RECVLCLIFADDR | Indicates if the local interface that a datagram to be received should be returned. A value of 1 indicates the first 4 bytes of the reserved field of the sockaddr structure will contain the local interface. This option is only supported for sockets with an address family of AF_INET and type of SOCK_DGRAM. |
IP_DONTFRAG | Set or reset the don't fragment flag in the IP header. This option is supported for sockets with an address family of AF_INET and type of SOCK_DGRAM or SOCK_RAW only. |
Socket Options That Apply to the TCP Layer (IPPROTO_TCP)
Option | Description |
---|---|
TCP_NODELAY | Specifies whether TCP should follow the Nagle algorithm for deciding when to send data. By default, TCP will follow the Nagle algorithm. To disable this behavior, applications can enable TCP_NODELAY to force TCP to always send data immediately. For example, TCP_NODELAY should be used when there is an application using TCP for a request/response. This option is only supported for sockets with an address family of AF_INET or AF_INET6 and type of SOCK_STREAM. |
Socket Options That Apply to the Socket Layer (SOL_SOCKET )
Option | Description |
---|---|
SO_ACCEPTECONNABORTED |
Enable the listening socket such that a blocking accept() will return
ECONNABORTED when a connection
on the listening backlog is reset prior to the accept().
A backlog entry is created when a new connection is established to the listener socket.
The accept() call will consume all leading invalid entries in the backlog,
until a valid entry is located or the backlog is exhausted. If the backlog
contains no entries or a valid entry is located, the option has no effect. If
the backlog contains at least one invalid entry and there are no valid entries,
the accept() will return -1 with errno set to ECONNABORTED.
The option is only valid on a socket that has successfully issued
the listen() call. The option has no effect on non-blocking sockets.
This option is only used by sockets with an address family of AF_INET
or AF_INET6.
|
SO_BROADCAST | Enable the socket for issuing messages to a
broadcast address. This option is only supported for sockets with an address
family of AF_INET and type SOCK_DGRAM or
SOCK_RAW. The broadcast address to be used may be determined by
issuing an ioctl() with the SIOCGIFBRDADDR request. |
SO_DEBUG | Indicates if low level-debugging is active. |
SO_DONTROUTE | Bypass normal routing mechanisms. This option is only supported by sockets with an address family of AF_INET or AF_INET6. |
SO_KEEPALIVE | Keep the connection up by sending periodic transmissions. This option is only supported for sockets of an address family of AF_INET or AF_INET6 and type SOCK_STREAM. |
SO_LINGER | Indicates if the system attempts delivery of any
buffered data or if the system discards it when a close() is issued.
For sockets that are using a connection-oriented transport service with an address family of AF_INET or AF_INET6, the default is off (which means that the system attempts to send any queued data, with an infinite wait-time). |
SO_OOBINLINE | Indicates whether out-of-band data is received inline with normal data. This option is only supported for sockets with an address family of AF_INET or AF_INET6. |
SO_RCVBUF | Set the size of the receive buffer. |
SO_RCVLOWAT | Set the size of the receive low-water mark. The
default size is 1. This option is only supported for sockets with a type of
SOCK_STREAM. |
SO_RCVTIMEO | Set the receive timeout value. This option is not supported unless _XOPEN_SOURCE is defined to be 520 or greater. |
SO_REUSEADDR | Indicates if the local socket address can be reused. This option is supported by sockets with an address family of AF_INET or AF_INET6 and a type of SOCK_STREAM or SOCK_DGRAM. |
SO_SNDBUF | Set the size of the send buffer. |
SO_SNDLOWAT | Set the size of the send low-water mark. This option is not supported. |
SO_SNDTIMEO | Set the send timeout value. This option is not supported unless _XOPEN_SOURCE is defined to be 520 or greater. |
SO_USELOOPBACK | Indicates if the loopback feature is being used. This option is not supported. |
Socket Options That Apply to the IPv6 Layer (IPPROTO_IPV6)
Option | Description |
---|---|
IPV6_UNICAST_HOPS | Set the hop limit value that will be used for subsequent unicast packets sent by this socket. An option_value parameter of type int is used to set this value between 0 and 255. This option is supported for sockets with an address family of AF_INET6 only. |
IPV6_MULTICAST_IF | Set the interface over which outgoing multicast datagrams will be sent. An option_value parameter of type unsigned int is used to set the interface index that is associated with the interface over which outgoing multicast datagrams will be sent. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW. |
IPV6_MULTICAST_HOPS | Set the hop limit value that will be used for subsequent multicast packets sent by this socket. An option_value parameter of type int is used to set this value between 0 and 255. If IPV6_MULTICAST_HOPS is not set, the default is 1. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW. |
IPV6_MULTICAST_LOOP | Set the multicast looping mode. A value of 1 (default), indicates that multicast datagrams sent by this system should also be delivered to this system as long as it is a member of the multicast group. If this option is 0, a copy of the datagram will not be delivered to the sending host. An option_value parameter of type unsigned int is used to set this value. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW. |
IPV6_JOIN_GROUP | Joins a multicast group as specified in the option_value parameter of type struct ipv6_mreq. A maximum of IP_MAX_MEMBERSHIPS groups may be joined per socket. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW. |
IPV6_LEAVE_GROUP | Leaves a multicast group as specified in the option_value parameter of type struct ipv6_mreq. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW. |
IPV6_V6ONLY | Set the AF_INET6 communication restrictions. A non-zero value indicates that this AF_INET6 socket is restricted to IPv6 communications only. This option stores an int value. This is a boolean option. By default this option is turned off. This option is supported for sockets with an address family of AF_INET6 only. |
IPV6_CHECKSUM | Set if the kernel will calculate and insert a checksum for output and verify the received checksum on input, discarding the packet if the checksum is in error for this socket. An option_value parameter of type int is used to set this value. If this option is -1 (the default), this socket option is disabled. A value of 0 or greater specifies an integer offset into the user data of where the checksum is located. This must be an even integer value. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_RAW with a protocol other than IPPROTO_ICMPV6. The checksum is automatically computed for protocol IPPROTO_ICMPV6. |
IPV6_DONTFRAG | Set if the kernel will not implement the automatic insertion of a fragment header in the packet if the packet is too big for the path MTU. By default this socket option is disabled. Setting the value to 0 also disables the option. If this option is set to a non-zero value the kernel will discard the packet instead of inserting the fragment header. This option is supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW only. |
Socket Options That Apply to the ICMPv6 Layer (IPPROTO_ICMPV6)
Option | Description |
---|---|
ICMP6_FILTER | Set the ICMPv6 Type Filtering. An option_value parameter of type struct icmp6_filter, defined in <netinet/icmp6.h> is used to set this value. The following macros, defined in <netinet/icmp6.h> can be used to update the type filtering structure to specify whether or not specific ICMPv6 message types will be passed to the application or be blocked: ICMP6_FILTER_SETPASS, ICMP6_FILTER_SETBLOCK, ICMP6_FILTER_SETPASSALL, and ICMP6_FILTER_SETBLOCKALL. The default is to pass all ICMPv6 message types to the application. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_RAW with a protocol of IPPROTO_ICMPV6. |
Note: For the IP_TOS and IP_TTL options, only the rightmost octet (least significant octet) of the integer value is used.
The following options can be set by specifying a nonzero value for the option_value parameter:
For the SO_LINGER option, option_value is a pointer to the structure struct linger, defined in <sys/socket.h>.
struct linger [ int l_onoff; int l_linger; ];
The l_onoff field determines if the linger option is set. A nonzero value indicates the linger option is set and is using the l_linger value. A zero value indicates that the option is not set. The l_linger field is the time to wait before any buffered data to be sent is discarded. The following occur on a close():
Note: An application must implement an application level confirmation. Guaranteed receipt of data by the partner program is required. Setting SO_LINGER does not guarantee delivery.
For the SO_RCVTIME and SO_SNDTIME options, option_value is a pointer to where the structure timeval is stored. The structure timeval is defined in <sys/time.h>.
struct timeval { long tv_sec; long tv_usec; };
For the IP_OPTIONS option, option_value is a pointer to a character string representing the IP options as specified in RFC 791. The character string varies depending on which options are selected. Each option is made up of a single byte representing the option code, and may be followed by a length field (1 byte) and data for the option. The IP options that can be set are:
For the IP_MULTICAST_IF option, option_value is a pointer to the structure in_addr, defined in <netinet/in.h> as:
struct in_addr [ u_long s_addr; /* IP address */ ];
The s_addr field specifies the local IP address that is associated with the interface over which outgoing multicast datagrams should be sent.
For the IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP options, option_value is a pointer to the structure ip_mreq, defined in <netinet/in.h> as:
struct ip_mreq [ struct in_addr imr_multiaddr; /* IP multicast address of group */ struct in_addr imr_interface; /* local IP address of interface */ ];
The imr_multiaddr field is used to specify the multicast group to join or leave. The imr_interface field is used to specify the local IP address that is associated with the interface to which this request applies. If INADDR_ANY is specified for the local interface, the default multicast interface will be selected.
For the IPV6_JOIN_GROUP and IPV6_LEAVE_GROUP options, option_value is a pointer to the structure ipv6_mreq, defined in <netinet/in.h> as:
struct ipv6_mreq [ struct in6_addr ipv6mr_multiaddr; /* IPv6 multicast address */ unsigned int ipv6mr_interface; /* interface index */ ];
The ipv6mr_multiaddr field is used to specify the multicast group to join or leave. The ipv6mr_interface field is used to specify the interface to which this request applies. If 0 is specified for the interface, the system will choose the local interface.
Note: Reception of IP multicast datagrams may require configuration changes to the line description to enable the adapter to receive packets with a multicast destination address. On Ethernet, for example, the Ethernet group address that is associated with the IP group address must be specified by the GRPADR parameter on the line description. To determine the Ethernet group address for a particular IP group address, the low-order 23 bits of the IP address are placed into the low-order 23 bits of the Ethernet group address 01.00.5E.xx.xx.xx.
Notes:
setsockopt() returns an integer. Possible values are:
When setsockopt() fails, errno can be set to one of the following:
Address already in use. This error code indicates that the socket_descriptor parameter specified for the IP_ADD_MEMBERSHIP operation is already a member of the specified multicast group.
Address not available. For the IP_ADD_MEMBERSHIP or IP_DROP_MEMBERSHIP operations, this error code indicates that an incorrect address was specified for either the imr_multiaddr or imr_interface parameter value.
Descriptor not valid.
Connection ended abnormally.
This error code indicates that the transport provider ended the connection abnormally because of one of the following:
Bad address.
The system detected an address which was not valid while attempting to access the option_value parameter.
Parameter not valid.
This error code indicates one of the following:
Input/output error.
There is not enough buffer space for the requested operation.
The protocol does not support the specified option.
This error code indicates one of the following:
Requested operation requires a connection.
This error code is only returned if the level parameter specifies a level other than SOL_SOCKET and the socket_descriptor parameter points to a socket that is using a connection-oriented transport service that has had its connection broken.
The specified descriptor does not reference a socket.
Operation not permitted.
The executing user profile must have *IOSYSCFG special authority to set options when the level parameter specifies IPPROTO_IP and the option_value parameter is IP_OPTIONS.
The operation would have exceeded the maximum number of references allowed for this socket.
The protocol required to support the specified address family is not available at this time.
Unknown system state.
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
CPFA081 E | Unable to set return value or error code. |
Notes:
Top | UNIX-Type APIs | APIs by category |