The Linux Programming Interface

(nextflipdebug5) #1

1198 Chapter 59


Internet domain datagram sockets are implemented on top of UDP. UDP sockets
are similar to their UNIX domain counterparts, but note the following differences:

z UNIX domain datagram sockets are reliable, but UDP sockets are not—data-
grams may be lost, duplicated, or arrive in a different order from that in which
they were sent.
z Sending on a UNIX domain datagram socket will block if the queue of data for
the receiving socket is full. By contrast, with UDP, if the incoming datagram
would overflow the receiver’s queue, then the datagram is silently dropped.

59.2 Network Byte Order


IP addresses and port numbers are integer values. One problem we encounter
when passing these values across a network is that different hardware architectures
store the bytes of a multibyte integer in different orders. As shown in Figure 59-1,
architectures that store integers with the most significant byte first (i.e., at the lowest
memory address) are termed big endian; those that store the least significant byte
first are termed little endian. (The terms derive from Jonathan Swift’s 1726 satirical
novel Gulliver’s Travels, in which the terms refer to opposing political factions who
open their boiled eggs at opposite ends.) The most notable example of a little-
endian architecture is x86. (Digital’s VAX architecture was another historically
important example, since BSD was widely used on that machine.) Most other archi-
tectures are big endian. A few hardware architectures are switchable between the
two formats. The byte ordering used on a particular machine is called the host byte order.

Figure 59-1: Big-endian and little-endian byte order for 2-byte and 4-byte integers

Since port numbers and IP addresses must be transmitted between, and under-
stood by, all hosts on a network, a standard ordering must be used. This ordering is
called network byte order, and happens to be big endian.
Later in this chapter, we look at various functions that convert hostnames (e.g.,
http://www.kernel.org) and service names (e.g., http) into the corresponding numeric
forms. These functions generally return integers in network byte order, and these
integers can be copied directly into the relevant fields of a socket address structure.

1
(MSB)

0
(LSB)

address
N

address
N + 1
Big-endian
byte order

Little-endian
byte order

0
(LSB)

1
(MSB)

address
N

address
N + 1

3
(MSB)

2

address
N

address
N + 1
10
(LSB)

address
N + 2

address
N + 3

0
(LSB)

1

address
N

address
N + 1
23
(MSB)

address
N + 2

address
N + 3

2-byte integer 4-byte integer

MSB = Most Significant Byte, LSB = Least Significant Byte
Free download pdf