Page 295 - DCAP403_Operating System
P. 295
Operating System
Notes links to the BSD socket data structure. This linkage can be seen in Figure 13.14. It links the sock
data structure to the BSD socket data structure using the data pointer in the BSD socket. This
means that subsequent INET socket calls can easily retrieve the sock data structure. The sock
data structure’s protocol operations pointer is also set up at creation time and it depends on the
protocol requested. If TCP is requested, then the sock data structure’s protocol operations pointer
will point to the set of TCP protocol operations needed for a TCP connection.
Creating a BSD Socket
The system call to create a new socket passes identifi ers for its address family, socket type and
protocol.
Firstly the requested address family is used to search the pops vector for a matching address
family. It may be that a particular address family is implemented as a kernel module and, in
this case, the kerneld daemon must load the module before we can continue. A new socket data
structure is allocated to represent the BSD socket. Actually the socket data structure is physically
part of the VFS inode data structure and allocating a socket really means allocating a VFS inode.
This may seem strange unless you consider that sockets can be operated on in just the same way
that ordinary files can. As all files are represented by a VFS inode data structure, then in order to
support file operations, BSD sockets must also be represented by a VFS inode data structure.
The newly created BSD socket data structure contains a pointer to the address family specifi c
socket routines and this is set to the proto_ops data structure retrieved from the pops vector. Its
type is set to the socket type requested; one of SOCK_STREAM, SOCK_DGRAM and so on. The
address family specific creation routine is called using the address kept in the proto_ops data
structure.
A free file descriptor is allocated from the current processes fd vector and the file data structure
that it points at is initialized. This includes setting the file operations pointer to point to the set of
BSD socket file operations supported by the BSD socket interface. Any future operations will be
directed to the socket interface and it will in turn pass them to the supporting address family by
calling its address family operation routines.
Binding an Address to an INET BSD Socket
In order to be able to listen for incoming internet connection requests, each server must create an
INET BSD socket and bind its address to it. The bind operation is mostly handled within the INET
socket layer with some support from the underlying TCP and UDP protocol layers. The socket
having an address bound to cannot be being used for any other communication. This means that
the socket’s state must be TCP_CLOSE. The sockaddr pass to the bind operation contains the IP
address to be bound to and, optionally, a port number. Normally the IP address bound to would
be one that has been assigned to a network device that supports the INET address family and
whose interface is up and able to be used. You can see which network interfaces are currently
active in the system by using the ifconfig command. The IP address may also be the IP broadcast
address of either all 1’s or all 0’s. These are special addresses that mean ``send to everybody’’.
The IP address could also be specified as any IP address if the machine is acting as a transparent
proxy or firewall, but only processes with superuser privileges can bind to any IP address.
The IP address bound to is saved in the sock data structure in the recv_addr and saddr fi elds.
These are used in hash lookups and as the sending IP address respectively. The port number is
optional and if it is not specified the supporting network is asked for a free one. By convention,
port numbers less than 1024 cannot be used by processes without superuser privileges. If the
underlying network does allocate a port number it always allocates ones greater than 1024.
As packets are being received by the underlying network devices they must be routed to the
correct INET and BSD sockets so that they can be processed. For this reason UDP and TCP
288 LOVELY PROFESSIONAL UNIVERSITY