RC: W3 D4 — The TCP/IP stack
February 29, 2024Today was “impossible stuff day” at RC which means that we were supposed to work on a task that looks impossible to us. I have absolutely no idea how packets are sent onto the network, so I decided I would reimplement a TCP/IP stack. Of course, at the end of the day, I had not implemented anything (that’s the point of impossible day). But, I learned a ton about computer networking, and that is what I will talk about today!
The TCP/IP stack (or TCP/IP model) is a suite of protocols, organized in layers, that dictate the way data transfer works on the internet. A protocol is a set of rules for formatting data so that devices can process it (i.e. encode and decode it).
To describe how it works, we need to know what the following terms refer to:
- a port allows to identify a specific application on a machine (e.g. port 80 is commonly used for HTTP traffic, directed to web servers).
- an IP address allows to identify a specific machine/computer on either the internet or a local network (e.g. 192.0.2.1)
- a MAC address allows to identify a specific network interface on a device within a local network (e.g. the Ethernet or WiFi adapter’s unique hardware identifier such as 00:1A:C2:9B:00:59).
When data needs to be sent over a network, it will require all of these pieces of information to know where to send the data to. Each layer of the TCP/IP stack is responsible for adding one of these to the message sent.
The different layers (applicative, transport, internet, network) of the TCP/IP stack and the protocols (HTTP, TCP/UDP, IP, LAN) used to process the information passed when sending a message from one machine to another can be represented as follows:
+------+ +------+
| HTTP | | HTTP | Application layer
+------+ +------+
| ^
V |
+------+ +------+
| TCP | | TCP | Transport layer
+------+ +------+
| ^
V |
+------+ +-------------+ +-------------+ +------+
| IP | | IP | | IP | | IP | Internet layer
+------+ +-------------+ +-------------+ +------+
| ^ | ^ | ^
V | V | V |
+------+ +-----+ +-----+ +-----+ +-----+ +------+
| LAN | --> | LAN | | LAN | --> | LAN | | LAN | --> | LAN | Network layer
+------+ +-----+ +-----+ +-----+ +-----+ +------+
This is what happens at each stage:
- Application layer: Encodes the message. For example, if this is a web application, when a user enters a URL, it could be translated into a GET request (that is encoded in bytes).
- Transport layer: Adds the source and destination port address so that the message is sent to the correct service or application. It also adds a message number and a flag used to track if the message was received or not, and more generally handles packet fragmentation.
- Internet layer: Adds the source and destination IP addresses to route the message to the correct machine.
- Network layer: Adds the source and destination MAC addresses to route the message to the next router or destination machine.
After adding all of these bits of information, here is what the data sent looks like:
<-------------------------------- FRAME -------------------------------->
<---------------------- PACKET ---------------------->
<------------ SEGMENT ------------->
+------------------+-----------------+----------------+-----------------+
| Dest MAC address | Dest IP address | Dest Port | Encoded message |
| Src MAC address | Src IP address | Src Port | |
| | | Message Number | |
| | | Flag | |
+------------------+-----------------+----------------+-----------------+
^ ^ ^ ^
| | | |
Added by the Added by the Added by the Added by the
Network Internet Transport Applicative
layer layer layer layer
The packet (message + source/destination port + source/destination addresses) remains the same all along the transfer. However, the part corresponding to the source and destination MAC address changes at every “hop”: every time a new router is reached, it removes this part, reads the destination IP address, searches for the next router that will take the packet closer to its destination, and adds the new destination MAC address (i.e. the one of the next router), as well its own MAC address as source.
That’s already long enough, so that will be all for today! I didn’t know any of this before this morning, so I am pretty happy about all that I learned today (even though there are still some holes in my understanding). Hopefully, in the future, I will be able to implement this somehow!