setsockopt Subroutine
Purpose
Sets the 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 the options that are associated with a socket. The options can exist at a multiple protocol levels. The options are 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 timeouts, or to allow socket data broadcasts. The /usr/include/sys/socket.h file defines the options that are available to the setsockopt subroutine.
Specify the protocol level at which the option is located and the name of the option when you set socket options.
Use the OptionValue and OptionLength parameters to access option values for the setsockopt subroutine. The OptionValue and OptionLength parameters identify a buffer in which the value for the requested option or is returned.
The applications that contain the setsockopt subroutine
must be compiled with the _BSD macro set to a specific value. The acceptable values
are 43 and 44. In addition, all socket applications
must include the Berkeley Software Distribution (BSD) libbsd.a library.
Parameters
| Item | Description |
|---|---|
| Socket | Specifies the unique name of the socket. |
| Level | Specifies the protocol level at which the option is located. You can set the socket options
at the following levels:
|
| OptionName | Specifies the option to set. The OptionName parameter and the 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. You can enable or
disable the socket level options and they operate in a toggle fashion. The following list defines the socket protocol level options that are available in the sys/socket.h file:
|
| OptionName |
|
| OptionName |
The following list defines the TCP protocol level options that are available in the netinet/tcp.h file:
|
| OptionName |
The TCP protocol level socket options are inherited from listening sockets to new sockets. The following list defines the ATM protocol-level options that are available in the sys/atmsock.h file:
|
| OptionName |
The following list defines the
|
| OptionValue | The OptionValue parameter takes an Int parameter. To
enable a Boolean option, you must set the OptionValue parameter to a nonzero
value. To disable a Boolean option, set the OptionValue parameter to
0. The following options are enabled and disabled 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. Use the IP_DONTFRAG option to detect decreases in Path MTU (PMTU), UDP applications. |
| IP_FINDPMTU | Sets enable or disable PMTU discovery for this path. Enable the protocol-level path to MTU discovery for the discovery to happen. |
| IP_PMTUAGE | Sets the age of the PMTU. Specifies the frequency of the PMT reductions discovery for the session. Setting the PMTU age to 0 implies an infinite age and PMTU reduction discovery is not attempted and replaces the previously set PMTU age. The new PMTU age is effective after the currently set timer expires. 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 (TTL) field in the IP header for every packet. However, for raw sockets, the default MAXTTL value is used while sending the messages irrespective of the value set by using the setsockopt subroutine. |
| IP_HDRINCL | Allows you to build your 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 you do not specify the interface, the interface leading to the default route
is used. |
| IP_MULTICAST_LOOP | Sets a multicast loopback, determining whether the 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 TTL for multicast packets. An OptionValue parameter of the
char type sets the value of TTL that ranges from 0 to
255. |
| IP_BLOCK_SOURCE | Blocks the 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, you must use the IP_DROP_MEMBERSHIP socket option. |
| Item | Description | Value | |
|---|---|---|---|
| IPPROTO_IPV6 | Restricts the AF_INET6 sockets to IPv6 communications
only. |
|
|
| Allows you to set the outgoing hop limit for unicast IPv6 packets. |
|
||
| Allows you to set the outgoing hop limit for multicast IPv6 packets. |
|
||
| Allows you to specify the interface that is 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 a checksum over the data and the pseudo-IPv6 header for a raw socket. The kernel computes the checksum for outgoing packets and verifies the checksum for incoming packets on that socket. Incoming packets with incorrect checksum are 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 of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets. |
|
||
| Causes the hop-by-hop options header of incoming IPv6 packets to be received as ancillary data on UDP and raw sockets. |
|
||
| Causes the destination options header 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 that are sent on this socket. You can clear this option 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. You can clear this option by doing a regular setsockopt with a 0 length. A memory pointer must be supplied for the option value. |
|
||
| Sets the traffic class for outgoing IPv6 datagrams on this socket. To clear this option, the application can specify the value as -1. |
|
||
| 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. 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. 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 follows a routing header and is used when the routing header is not specified. This option can be cleared by doing a regular setsockopt with a 0 length. 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 precedes a routing header. If no routing header is specified, this option is silently ignored. This option can be cleared by doing a regular setsockopt with a 0 length. A memory pointer must still be supplied for the option value in this case. |
|
||
| Sets this option to control IPv6 path MTU discovery. |
|
||
| Set this option to prevent fragmentation of outgoing IPv6 packets on this socket. If a packet is being sent that is larger than the outgoing interface MTU, the packet is discarded. |
|
||
| Enables the receipt of the 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, you must use the IPV6_LEAVE_GROUP or MCAST_LEAVE_GROUP socket option. |
|
||
| Item | Description | Value |
|---|---|---|
| IPPROTO_ICMPV6 | Filters the ICMPV6 messages by the ICMPV6
type field. Issue a setsockopt call with zero length to clear an existing
filter. |
|
dacinet
functions.
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 is shutdown. |
| ENOBUFS | Insufficient memory for an internal data structure. |
| ENOTSOCK | The Socket parameter refers to a file, not to a socket. |
| ENOPROTOOPT | The option is unknown. |
| EOPNOTSUPP | The socket family or the socket type does not support the EOPNOTSUPP option. |
| 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, enter the following command:
int on=1; setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)); - To turn on the TCP_NODELAYACK option, enter the following command:
int on=1; setsockopt(s, IPPROTO_TCP, TCP_NODELAYACK, &on, sizeof(on));