> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/networking-fundamentals/ip-address-internet-protocol-address.md).

# IP Address (Internet Protocol Address)

An IP address is a unique identifier for devices on a network, not for the network itself. Each device connected to a network is assigned a unique IP address, which is used to identify and communicate with that device.

***

### **Types of IP Addresses:-** <a href="#id-431d" id="id-431d"></a>

1. **IPv4 (Internet Protocol version 4)**
2. **IPv6 (Internet Protocol version 6)**

***

## **IPv4 (Internet Protocol version 4)**

**IPv4** (Internet Protocol version 4) is the fourth version of the Internet Protocol, which is widely used for identifying devices on a network.&#x20;

* **Structure**: 32-bit address.
* **Sections**: 4 sections, each 8 bits.
* **Range per section**: 0 to 255.
* **Address range**: `0.0.0.0` to `255.255.255.255`.
* **Total addresses**: (2^32 = 4,294,967,296).

### **Structure of IPv4 Address**

1. **Bit Size**:
   * IPv4 uses **32 bits** to represent an IP address.
   * This is a fixed length, meaning every IPv4 address is 32 bits long.
2. **Sections (Octets)**:
   * IPv4 addresses are divided into **4 sections**, each consisting of **8 bits**.
   * Each section is separated by a dot (`.`).
3. **Value Range for Each Section**:
   * Each 8-bit section can store values ranging from **0 to 255** (decimal).
   * This is because the maximum value of 8 bits in binary is `11111111`, which equals **255** in decimal.

**Address Range:**

* **Smallest IPv4 Address**: `00000000.00000000.00000000.00000000` (Binary) = `0.0.0.0` (Decimal).
* **Largest IPv4 Address**: `11111111.11111111.11111111.11111111` (Binary) = `255.255.255.255` (Decimal).

#### **Example: IPv4 Address**

Let’s convert the binary IPv4 address **`11000000.10101000.00000001.00000001`** into decimal.

#### **Explanation**

1. Each bit in the binary number represents a power of 2, starting from (2^0) (rightmost bit) to (2^7) (leftmost bit) for an 8-bit binary number.
2. Multiply each bit by its corresponding power of 2.
3. Add the results of all bits to get the decimal value of the binary number.

***

**Step 1: Break into Octets**

The binary address consists of four 8-bit sections:

* Octet 1: `11000000`
* Octet 2: `10101000`
* Octet 3: `00000001`
* Octet 4: `00000001`

***

**Step 2: Convert Each Octet**

| **Bit Position**       | (2^7) (128) | (2^6) (64) | (2^5) (32) | (2^4) (16) | (2^3) (8) | (2^2) (4) | (2^1) (2) | (2^0) (1) | **Decimal Value**    |
| ---------------------- | ----------- | ---------- | ---------- | ---------- | --------- | --------- | --------- | --------- | -------------------- |
| **Octet 1 (11000000)** | 1           | 1          | 0          | 0          | 0         | 0         | 0         | 0         | (128 + 64 = 192)     |
| **Octet 2 (10101000)** | 1           | 0          | 1          | 0          | 1         | 0         | 0         | 0         | (128 + 32 + 8 = 168) |
| **Octet 3 (00000001)** | 0           | 0          | 0          | 0          | 0         | 0         | 0         | 1         | (1 = 1)              |
| **Octet 4 (00000001)** | 0           | 0          | 0          | 0          | 0         | 0         | 0         | 1         | (1 = 1)              |

***

**Step 3: Combine the Decimal Values**

Now, write the decimal values of all four octets together to get the full IPv4 address:

`[ 11000000.10101000.00000001.00000001 = 192.168.1.1 ]`

***

### **Classful and Classless IP Addressing**

IP addressing can follow two models: **Classful** (fixed structure) and **Classless** (flexible structure). Below is an in-depth explanation with examples.

***

#### **Classful IP Addressing**

In **classful addressing**, IP addresses are divided into fixed classes: A, B, C, D, and E. Each class has a predefined number of **network bits** and **host bits**, limiting the number of available networks and hosts per network.

#### **Class A**

**Purpose:** Reserved for governments, large corporations, and ISPs requiring very large networks.

**Binary Range:**

```
0 0000000 . 00000000 . 00000000 . 00000000 (0.0.0.0)  
to  
0 1111111 . 11111111 . 11111111 . 11111111 (127.255.255.255)  
```

The first bit is always `0`.

**IP Range:**

```
0.0.0.0 to 127.255.255.255
```

**Structure:**

```
OFFNNNNNNN . HHHHHHHH . HHHHHHHH . HHHHHHHH  
N.H.H.H  
```

* `N`: Network bits (7 bits)
* `H`: Host bits (24 bits)

**Network and Host Bits:**

* **Network Bits:** 7
* **Host Bits:** 24

**Number of Networks:**

```
2^7 - 2 = 126  
(0 is reserved for default routing, 127 is reserved for loopback testing)
```

**Number of Hosts per Network:**

```
2^24 - 2 = 16,777,214  
(Subtract 2 for network and broadcast addresses)
```

**Decimal Octet | Binary Representation | Explanation:**

| Decimal Octet | Binary Representation | Explanation                |
| ------------- | --------------------- | -------------------------- |
| 0             | 00000000              | Starting range for Class A |
| 127           | 01111111              | Ending range for Class A   |
| Remaining     | HHHHHHHH              | Host portion (24 bits)     |

***

#### **Class B**

**Purpose:** Designed for medium-sized networks like multinational companies.

**Binary Range:**

```
10 000000 . 00000000 . 00000000 . 00000000 (128.0.0.0)  
to  
10 111111 . 11111111 . 11111111 . 11111111 (191.255.255.255)  
```

The first two bits are always `10`.

**IP Range:**

```
128.0.0.0 to 191.255.255.255
```

**Structure:**

```
ONOFFNNNNNN . HHHHHHHH . HHHHHHHH . HHHHHHHH  
N.N.H.H  
```

* `N`: Network bits (14 bits)
* `H`: Host bits (16 bits)

**Network and Host Bits:**

* **Network Bits:** 14
* **Host Bits:** 16

**Number of Networks:**

```
2^14 = 16,384  
```

**Number of Hosts per Network:**

```
2^16 - 2 = 65,534  
(Subtract 2 for network and broadcast addresses)
```

**Decimal Octet | Binary Representation | Explanation:**

| Decimal Octet | Binary Representation | Explanation                |
| ------------- | --------------------- | -------------------------- |
| 128           | 10000000              | Starting range for Class B |
| 191           | 10111111              | Ending range for Class B   |
| Remaining     | HHHHHHHH              | Host portion (16 bits)     |

***

#### **Class C**

**Purpose:** Allocated for small companies with smaller networks.

**Binary Range:**

```
110 00000 . 00000000 . 00000000 . 00000000 (192.0.0.0)  
to  
110 11111 . 11111111 . 11111111 . 11111111 (223.255.255.255)  
```

The first three bits are always `110`.

**IP Range:**

```
192.0.0.0 to 223.255.255.255
```

**Structure:**

```
ONONOFFNNNNN . HHHHHHHH . HHHHHHHH . HHHHHHHH  
N.N.N.H  
```

* `N`: Network bits (21 bits)
* `H`: Host bits (8 bits)

**Network and Host Bits:**

* **Network Bits:** 21
* **Host Bits:** 8

**Number of Networks:**

```
2^21 = 2,097,152  
```

**Number of Hosts per Network:**

```
2^8 - 2 = 254  
(Subtract 2 for network and broadcast addresses)
```

**Decimal Octet | Binary Representation | Explanation:**

| Decimal Octet | Binary Representation | Explanation                |
| ------------- | --------------------- | -------------------------- |
| 192           | 11000000              | Starting range for Class C |
| 223           | 11011111              | Ending range for Class C   |
| Remaining     | HHHHHHHH              | Host portion (8 bits)      |

***

#### **Class D**

**Purpose:** Reserved for multicast communication (e.g., streaming services).

**Binary Range:**

```
1110 0000 . 00000000 . 00000000 . 00000000 (224.0.0.0)  
to  
1110 1111 . 11111111 . 11111111 . 11111111 (239.255.255.255)  
```

The first four bits are always `1110`.

**IP Range:**

```
224.0.0.0 to 239.255.255.255
```

**Structure:**\
No network/host division; used for group communication.

***

#### **Class E**

**Purpose:** Reserved for experimental and future use.

**Binary Range:**

```
1111 0000 . 00000000 . 00000000 . 00000000 (240.0.0.0)  
to  
1111 1111 . 11111111 . 11111111 . 11111111 (255.255.255.255)  
```

The first four bits are always `1111`.

**IP Range:**

```
240.0.0.0 to 255.255.255.255
```

**Structure:**\
Reserved for future use or research purposes.

***

#### **Summary Table**

| **Class** | **IP Range**                | **Network Bits** | **Host Bits** | **Networks** | **Hosts/Network**          |
| --------- | --------------------------- | ---------------- | ------------- | ------------ | -------------------------- |
| A         | 0.0.0.0 - 127.255.255.255   | 7                | 24            | 126          | 16,777,214                 |
| B         | 128.0.0.0 - 191.255.255.255 | 14               | 16            | 16,384       | 65,534                     |
| C         | 192.0.0.0 - 223.255.255.255 | 21               | 8             | 2,097,152    | 254                        |
| D         | 224.0.0.0 - 239.255.255.255 | Multicast        | Multicast     | Multicast    | Multicast                  |
| E         | 240.0.0.0 - 255.255.255.255 | Experimental     | Experimental  | Experimental | <p></p><p>Experimental</p> |

***

### Private and Public IPs

**Private IPs**

* Used for internal communication in LAN.
* Free of cost.
* Cannot access the internet directly.
* **Ranges**:
  * **Class A**: `10.0.0.0` – `10.255.255.255`
  * **Class B**: `172.16.0.0` – `172.31.255.255`
  * **Class C**: `192.168.0.0` – `192.168.255.255`

**Public IPs**

* Used to communicate over the internet.
* Assigned by ISPs and not free.
* **Ranges**:
  * **Class A**: `1.0.0.0` – `126.255.255.255`
  * **Class B**: `128.0.0.0` – `191.255.255.255`
  * **Class C**: `192.0.0.0` – `223.255.255.255`

***

### **Principles for Assigning IPs**

1. Never assign IPs without enabled network or host bits (e.g., `0.0.0.0`).
2. Avoid IPs where no host bits are enabled (e.g., `1.0.0.0`).
3. Avoid IPs with all host bits enabled (e.g., `1.255.255.255`).
4. Ensure IPs are unique within a network.

***

## **Subnet Mask / Netmask / Network Mask**

A **Subnet Mask** (also called **Netmask** or **Network Mask**) is used in IP networking to divide an IP address into **network** and **host** portions. It tells which part of the IP address identifies the **network** and which part identifies the **device (host)**.

***

### **Network Mask Structure**

* **Total Bits**: 32 bits (just like an IPv4 address)
* **Sections**: 4 sections (octets)
* **Bits per Section**: 8 bits each
* **Representation**: Binary or Decimal (e.g., `11111111.00000000.00000000.00000000` = `255.0.0.0`)
* **Binary Pattern**:
  * **Network part**: All bits are `1`
  * **Host part**: All bits are `0`

***

### **Subnet Masks by IP Class**

#### **Class A**

* **Example IP**: `1.0.0.1`
* **Structure**: `N.H.H.H` (Network.Host.Host.Host)
* **Binary Mask**: `11111111.00000000.00000000.00000000`
* **Decimal Mask**: `255.0.0.0`

#### **Class B**

* **Example IP**: `128.0.0.1`
* **Structure**: `N.N.H.H` (Network.Network.Host.Host)
* **Binary Mask**: `11111111.11111111.00000000.00000000`
* **Decimal Mask**: `255.255.0.0`

#### **Class C**

* **Example IP**: `192.0.0.1`
* **Structure**: `N.N.N.H` (Network.Network.Network.Host)
* **Binary Mask**: `11111111.11111111.11111111.00000000`
* **Decimal Mask**: `255.255.255.0`

***

### **Purpose of a Subnet Mask**

* **Identifies** how many bits are used for the network and how many for hosts.
* **Helps routers** understand where to send packets.
* **Enables subnetting**, which breaks large networks into smaller, manageable parts.
