# Unit 5: IP Addressing Scheme & DNS

## 5.1 Network Addressing (IPv4) & Frame Format (IPv4)

### 5.1.1 Internet Protocol (IP)
**Internet Protocol (IP)** is the principal communications protocol in the Internet protocol suite for relaying datagrams across network boundaries. Its routing function enables internetworking, and essentially establishes the Internet.

#### Need for Internet Protocol
- **Logical Addressing:** While MAC addresses (physical addresses) are used for local network communication, IP addresses (logical addresses) are necessary for routing data packets across different networks globally.
- **Routing:** IP provides the mechanism to determine the best path for data to travel from the source to the destination across interconnected networks.
- **Fragmentation and Reassembly:** IP handles the division of large data packets into smaller fragments if the underlying network cannot handle the original packet size, and reassembles them at the destination.

#### Types of IP
There are primarily two versions of the Internet Protocol in use today:
1. **IPv4 (Internet Protocol version 4):** The foundational protocol of the internet, using 32-bit addresses.
2. **IPv6 (Internet Protocol version 6):** The successor to IPv4, designed to solve the address exhaustion problem by using 128-bit addresses.

### 5.1.2 IPv4 Addressing Scheme
An IPv4 address is a 32-bit logical address that uniquely and universally defines the connection of a device (for example, a computer or a router) to the Internet.

#### Address Space
The address space of a protocol is the total number of addresses it can support. Since IPv4 uses 32 bits, its total address space is **2^32**, which equals approximately **4.3 billion** unique addresses.

#### Notations
There are three common notations to represent an IPv4 address:
1. **Binary Notation:** The address is displayed as 32 bits. To make it readable, it is usually divided into four 8-bit sections called octets.
   *Example: 01110101 10010101 00011101 11101010*
2. **Dotted-Decimal Notation:** This is the most common format. Each of the four octets is converted to its decimal equivalent (ranging from 0 to 255) and separated by dots.
   *Example: 117.149.29.234*
3. **Hexadecimal Notation:** Each 8-bit section is converted into a 2-digit hexadecimal number.
   *Example: 75.95.1D.EA*

#### NetID and HostID
Every IP address consists of two parts:
- **Network ID (NetID):** Identifies the specific network to which the host belongs. All devices on the same network share the same NetID.
- **Host ID (HostID):** Uniquely identifies a specific device (host) within that network.

### 5.1.3 Classful Addressing Scheme
In the early days of IPv4, the address space was divided into five classes. This is known as classful addressing. The class of an address determines the boundary between the NetID and the HostID.

1. **Class A:** 
   - **Structure:** 8 bits for NetID, 24 bits for HostID.
   - **Leading bit:** `0`
   - **Range:** 0.0.0.0 to 127.255.255.255
   - **Purpose:** Designed for massive networks with millions of hosts.
2. **Class B:** 
   - **Structure:** 16 bits for NetID, 16 bits for HostID.
   - **Leading bits:** `10`
   - **Range:** 128.0.0.0 to 191.255.255.255
   - **Purpose:** Used for medium-to-large-sized networks.
3. **Class C:** 
   - **Structure:** 24 bits for NetID, 8 bits for HostID.
   - **Leading bits:** `110`
   - **Range:** 192.0.0.0 to 223.255.255.255
   - **Purpose:** Used for small networks (maximum 254 hosts per network).
4. **Class D:** 
   - **Leading bits:** `1110`
   - **Range:** 224.0.0.0 to 239.255.255.255
   - **Purpose:** Reserved for multicasting (sending data to multiple specific destinations simultaneously).
5. **Class E:** 
   - **Leading bits:** `1111`
   - **Range:** 240.0.0.0 to 255.255.255.255
   - **Purpose:** Reserved for future use or experimental purposes.

### 5.1.4 IPv4 Frame Format (Header)
Data sent over the network is encapsulated into IP packets (datagrams). An IPv4 datagram consists of a header and data. The standard length of the IPv4 header is 20 to 60 bytes.

Key fields in the IPv4 Header:
- **Version (4 bits):** Indicates the IP version (value is 4 for IPv4).
- **Header Length (HLEN) (4 bits):** Specifies the length of the header in 32-bit words.
- **Type of Service (ToS) (8 bits):** Defines the priority and quality of service requirements.
- **Total Length (16 bits):** Length of the entire datagram (header + data).
- **Identification, Flags, Fragmentation Offset:** These three fields are used to manage the fragmentation and reassembly of packets.
- **Time to Live (TTL) (8 bits):** Limits the lifespan of a packet to prevent it from looping infinitely in the network.
- **Protocol (8 bits):** Identifies the upper-layer protocol (e.g., TCP, UDP) to which the payload belongs.
- **Header Checksum (16 bits):** Used for error-checking the header.
- **Source IP Address (32 bits):** The IP address of the sender.
- **Destination IP Address (32 bits):** The IP address of the intended recipient.
- **Options (Variable length):** Used for network testing and debugging (optional).

---

## 5.2 Need for IPv6

The rapid expansion of the internet, proliferation of smart devices (IoT), and the limitations of IPv4 necessitated the development of a new protocol: **Internet Protocol version 6 (IPv6)**.

**Primary Needs for IPv6:**
1. **Address Depletion:** IPv4's 4.3 billion addresses were rapidly exhausted due to the exponential growth of internet-connected devices. IPv6 solves this by offering a virtually unlimited address space.
2. **Efficient Routing:** IPv6 simplifies the header format, which reduces the processing overhead on routers and improves packet forwarding efficiency.
3. **Built-in Security:** IPv6 incorporates IPSec (Internet Protocol Security) as a mandatory feature, providing built-in confidentiality, data integrity, and authentication.
4. **No Need for NAT:** Network Address Translation (NAT) was used in IPv4 to share a single public IP among multiple private IPs. With IPv6's massive address space, every device can have a unique, globally routable IP address, restoring end-to-end connectivity.
5. **Better Multicasting:** IPv6 has improved multicast support and introduces "anycast" routing, which allows a packet to be delivered to the nearest of multiple interfaces.

### 5.2.1 IPv6 Address Space
IPv6 uses a **128-bit** address structure, which provides an address space of **2^128** (approximately 3.4 x 10^38) unique addresses.

### 5.2.2 IPv6 Notations and Examples
IPv6 addresses are typically represented in **Hexadecimal Colon Notation**. 
The 128-bit address is divided into eight 16-bit blocks. Each block is converted into a 4-digit hexadecimal number, separated by colons (`:`).

**Example:**
`2001 : 0DB8 : 85A3 : 0000 : 0000 : 8A2E : 0370 : 7334`

#### Abbreviation Rules (Zero Compression)
To make IPv6 addresses shorter and more manageable, certain rules can be applied:
1. **Omit Leading Zeros:** Leading zeros within any 16-bit block can be removed.
   - *Example:* `0DB8` becomes `DB8`, `0000` becomes `0`, `0370` becomes `370`.
   - *Result:* `2001 : DB8 : 85A3 : 0 : 0 : 8A2E : 370 : 7334`

2. **Double Colon (::):** Consecutive blocks of zeros can be replaced by a double colon (`::`). This can only be done **once** in any given IPv6 address to avoid ambiguity.
   - *Result:* `2001:DB8:85A3::8A2E:370:7334`

**More Examples:**
- Uncompressed: `FE80:0000:0000:0000:0202:B3FF:FE1E:8329`
- Compressed: `FE80::202:B3FF:FE1E:8329`

- Uncompressed: `0000:0000:0000:0000:0000:0000:0000:0001` (Loopback Address)
- Compressed: `::1`

---

## 5.3 Comparison between IPv4 and IPv6

| Feature | IPv4 | IPv6 |
| :--- | :--- | :--- |
| **Address Length** | 32 bits | 128 bits |
| **Address Space** | 2^32 (approx. 4.3 billion) | 2^128 (vastly larger) |
| **Notation** | Dotted-decimal (e.g., 192.168.1.1) | Hexadecimal Colon (e.g., 2001:db8::1) |
| **Header Size** | Variable (20 to 60 bytes) | Fixed (40 bytes) |
| **Configuration** | Manual or via DHCP | Supports auto-configuration (SLAAC) and DHCPv6 |
| **IPSec (Security)**| Optional | Built-in / Mandatory |
| **NAT Support** | Crucial to conserve addresses | Not needed due to massive address space |
| **Checksum Field** | Included in header | Removed from header (handled by upper layers) |
| **Fragmentation** | Done by sending host and routers | Done only by the sending host |
| **Broadcasting** | Supported | Not supported (uses Multicast and Anycast instead) |
| **Classes** | Uses Classes A, B, C, D, E | Does not use classes (Classless) |

---

## 5.4 DNS (Domain Name System)

### 5.4.1 Introduction
The **Domain Name System (DNS)** is often referred to as the "phonebook of the Internet." It is a hierarchical and decentralized naming system for computers, services, or any resource connected to the Internet or a private network. Its primary function is to translate human-readable domain names (like `www.google.com`) into machine-readable IP addresses (like `142.250.190.46`).

### 5.4.2 Need of DNS
1. **Human Memory Limitations:** Humans are good at remembering names (words) but poor at memorizing complex numerical sequences. Remembering `wikipedia.org` is much easier than remembering `103.102.166.224`.
2. **Abstraction and Flexibility:** If a website changes its server (and thus its IP address), users don't need to learn a new number. The DNS record is updated to point the same domain name to the new IP address, making the change seamless for users.
3. **Load Balancing:** Large websites use multiple servers with different IP addresses. DNS can return a list of IP addresses for a single domain name, distributing incoming traffic across multiple servers.
4. **Service Locating:** DNS is used not just for web browsing, but also to locate mail servers (MX records) and other essential network services.

### 5.4.3 Domain Names & its Types
A **Domain Name** is a string of text that maps to a numeric IP address, used to access a website from client software. Domain names are organized in a hierarchy from right to left.

#### Domain Name Hierarchy
- **Root Domain:** The highest level, represented by a dot (`.`), though it is usually invisible to users.
- **Top-Level Domain (TLD):** The suffix at the end of the domain name (e.g., `.com`, `.net`, `.org`).
- **Second-Level Domain (SLD):** The specific name registered by an entity, situated directly to the left of the TLD (e.g., `google` in `google.com`).
- **Subdomain (Third-Level):** A further division to the left, used to organize specific sections of a site (e.g., `mail` in `mail.google.com` or `www` in `www.google.com`).

#### Types of Top-Level Domains (TLDs)
The DNS hierarchy categorizes top-level domains into different types based on their purpose:

1. **Generic Top-Level Domains (gTLD):** 
   These are the most common and are theoretically designated for specific purposes, though many are used openly today.
   - `.com` (Commercial businesses)
   - `.org` (Organizations, typically non-profit)
   - `.net` (Network infrastructure, though used widely)
   - `.edu` (Educational institutions)
   - `.gov` (Government agencies)
   - `.mil` (Military organizations)

2. **Country-Code Top-Level Domains (ccTLD):**
   These are two-letter domains designated for specific countries, sovereign states, or dependent territories.
   - `.in` (India)
   - `.uk` (United Kingdom)
   - `.us` (United States)
   - `.au` (Australia)
   - `.jp` (Japan)

3. **Infrastructure Top-Level Domain:**
   There is only one domain in this category, used exclusively for internet infrastructure purposes.
   - `.arpa` (Address and Routing Parameter Area): Used primarily for reverse DNS lookups (translating an IP address back to a domain name).
