BSD Sockets

Generally speaking, any operating system must define a common Application Programming Interface (API) between the User Mode program and the networking code. The Linux networking API is based on BSD sockets. They were introduced in Berkeley's Unix 4.1cBSD and are available in almost all Unix-like operating systems, either natively or by means of a User Mode helper library. HI

[!] An alternative API between User Mode programs and networking code is provided by the Transport Layer Interface (TLI), introduced by System V Release 3.0. In general, TLI is implemented as a User Mode library that uses the STREAMS I/O subsystem. As mentioned in Section 1.1, the Linux kernel does not implement the STREAMS I/O subsystem.

A socket is a communication endpoint — the terminal gate of a channel that links two processes. Data is pushed on a terminal gate, and after some delay, shows up at the other gate. The communicating processes may be on different computers; it's up to the kernel's networking code to forward the data between the two endpoints.

Linux implements BSD sockets as files that belong to the sockfs special filesystem (see Section 12.3.1). More precisely, for every new BSD socket, the kernel creates a new inode in the sockfs special filesystem. The attributes of the BSD socket are stored in a socket data structure, which is an object included in the filesystem-specific u.socket_i field of the sockfs's inode.

The most important fields of the BSD socket object are:

inode

Points to the sockfs's inode object file

Points to the file object of the sockfs's file state

Stores the connection status of the socket: ss_free (not allocated), ss_unconnected (not yet connected), ss_connecting (in process of connecting), ss_connected (connected), ss_disconnecting (in process of disconnecting).

Points to a proto_ops data structure, which stores the methods of the socket object; they are listed in Table 18-2. Most of the methods refer to system calls that operate on sockets. Each network architecture implements the methods by means of its own functions; hence, the same system call acts differently according to the networking architecture to which the target socket belongs.

Table 18-2. The methods of the BSD socket object

Method

Description

release

Close the socket

bind

Assign a local address (a name)

connect

Either establish a connection (TCP) or assign a remote address (UDP)

socketpair

Create a pair of sockets for two-way data flow

accept

Wait for connection requests

getname

Return the local address

ioctl

Implement ioctl( )'s commands

listen

Initialize the socket to accept connection requests

shutdown

Close a half or both halves of a full-duplex connection

setsockopt

Set the value of the socket flags

getsockopt

Get the value of the socket flags

sendmsg

Send a packet on the socket

recvmsg

Receive a packet from the socket

mmap

File memory-mapping (not used by network sockets)

sendpage

Copy data directly from/to a file (sendfile( ) system call)

sk sk

Points to the low-level struct sock socket descriptor (see the next section). 18.1.4 INET Sockets

INETsockets are data structures of type struct sock. Any BSD socket that belongs to the IPS network architecture stores the address of an INET socket in the sk field of the socket object.

INET sockets are required because the socket objects (describing BSD sockets) include only fields that are meaningful to all network architectures. However, the kernel must also keep track of several other bits of information for any socket of every specific network architecture. For instance, in each INET socket, the kernel records the local and remote IP addresses, the local and remote port numbers, the relative transport protocol, the queue of packets that were received from the socket, the queue of packets waiting to be sent to the socket, and several tables of methods that handle the packets traveling on the socket. These attributes are stored, together with many others, in the INET socket.

The INET socket object also defines some methods specific to the type of transport protocol adopted (TCP or UDP). The methods are stored in a data structure of type proto and are listed in Table 18-3.

Table 18-3. The methods of the INET socket object

Method

Description

close

Close the socket

connect

Either establish a connection or assign a remote address

disconnect

Relinquish an established connection

accept

Wait for connection request

ioctl

Implement ioctl( )'s commands

init

INET socket object constructor

destroy

INET socket object destructor

shutdown

Close a half or both halves of a full-duplex connection

setsockopt

Set the value of the socket flags

getsockopt

Get the value of the socket flags

sendmsg

Send a packet on the socket

recvmsg

Receive a packet from the socket

bind

Assign a local address (a name)

backlog rcv

Callback function invoked when receiving a packet

hash

Add the INET socket to the per-protocol hash table

unhash

Remove the INET socket from the per-protocol hash table

As you may notice, many methods replicate the methods of the BSD socket object (Table 18-2). Actually, a BSD socket method usually invokes the corresponding INET socket method, if it is defined.

The sock object includes no less than 80 fields; many of them are pointers to other objects, tables of methods, or other data structures that deserve a detailed description by themselves. Rather than including a boring list of field names, we introduce a few fields of the sock object whenever we encounter them in the rest of the chapter.

Was this article helpful?

0 0

Post a comment