Loading W Code...
Open Systems Interconnection - 7 Layer Architectural Model
Mnemonic (Bottom to Top): "Please Do Not Throw Sausage Pizza Away" (Physical, Data Link, Network, Transport, Session, Presentation, Application)
Data Unit: Data
Protocols
HTTP, HTTPS, FTP, SMTP, DNS, DHCP, SSH
Devices
Gateways, Next-Gen Firewalls
Think of the Application Layer as an executive at a desk. The executive does not need to know how delivery trucks or cargo planes operate; they simply interact with their email client or web browser to initiate a transaction.
The Application Layer sits at the top of the OSI stack. It provides standard communication protocols directly to end-user software applications.
// HTTP/1.1 Request Payload Example
GET /api/v1/user/profile HTTP/1.1
Host: api.wcode.edu
User-Agent: WCodeClient/2.0 (Linux x86_64)
Authorization: Bearer eyJhbGciOiJIUzI1Ni...
Accept: application/json
// Server HTTP/1.1 Response Payload
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 84
{"status": "success", "user": {"id": 402, "role": "student", "enrolled": true}}Data Unit: Data
Protocols
SSL/TLS, JPEG, PNG, MPEG, ASCII, UTF-8
Devices
Application Gateway
Imagine a diplomatic translator and security agent. If a document arrives written in a foreign format, the translator converts it into the native language. If the message is confidential, the agent encrypts it before sending and decrypts it upon arrival.
The Presentation Layer acts as the network's syntax and formatting engine, ensuring data sent by one system is readable by another.
// SSL/TLS Session Cipher & Serialization Example (C / OpenSSL)
#include <openssl/ssl.h>
#include <openssl/err.h>
void initialize_tls_session() {
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
SSL_CTX *ctx = SSL_CTX_new(TLS_client_method());
SSL *ssl = SSL_new(ctx);
// Symmetric AES-256-GCM Session Established
}Data Unit: Data
Protocols
NetBIOS, RPC, SOCKS, WebSockets, PPTP
Devices
Session Border Controller, Gateway
Imagine a telephone operator managing a conference call. The operator places the call, ensures both callers can speak without cutting each other off, and automatically reconnects the line if there is a brief disconnect.
The Session Layer establishes, maintains, synchronizes, and terminates long-lived communication channels between applications.
// POSIX Session Keep-Alive & RPC Control
#include <sys/socket.h>
#include <netinet/tcp.h>
int optval = 1;
// Enable TCP Keep-Alive probes at Session Layer boundary
setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval));
// Set Keep-Alive Probe Parameters (Idle: 60s, Interval: 10s, Count: 3)
int idle = 60, interval = 10, count = 3;
setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle));
setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(interval));Data Unit: Segment (TCP) / Datagram (UDP)
Protocols
TCP, UDP, SCTP, QUIC
Devices
Firewall, Load Balancer, Gateway
Imagine a registered courier service vs. a fast flyer drop. The registered courier (TCP) numbers every parcel, requests a signed receipt for each one, and re-delivers lost packages. The flyer drop (UDP) tosses packets quickly without waiting for confirmation.
The Transport Layer manages process-to-process delivery across the network using port numbers.
80/443 for Web, Port 22 for SSH).// C POSIX TCP Socket Client Connection
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int client_fd = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in serv_addr = {
.sin_family = AF_INET,
.sin_port = htons(443) // Destination Port 443 (HTTPS)
};
inet_pton(AF_INET, "104.21.32.88", &serv_addr.sin_addr);
// Executes TCP 3-Way Handshake (SYN -> SYN-ACK -> ACK)
connect(client_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));Data Unit: Packet
Protocols
IPv4, IPv6, ICMP, OSPF, BGP, IPsec
Devices
Router, Layer 3 Switch
Imagine the International Postal Routing System. When you post a letter to another country, local post offices inspect the destination country code and postal ZIP code (IP Address) to choose the best cargo flight or highway route.
The Network Layer handles logical addressing and packet routing across multiple interconnected networks.
| Field Name | Bit Width | Purpose |
|---|---|---|
| Version & IHL | 8 bits | IP protocol version (IPv4=4) and Header Length |
| Total Length | 16 bits | Complete packet size (Header + Payload) in bytes |
| Identification & Flags | 32 bits | Packet fragmentation tracking and Don't Fragment (DF) flags |
| Time to Live (TTL) | 8 bits | Hop counter decremented by routers to prevent infinite loops |
| Protocol | 8 bits | Identifies upper layer protocol (6=TCP, 17=UDP, 1=ICMP) |
| Source / Destination IP | 64 bits | 32-bit Origin and Target IPv4 addresses |
// Linux Kernel IPv4 Header C Structure (netinet/ip.h)
struct iphdr {
unsigned int ihl:4; // IP Header Length
unsigned int version:4; // Version (IPv4)
uint8_t tos; // Type of Service
uint16_t tot_len; // Total Packet Length
uint16_t id; // Packet Identification ID
uint16_t frag_off; // Fragment Offset & Flags
uint8_t ttl; // Time to Live (Hop limit)
uint8_t protocol; // Transport Protocol (TCP=6, UDP=17)
uint16_t check; // IP Header Checksum
uint32_t saddr; // Source IP Address
uint32_t daddr; // Destination IP Address
};Data Unit: Frame
Protocols
Ethernet (802.3), Wi-Fi (802.11), ARP, PPP
Devices
Network Switch, Bridge, NIC
Imagine a local courier delivery van. Once a parcel arrives at your city's regional hub, the local van uses your physical street door plate number (MAC Address) to hand-deliver the parcel directly to your doorstep.
The Data Link Layer manages error-free transmission of data frames between two directly connected nodes on the same physical local network (LAN).
| Field | Preamble & SFD | Destination MAC | Source MAC | EtherType | Payload Data | CRC Checksum |
|---|---|---|---|---|---|---|
| Length | 8 Bytes | 6 Bytes | 6 Bytes | 2 Bytes | 46 – 1500 Bytes | 4 Bytes (FCS) |
// Linux Kernel Ethernet II Frame Header (net/ethernet.h)
struct ether_header {
uint8_t ether_dhost[6]; // Destination MAC (e.g. 00:1A:2B:3C:4D:5E)
uint8_t ether_shost[6]; // Source MAC (e.g. AA:BB:CC:11:22:33)
uint16_t ether_type; // EtherType (0x0800 = IPv4, 0x86DD = IPv6, 0x0806 = ARP)
} __attribute__((packed));Data Unit: Bits
Protocols
100BASE-TX, 1000BASE-T, USB, Bluetooth, RS-232
Devices
Network Hub, Repeater, Network Cables, Optical Transceivers
Imagine the actual copper wire, optical glass fiber filaments, or radio frequency waves carrying electrical voltage pulses or light flashes down the highway.
The Physical Layer is the bottom layer of the OSI stack. It is responsible for transmitting raw, unformatted binary bits (0s and 1s) over physical communication media.
0 and 1 are converted into electrical voltages, optical pulses, or radio frequencies (e.g., Manchester encoding).| Cable Type | Max Throughput | Max Segment Distance | Electromagnetic Immunity |
|---|---|---|---|
| Cat6a Twisted Pair | 10 Gbps | 100 Meters | Moderate (Copper Wire) |
| Single-Mode Fiber | 100+ Gbps | 40 Kilometers | Immune (Optical Light) |
| Wi-Fi 6E (802.11ax) | 9.6 Gbps | ~50 Meters | High (Radio Frequency) |
# Querying Physical Hardware Link Properties via ethtool
$ ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 1000baseT/Full 10000baseT/Full
Speed: 10000Mb/s
Duplex: Full
Auto-negotiation: on
Link detected: yesData Encapsulation Flow: Data (L7-L5) → Segment (L4) → Packet (L3) → Frame (L2) → Bits (L1)
Hardware Mapping: Router = Network Layer (L3), Switch = Data Link Layer (L2), Hub/Cable = Physical Layer (L1)
OSI vs TCP/IP: OSI is a 7-layer reference model; TCP/IP is a practical 4-layer protocol suite powering the Internet.