#include <sys/types.h>
#include <sys/socket.h>
int getsockopt(int socket_descriptor,
int level,
int option_name,
char *option_value,
int *option_length)
|
The getsockopt() function is used to retrieve information about socket options.
Parameters
Note: Options directed to a specific protocol level are only supported by that protocol. An option that is directed to level SOL_SOCKET usually completes successfully. If the underlying protocol does not provide support for the option, the socket library retrieves one of the following:
This provides compatibility with Berkeley Software Distributions
implementations that also shield the application from protocols that do
not support an option.
Figure 1-3. Socket Options That Apply to the
IP Layer (IPPROTO_IP)
| Option | Description |
|---|---|
| IP_OPTIONS | Determine what options are set in the IP header. This is only supported for sockets with an address family of AF_INET. |
| IP_TOS | Get 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 | Get Time To Live (TTL) in the IP header. This option is only supported for sockets with an address family of AF_INET. |
| IP_MULTICAST_IF | Get interface over which outgoing multicast datagrams will be sent. An option_value parameter of type in_addr is used to retrieve the local IP address 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_INET and type of SOCK_DGRAM or SOCK_RAW. |
| IP_MULTICAST_TTL | Get Time To Live (TTL) from the IP header for outgoing multicast datagrams. An option_value parameter of type char is used into which a value between 0 and 255 is retrieved. This option is only supported for sockets with an address family of AF_INET and type of SOCK_DGRAM or SOCK_RAW. |
| IP_GETPMTU | Get the discovered path MTU value. A value of 0 indicates that path MTU discovery is not enabled. A value of -1 indicates discovery is not complete; no MTU value is available. A postive value indicates the current MTU value. This option is supported for sockets with an address family of AF_INET and type of SOCK_DGRAM only. |
| IP_DONTFRAG | Return the current Don't fragment flag setting in the IP header. A value of 0 indicates that it is reset. A value of 1 indicates that it is set. This option is supported for sockets with an address family of AF_INET and type of SOCK_DGRAM only. |
| IP_PMTUAGE | Return the system setting of the path MTU discovery time out value. A value of 0 indicates infinite age; no rediscovery time is set. A positive value indicates the path MTU discovery time out value; the returned value is in minutes. This option is supported for sockets with an address family of AF_INET and type of SOCK_DGRAM only. |
| IP_MULTICAST_LOOP | Determine the multicast looping mode. A non-zero value 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 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 retrieve the current setting. This option is only supported for sockets with an address family of AF_INET and type of SOCK_DGRAM or SOCK_RAW. |
| IP_RECVLCLIFADDR | Determine if the local interface that a datagram was received will 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. |
Figure 1-4. Socket Options That Apply to the
TCP Layer (IPPROTO_TCP)
| Option | Description |
|---|---|
| TCP_NODELAY | Determine if TCP is buffering data. This option is only supported for sockets with an address family of AF_INET and type SOCK_STREAM. |
| TCP_MAXSEG | Determine TCP maximum segment size. This option is only supported for sockets with an address family of AF_INET and type SOCK_STREAM. |
Figure 1-5. Socket Options That Apply to the
IPX or SPX Layer (NNSPROTO_IPX or NNSPROTO_SPX)
| Option | Description |
|---|---|
| SO_HEADERS_ON_INPUT | Determine if the transport will 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 | Determine if the transport will 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 | Retrieve 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-6. Socket Options That Apply to the
SPX Layer Only (NNSPROTO_SPX)
| Option | Description |
|---|---|
| SO_LAST_HEADER | Retrieve the last SPX header received. This is only supported for sockets with an address family of AF_NS and a type of SOCK_SEQPACKET or SOCK_STREAM. |
| SO_MTU | Retrieve 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-7. Socket Options That Apply to the
Socket Layer (SOL_SOCKET )
| Option | Description |
|---|---|
| SO_BROADCAST | Determine if messages can be
sent to the 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. This option is only supported for sockets with
an address family of AF_INET and type SOCK_DGRAM or
SOCK_RAW. For sockets with an address family of AF_INET,
the broadcast address can be determined by issuing an ioctl()
specifying the SIOCGIFBRDADDR request. For sockets with an
address family of AF_NS, a broadcast address is an address
that specifies 0xFF for all six bytes of the host portion of the address.
|
| SO_DEBUG | Determine if low level-debugging is active. This option is not supported. |
| SO_DONTROUTE | Determine if the normal routing mechanism is being bypassed. This option is only supported by sockets with an address family of AF_INET or AF_NS. |
| SO_ERROR | Return any pending errors in the socket. The value returned corresponds to the standard error codes defined in <errno.h> |
| SO_KEEPALIVE | Determine if the connection is being kept up by periodic transmissions. This option is only supported for sockets with 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 | Determine whether the system
attempts to deliver 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 | Determine if 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 | Determine the size of the receive buffer. |
| SO_RCVLOWAT | Determine the size of the receive
low-water mark. This option is only supported for sockets with an address
family of AF_INET and type of SOCK_STREAM. |
| SO_RCVTIMEO | Determine the receive timeout value. This option is not supported. |
| SO_REUSEADDR | Determine 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 | Determine the size of the send buffer. |
| SO_SNDLOWAT | Determine the size of the send low-water mark. This option is not supported. |
| SO_SNDTIMEO | Determine the send timeout value. This option is not supported. |
| SO_TYPE | Determine the value for the socket type. |
| SO_USELOOPBACK | Determine if the loopback feature is being used. This option is not supported. |
The following options should be considered as set if a nonzero value for the option_value parameter is returned:
For the SO_LINGER option, option_value is a pointer to where the structure linger is stored. The structure linger is 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 storage in which data representing the IP options (as specified in RFC
791) is stored. getsockopt() returns the options in the following
format:
IP address is a 4-byte IP address, and IP options identifies the IP options that were set using setsockopt(). If an IP option set using setsockopt() contained a source routing option (strict or loose), the first IP address in the source routing option list is removed. The IP options are adjusted accordingly. (For this adjustment, the length in the IP options portion is changed, and alignment is kept by adding no-operation option). The buffer is returned in the same format. The first 4 bytes are the IP address that was removed, and this is followed by the remaining IP options, if any. If the IP options portion does not contain a source routing option, the first 4 bytes are set to zero.
For the IP_MULTICAST_IF option, option_value is a pointer to storage in which the structure in_addr, defined in <netinet/in.h> as the following, will be stored:
struct in_addr {
u_long s_addr; /* IP address */
};
The s_addr field that is returned will be the local IP address that is associated with the interface over which outgoing multicast datagrams are being sent.
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>.
For the SO_LAST_HEADER option, option_value is a pointer to where the structure sphdr is stored.
Notes:
Note: For option values that are of type integer, the length of the option_value pointed to by the option_length parameter must be set to a value that is greater or equal to the size of an integer. If the length is not set correctly, a correct option value is not received.
Return Value
getsockopt() returns an integer. Possible values are:
Error Conditions
When getsockopt() 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 or option_length parameters.
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 get options when the level parameter specifies IPPROTO_IP and the option_value parameter is IP_OPTIONS .
Error Messages
Usage Notes
Notes:
Related Information
| Top | Sockets APIs | APIs by category |
| [Information Center Home Page | Feedback ] | [Legal | AS/400 Glossary] |