Layers
The four-layer TCP/IP model, encapsulation, and why layers are used.
Do this lesson in the simulatorNetworking is a big problem, so it is split into layers. Each layer does one job, uses the layer below it, and provides a service to the layer above. The web browser does not need to know whether your data goes by Wi-Fi or cable; the Wi-Fi does not need to know it is carrying a web page. This lesson describes the TCP/IP model and shows data being wrapped and unwrapped as it goes down and up the layers.
The TCP/IP model
| Layer | What it does | Protocols |
|---|---|---|
| 4. Application | the programs people use, and the rules for their data | HTTP, HTTPS, FTP, SMTP, POP, IMAP |
| 3. Transport | splits data into packets, numbers them, checks they arrive, and deals with the port for each application | TCP, UDP |
| 2. Internet | adds the source and destination IP addresses, and routes packets between networks | IP |
| 1. Link | sends the data across the physical network, using MAC addresses | Ethernet, Wi-Fi |
Down one side, up the other
When you send data, it goes down the layers. Each layer adds its own header to the front, a label for the matching layer at the other end. This wrapping is called encapsulation. At the receiving device, the data goes up the layers, and each one reads and removes its header.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
data = "GET /battery"
layers = ["TCP 80", "IP 10.0.0.2>10.0.0.9", "WIFI 3C-02>3C-09"]
for header in layers:
data = header + " | " + data # each layer adds its header in front
print(data)
print()
for header in reversed(layers):
data = data.split(" | ", 1)[1] # each layer takes its own header off
print(data)
Why layers?
- Each layer can be designed, built and tested on its own: a smaller problem.
- A layer can be changed without changing the others. Wi-Fi 6 replaced older Wi-Fi without any change to web browsers.
- Different companies can each make one layer, because the rules between the layers are standards.
- It is easier to find a fault: the problem is in one layer.
Task: wrap and unwrap
Write wrap(data, headers) and unwrap(frame). wrap adds each header in headers, in order, in front of the data with | between, printing <layer>: <data so far> after each (starting with application: GET /battery before any header). Send the finished frame by radio. Then unwrap removes the three headers, and you print received by application: <data>.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
data = "GET /battery"
names = ["transport", "internet", "link"]
headers = ["TCP 80", "IP 10.0.0.2>10.0.0.9", "WIFI 3C-02>3C-09"]
Challenges
- Change the link layer header to
ETH 3C-02>3C-09. Which other lines had to change? - A router reads the internet layer header but not the application data. Print just the header a router needs.
- Match each device to the highest layer it reads: switch, router, web browser.