> For the complete documentation index, see [llms.txt](https://amartyushov.gitbook.io/tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://amartyushov.gitbook.io/tech/os-components/network/tcp-ip-model/ip-address-in-action.md).

# IP address in action

## LAN

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FU9hy5lNeFiszz9cDl5Ca%2FScreenshot%202023-10-11%20at%2009.44.00.png?alt=media&amp;token=55dc88b0-07ae-42d4-b8c2-a31dcf564794" alt="" width="375"><figcaption></figcaption></figure>

* Network ID = 202.120.10.0
* Host ID = e.g. 42, or 164 (in this case)
* IP = Network ID + Host ID

In order to interconnect LANs we need a router (interconnection happens through router).

The router interface that connects LAN to the router is called **default gateway**&#x20;

* must have same Network ID
* usually has the lowest host address in LAN
* in above case it will have IP 202.120.10.1

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FGhSozchldAKlPlIxupe3%2FScreenshot%202023-10-11%20at%2009.53.40.png?alt=media&amp;token=e64244e5-dab7-411d-ae54-72ff297674f8" alt=""><figcaption></figcaption></figure>

Ranges of addresses that are designed as private IP addresses:

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FRFuQpnfy629ggyDZPLwI%2FScreenshot%202023-10-13%20at%2009.43.40.png?alt=media&amp;token=0126f240-393b-4e1c-8517-3d25854fd1cd" alt=""><figcaption></figcaption></figure>

127.0.0.0/8 subnet is reserved as loopback address

### Subnet mask

Every TCP/IP host needs to know whether the destination IP is local or long distance. Subnet mask helps here.

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2Fq6cDEOrEMJ40PMnguhCA%2FScreenshot%202023-10-11%20at%2010.03.27.png?alt=media&amp;token=201d956c-c040-45ce-97bb-4415cfd182bb" alt=""><figcaption></figcaption></figure>

Short notation for subnet masks:

* 11111111.11111111.11111111.00000000 = /24 (24 ones)
* 11111111.11111111.00000000.00000000 = /16 (16 ones)

Below picture shows how Subnet mask helps to understand that Computer A and B are in the same LAN.

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FSA0cya9mQI40vg6H37Fk%2FScreenshot%202023-10-11%20at%2010.06.03.png?alt=media&amp;token=bddd43c4-b773-4de1-9dd1-ba08af53da36" alt=""><figcaption></figcaption></figure>

If we need to send a request to another LAN:

* send ARP request to realise MAC address of default gateway
* IP packet will be sent to default gateway

## CIDR (Classless inter-domain routing)

CIDR and subnetting are virtually the same thing.

Subnetting enables much more efficient use of IP addresses compared to class blocks.

<details>

<summary>example of subnet calculation</summary>

there is a cafe with network ID 192.168.4.0/24 and you need to split it to subnets.

* start with a given subnet mask and mote it to the right until you have a number of subnets you need
* forget the dots (**never try to subnet without first converting to binary**)

How many hosts we have on /24 subnet? 2^8 - 2 = 254

How many hosts we have on /26 subnet? 32 digits - 26 = 6 => you have 6 zeroes left after 26 ones => 2^6 - 2 = 62 hosts.

Original network ID: 192.168.4.0/24 translates to this binary (grey below):

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FWZfQkhstgFKhgbZ1Hq3e%2FScreenshot%202023-10-13%20at%2009.05.13.png?alt=media\&token=ad36a715-da0e-4ba0-9142-ec7e7aeba268)

By taking 2 more bits (red ones) for subnetting we can have 2^2 new subnets (we get 4 new Network ids).

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FvkjOoGQa8GqZky7f6nz4%2FScreenshot%202023-10-13%20at%2009.12.36.png?alt=media\&token=fe404de5-2dc1-4288-a306-f228227fcb3c)

</details>

## DHCP (Dynamic IP addressing)

```
DORA (DHCP four-way handshake):

(Discover) [Client] -> {broadcast} -> is there DHCP server here
(Offer) [DHCP Server] -> [Client] : I am, here is your IP address, subnet mask, default gateway
(Request) [Client] -> [DHCP Server] : I am accepting your information
(Acknowledgmnet) [DHCP Server] -> [Client] : ok, I save you IP and MAC to my db

------ in around 5-6 days:
(Request) [Client] -> [DHCP Server] : can I use my setting?
(Acknowledgmnet) [DHCP Server] -> [Client] : I know you, I look up your data in db and give it back
```

Usually routers are blocking broadcasting traffic, but what if in big company there are many subnets and just one DHCP server for convenience =>

### DHCP relay

DHCP relay enables DHCP traffic (broadcast) to cross routers.

<figure><img src="https://415484505-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LxtoAXZwwOc4XGto8vb%2Fuploads%2FOXh4LKBkUsZAAYr4qMuU%2FScreenshot%202023-10-13%20at%2009.37.51.png?alt=media&amp;token=a584469a-6a5a-4ce0-b2c5-c03a5f086cb2" alt=""><figcaption></figcaption></figure>
