OSI Model

The OSI (Open Systems Interconnection) model is a conceptual framework used to understand how different networking protocols interact to provide communication services in a network. It has 7 layers, and data flows down from the Application Layer to the Physical Layer on the sender's side, and then back up from the Physical Layer to the Application Layer on the receiver's side.

We'll walk through each layer using the example of making an HTTPS request: www.example.com/productid=123.

Layer 7: Application Layer

This is the topmost layer where applications interact with the network. It deals with application-specific protocols like HTTP, HTTPS, FTP, and DNS.

  1. When you type www.example.com/productid=123 in the browser, the Application Layer uses DNS to resolve the domain name (www.example.com) to an IP address (e.g., 192.xx.xxx.xxx).
  2. The Host header in the HTTP request is required to be the domain name (e.g., www.example.com) because this is what the web server expects to know which website or virtual host to serve from a potentially shared server.
  3. Even though the browser now knows the IP address (e.g., 192.xx.xxx.xxx), it still sends the Host header with the domain name, not the IP address.
  4. The HTTP request looks like:
    GET /productid=123 HTTP/1.1
    Host: www.example.com
    

Layer 6: Presentation Layer

This layer ensures that the data is in the correct format for the receiver. It also handles encryption, decryption, and compression.

  1. Encryption (TLS/SSL): Once the DNS resolves the IP, the browser establishes a secure connection using TLS/SSL. The Presentation Layer is responsible for encrypting the HTTP request into ciphertext.
  2. Before encryption, the HTTP request is in plaintext, like:
    GET /productid=123 HTTP/1.1
    Host: www.example.com
    
  3. After encryption, it looks like binary gibberish (ciphertext), for example:
    %X#l23..@1B8&....Lx3
    
  4. If compression is enabled, this layer would also handle data compression.

Layer 5: Session Layer

The Session Layer manages sessions between applications. It establishes, manages, and terminates connections between systems.

  1. While this layer doesn't always have a direct impact in common HTTP(S) requests, it ensures that the communication session between the client and server is established, maintained, and properly closed after the request/response cycle.
  2. In HTTPS, the Session Layer also handles the TLS handshake, ensuring a secure and persistent session.

Layer 4: Transport Layer

Role: The Transport Layer is responsible for end-to-end communication and error recovery. TCP and UDP are the primary protocols used at this layer.

  1. The Application Layer's encrypted HTTP request is handed over to the Transport Layer, where TCP handles the segmentation of data into TCP segments.
  2. Each segment contains a TCP header that includes information like source and destination ports (e.g., port 443 for HTTPS), sequence numbers, and flags.
  3. TCP ensures that data is transmitted reliably by acknowledging received segments and retransmitting lost ones.
  4. A TCP segment might look like this:
    [TCP Header]
    Source Port: 49283
    Destination Port: 443
    Sequence Number: 1
    Acknowledgment Number: 0
    [Encrypted Data]
    

Layer 3: Network Layer

Role: The Network Layer is responsible for addressing and routing data across different networks. The primary protocol at this layer is IP (Internet Protocol).

  1. The Transport Layer's TCP segment is encapsulated in an IP packet.
  2. The IP packet contains source and destination IP addresses (e.g., 192.168.1.10 as the source and 192.xx.xxx.xxx as the destination).
  3. The Network Layer determines the best route for the data to take across networks and ensures that the data reaches the correct destination.
  4. An IP packet might look like this:
    [IP Header]
    Source IP: 192.168.1.10
    Destination IP: 192.xx.xxx.xxx
    Protocol: TCP
    [TCP Segment]
    

Layer 2: Data Link Layer

The Data Link Layer is responsible for local delivery of data on the same network. It deals with MAC addresses (Media Access Control) and ensures that data is transferred over the physical link.

  1. The Network Layer's IP packet is encapsulated in a frame.

  2. The Data Link Layer adds MAC addresses (for local network communication) to the frame and ensures the data can be delivered to the next hop (e.g., router or switch).

  3. An Ethernet frame might look like this:

    [Ethernet Header]
    Source MAC: 00:1A:2B:3C:4D:5E
    Destination MAC: 00:1A:2B:3C:4D:6F
    [IP Packet]
    [Ethernet Trailer]
    

Layer 1: Physical Layer

The Physical Layer is responsible for the actual transmission of bits over a physical medium (e.g., electrical signals over copper cables, light pulses over fiber, or radio waves for wireless).

  1. The Data Link Layer's Ethernet frame is converted into electrical, optical, or radio signals by the Physical Layer for transmission over the network medium.
  2. These signals are transmitted over cables or wireless networks to the destination.