Networking - DHCP and NAT in networking

1. DHCP (Dynamic Host Configuration Protocol)

What is DHCP?

DHCP is a network protocol used to automatically assign IP addresses and other network configurations to devices in a network.

Instead of manually assigning IPs, DHCP automatically gives them to devices when they connect to the network.


How DHCP Works

When your laptop, phone, or printer connects to a network:

  1. DHCP Discover → The device broadcasts a request for an IP.

  2. DHCP Offer → The DHCP server replies with an available IP.

  3. DHCP Request → The device requests the offered IP.

  4. DHCP Acknowledge → The DHCP server confirms the IP assignment.

This process is called DORA.


DHCP DORA Process

Step Message Purpose
1 Discover Client asks for an IP
2 Offer Server offers an IP
3 Request Client accepts the offer
4 Acknowledge Server finalizes assignment

Example

  • DHCP Server IP → 192.168.1.1

  • Device → Needs IP

  • DHCP assigns:

    • IP Address: 192.168.1.100

    • Subnet Mask: 255.255.255.0

    • Default Gateway: 192.168.1.1

    • DNS Server: 8.8.8.8


Advantages of DHCP

  • No need for manual IP configuration.

  • Avoids IP conflicts.

  • Easy to manage large networks.


Diagram: How DHCP Works

Device (Client)             DHCP Server
      |                         |
      | ---- Discover --------> |
      | <---- Offer ----------  |
      | ---- Request --------> |
      | <---- Acknowledge ---- |
      |
   Device gets IP automatically

2. NAT (Network Address Translation)

What is NAT?

NAT is a technique used by routers to convert private IP addresses into public IP addresses when devices access the internet.

Since private IPs cannot be used directly on the internet, NAT allows multiple devices in a local network to share a single public IP.


How NAT Works

Example:

  • Laptop → 192.168.1.10 (private)

  • Mobile → 192.168.1.11 (private)

  • Router → 103.24.67.5 (public)

When your laptop sends a request to www.google.com:

  1. Laptop sends data → Router.

  2. Router replaces the laptop’s private IP with its public IP.

  3. Google replies → Router → Router sends data back to the laptop.


Types of NAT

Type Description Use Case
SNAT (Static NAT) Maps one private IP to one public IP Hosting servers
DNAT (Dynamic NAT) Chooses any available public IP General browsing
PAT (Port Address Translation) / NAT Overload Multiple private IPs share one public IP using different ports Home networks

Example of NAT Table

Private IP Public IP Port
192.168.1.10 103.24.67.5 5501
192.168.1.11 103.24.67.5 5502
192.168.1.12 103.24.67.5 5503

Advantages of NAT

  • Saves public IP addresses.

  • Provides security by hiding internal IPs.

  • Allows multiple devices to use the internet using one public IP.


DHCP vs NAT

Feature DHCP NAT
Purpose Assigns IP addresses automatically Translates private IPs to public IPs
Works On Inside the local network Between local and internet
Where DHCP server / router Router / firewall
Example Gives 192.168.1.100 to a laptop Converts 192.168.1.100103.24.67.5

Diagram: How NAT Works

    Local Network (Private IPs)           Internet (Public IPs)
   ┌──────────┐      ┌──────────┐
   │ Laptop   │      │ Mobile   │
   │192.168.1.10│    │192.168.1.11│
   └─────┬────┘      └─────┬────┘
         |                 |
         |   Router (NAT)  | Public IP: 103.24.67.5
         └───────┬─────────┘
                 |
          Internet Access