Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 12: Networks


communication. For example, only TCP (for streams) or UDP (for datagram services) can be used
as the transport protocol for a socket to which an Internet address such as192.168.1.20has been
assigned.


Sockets are generated using thesocketlibrary function, which communicates with the kernel via a
system call discussed in Section 12.10.3. A third argument could be used in addition to address family
and communication type (stream or datagram) in order to select a protocol; however, as already stated,
this is not necessary because the protocol is uniquely defined by the first two parameters. Specifying 0
for the third argument instructs the function to use the appropriate default.


Once thesocketfunction has been invoked, it is clear what the format of the socket address must be (or
in which address family it resides), but no local address has yet been assigned to it.

Thebindfunction to which asockaddr_typestructure must be passed as an argument is used for this
purpose. The structure then defines the address. Because address types differ from address family to
address family, there is a different version of the structure for each family so that various requirements
can be satisfied.typespecifies the desired address type.


Internet addresses are uniquely identified by IP number and port number, which is whysockaddr_inis
defined as follows:


<in.h>
struct sockaddr_in {
sa_family_t sin_family; /* Address family */
__be16 sin_port; /* Port number */
struct in_addr sin_addr; /* Internet address */
...
}

An IP address and a port number are also needed in addition to the address family (here,AF_INET).


The IP address is not expected in the usual dotted decimal notation (four numbers
separated by dots, i.e.,192.168.1.10), but must be specified as a number. The
inet_atonlibrary function converts an ASCII string into the format required by the
kernel (and by the C library). For example, the numeric representation of the
address192.168.1.20is 335653056. It is generated by writing the 1-byte-long
sections of the IP address successively into a 4-byte data type that is then interpreted
as a number. This permits the unique conversion of both representations.

As stated in Chapter 1, CPUs apply two popular conventions for storing numeric values — little and
big endian. An explicitnetwork byte ordercorresponding to the big endian format has been defined to
ensure that machines with different byte arrangements are able to communicate with each other easily.
Numeric values appearing in protocol headers must therefore always be specified in this format. The
fact that both the IP address and the port number consist only of numbers must be taken into account
when defining the values in thesockaddr_instructure. The C library features numerous functions for
converting numbers from the native format of the CPU to the network byte order (if the CPU and the
network have the same byte order, the functions leave it unchanged). Good network applications always
use these functions even if they are developed on big endian machines to ensure that they can be ported
to different machine types.


To represent little and big endian types explicitly, the kernel provides several data types.be16,be32,
andbe64represent big endian numbers with 16, 32, and 64 bits, while the variants with prefixleare

Free download pdf