setsockopt Subroutine

Purpose

Sets socket options.

Library

Standard C Library (libc.a)

Syntax

#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;

Description

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.

Parameters

Item Description
Socket Specifies the unique socket name.
Level Specifies the protocol level at which the option resides. To set options at:
Socket level
Specifies the Level parameter as SOL_SOCKET.
Other levels
Supplies the appropriate protocol number for the protocol controlling the option. For example, to indicate that an option will be interpreted by the TCP protocol, set the Level parameter to the protocol number of TCP, as defined in the netinet/in.h file. Similarly, to indicate that an option will be interpreted by ATM protocol, set the Level parameter to NDDPROTO_ATM, as defined in sys/atmsock.h.
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:

SO_DEBUG
Turns on recording of debugging information. This option enables or disables debugging in the underlying protocol modules. Set this option in one of the following ways at the command level:
  • Use the sodebug command, which turns on or off this option for existing sockets.
  • Specify |DEBUG[=level] in the wait/nowait field of a service in inetd.conf in order to turn on this option for the specific service.
  • Set the sodebug_env parameter to no, and specify SODEBUG=level in the process environment. This turns on or off this option for all subsequent sockets created by the process.
The value for level can be either min, normal, or detail.
SO_REUSEADDR
Specifies that the rules used in validating addresses supplied by a bind subroutine should allow reuse of a local port.

SO_REUSEADDR allows an application to explicitly deny subsequent bind subroutine to the port/address of the socket with SO_REUSEADDR set. This allows an application to block other applications from binding with the bind subroutine.

SO_REUSEPORT
Specifies that the rules used in validating addresses supplied by a bind subroutine should allow reuse of a local port/address combination. Each binding of the port/address combination must specify the SO_REUSEPORT socket option
SO_CKSUMREV
Enables performance enhancements in the protocol layers. If the protocol supports this option, enabling causes the protocol to defer checksum verification until the user's data is moved into the user's buffer (on recv, recvfrom, read, or recvmsg thread). This can cause applications to be awakened when no data is available, in the case of a checksum error. In this case, EAGAIN is returned. Applications that set this option must handle the EAGAIN error code rturned from a receive call.
SO_KEEPALIVE
Monitors the activity of a connection by enabling or disabling the periodic transmission of ACK messages on a connected socket. The idle interval time can be designated using the TCP/IP no command. Broken connections are discussed in "Understanding Socket Types and Protocols" in Communications Programming Concepts.
OptionName
SO_DONTROUTE
Does not apply routing on outgoing messages. Indicates that outgoing messages should bypass the standard routing facilities. Instead, they are directed to the appropriate network interface according to the network portion of the destination address.
SO_BROADCAST
Permits sending of broadcast messages.
SO_LINGER
Lingers on a close subroutine if data is present. This option controls the action taken when an unsent messages queue exists for a socket, and a process performs a close subroutine on the socket.

If SO_LINGER is set, the system blocks the process during the close subroutine until it can transmit the data or until the time expires. If SO_LINGER is not specified and a close subroutine is issued, the system handles the call in a way that allows the process to continue as quickly as possible.

The sys/socket.h file defines the linger structure that contains the l_linger member for specifying linger time interval. If linger time is set to anything but 0, the system tries to send any messages queued on the socket. The maximum value that the l_linger member can be set to is 65535. If the application has requested SPEC1170 compliant behavior by exporting the XPG_SUS_ENV environment variable , the linger time is n seconds; otherwise, the linger time is n/100 seconds (ticks), where n is the value of the l_linger member.

SO_OOBINLINE
Leaves received out-of-band data (data marked urgent) in line.
SO_SNDBUF
Sets send buffer size.
SO_RCVBUF
Sets receive buffer size.
SO_SNDLOWAT
Sets send low-water mark.
SO_RCVLOWAT
Sets receive low-water mark.
SO_SNDTIMEO
Sets send time out. This option is setable, but currently not used.
SO_RCVTIMEO
Sets receive time out. This option is setable, but currently not used.
SO_ERROR
Sets the retrieval of error status and clear.
SO_TYPE
Sets the retrieval of a socket type.
OptionName

The following list defines TCP protocol level options found in the netinet/tcp.h file:

TCP_CWND_IF
Increases the factor of the TCP congestion window (cwnd) during the congestion avoidance. The value must be in the range 0 - 100 (0 is disable). The tcp_cwnd_modified network tunable option must be enabled.
TCP_CWND_DF
Decrease the factor of the TCP cwnd during the congestion avoidance. The value must be in the range 0 - 100 (0 is disable). The tcp_cwnd_modified network tunable option must be enabled.
TCP_NOTENTER_SSTART
Avoids reentering the slow start after the retransmit timeout, which might reset the cwnd to the initial window size, instead of the size of the current slow-start threshold (ss_threshold) value or half of the maximum cwnd (max cwnd/2). The values are 1 for enable and 0 for disable. The tcp_cwnd_modified network tunable option must be enabled.
TCP_ NOREDUCE_CWND_IN_FRXMT
Not decrease the cwnd size when in the fast retransmit phrase. The values are 1 for enable and 0 for disable. The tcp_cwnd_modified network tunable option must be enabled.
TCP_ NOREDUCE_CWND_EXIT_FRXMT
Not decrease the cwnd size when exits the fast retransmit phrase. The values are 1 for enable and 0 for disable. The tcp_cwnd_modified network tunable option must be enabled.
TCP_KEEPCNT
Specifies the maximum number of keepalive packets to be sent to validate a connection. This socket option value is inherited from the parent socket. The default is 8.
TCP_KEEPIDLE
Specifies the number of seconds of idle time on a connection after which TCP sends a keepalive packet. This socket option value is inherited from the parent socket from the accept system call. The default value is 7200 seconds (14400 half seconds).
TCP_KEEPINTVL
Specifies the interval of time between keepalive packets. It is measured in seconds. This socket option value is inherited from the parent socket from the accept system call. The default value is 75 seconds (150 half seconds).
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.
OptionName
TCP_RFC1323
Enables or disables RFC 1323 enhancements on the specified TCP socket. An application might contain the following lines to enable RFC 1323:
int on=1;
setsockopt(s,IPPROTO_TCP,TCP_RFC1323,&on,
           sizeof(on));
TCP_STDURG
Enables or disables RFC 1122 compliant urgent point handling. By default, TCP implements urgent pointer behavior compliant with the 4.2 BSD operating system, i.e., this option defaults to 0.
TCP_NODELAYACK
Specifies if TCP needs to send immediate acknowledgement packets to the sender. If this option is not set, TCP delays sending the acknowledgement packets by up to 200 ms. This allows the acknowledgements to be sent along with the data on a response and minimizes system overhead. Setting this TCP option might cause a slight increase in system overhead, but can result in higher performance for network transfers if the sender is waiting on the receiver's acknowledgements.

TCP protocol level socket options are inherited from listening sockets to new sockets.

The following list defines ATM protocol level options found in the sys/atmsock.h file:

SO_ATM_PARAM
Sets all ATM parameters. This socket option can be used instead of using individual sockets options described below. It uses the connect_ie structure defined in sys/call_ie.h file.
SO_ATM_AAL_PARM
Sets ATM AAL(Adaptation Layer) parameters. It uses the aal_parm structure defined in sys/call_ie.h file.
SO_ATM_TRAFFIC_DES
Sets ATM Traffic Descriptor values. It uses the traffic structure defined in sys/call_ie.h file.
SO_ATM_BEARER
Sets ATM Bearer capability. It uses the bearer structure defined in sys/call_ie.h file.
SO_ATM_BHLI
Sets ATM Broadband High Layer Information. It uses the bhli structure defined in sys/call_ie.h file.
SO_ATM_BLLI
Sets ATM Broadband Low Layer Information. It uses the blli structure defined in sys/call_ie.h file.
SO_ATM_QOS
Sets ATM Quality Of Service values. It uses the qos_parm structure defined in sys/call_ie.h file.
SO_ATM_TRANSIT_SEL
Sets ATM Transit Selector Carrier. It uses the transit_sel structure defined in sys/call_ie.h file.
OptionName
SO_ATM_ACCEPT
Indicates acceptance of an incoming ATM call, which was indicated to the application via ACCEPT system call. This must be issues for the incoming connection to be fully established. This allows negotiation of ATM parameters.
SO_ATM_MAX_PEND
Sets the number of outstanding transmit buffers that are permitted before an error indication is returned to applications as a result of a transmit operation. This option is only valid for non best effort types of virtual circuits. OptionValue/OptionLength point to a byte which contains the value that this parameter will be set to.

The following list defines IPPROTO_TCP protocol level options found in the netinet/sctp.h file:

SCTP_PEER_ADDR_PARAMS
Enables or disables heartbeats for an association and modifies the heartbeat interval of the association. This option uses the sctp_paddrparams structure defined in the netinet/sctp.h file. For spp_address field, AIX® only supports wildcard address now. The SPP_HB_ENABLE, SPP_HB_DISABLE, and SPP_HB_TIME_ISZERO flags are supported for the spp_flags field. The spp_hbinterval field can be set to a minimum value of 50 milliseconds.
SCTP_MAXSEG
Sets the maximum size of any outgoing SCTP DATA chunk. If the message is larger than the specified size, the message is fragmented by SCTP into the specified size. It uses the sctp_assoc_value structure that is defined 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:

  • SO_DEBUG
  • SO_REUSEADDR
  • SO_KEEPALIVE
  • SO_DONTROUTE
  • SO_BROADCAST
  • SO_OOBINLINE
  • SO_LINGER
  • TCP_RFC1323
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. To detect decreases in Path MTU, UDP applications use the IP_DONTFRAG 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 is effective after the currently set timer expires. Currently, this option is unused because UDP applications must set the IP_DONTFRAG socket 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.
Option Type:
int (Boolean interpretation)
  Allows the user to set the outgoing hop limit for unicast IPv6 packets.
Option Type:
int (x)
Option Value:
x < -1 Error EINVAL
x == -1 Use kernel default
0 <= x <= 255 Use
x x >= 256 Error EINVAL
  Allows the user to set the outgoing hop limit for multicast IPv6 packets.
Option Type:
int (x)
Option Value:
Interpretation is same as IPV6_UNICAST_HOPS (listed above).
  Allows the user to specify the interface being used for outgoing multicast packets. If specified as 0, the system selects the outgoing interface.
Option Type:
unsigned int (index of interface to use)
  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.
Option Type:
unsigned int
  Joins a multicast group on a specified local interface. If the interface index is specified as 0, the kernel chooses the local interface.
Option Type:
struct ipv6_mreq as defined in the netinet/in.h file
  Leaves a multicast group on a specified interface.
Option Type:
struct ipv6_mreq as defined in the netinet/in.h file
  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.
Option Type:
int
Option Value:
Offsets into the user data where the checksum result must be stored. This must be a positive even value. Setting the value to -1 will disable the option.
  Causes the destination IPv6 address and arriving interface index of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets.
Option Type:
int (Boolean interpretation)
  Causes the hop limit of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets.
Option Type:
int (Boolean interpretation)
  Causes the traffic class of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets.
Option Type:
int (Boolean interpretation)
  Causes the routing header (if any) of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets.
Option Type:
int (Boolean interpretation)
  Causes the hop-by-hop options header (if any) of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets.
Option Type:
int (Boolean interpretation)
  Causes the destination options header (if any) of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets.
Option Type:
int (Boolean interpretation)
  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.
Option Type:
struct in6_pktinfo defined in the netinet/in.h file.
  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.
Option Type:
struct sockaddr_in6 defined in the netinet/in.h file.
  Sets the traffic class for outgoing IPv6 datagrams on this socket. To clear this option, the application can specify -1 as the value.
Option Type:
int (x)
Option Value:
x < -1 Error EINVAL
x == -1 Use kernel default
0 <= x <= 255 Use
x x >= 256 Error EINVAL
  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.
Option Type:
struct ip6_rthdr defined in the netinet/ip6.h file.
  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.
Option Type:
struct ip6_hbh defined in the netinet/ip6.h file.
  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.
Option Type:
struct ip6_dest defined in the netinet/ip6.h file.
  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.
Option Type:
struct ip6_dest defined in the netinet/ip6.h file.
  Sets this option to control IPv6 path MTU discovery.
Option Type:
int
Option Type:
-1 Performs path MTU discovery for unicast destinations, but does not perform it for multicast destinations.0 Always performs path MTU discovery.1 Always disables path MTU discovery and sends packets at the minimum MTU.
  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.
Option Type:
int (Boolean interpretation)
  Enables the receipt of IPV6_PATHMTU ancillary data items by setting this option.
Option Type:
int (Boolean interpretation)
  Sets the address selection preferences for this socket.
Option Type:
int
Option Value:
Combination of the IPV6_PREFER_SRC_* flags defined in netinet/in.h
  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.
Option Type:
struct group_req as defined in the netinet/in.h file
  Leaves the multicast group as specified in the OptionValue parameter of the group_req structure.
Option Type:
struct group_req as defined in the netinet/in.h file
  Blocks data from the specified source to the specified multicast group.
Option Type:
struct group_source_req as defined in the netinet/in.h file
  Unblocks data from the specified source to the specified multicast group. The option is used to undo the MCAST_BLOCK_SOURCE operation.
Option Type:
struct group_source_req as defined in the netinet/in.h file
  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.
Option Type:
struct group_source_req as defined in the netinet/in.h file
  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.
Option Type:
struct group_source_req as defined in the netinet/in.h file
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.
Option Type:
The icmp6_filter structure defined in the netinet/icmp6.h file.
The following values (defined in the /usr/include/netint/tcp.h file) are used by the setsockopt subroutine to configure the dacinet functions.
Note: The DACinet facility is available only in a CAPP/EAL4+ configured AIX system.
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 */

Return Values

Upon successful completion, a value of 0 is returned.

If the setsockopt subroutine is unsuccessful, the subroutine handler performs the following functions:

  • Returns a value of -1 to the calling program.
  • Moves an error code, indicating the specific error, into the errno global variable.

Error Codes

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

Examples

  • To mark a socket for broadcasting:
    int on=1;
    setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
  • To turn on the TCP_NODELAYACK option, run the following:
    int on=1;
    setsockopt(s, IPPROTO_TCP, TCP_NODELAYACK, &on, sizeof(on));