Network Layers

This is an illustration of the network layers involved in requesting a web page.

Extended reading references

User layer

Someone types http://www.cs.unca.edu/tnc into their browser and sees a web page.

Application layer

Client connection setup

Many programming languages provides useful classes and libraries for this operation. For an example, see Java's Socket constructor.

Server connection setup

Again many programming languages provides useful classes and libraries for writing servers, such as Java's ServerSocket class. However, it is rare that it isn't necessary to create a Thread for each client connection.

The HTTP protocol

Now the client and server follow the HTTP protocol (RFC 2616) which specifies in great detail the lines exchanged between the client and server. Even the format of line separators (<CR><LF>) is given.

The client writes a request like the following. There is a blank line after the request.

GET /tcn HTTP/1.1
Host: www.cs.unca.edu

If you use the telnet program to open a connection to port 80 of www.cs.unca.edu you should be able to type in these commands and see the server's response. You will need to type very carefully.

Note that the server's response is not "pure" HTML. It starts with a header. The HTML will follow. The first line of the header has a three-digit status code. Some of these numbers are so well-known that they have been printed on T-shirts or have inspired the names of web sites for UNCA faculty.

Here's the expected header for the client's request.

HTTP/1.1 301 Moved Permanently
Date: Mon, 24 Jan 2011 15:23:26 GMT
Server: Apache/2.2.3 (Red Hat)
Location: http://www.cs.unca.edu/tcn/
Content-Length: 316
Content-Type: text/html; charset=iso-8859-1

A little work for you

A web browser has to work to get the information needed to display the web page. Usually it must download several images, style sheets, etc. It also has to deal with moved or redirected files. Right now, your job is to use telnet, or the more useful and more difficult nc, to get the real web page.

Transport layer

The calls mentioned above comprise the Berkeley socket API. Many of these calls, such as socket, bind, and listen, are implemented by allocating and modifying data structures of the operating system.

Others require hosts to interact over the network. This interaction is implemented by TCP, which is documented in RFC 793.

Sequence numbers

Making the connection

It's all described by a FSM, finite state machine, in RFC 793.

                              +---------+ ---------\      active OPEN  
                              |  CLOSED |            \    -----------  
                              +---------+<---------\   \   create TCB  
                                |     ^              \   \  snd SYN    
                   passive OPEN |     |   CLOSE        \   \           
                   ------------ |     | ----------       \   \         
                    create TCB  |     | delete TCB         \   \       
                                V     |                      \   \     
                              +---------+            CLOSE    |    \   
                              |  LISTEN |          ---------- |     |  
                              +---------+          delete TCB |     |  
                   rcv SYN      |     |     SEND              |     |  
                  -----------   |     |    -------            |     V  
 +---------+      snd SYN,ACK  /       \   snd SYN          +---------+
 |         |<-----------------           ------------------>|         |
 |   SYN   |                    rcv SYN                     |   SYN   |
 |   RCVD  |<-----------------------------------------------|   SENT  |
 |         |                    snd ACK                     |         |
 |         |------------------           -------------------|         |
 +---------+   rcv ACK of SYN  \       /  rcv SYN,ACK       +---------+
   |           --------------   |     |   -----------                  
   |                  x         |     |     snd ACK                    
   |                            V     V                                
   |  CLOSE                   +---------+                              
   | -------                  |  ESTAB  |                              
   | snd FIN                  +---------+                              
   |                   CLOSE    |     |    rcv FIN                     
   V                  -------   |     |    -------                     
 +---------+          snd FIN  /       \   snd ACK          +---------+
 |  FIN    |<-----------------           ------------------>|  CLOSE  |
 | WAIT-1  |------------------                              |   WAIT  |
 +---------+          rcv FIN  \                            +---------+
   | rcv ACK of FIN   -------   |                            CLOSE  |  
   | --------------   snd ACK   |                           ------- |  
   V        x                   V                           snd FIN V  
 +---------+                  +---------+                   +---------+
 |FINWAIT-2|                  | CLOSING |                   | LAST-ACK|
 +---------+                  +---------+                   +---------+
   |                rcv ACK of FIN |                 rcv ACK of FIN |  
   |  rcv FIN       -------------- |    Timeout=2MSL -------------- |  
   |  -------              x       V    ------------        x       V  
    \ snd ACK                 +---------+delete TCB         +---------+
     ------------------------>|TIME WAIT|------------------>| CLOSED  |
                              +---------+                   +---------+

                      TCP Connection State Diagram
                               Figure 6.

There are lots of rules that are needed in case packets are lost, but usually the "three way handshake" creates the connction.

  1. Client sends its sequence number
  2. Server acknowleges client sequence number and send its sequence number
  3. Client acknowleges server sequence number

Reliable data transmission

Both clients and servers use the read and write calls.

TCP header

TCP is IP protocol number 6. The format of the header is given in RFC 793.

    0                   1                   2                   3   
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |          Source Port          |       Destination Port        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                        Sequence Number                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Acknowledgment Number                      |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Data |           |U|A|P|R|S|F|                               |
   | Offset| Reserved  |R|C|S|S|Y|I|            Window             |
   |       |           |G|K|H|T|N|N|                               |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |           Checksum            |         Urgent Pointer        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                             data                              |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                            TCP Header Format

          Note that one tick mark represents one bit position.

                               Figure 3.

OS implementation

Look at /usr/include/netinet/tcp.h on your Linux system for some C data structures used to implement TCP.

A little work for you

Use the netstat command, with the -an option, to look at the active TCP connections on your computer. Then use the ssh command to connect to www.cs.unca.edu and see the active TCP connections there. Also, use the -s option to display network related statistics.

Internetwork layer

The role of the internetwork layer of TCP/IP is to make a good effort to deliver a packet accress several networks. This layer is implemented by the Internet Protocol which is documented in RFC 791. Data flows down/up from IP layer

Actions of IP

In theory, the IP layer should know lots of routes between the networks. In practice, normal computers only need know how to get to their network's routers. However major network routers must lots of routes and routing protocols.

The routing table

Each routing table entry has (at least) three fields. Masking operations are used to determine the next hop for an IP packet.

  1. Network
  2. Network mask
  3. Destination: Gateway or interface

Some networks are supposed to be private and are otften implmented using NAT. The UNCA ResNet is one of these.

IP header

This is copied from RFC 791.

   0                   1                   2                   3   
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |Version|  IHL  |Type of Service|          Total Length         |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |         Identification        |Flags|      Fragment Offset    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |  Time to Live |    Protocol   |         Header Checksum       |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                       Source Address                          |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Destination Address                        |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   |                    Options                    |    Padding    |
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

                    Example Internet Datagram Header

                               Figure 4.

OS implementation

Look at /usr/include/netinet/ip.h on your Linux system for some C data structures used to implement IP.

IPv6

A little work for you

Again use the netstat command, with the -rn option, to look at the routing table for your computer. Continue to use the -s option to display network related statistics.

Also use /sbin/ifconfig to examine the interfaces connected to your machine.

Interface layer

There are many possible hardware interfaces. The most common are Ethernet and WiFi.

Ethernet -- 802.3

Supporting protocols

A quick look/review with older notes