DESCRIPTION
IP is the transport layer protocol used by the Internet protocol family. Options may be set at the IP level when using higher-level protocols that are based on IP (such as TCP and UDP). It may also be accessed through a "raw socket" when developing new protocols, or special-purpose applications. There are several IP-level setsockopt(2) and getsockopt(2) options. IP_OPTIONS may be used to provide IP options to be transmitted in the IP header of each outgoing packet or to examine the header options on incoming packets. IP options may be used with any socket type in the Internet family. The format of IP options to be sent is that specified by the IP protocol specification (RFC-791), with one exception: the list of addresses for Source Route options must include the first-hop gateway at the beginning of the list of gateways. The first-hop gateway address will be extracted from the option list and the size adjusted accordingly before use. To disable previously specified options, use a zero-length buffer:
setsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0);
IP_TOS and IP_TTL may be used to set the type-of-service and time-to-live fields in the IP header for SOCK_STREAM, SOCK_DGRAM, and certain types of SOCK_RAW sockets. For example,
int tos = IPTOS_LOWDELAY; /* see <netinet/ip.h> */
setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
int ttl = 60; /* max = 255 */
setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
IP_MINTTL may be used to set the minimum acceptable TTL a packet must have when received on a socket. All packets with a lower TTL are silently dropped. This option is only really useful when set to 255 preventing packets from outside the directly connected networks reaching local listeners on sockets.
IP_DONTFRAG may be used to set the Dont Fragment flag on IP packets. Currently this option is respected only on udp(4) and Raw ip(4) sockets, unless the IP_HDRINCL option has been set. On tcp(4) sockets the Dont Fragment flag is controlled by the Path MTU Discovery option. Sending a packet larger than the MTU size of the egress interface, determined by the destination address, returns an EMSGSIZE error.
If the IP_RECVDSTADDR option is enabled on a SOCK_DGRAM socket, the recvmsg(2) call will return the destination IP address for a UDP datagram. The
.Vt msg_control field in the
.Vt msghdr structure points to a buffer that contains a
.Vt cmsghdr structure followed by the IP address. The
.Vt cmsghdr fields have the following values:
cmsg_len = sizeof(struct in_addr)
cmsg_level = IPPROTO_IP
cmsg_type = IP_RECVDSTADDR
The source address to be used for outgoing UDP datagrams on a socket that is not bound to a specific IP address can be specified as ancillary data with a type code of IP_SENDSRCADDR. The msg_control field in the msghdr structure should point to a buffer that contains a
.Vt cmsghdr structure followed by the IP address. The cmsghdr fields should have the following values:
cmsg_len = sizeof(struct in_addr)
cmsg_level = IPPROTO_IP
cmsg_type = IP_SENDSRCADDR
For convenience, IP_SENDSRCADDR is defined to have the same value as IP_RECVDSTADDR, so the IP_RECVDSTADDR control message from recvmsg(2) can be used directly as a control message for sendmsg(2).
If the IP_ONESBCAST option is enabled on a SOCK_DGRAM or a SOCK_RAW socket, the destination address of outgoing broadcast datagrams on that socket will be forced to the undirected broadcast address, INADDR_BROADCAST, before transmission. This is in contrast to the default behavior of the system, which is to transmit undirected broadcasts via the first network interface with the IFF_BROADCAST flag set.
This option allows applications to choose which interface is used to transmit an undirected broadcast datagram. For example, the following code would force an undirected broadcast to be transmitted via the interface configured with the broadcast address 192.168.2.255:
char msg[512];
struct sockaddr_in sin;
u_char onesbcast = 1; /* 0 = disable (default), 1 = enable */
setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &onesbcast, sizeof(onesbcast));
sin.sin_addr.s_addr = inet_addr("192.168.2.255");
sin.sin_port = htons(1234);
sendto(s, msg, sizeof(msg), 0, &sin, sizeof(sin));
It is the applications responsibility to set the IP_TTL option to an appropriate value in order to prevent broadcast storms. The application must have sufficient credentials to set the SO_BROADCAST socket level option, otherwise the IP_ONESBCAST option has no effect.
If the IP_RECVTTL option is enabled on a SOCK_DGRAM socket, the recvmsg(2) call will return the IP TTL (time to live) field for a UDP datagram. The msg_control field in the msghdr structure points to a buffer that contains a cmsghdr structure followed by the TTL. The cmsghdr fields have the following values:
cmsg_len = sizeof(u_char)
cmsg_level = IPPROTO_IP
cmsg_type = IP_RECVTTL
If the IP_RECVIF option is enabled on a SOCK_DGRAM socket, the recvmsg(2) call returns a
.Vt "struct sockaddr_dl" corresponding to the interface on which the packet was received. The msg_control field in the
.Vt msghdr structure points to a buffer that contains a
.Vt cmsghdr structure followed by the
.Vt "struct sockaddr_dl" . The
.Vt cmsghdr fields have the following values:
cmsg_len = sizeof(struct sockaddr_dl)
cmsg_level = IPPROTO_IP
cmsg_type = IP_RECVIF
IP_PORTRANGE may be used to set the port range used for selecting a local port number on a socket with an unspecified (zero) port number. It has the following possible values: