#include <sys/types.h> #include <sys/socket.h> int getsockopt(int socket_descriptor, int level, int option_name, char *option_value, int *option_length)
#define _XOPEN_SOURCE 520 #include <sys/socket.h> int getsockopt(int socket_descriptor, int level, int option_name, void *option_value, socklen_t *option_length)
The getsockopt() function is used to retrieve information about socket options.
There are two versions of the API, as shown above. The base i5/OS API uses BSD 4.3 structures and syntax. The other uses syntax and structures compatible with the UNIX 98 programming interface specifications. You can select the UNIX 98 compatible interface with the _XOPEN_SOURCE macro.
IPPROTO_IP | Request applies to IP protocol layer. |
IPPROTO_TCP | Request applies to TCP protocol layer. |
SOL_SOCKET | Request applies to socket layer. |
IPPROTO_IPV6 | Request applies to IPv6 protocol layer. |
IPPROTO_ICMPV6 | Request applies to ICMPv6 protocol layer. |
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.
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_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 or SOCK_RAW 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. |
Socket Options That Apply to the TCP Layer (IPPROTO_TCP)
Option | Description |
---|---|
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. A non-zero option_value returned by getsockopt indicates TCP_NODELAY is enabled. For example, TCP_NODELAY should be used when there is an appliciation using TCP for a request/response. This option is only supported for sockets with an address family of AF_INET or AF_INET6 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 or AF_INET6 and type SOCK_STREAM. |
Socket Options That Apply to the Socket Layer (SOL_SOCKET )
Option | Description |
---|---|
SO_ACCEPTCONN | Reports whether socket listening is enabled. This option stores an int value. This is a boolean option. |
SO_ACCEPTECONNABORTED |
Determine if the listening socket will return ECONNABORTED when a connection
on the listening backlog is reset prior to a blocking accept().
The option is only valid on a socket that has successfully issued
the listen() call. The option has no effect on non-blocking sockets.
This option is only used by sockets with an address family of AF_INET
or AF_INET6.
|
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. The broadcast address can be determined by issuing an
ioctl() specifying the SIOCGIFBRDADDR request. |
SO_DEBUG | Determine if low level-debugging is active. |
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_INET6. |
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 or AF_INET6 and type 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_INET or AF_INET6, the default is off (which means that the system attempts to send any queued data, with an infinite wait-time). |
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_INET6. |
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 a type of SOCK_STREAM. |
SO_RCVTIMEO | Determine the receive timeout value. This option is not supported unless _XOPEN_SOURCE is defined to be 520 or greater. |
SO_REUSEADDR | Determine if the local socket address can be reused. This option is supported by sockets with an address family of AF_INET or AF_INET6 and a type of SOCK_STREAM or SOCK_DGRAM. |
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 unless _XOPEN_SOURCE is defined to be 520 or greater. |
SO_TYPE | Determine the value for the socket type. |
SO_USELOOPBACK | Determine if the loopback feature is being used. This option is not supported. |
Socket Options That Apply to the IPv6 Layer (IPPROTO_IPV6)
Option | Description |
---|---|
IPV6_UNICAST_HOPS | Get the hop limit value that will be used for subsequent unicast packets sent by this socket. An option_value parameter of type int is used to retrieve the current setting. This option is only supported for sockets with an address family of AF_INET6. |
IPV6_MULTICAST_IF | Get the interface over which outgoing multicast datagrams will be sent. An option_value parameter of type unsigned int is used to retrieve the interface index 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_INET6 and type of SOCK_DGRAM or SOCK_RAW. |
IPV6_MULTICAST_HOPS | Get the hop limit value that will be used for subsequent multicast packets sent by this socket. An option_value parameter of type int is used to retrieve the current setting. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW. |
IPV6_MULTICAST_LOOP | Determine the multicast looping mode. A value of 1 (default), 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 0, a copy of the datagram will not be delivered to the sending host. An option_value parameter of type unsigned int is used to retrieve the current setting. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW. |
IPV6_V6ONLY | Determine the AF_INET6 communication restrictions. A non-zero value indicates that this AF_INET6 socket is restricted to IPv6 communications only. This option stores an int value. This is a boolean option. By default this option is turned off. This option is only supported for sockets with an address family of AF_INET6. |
IPV6_CHECKSUM | Determine if the kernel will calculate and insert a checksum for output and verify the received checksum on input, discarding the packet if the checksum is in error for this socket. An option_value parameter of type int is used to retrieve the current setting. If this option is -1 (the default), this socket option is disabled. A value of 0 or greater specifies an integer offset into the user data of where the checksum is located. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_RAW with a protocol other than IPPROTO_ICMPV6. The checksum is automatically computed for protocol IPPROTO_ICMPV6. |
IPV6_DONTFRAG | Determine if the kernel will not implement the automatic insertion of a fragment header in the packet if the packet is too big for the path MTU. A value of 0 disables the option meaning the default of automatic insertion will be used. A non-zero value indicates that the option is set meaning the kernel will discard the packet instead of inserting the fragment header. This option is supported for sockets with an address family of AF_INET6 and type of SOCK_DGRAM or SOCK_RAW only. |
Socket Options That Apply to the ICMPv6 Layer (IPPROTO_ICMPV6)
Option | Description |
---|---|
ICMP6_FILTER | Determine the current ICMPv6 Type Filtering. An option_value parameter of type struct icmp6_filter, defined in <netinet/icmp6.h> is used to retrieve the current setting. The following macros, defined in <netinet/icmp6.h> can be used after retrieval of the type filtering structure to determine whether or not specific ICMPv6 message types will be passed to the application or be blocked: ICMP6_FILTER_WILLPASS and ICMP6_FILTER_WILLBLOCK. This option is only supported for sockets with an address family of AF_INET6 and type of SOCK_RAW with a protocol of IPPROTO_ICMPV6. |
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 SO_RCVTIME and SO_SNDTIME options, option_value is a pointer to where the structure timeval is stored. The structure timeval is defined in <sys/time.h>.
struct timeval { long tv_sec; long tv_usec; };
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 | IP options | ... | IP options |
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.
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.
getsockopt() returns an integer. Possible values are:
When getsockopt() fails, errno can be set to one of the following:
[EBADF] | Descriptor not valid. |
[ECONNABORTED] | Connection ended abnormally.
This error code indicates that the transport provider ended the connection abnormally because of one of the following:
|
[EFAULT] | Bad address.
The system detected an address which was not valid while attempting to access the option_value or option_length parameters. |
[EINVAL] | Parameter not valid.
This error code indicates one of the following:
|
[EIO] | Input/output error. |
[ENOBUFS] | There is not enough buffer space for the
requested operation. |
[ENOPROTOOPT] | The protocol does not support the specified
option.
This error code indicates one of the following:
|
[ENOTCONN] | Requested operation requires a connection.
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. |
[ENOTSOCK] | The specified descriptor does not reference a
socket. |
[EPERM] | Operation not permitted.
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 . |
[EUNKNOWN] | Unknown system state. |
[EUNATCH] | The protocol required to support the specified address family is not available at this time. |
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
CPFA081 E | Unable to set return value or error code. |
Notes:
Top | UNIX-Type APIs | APIs by category |