The OSI Model: Why It Matters
The OSI (Open Systems Interconnection) Model is often taught as boring theory. It’s not. It is the map of the entire internet. When you debug a problem, you are traversing this map.
- “Is the cable plugged in?” -> Layer 1
- “Can I ping the router?” -> Layer 3
- “Is the port open?” -> Layer 4
- “Is the JSON valid?” -> Layer 7
1. The 7 Layers (Top-Down)
| # | Layer | Function | Where it lives | Unit |
|---|---|---|---|---|
| 7 | Application | Network Process to Application | User Space (HTTP, SMTP) | Data |
| 6 | Presentation | Data Formatting, Encryption (SSL/TLS), Compression | User Space (Libraries like OpenSSL) | Data |
| 5 | Session | Dialogue Control, Sync Points | User Space (OS APIs, Sockets) | Data |
| 4 | Transport | End-to-End Reliability, Ports (TCP, UDP) | Kernel Space (TCP Stack) | Segment |
| 3 | Network | Routing, Logical Addressing (IP) | Kernel Space (IP Stack) | Packet |
| 2 | Data Link | Physical Addressing (MAC), Error Detection | NIC Driver / Hardware | Frame |
| 1 | Physical | Binary Transmission, Voltage Levels | Network Card (NIC) / Cable | Bits |
2. Deep Dive: Where do they live?
One of the biggest misconceptions is that these layers are just “theory”. They map to real physical locations in your computer.
Layers 7-5: User Space
This is where your code lives. When you write a Go or Java program, you are operating here.
- Layer 7: Your
http.Get("url")call. - Layer 6: The JSON parser or the GZIP library.
- Layer 5: The socket library managing the connection state.
Layers 4-3: Kernel Space
Once you call write() to a socket, data crosses the System Call Interface into the Operating System (Kernel).
- Layer 4: The OS wraps your data in a TCP Header (Source Port, Dest Port, Seq Number).
- Layer 3: The OS wraps that segment in an IP Header (Source IP, Dest IP, TTL).
Layers 2-1: Hardware (NIC)
The Kernel passes the IP Packet to the Network Interface Card (NIC) driver.
- Layer 2: The NIC adds the MAC Header (Source MAC, Dest MAC) and the FCS (Frame Check Sequence) trailer.
- Layer 1: The NIC’s transceiver converts the bits into electrical signals (Copper) or light pulses (Fiber).
3. Interactive: Encapsulation Studio
Visualize how data gets wrapped in headers as it moves down the stack (Encapsulation) and unwrapped as it moves up (Decapsulation).
4. Code Example: The Headers
To prove this isn’t magic, here is what these headers actually look like in code.
- Go
- Java
package main
// A simplified view of what the Kernel builds
type TCPHeader struct {
SourcePort uint16
DestPort uint16
SeqNum uint32
AckNum uint32
Flags uint8 // SYN, ACK, FIN, etc.
}
type IPHeader struct {
Version uint8 // IPv4 or IPv6
TTL uint8 // Time To Live
Protocol uint8 // TCP (6) or UDP (17)
SourceIP [4]byte
DestIP [4]byte
}
type EthernetFrame struct {
DestMAC [6]byte
SourceMAC [6]byte
EtherType uint16 // IPv4, ARP, etc.
Payload []byte // The IP Packet goes here
FCS uint32 // Checksum
}
public class NetworkHeaders {
// Java representation of headers
static class TCPHeader {
short sourcePort;
short destPort;
int seqNum;
int ackNum;
byte flags; // SYN, ACK, FIN, etc.
}
static class IPHeader {
byte version;
byte ttl;
byte protocol;
byte[] sourceIp = new byte[4];
byte[] destIp = new byte[4];
}
static class EthernetFrame {
byte[] destMac = new byte[6];
byte[] sourceMac = new byte[6];
short etherType;
byte[] payload; // The IP Packet
int fcs;
}
}