# DNS, DHCP, and ICMP

These essential network services handle name resolution, address assignment, and network diagnostics.

## DNS (Domain Name System)

DNS translates human-readable domain names to IP addresses. It's like the internet's phone book.

### DNS Hierarchy
```mermaid
graph TD
    A[Root DNS] --> B[.com TLD]
    A --> C[.org TLD]
    A --> D[.io TLD]
    B --> E[example.com]
    E --> F[www.example.com]
    E --> G[api.example.com]
```

### DNS Resolution Process
1. **Local cache check** - Browser/OS cache
2. **Recursive resolver** - ISP or public DNS (8.8.8.8, 1.1.1.1)
3. **Root nameserver** - Points to TLD
4. **TLD nameserver** - Points to authoritative
5. **Authoritative nameserver** - Returns IP address
6. **Response cached** and returned

### Common DNS Record Types

| Record | Purpose | Example |
|---|---|---|
| **A** | IPv4 address | example.com → 93.184.216.34 |
| **AAAA** | IPv6 address | example.com → 2606:2800:... |
| **CNAME** | Canonical name (alias) | www.example.com → example.com |
| **MX** | Mail exchange | example.com → mail.example.com (priority) |
| **TXT** | Text records (SPF, DKIM, verification) | Various purposes |
| **NS** | Nameserver | Delegation info |
| **SOA** | Start of authority | Zone information |
| **PTR** | Reverse DNS (IP → name) | Used for lookups |
| **SRV** | Service location | Service discovery |

### DNS Tools
```bash
# Query DNS
dig example.com
nslookup example.com
host example.com

# Trace DNS resolution
dig +trace example.com

# Check specific record type
dig example.com MX
dig example.com TXT
```

## DHCP (Dynamic Host Configuration Protocol)

DHCP automatically assigns IP addresses, subnet masks, gateways, and DNS servers to devices.

### DHCP DORA Process
```mermaid
sequenceDiagram
    participant Client
    participant Server
    Client->>Server: Discover (broadcast)
    Server->>Client: Offer (unicast/broadcast)
    Client->>Server: Request (broadcast)
    Server->>Client: Acknowledge (unicast/broadcast)
    Note over Client,Server: IP Assigned
```

| Message | Purpose |
|---|---|
| **Discover** | Client broadcasts to find DHCP servers |
| **Offer** | Server offers an IP address |
| **Request** | Client requests the offered address |
| **Acknowledge** | Server confirms lease |

### DHCP Lease
- **Lease time**: How long IP is assigned
- **Renewal (T1)**: At 50% of lease time, client requests renewal
- **Rebinding (T2)**: At 87.5% if renewal fails, broadcasts for any DHCP server

### DHCP Options
Common options include: subnet mask (1), router/gateway (3), DNS servers (6), domain name (15), lease time (51)

## ICMP (Internet Control Message Protocol)

ICMP is used for error reporting and diagnostics. It's a network layer protocol (IP protocol 1).

### Common ICMP Messages

| Type/Code | Message | Use |
|---|---|---|
| **Type 8, Code 0** | Echo Request | ping |
| **Type 0, Code 0** | Echo Reply | ping response |
| **Type 3** | Destination Unreachable | Network/host/port unreachable |
| **Type 11** | Time Exceeded | TTL expired (used by traceroute) |
| **Type 5** | Redirect | Better route exists |

### Diagnostic Tools

```bash
# Ping - test connectivity
ping example.com
ping -c 4 8.8.8.8

# Traceroute/Tracepath - path to destination
traceroute example.com
tracert example.com  # Windows
mtr example.com      # Combined ping+traceroute

# Path MTU discovery
ping -M do -s 1472 example.com
```

### How Traceroute Works
1. Sends UDP packets (or ICMP) with TTL=1
2. First router decrements TTL to 0, sends ICMP Time Exceeded back
3. Increments TTL each time to discover each hop
4. When reaches destination, gets response

## Troubleshooting Scenarios

| Problem | Likely Cause | Tools to Debug |
|---|---|---|
| **Can't resolve names** | DNS issue | dig, nslookup, check /etc/resolv.conf |
| **No IP address** | DHCP issue | Check DHCP server, release/renew (ipconfig/dhclient) |
| **Can ping IP but not name** | DNS resolution failing | Use dig/nslookup |
| **Can't reach remote host** | Routing/ICMP issue | ping, traceroute |
| **Intermittent connectivity** | MTU, routing changes | mtr, packet capture |

## Related Notes

- [[Core-Protocols/IP-TCP-UDP.md]] — Protocol stack
- [[Automation-Observability/Packet-Analysis-Wireshark.md]] — See DNS/DHCP/ICMP
- [[Network-Services/NAT-And-PAT.md]] — DNS in NAT environments
- [[Cloud-Modern-Networking/VPC-Fundamentals.md]] — DNS in cloud

## Sources

* RFC 1034/1035 (DNS)  
* RFC 2131 (DHCP)  
* RFC 792 (ICMP)  
* Kurose & Ross - *Computer Networking*  
