QNX Developer Support
|This version of this document is no longer maintained. For the latest documentation, see http://www.qnx.com/developers/docs.|
Get options associated with a socket
#include <sys/types.h> #include <sys/socket.h> int getsockopt( int s, int level, int optname, void * optval, socklen_t * optlen );
- The file descriptor of the socket that the option is to be applied to, as returned by socket().
- The protocol layer that the option is to be applied to. In most cases, it's a socket-level option and is indicated by SOL_SOCKET.
- The option for the socket file descriptor. For a list of options, see "Options," below.
- A pointer to the value of the option (in most cases, whether the option is to be turned on or off). If no option value is to be returned, optval may be NULL.
- A pointer to the length of the value of the option. This argument is a value-result parameter; initialize it to indicate the size of the buffer pointed to by optval.
Use the -l socket option to qcc to link against this library.
The getsockopt() function gets options associated with a socket.
When manipulating a socket option, you must specify the option's name (optname) and the level (level) at which the option resides.
To manipulate options at the socket-level, specify level as SOL_SOCKET. When manipulating options any other level, the value that you specify for level is represented by the protocol number of the appropriate protocol controlling the option. You can obtain the value in a variety of ways:
- from the "Options" section below, use the symbolic constant (e.g. IPPROTO_IP, IPPROTO_TCP) that corresponds to the option
- from /etc/protocols, specify the protocol number for the appropriate protocol
- call getprotobyname() and pass the appropriate protocol (e.g. getprotobyname( tcp );) to retrieve the number of the protocol level.
|The latter two ways might not work if you have customized /etc/protocols.|
The optname parameter and any specified options are passed uninterpreted to the appropriate protocol module for interpretation. The <sys/socket.h> header file contains definitions for the socket-level options. Options at other protocol levels vary in format and name.
|Since levels (e.g. SOL_SOCKET, IPPROTO_IP and IPPROTO_TCP)
and the options within the levels can vary, you need to ensure the proper headers are included for both.
For example, when setting TCP_NODELAY:
int on = 1; setsockopt(s, IPPROTO_TCP, TCP_NODELAY, &on);
the level IPPROTO_TCP is defined in <netinet/in.h>, whereas the TCP_NODELAY option is defined in <netinet/tcp.h>.
This section describes some of the more common options and their corresponding level.
Here are some links to help you find an option:
IP_HDRINCL · IP_TOS · SO_BINDTODEVICE · SO_BROADCAST · SO_DEBUG · SO_DONTROUTE · SO_ERROR · SO_KEEPALIVE · SO_LINGER · SO_OOBINLINE · SO_RCVBUF · SO_SNDBUF · SO_RCVLOWAT · SO_RCVTIMEO · SO_REUSEADDR · SO_REUSEPORT · SO_SNDLOWAT · SO_SNDTIMEO · SO_TIMESTAMP · SO_TYPE · SO_USELOOPBACK · TCP_KEEPALIVE · TCP_NODELAY
|Except where noted, you can examine the state of the option by calling
getsockopt(), and set the state by calling
For the list of options that the tiny TCP/IP stack supports, see npm-ttcpip.so in the Utilities Reference.
Get or set the custom IP header that's included with your data. You can use it only for raw sockets. For example:
(socket(AF_INET, SOCK_RAW, ...)
Get or set the type-of-service field in the IP header for SOCK_STREAM and SOCK_DGRAM (not applicable for npm-ttcpip.so) sockets.
Applies to setsockopt() only.
Allow packets to be sent or received on this specified interface only. If the interface specified conflicts with the parameters of bind(), or with the routing table, an error or undesired behavior may occur.
This option accepts the ifreq structure with the ifr_name member set to the interface name (e.g. en0). Currently, you can use this option only for UDP sockets.
Enable or disable the permission to transmit broadcast messages. You can use this option only for UDP sockets. For example:
(socket(AF_INET, SOCK_DGRAM, ...))
"Broadcast" was a privileged operation in earlier versions of the system.
Enable or disable the recording of debug information in the underlying protocol modules.
Enable or disable the bypassing of routing tables for outgoing messages. Indicates that outgoing messages should bypass the standard routing facilities. The messages are directed to the appropriate network interface according to the network portion of the destination address.
Applies to getsockopt() only.
Get any pending error on the socket and clears the error status. You can use it to check for asynchronous errors on connected datagram sockets or for other asynchronous errors.
Enable or disable the periodic (at least every 2 hours) transmission of messages on a connected socket. Should the connected party fail to respond to these messages, the connection is considered broken, and processes that are using the socket are notified via a SIGPIPE signal when they attempt to send data.
Controls the action that's taken when unsent messages are queued on socket when a close() is performed.
If it's enabled and the socket promises reliable delivery of data, the system blocks the process on the close() attempt until it's able to transmit the data or until it decides it can't deliver the information (a timeout period, termed the linger interval, is specified in the setsockopt() call when SO_LINGER is requested).
If it's disabled, the system processes the close() in a way that lets the process continue as quickly as possible.
The struct linger parameter (defined in <sys/socket.h>) specifies the desired state of the option in the l_onoff field and the linger interval in the l_linger field, in seconds. A value of 0 causes a reset on the socket when the application closes the socket.
For protocols that support out-of-band data, allows or disallows out-of-band data to be placed in the normal data input queue as received. The data is accessible using the recv() or read() calls without the MSG_OOB flag. Some protocols always behave as if this option is set.
Gets or sets the normal buffer sizes allocated for output (SO_SNDBUF) and input (SO_RCVBUF) buffers. You can increase the buffer size for high-volume connections, or decrease it to limit the possible backlog of incoming data. The system places an absolute limit on these values and defaults them to at least 16K for TCP sockets.
Gets or sets the minimum count for input operations (default is 1). In general, receive calls block until any (nonzero) amount of data is received, and then return with the amount available or the amount requested, whichever is smaller.
If you set the value to be larger than the default, blocking receive calls will wait until they've received the low-water mark value or the requested amount, whichever is smaller. Receive calls may still return less than the low-water mark if: an error occurs, a signal is caught, or if the type of data next in the receive queue differs from that returned.
Gets or sets a timeout value for input operations. It accepts a struct timeval parameter (defined in <sys/time.h>) with the number of seconds and microseconds used to limit waits for input operations to complete.
In the current implementation, this timer is restarted each time additional data is received by the protocol, so the limit is in effect an inactivity timer. If a receive operation has been blocked for this much time without receiving additional data, it returns with a short count or, if no data was received, with the error EWOULDBLOCK.
Enables or disables the reuse of duplicate addresses and port bindings. Indicates that the rules used in validating addresses supplied in a bind() call allows/disallows local addresses to be reused.
Enables or disables duplicate address and port bindings. Complete duplicate bindings by multiple processes are allowed when they all set SO_REUSEPORT before binding the port. This option permits multiple instances of a program to each receive UDP/IP multicast or broadcast datagrams destined for the bound port. See the reuseport_unicast option of the npm-tcpip.so utility to see how unicast packets are also received on all sockets bound to the same port.
Gets or sets the minimum count for output operations. In BSD, this count is typically 2048, but it is a calculated value in Neutrino. If you require a specific SO_SNDLOWAT, you must specify the count. Most output operations process all of the data supplied by the call, delivering data to the protocol for transmission and blocking as necessary for flow control. Nonblocking output operations will process as much data as permitted (subject to flow control without blocking), but will process no data if flow control doesn't allow the smaller of the low-water mark value or the entire request to be processed.
A select() operation that tests the ability to write to a socket returns true only if the low-water mark amount could be processed.
Gets or sets a timeout value for output operations. It accepts a struct timeval parameter (defined in <sys/time.h>) that includes the number of seconds and microseconds that are used to limit waits for output operations to complete. If a send operation has blocked for this much time, it returns with a partial count or with the error EWOULDBLOCK if data weren't sent.
This timer is restarted each time additional data is delivered to the protocol, implying that the limit applies to output portions ranging in size from the low-water mark to the high-water mark for output. Timeouts are restricted to 32 seconds or under.
Enables or disables the reception of a timestamp with datagrams. If enabled on a SOCK_DGRAM socket, the recvmsg() call returns a timestamp corresponding to when the datagram was received. The msg_control field in the msghdr structure points to a buffer that contains a cmsghdr structure followed by a struct timeval. The cmsghdr fields have the following values:
cmsg_len = sizeof(struct cmsghdr) + sizeof(struct timeval) cmsg_level = SOL_SOCKET cmsg_type = SCM_TIMESTAMP
Applies to getsockopt() only.
Gets the type of the socket (e.g. SOCK_STREAM). This information is useful for servers that inherit sockets on startup.
Enables or disables the sending process to receive its own routing messages.
Gets or sets the amount of time in seconds between keepalive probes (the default value is 2 hours). It accepts a struct timeval parameter with the number of seconds to wait between the keepalive probes.
Don't delay sending in order to coalesce packets. Under most circumstances, TCP sends data when it's presented. When outstanding data hasn't yet been acknowledged, TCP gathers small amounts of output to be sent in a single packet once an acknowledgment is received.
|For a few clients (such as windowing systems that send a stream of mouse events that receive no replies), this packetization may cause significant delays. Therefore, TCP provides a boolean option, TCP_NODELAY, to defeat this algorithm.|
- An error occurred (errno is set).
- Invalid file descriptor s.
- Value was set out of range.
- The address pointed to by optval isn't in a valid part of the process address space. For getsockopt(), this error may also be returned if optlen isn't in a valid part of the process address space.
- The optval argument can't be NULL; optlen can't be 0.
- The option is unknown at the level indicated.
/etc/protocols in the Utilities Reference