Understanding the IPv4 Protocol Stack
The foundation of the modern internet, explained in a detailed and comprehensive way.
The TCP/IP Model: The Internet's Blueprint
To understand IPv4, you must first understand its place within the TCP/IP (Transmission Control Protocol/Internet Protocol) model, which defines how data is exchanged over a network. This model is a four-layer conceptual framework.
Application Layer
Protocols like HTTP, FTP, and SMTP live here. This is where users interact with network services, and data is created and consumed by applications.
Transport Layer
Handles the reliable or unreliable delivery of data between applications. Protocols include TCP (connection-oriented) and UDP (connectionless).
Internet Layer (IPv4)
This is the heart of routing. IPv4 is the primary protocol here, responsible for addressing and routing packets across different networks (inter-networking).
Link Layer
Manages the physical transmission of data over a specific physical medium, such as an Ethernet cable or Wi-Fi signal.
The IPv4 Packet: A Detailed Look
Every piece of data sent over the internet using IPv4 is encapsulated within a packet. The packet header is a critical 20-byte component that contains all the necessary information for a router to forward it.
Version (4 bits)
Specifies the IP version. For IPv4, this value is always 4.
IHL (Header Length) (4 bits)
Indicates the length of the IP header in 32-bit words. The minimum value is 5 (for a 20-byte header).
Total Length (16 bits)
The total size of the entire IP packet (header + data) in bytes. The maximum size is 65,535 bytes.
Identification, Flags & Fragment Offset (32 bits)
These fields are crucial for packet fragmentation. If a packet is too large for a network's maximum transmission unit (MTU), it is split into fragments. These fields allow the destination to correctly reassemble the fragments in the right order.
Time to Live (TTL) (8 bits)
A counter that prevents packets from circulating endlessly. It starts with a value and is decremented by one at each router (hop). When the TTL reaches zero, the packet is discarded.
Protocol (8 bits)
Identifies the protocol of the data payload. For example, 6 for TCP, 17 for UDP, and 1 for ICMP.
Header Checksum (16 bits)
Used to check for header corruption. A router recalculates the checksum at each hop to ensure the header is intact. If it's not, the packet is dropped.
Source and Destination IP Addresses (32 bits each)
The 32-bit addresses that uniquely identify the sending and receiving devices on the network. They are the most important fields for routing.
How Routing Works
The primary job of a router is to forward packets to their correct destination. It does this by inspecting the **destination IP address** in the packet header and consulting its **routing table**.
The Routing Process:
- A router receives an IPv4 packet on one of its interfaces.
 - It reads the destination IP address from the packet header.
 - The router checks its routing table for a matching network address.
 - If a match is found, the router forwards the packet to the next hop (another router or the final destination) as specified in the table.
 - If no match is found, the packet is forwarded to a "default gateway" or dropped.
 
The Addresses: Classes and CIDR
IPv4 addresses are 32-bit numbers, but they're not all created equal. Historically, they were divided into classes, but today, we use a more flexible system called CIDR.
Classful Addressing (Legacy)
IP addresses were originally categorized into classes (A, B, C) based on the first few bits. This defined the network and host portions of the address.
- Class A: For very large networks, e.g., 
10.0.0.0. - Class B: For medium-sized networks, e.g., 
172.16.0.0. - Class C: For small networks, e.g., 
192.168.0.0. 
Classless Inter-Domain Routing (CIDR)
                        CIDR, or "slash notation," is the modern standard. It replaces the fixed class system with a more flexible subnet mask. The slash followed by a number indicates the number of bits used for the network portion of the address. For example, 192.168.1.0/24 means the first 24 bits are the network address.
                    
Conclusion: A Resilient Protocol
IPv4 has been the backbone of the internet for decades, and its robust design has allowed it to scale far beyond what its creators imagined. While IPv6 is the future, understanding the fundamentals of IPv4 remains a cornerstone of networking knowledge.