Sets socket options.
Standard C Library (libc.a)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/atmsock.h> /*Needed for SOCK_CONN_DGRAM socket type
only*/
int setsockopt
(Socket, Level, OptionName, OptionValue, OptionLength)
int Socket, Level, OptionName;
const void * OptionValue;
socklen_t OptionLength;
The setsockopt subroutine sets options associated with a socket. Options can exist at multiple protocol levels. The options are always present at the uppermost socket level.
The setsockopt subroutine provides an application program with the means to control a socket communication. An application program can use the setsockopt subroutine to enable debugging at the protocol level, allocate buffer space, control time outs, or permit socket data broadcasts. The /usr/include/sys/socket.h file defines all the options available to the setsockopt subroutine.
When setting socket options, specify the protocol level at which the option resides and the name of the option.
Use the parameters OptionValue and OptionLength to access option values for the setsockopt subroutine. These parameters identify a buffer in which the value for the requested option or options is returned.
All applications containing the setsockopt subroutine must be compiled with the _BSD macro set to a specific value. Acceptable values are 43 and 44. In addition, all socket applications must include the BSD libbsd.a library.
Item | Description |
---|---|
Socket | Specifies the unique socket name. |
Level | Specifies the protocol level at which the option resides.
To set options at:
|
OptionName | Specifies the option to set. The OptionName parameter
and any specified options are passed uninterpreted to the appropriate
protocol module for interpretation. The sys/socket.h file
defines the socket protocol level options. The netinet/tcp.h file
defines the TCP protocol level options. The socket level options can
be enabled or disabled; they operate in a toggle fashion. The following list defines socket protocol level options found in the sys/socket.h file:
|
OptionName |
|
OptionName | The following list defines TCP protocol level options found in the netinet/tcp.h file:
|
OptionName |
Beginning at AIX 4.3.2, TCP protocol level socket options are inherited from listening sockets to new sockets. Prior to 4.3.2, only the TCP_RFC1323 option was inherited. The following list defines ATM protocol level options found in the sys/atmsock.h file:
|
OptionName |
The following list defines IPPROTO_TCP protocol level options found in the netinet/sctp.h file:
|
OptionValue | The OptionValue parameter takes an Int parameter.
To enable a Boolean option, set the OptionValue parameter
to a nonzero value. To disable an option, set the OptionValue parameter
to 0. The following options enable and disable in the same manner:
|
OptionLength | The OptionLength parameter contains the size of the buffer pointed to by the OptionValue parameter. |
Options at other protocol levels vary in format and name.
Item | Description |
---|---|
IP_DONTFRAG | Sets DF bit from now on for every packet in the IP header. Beginning with AIX 5.3, to detect decreases in Path MTU, UDP applications will always need to set this option. |
IP_FINDPMTU | Sets enable/disable PMTU discovery for this path. Protocol level path MTU discovery should be enabled for the discovery to happen. |
IP_PMTUAGE | Sets the age of PMTU. Specifies the frequency of PMT reductions discovery for the session. Setting it to 0 (zero) implies infinite age and PMTU reduction discovery will not be attempted. This will replace the previously set PMTU age. The new PMTU age will become effective after the currently set timer expires. Beginning with AIX 5.3, this option is unused because UDP applications will always need to set the IP_DONTFRAG socket option to detect decreases in PMTU immediately. |
IP_TTL | Sets the time-to-live field in the IP header for every packet. However, for raw sockets, the default MAXTTL value will be used while sending the messages irrespective of the value set using the setsockopt subroutine. |
IP_HDRINCL | This option allows users to build their own IP header. It indicates that the complete IP header is included with the data and can be used only for raw sockets. |
IP_ADD_MEMBERSHIP | Joins a multicast group as specified in the OptionValue parameter of the ip_mreq structure type. |
IP_DROP_MEMBERSHIP | Leaves a multicast group as specified in the OptionValue parameter of the ip_mreq structure type. |
IP_MULTICAST_IF | Permits sending of multicast messages on an interface as specified in the OptionValue parameter of the ip_addr structure type. An address of INADDR_ANY (0x000000000) removes the previous selection of an interface in the multicast options. If no interface is specified, the interface leading to the default route is used. |
IP_MULTICAST_LOOP | Sets multicast loopback, determining whether or not transmitted messages are delivered to the sending host. An OptionValue parameter of the char type controls the loopback to be on or off. |
IP_MULTICAST_TTL | Sets the time-to-live (TTL) for multicast packets. An OptionValue parameter of the char type sets the value of TTL ranging from 0 through 255. |
IP_BLOCK_SOURCE | Blocks data from a given source to a given group. |
IP_UNBLOCK_SOURCE | Unblocks a blocked source (to undo the IP_BLOCK_SOURCE operation). |
IP_ADD_SOURCE_MEMBERSHIP | Joins a source-specific multicast group. If the host is a member of the group, accept data from the source; otherwise, join the group and accept data from the given source. |
IP_DROP_SOURCE_MEMBERSHIP | Leaves a source-specific multicast group. Drops the source from the given multicast group list. To drop all sources of a given group, use the IP_DROP_MEMBERSHIP socket option. |
Item | Description | Value | |
---|---|---|---|
IPPROTO_IPV6 | Restricts AF_INET6 sockets to IPv6 communications only. |
|
|
Allows the user to set the outgoing hop limit for unicast IPv6 packets. |
|
||
Allows the user to set the outgoing hop limit for multicast IPv6 packets. |
|
||
Allows the user to specify the interface being used for outgoing multicast packets. If specified as 0, the system selects the outgoing interface. |
|
||
If a multicast datagram is sent to a group that the sending host belongs to, a copy of the datagram is looped back by the IP layer for local delivery (if the option is set to 1). If the option is set to 0, a copy is not looped back. |
|
||
Joins a multicast group on a specified local interface. If the interface index is specified as 0, the kernel chooses the local interface. |
|
||
Leaves a multicast group on a specified interface. |
|
||
Specifies that the kernel computes checksums over the data and the pseudo-IPv6 header for a raw socket. The kernel will compute the checksums for outgoing packets as well as verify checksums for incoming packets on that socket. Incoming packets with incorrect checksums will be discarded. This option is disabled by default. |
|
||
Causes the destination IPv6 address and arriving interface index of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets. |
|
||
Causes the hop limit of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets. |
|
||
Causes the traffic class of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets. |
|
||
Causes the routing header (if any) of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets. |
|
||
Causes the hop-by-hop options header (if any) of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets. |
|
||
Causes the destination options header (if any) of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets. |
|
||
Sets the source IPv6 address and outgoing interface index for all IPv6 packets being sent on this socket. This option can be cleared by doing a regular setsockopt with ipi6_addr being in6addr_any and ipi6_ifindex being 0. |
|
||
Sets the next hop for outgoing IPv6 datagrams on this socket. This option can be cleared by doing a regular setsockopt with a 0 length. Note that a memory pointer must still be supplied for the option value in this case. |
|
||
Sets the traffic class for outgoing IPv6 datagrams on this socket. To clear this option, the application can specify -1 as the value. |
|
||
Sets the routing header to be used for outgoing IPv6 datagrams on this socket. This option can be cleared by doing a regular setsockopt with a 0 length. Note that a memory pointer must still be supplied for the option value in this case. |
|
||
Sets the hop-by-hop options header to be used for outgoing IPv6 datagrams on this socket. This option can be cleared by doing a regular setsockopt with a 0 length. Note that a memory pointer must still be supplied for the option value in this case. |
|
||
Sets the destination options header to be used for outgoing IPv6 datagrams on this socket. This header will follow a routing header (if present) and will also be used when there is no routing header specified. This option can be cleared by doing a regular setsockopt with a 0 length. Note that a memory pointer must still be supplied for the option value in this case. |
|
||
Sets the destination options header to be used for outgoing IPv6 datagrams on this socket. This header will precede a routing header (if present). If no routing header is specified, this option will be silently ignored. This option can be cleared by doing a regular setsockopt with a 0 length. Note that a memory pointer must still be supplied for the option value in this case. |
|
||
Sets this option to control IPv6 path MTU discovery. |
|
||
Setting this option prevents fragmentation of outgoing IPv6 packets on this socket. If a packet is being sent that is larger than the outgoing interface MTU, the packet will be discarded. |
|
||
Enables the receipt of IPV6_PATHMTU ancillary data items by setting this option. |
|
||
Sets the address selection preferences for this socket. |
|
||
Joins the multicast group as specified in the OptionValue parameter of the group_req structure. If the specified interface index is 0, the kernel chooses the default interface. |
|
||
Leaves the multicast group as specified in the OptionValue parameter of the group_req structure. |
|
||
Blocks data from the specified source to the specified multicast group. |
|
||
Unblocks data from the specified source to the specified multicast group. The option is used to undo the MCAST_BLOCK_SOURCE operation. |
|
||
Joins a source-specific multicast group. If the host is already a member of the group, accept data from the specified source; otherwise, join the group and accept data from the specified source. |
|
||
Leaves a source-specific multicast group. Leaves the specified source from the specified multicast group. To leave all sources of the multicast group, use the IPV6_LEAVE_GROUP or MCAST_LEAVE_GROUP socket option. |
|
Item | Description | Value |
---|---|---|
IPPROTO_ICMPV6 | Allows the user to filter ICMPV6 messages by the ICMPV6 type field. In order to clear an existing filter, issue a setsockopt call with zero length. |
|
tcp.h:#define TCP_ACLFLUSH 0x21 /* clear all DACinet ACLs */
tcp.h:#define TCP_ACLCLEAR 0x22 /* clear DACinet ACL */
tcp.h:#define TCP_ACLADD 0x23 /* Add to DACinet ACL */
tcp.h:#define TCP_ACLDEL 0x24 /* Delete from DACinet ACL */
tcp.h:#define TCP_ACLLS 0x25 /* List DACinet ACL */
tcp.h:#define TCP_ACLBIND 0x26 /* Set port number for TCP_ACLLS */
tcp.h:#define TCP_ACLGID 0x01 /* id being added to ACL is a gid */
tcp.h:#define TCP_ACLUID 0x02 /* id being added to ACL is a gid */
tcp.h:#define TCP_ACLSUBNET 0x04 /* address being added to ACL is a subnet */
tcp.h:#define TCP_ACLDENY 0x08 /* this ACL entry is for denying access */
Upon successful completion, a value of 0 is returned.
If the setsockopt subroutine is unsuccessful, the subroutine handler performs the following functions:
Item | Description |
---|---|
EBADF | The Socket parameter is not valid. |
EFAULT | The Address parameter is not in a writable part of the user address space. |
EINVAL | The OptionValue parameter or the OptionLength parameter is invalid or the socket has been shutdown. |
ENOBUFS | There is insufficient memory for an internal data structure. |
ENOTSOCK | The Socket parameter refers to a file, not a socket. |
ENOPROTOOPT | The option is unknown. |
EOPNOTSUPP | The option is not supported by the socket family or socket type. |
EPERM | The user application does not have the permission to get or to set this socket option. Check the network tunable option |
int on=1;
setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
int on=1;
setsockopt(s, IPPROTO_TCP, TCP_NODELAYACK, &on, sizeof(on));