Syntax
#include <sys/types.h>
#include <sys/socket.h>
int setsockopt(int socket_descriptor,
int level,
int option_name,
char *option_value,
int option_length)
|
The setsockopt() function is used to set socket options.
Parameters
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.
Figure 1-13. 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_SETPMTU | Enable or disable path MTU discovery. This option is supported for sockets with an address family of AF_INET and type of SOCK_DGRAM only. | ||||||||||||
| 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 only. |
Figure 1-14. Socket Options That Apply to the TCP Layer (IPPROTO_TCP)
| Option | Description |
|---|---|
| TCP_NODELAY | Indicates if TCP is to buffer data. This option is only supported for sockets with an address family of AF_INET and type of SOCK_STREAM. |
Figure 1-15. Socket Options That Apply to the IPX or SPX Layer (NNSPROTO_IPX or NNSPROTO_SPX)
| Option | Description |
|---|---|
| SO_HEADERS_ON_INPUT | Request the transport to deliver the received protocol headers with the data on input operations such as recv(). This is only supported for sockets with an address family of AF_NS and a type of SOCK_SEQPACKET or SOCK_DGRAM. If the type is SOCK_SEQPACKET, this option requests that the transport place the SPX headers received with the data packets in the first 12 bytes of the supplied user buffer on input operations. The default is that the option be set on. If the type is SOCK_DGRAM, this option requests that the transport place the IPX headers received with the data packets in the first 30 bytes of the supplied user buffer on input operations. The default is that the option be set off. Changing the setting of this option does not affect packets that have already been received by the system and are waiting to be delivered to your application. |
| SO_HEADERS_ON_OUTPUT | Request the transport to accept the protocol header as the first 12 or 30 bytes of data in the user-supplied buffer on output operations such as send(). This is only supported for sockets with an address family of AF_NS and a type of SOCK_SEQPACKET or SOCK_DGRAM. If the type is SOCK_SEQPACKET, the user must supply the SPX header as the first 12 bytes of the buffer on output operations. Only the end of message indicator and data stream type fields of the specified header are used. The default is that the option be set on. If the type is SOCK_DGRAM, the user must supply the IPX header as the first 30 bytes of the buffer on output operations. All fields of the specified header will be used except checksum and packet length. The default is that the option be set off. Changing this option does not affect packets that have been sent by your application but have not yet been transmitted because of flow control or other reasons. |
| SO_DEFAULT_HEADERS | Specify the default protocol header to be used on output operations. This is only supported for sockets with an address family of AF_NS and a type of SOCK_SEQPACKET, SOCK_STREAM, or SOCK_DGRAM. For type SOCK_SEQPACKET or SOCK_STREAM, this is a default 12-byte SPX header. Only the end of message indicator and data stream type fields of the specified header are used. For type SOCK_DGRAM, this is a default 30-byte IPX header. Only the packet type field of the specified header is used. |
Figure 1-16. Socket Options That Apply to the SPX Layer Only (NNSPROTO_SPX)
| Option | Description |
|---|---|
| SO_MTU | Change the maximum transmission unit (MTU) size. This is only supported for sockets with an address family of AF_NS and a type of SOCK_SEQPACKET or SOCK_STREAM. |
Figure 1-17. Socket Options That Apply to the Socket Layer (SOL_SOCKET )
| Option | Description |
|---|---|
| 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, or an address family of AF_NS and type SOCK_DGRAM.
For sockets with an address family of AF_INET, 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. This option is not supported. |
| SO_DONTROUTE | Bypass normal routing mechanisms. This option is only supported by sockets with an address family of AF_INET or AF_NS. |
| SO_KEEPALIVE | Keep the connection up by sending periodic transmissions. This option is only supported for sockets of an address family of AF_INET and type SOCK_STREAM, or an address family of AF_NS and a type of SOCK_SEQPACKET or 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_NS or AF_INET, the default is off (which means that the system attempts to send any queued data, with an infinite wait-time).
For sockets that
are using a connection-oriented transport service with an
address family of AF_TELEPHONY, the default is on with a linger
time of 1 second (which means that the system will wait up to
1 second to send buffered data before clearing the telephone
connection).
|
| 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_NS. |
| 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 an address family of AF_NET
and type of SOCK_STREAM.
|
| SO_RCVTIMEO | Set the receive timeout value. This option is not supported. |
| SO_REUSEADDR | Indicates if the local socket address can be reused. This option is supported by sockets with an address family of AF_INET and a type of SOCK_STREAM or SOCK_DGRAM. It is also supported for sockets with an address family of AF_NS and a type of SOCK_SEQPACKET or SOCK_STREAM. |
| 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. |
| SO_USELOOPBACK | Indicates if the loopback feature is being used. This option is not supported. |
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 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.
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:
For the SO_DEFAULT_HEADERS option where level is specified as NNSPROTO_IPX, option_value is a pointer to where the structure idp is stored. The structure idp is defined in <netns/idp.h>. For the SO_DEFAULT_HEADERS option where level is specified as NNSPROTO_SPX, option_value is a pointer to where the structure sphdr is stored. The structure sphdr is defined in <netns/sp.h>.
Return Value
setsockopt() returns an integer. Possible values are:
Error Conditions
When setsockopt() fails, errno can be set to one of the following:
This error code indicates that the transport provider ended the connection abnormally because of one of the following:
The system detected an address which was not valid while attempting to access the option_value parameter.
This error code indicates one of the following:
This error code indicates one of the following:
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 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 .
Error Messages
Usage Notes
Notes:
Related Information
getsockopt()--Retrieve Information about Socket Options
| Top | Sockets APIs | APIs by category |
| [Information Center Home Page | Feedback ] | [Legal | AS/400 Glossary] |