7.6 IPv4 Socket Options
These socket options are processed by IPv4 and
have a level of
IPPROTO_IP. We defer discussion of the multicasting socket
options until Section 21.6.
IP_HDRINCL Socket Option
If this option is set for a raw IP socket
(Chapter
28), we must build our own IP header for all the datagrams we
send on the raw socket. Normally, the kernel builds the IP header
for datagrams sent on a raw socket, but there are some applications
(notably traceroute) that build their own IP header to
override values that IP would place into certain header fields.
When this option is set, we build a complete IP
header, with the following exceptions:
-
IP always calculates and stores the IP header
checksum.
-
If we set the IP identification field to 0, the
kernel will set the field.
-
If the source IP address is INADDR_ANY,
IP sets it to the primary IP address of the outgoing interface.
-
Setting IP options is implementation-dependent.
Some implementations take any IP options that were set using the
IP_OPTIONS socket option and append these to the header
that we build, while others require our header to also contain any
desired IP options.
-
Some fields must be in host byte order, and some
in network byte order. This is implementation-dependent, which
makes writing raw packets with IP_HDRINCL not as portable
as we'd like.
We show an example of this option in Section
29.7. Pages 1056鈥?057 of TCPv2 provide additional details on
this socket option.
IP_OPTIONS Socket Option
Setting this option allows us to set IP options
in the IPv4 header. This requires intimate knowledge of the format
of the IP options in the IP header. We will discuss this option
with regard to IPv4 source routes in Section 27.3.
IP_RECVDSTADDR Socket
Option
This socket option causes the destination IP
address of a received UDP datagram to be returned as ancillary data
by recvmsg. We will show an example of this option in
Section 22.2.
IP_RECVIF Socket Option
This socket option causes the index of the
interface on which a UDP datagram is received to be returned as
ancillary data by recvmsg. We will show an example of this
option in Section 22.2.
IP_TOS Socket Option
This option lets us set the type-of-service
(TOS) field (which contains the DSCP and ECN fields, Figure
A.1) in the IP header for a TCP, UDP, or SCTP socket. If we
call getsockopt for this option, the current value that
would be placed into the DSCP and ECN fields in the IP header
(which defaults to 0) is returned. There is no way to fetch the
value from a received IP datagram.
An application can set the DSCP to a value
negotiated with the network service provider to receive prearranged
services, e.g., low delay for IP telephony or higher throughput for
bulk data transfer. The diffserv architecture, defined in RFC 2474
[Nichols et al. 1998], provides for only limited backward
compatibility with the historical TOS field definition (from RFC
1349 [Almquist 1992]). Application that set IP_TOS to one
of the contents from <netinet/ip.h>, for instance,
IPTOS_LOWDELAY or IPTOS_THROUGHPUT, should
instead use a user-specified DSCP value. The only TOS values that
diffserv retains are precedence levels 6 ("internetwork control")
and 7 ("network control"); this means that applications that set
IP_TOS to IPTOS_PREC_NETCONTROL or
IPTOS_PREC_INTERNETCONTROL will work in a diffserv network.
RFC 3168 [Ramakrishnan, Floyd, and Black 2001]
contains the definition of the ECN field. Applications should
generally leave the setting of the ECN field to the kernel, and
should specify zero values in the low two bits of the value set
with IP_TOS.
IP_TTL Socket Option
With this option, we can set and fetch the
default TTL (Figure A.1) that the
system will use for unicast packets sent on a given socket. (The
multicast TTL is set using the IP_MULTICAST_TTL socket
option, described in Section 21.6.)
4.4BSD, for example, uses the default of 64 for both TCP and UDP
sockets (specified in the IANA's "IP Option Numbers" registry
[IANA]) and 255 for raw sockets. As with the TOS field, calling
getsockopt returns the default value of the field that the
system will use in outgoing datagrams鈥攖here is no way to obtain the
value from a received datagram. We will set this socket option with
our traceroute program in Figure 28.19.
|