# The OSI & TCP/IP Models

To understand how to secure a network, you must first understand how a network functions. When you type `google.com` into your browser, a complex sequence of events occurs in milliseconds to fetch that webpage.

Network communication is broken down into theoretical layers. The two main models used to describe this are the **OSI Model** (7 layers) and the practical **TCP/IP Model** (4 layers).

## The OSI Model (Open Systems Interconnection)
This is a conceptual framework used to understand and standardize the functions of a telecommunication system.

1.  **Layer 7: Application** - The layer the user interacts with (HTTP, DNS, SMTP, FTP).
2.  **Layer 6: Presentation** - Data translation, encryption (SSL/TLS), and compression.
3.  **Layer 5: Session** - Establishing, maintaining, and terminating "sessions" between applications.
4.  **Layer 4: Transport** - Reliable vs. unreliable delivery. Where TCP and UDP live. Data is packaged into *Segments*. Ports (like port 80 or 443) operate here.
5.  **Layer 3: Network** - IP Addressing and Routing. How data gets from one network to an entirely different network. Data is packaged into *Packets*. Routers operate here.
6.  **Layer 2: Data Link** - MAC Addressing. How data moves between devices on the *same* local network. Data is packaged into *Frames*. Switches operate here.
7.  **Layer 1: Physical** - The actual cables, radio waves (Wi-Fi), fiber optics, and electrical signals representing 1s and 0s.

## The TCP/IP Model
While the OSI model is great for theory, the internet actually runs on the TCP/IP model, which condenses the 7 layers into 4 practical layers:

1.  **Application Layer** (Combines OSI Layers 5, 6, 7): HTTP, DNS, SSH.
2.  **Transport Layer** (Matches OSI Layer 4): TCP, UDP.
3.  **Internet Layer** (Matches OSI Layer 3): IPv4, IPv6, ICMP.
4.  **Network Access Layer** (Combines OSI Layers 1, 2): Ethernet, Wi-Fi, MAC addresses.

## Encapsulation
When you send an email, the data moves *down* the OSI layers on your computer. 
*   Your email app (L7) passing data to the Transport layer (L4), which adds a TCP header.
*   It goes down to the Network layer (L3), which adds an IP header (Source and Dest IP).
*   It goes down to the Data Link layer (L2), which adds a MAC header.
*   It finally goes out the Physical layer (L1) as electricity across the wire.

When the receiving server gets the electrical signals, it reverses the process, moving *up* the layers (De-encapsulation) until the raw email text reaches the Application layer.