HPlogo HP 9000 Networking: BSD Sockets Interface Programmer's Guide > Chapter 3 Advanced Topics for Stream Sockets

Socket Options

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Glossary

 » Index

The operation of sockets is controlled by socket level options. The following options are supported for internet stream sockets:

  • SO_REUSEADDR

  • SO_KEEPALIVE

  • SO_DONTROUTE

  • SO_SNDBUF

  • SO_RCVBUF

  • SO_LINGER

  • SO_USELOOPBACK

  • SO_OOBINLINE

  • SO_SNDLOWAT

  • SO_RCVLOWAT

  • SO_SNDTIMEO

  • SO_RCVTIMEO

  • SO_TYPE

  • SO_ERROR

  • SO_BROADCAST

  • SO_REUSEPORT

All of these options may be used on either AF_INET or AF_UNIX sockets; the following, however, are really INET specific in their function and will not change UNIX socket behavior.

  • SO_KEEPALIVE

  • SO_REUSEADDR

  • SO_DONTROUTE

  • SO_USELOOPBACK

  • SO_OOBINLINE

  • SO_SNDTIMEO

  • SO_RCVTIMEO

  • SO_BROADCAST

  • SO_REUSEPORT

In addition, the SO_DEBUG option is supported for compatibility only; it has no functionality.

Options for protocol levels are described in the individual protocol manual pages, such as tcp(7p), udp(7p), and ip(7p).

The next section describes how to get the current value of a socket option and to set socket options, followed by a description of each available option. Refer to chapter 6 for a description of the SO_BROADCAST option.

Getting and Setting Socket Options

The socket options are defined in the sys/socket.h file. You can get the current status of an option with the getsockopt call, and you can set the value of an option with the setsockopt call.

getsockopt and its parameters are described in the following table:

Include files:

#include <sys/types.h> 
#include <sys/socket.h>

System call:

getsockopt(s, level, optname, optval, optlen) 
int s, level, optname;
char *optval;
int *optlen;

Parameter

Contents

INPUT Value

OUTPUT Value

s

socket descriptor

socket descriptor for which option values are to be returned

unchanged

level

protocol level

SOL_SOCKET

unchanged

optname

name of option

supported option name

unchanged

optval

pointer to current value of option

pointer to buffer where option's current value is to be returned

pointer to buffer that contains current option value

optlen

pointer to length of optval

pointer to maximum number of bytes to be returned by optval

pointer to actual size of optval returned

Function result:

0 if the option is set. If getsockopt fails for any reason, the function returns -1, and the option is not returned. An error code is stored in errno.

Example:

len = sizeof (optval))
getsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, &len;)

optval may never be zero. It must always point to data sent with the socket option and must always be at least the size of an integer.

The following socket options set socket parameter values. optval is an integer containing the new value:

  • SO_SNDBUF

  • SO_RCVBUF

  • SO_SNDLOWAT

  • SO_RCVLOWAT

  • SO_SNDTIMEO

  • SO_RCVTIMEO

The following socket options toggle socket behavior. optval is an integer containing a boolean flag for the behavior (1 = on, 0 = off):

  • SO_KEEPALIVE

  • SO_DEBUG

  • SO_DONTROUTE

  • SO_USELOOPBACK

  • SO_REUSEADDR

  • SO_OOBINLINE

  • SO_REUSEPORT

The SO_LINGER option is a combination. It sets a linger value, and also toggles linger behavior on and off. In previous releases SO_DONTLINGER was supported. For SO_LINGER, optval points to a struct linger, defined in /usr/include/sys/socket.h. The structure contains an integer boolean flag to toggle behavior on/off, and an integer linger value. Refer to the getsockopt(2) man page for more information on getsockopt.

setsockopt and its parameters are described in the following table:

Include files:

#include <sys/types.h> 
#include <sys/socket.h>

System call:

setsockopt(s, level, optname, optval, optlen) 
int s, level, optname;
char *optval;
int optlen;

Parameter

Description of Contents

INPUT Value

s

socket descriptor

socket descriptor for which options are to be set

level

protocol level

SOL_SOCKET

optname

name of option

supported option name

optval

pointer to option input value

Must be at least size of (int). Holds either value to be set or boolean flag

optlen

length of optval

size of optval

Function result:

0 if setsockopt is successful, -1 if failure occurs.

Example:

See the description of the SO_REUSEADDR option for an example.

Refer to the setsockopt(2) man page for more information on setsockopt.

SO_REUSEADDR

This option is AF_INET socket-specific.

SO_REUSEADDR enables you to restart a daemon which was killed or terminated.

This option modifies the rules used by bind to validate local addresses, but it does not violate the uniqueness requirements of an association. SO_REUSEADDR modifies the bind rules only when a wildcard Internet Protocol (IP) address is used in combination with a particular protocol port. The host still checks at connection time to be sure any other sockets with the same local address and local port do not have the same remote address and remote port. Connect fails if the uniqueness requirement is violated.

Example of the SO_REUSEADDR Option

A network daemon server is listening on a specific port: port 2000. If you executed netstat an, part of the output would resemble:

Active connections (including   servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp 0 0 *.2000 *.* LISTEN
Network Daemon Server Listening at Port 2000

When the network daemon accepts a connection request, the accepted socket will bind to port 2000 and to the address where the daemon is running (e.g. 192.6.250.100). If you executed netstat an, the output would resemble:

Active connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp 0 0 192.6.250.100.2000 192.6.250.101.4000 ESTABLISHED
tcp 0 0 *.2000 *.* LISTEN
New Connection Established, Daemon Server Still Listening

Here the network daemon has established a connection to the client (192.6.250.101.4000) with a new server socket. The original network daemon server continues to listen for more connection requests.

If the listening network daemon process is killed, attempts to restart the daemon fail if SO_REUSEADDR is not set. The restart fails because the daemon attempts to bind to port 2000 and a wildcard IP address (e.g. *.2000). The wildcard address matches the address of the established connection (192.6.250.100), so the bind aborts to avoid duplicate socket naming.

When SO_REUSEADDR is set, bind ignores the wildcard match, so the network daemon can be restarted. An example usage of this option is:

int optval = 1;
setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval,
sizeof(optval));
bind (s, &sin, sizeof(sin));

SO_KEEPALIVE

This option is AF_INET socket-specific.

This option enables the periodic transmission of messages on a connected socket. This occurs at the transport level and does not require any work in your application programs.

If the peer socket does not respond to these messages, the connection is considered broken. The next time one of your processes attempts to use a connection that is considered broken, the process is notified (with a SIGPIPE signal if you are trying to send, or an end-of-file condition if you are trying to receive) that the connection is broken.

SO_DONTROUTE

This option is AF_INET socket-specific.

SO_DONTROUTE indicates that outgoing messages should bypass the standard routing facilities. Instead, messages are directed to the appropriate network interface according to the network portion of the destination address.

SO_SNDBUF

SO_SNDBUF changes the send socket buffer size. Increasing the send socket buffer size allows a user to send more data before the user's application will block, waiting for more buffer space.

NOTE: Increasing buffer size to send larger portions of data before the application blocks may increase throughput, but the best method of tuning performance is to experiment with various buffer sizes.

You can increase a stream socket's buffer size at any time, but decrease it only prior to establishing a connection. The maximum buffer size for stream sockets is 262144 bytes. Here is an example:

int result;
int buffsize = 10,000;
result = setsockopt(s, SOL_SOCKET, SO_SNDBUF, &buffsize,
sizeof(buffsize));

SO_RCVBUF

SO_RCVBUF changes the receive socket buffer size.

You can increase a stream socket's buffer size at any time, but decrease it only prior to establishing a connection. The maximum buffer size for stream sockets is 262144 bytes. Here is an example:

int result;
int buffsize = 10,000;
result=setsockopt(s,SOL_SOCKET,SO_RCVBUF,&buffsize,sizeof
(buffsize));

Table 3-1 Summary Information for Changing Socket Buffer Size

Socket Type (Protocol)

stream (TCP)

When Buffer Size Increase Allowed

at any time

When Buffer Size Decrease Allowed

only prior to establishing a connection

Maximum Buffer Size

262144 bytes

 

SO_LINGER

SO_LINGER controls the actions taken when a close is executed on a socket that has unsent data. This option can be cleared by toggling. The default is off.

The linger timeout interval is set with a parameter in the setsockopt call. The only useful values are zero and nonzero:

  • If l_onoff is zero, close returns immediately, but any unsent data is transmitted (after close returns).

  • If l_onoff is nonzero and l_linger is zero, close returns immediately, and any unsent data is discarded.

  • If l_onoff is nonzero and l_linger is nonzero, close does not return until all unsent data is transmitted (or the connection is closed by the remote system).

In the default case (SO_LINGER is off), close is not blocked. The socket itself, however, goes through graceful disconnect, and no data is lost. Here is an example:

int result;
struct linger linger;
linger.l_onoff = 1;
/*0 = off (l_linger ignored), nonzero = on */
linger.l_linger =1;
/*0 = discard data, nonzero = wait for data sent */
result = setsockopt(s, SOL_SOCKET, SO_LINGER, &linger,
sizeof(linger));

Table 3-2 Summary of Linger Options on Close

Socket
Option

Option
Set

Linger
Interval

Graceful
Close

Hard
Close

Wait
for
Close

Does Not
Wait for
Close

SO_LINGER

off

don't care

x

x

SO_LINGER

on

zero

x

x

SO_LINGER

on

nonzero

x

x

 

SO_USELOOPBACK

This option is not applicable to UNIX Domain sockets.

SO_USELOOPBACK directs the network layer (IP) of networking code to use the local loopback address when sending data from this socket. Use this option only when all data sent will also be received locally.

SO_OOBINLINE

This option is not applicable to UNIX Domain sockets.

This option enables receipt of out-of-band data inline. Normally, OOB data is extracted from the data stream and must be read with the MSG_OOB flag specified in the recv() call. When SO_OOBINLINE is specified, OOB data arriving at that socket remains inline and can be read without MSG_OOB specified.

In both cases, a normal read() or recv() which would read past the OOB mark will halt at the mark, instead leaving the OOB byte the next byte to be read.

SO_SNDLOWAT

This option allows the user to set or fetch the low water mark for the socket's send socket buffer. At present, this option is not used. It is supported in anticipation of future use.

SO_RCVLOWAT

This option allows the user to set or fetch the low water mark for the socket's receive socket buffer. At present, this option is not used. It is supported in anticipation of future use.

SO_SNDTIMEO

This option allows the user to set or fetch the timeout value for a socket's send socket buffer.At present, this option is not used. It is supported in anticipation of future use.

SO_RCVTIMEO

This option allows the user to set or fetch the timeout value for the socket's receive socket buffer. At present, this option is not used. It is supported in anticipation of future use.

SO_TYPE

This option is used to return the socket type (e.g., stream, datagram, etc.). Use this option only with the getsockopt system call.

SO_ERROR

This option is used to get and clear any error that has occurred on the socket. Use this option only with the getsockopt system call.

SO_BROADCAST

This option is not supported for UNIX Domain sockets. Setting this option allows the user to send datagrams to a broadcast address. A broadcast address is defined as an internet address whose local address portion is all 1s.

SO_REUSEPORT

This option is AF_INET socket-specific. This option allows multiple processes to share a port. All incoming multicast or broadcast UDP datagrams that are destined for the port are delivered to all sockets that are bound to the port. All processes that share the port must specify this option. For more information on using this option, see "Sending and Receiving IP Multicast Datagrams," in Chapter 5.

© 1997 Hewlett-Packard Development Company, L.P.