Building networks from A to Z - Part 4 : Network !
Tags: computerscience, beginners, networks
Published: 2020-10-26 22:06:40

*This is the part 4 of the series "Building networks from A to Z", don't forget to read the 3 previous parts in order to fully understand what I will demonstrate here. Thank you 😄*

We are coming to the **BIG** part of networking, the one that is usually more known to developers and "large audience" as it involves the Internet Protocol, also known as IP.

We are now on layer 3 of the OSI model, and layer 2 of the TCP/IP model. This layer offers **wide** network properties that allow routing, which is one of the most important aspects of networking, and we will be covering it by every angle possible.

When talking about the Network layer, you can be sure that 99% of the time, Internet Protocol is involved. IP is currently available in 2 different versions : IPv4 and IPv6. The differences will be discussed later on this post.

## **Basical addressing**

The main feature of IP is its addressing scheme, that allows segmentation to locate both the network and the host. Such a feature is the key-component to routing. We will first discuss the IPv4 addressing.

IP addresses are composed of 4 bytes, written in decimal this time, and usually in a well-known format that is `10.0.0.1`. As every data sent on a network, the address is hard-coded in binary during transmissions. 

The main goal of the Internet Protocol is to cover every size of networks possible, and we will see that it works perfectly well, it is a very powerful protocol in doing so. 

Each IP network has a network address (the first address possible) and a broadcast address (the last address possible), so keep it in mind before we demonstrate it, all networks must have **at least** 2 addresses to work (but none left to hosts, so not very useful).


Historically, IP addressing was segmented by classes. Classes allow choosing the first byte to correspond to the good network size. For the sake of demonstrating how classes work, we will work with a little bit of binary, but don't worry, it is very simple and we will be only focusing on the first byte.

Here are the different classes : 

- Class A : The first bit **must** be `0`

It then goes from `0000 0001` (1) to `0111 1111` (127), so every network in this range is a class A network. `127.x.y.z` are reserved addresses, they are used for loopbacks, which we will be also talking about later. Network address is `0.0.0.0` and broadcast address is `126.255.255.255` (remember, 127.* addresses are reserved). `0.0.0.0` to 1.0.0.0 also reserved, so the first network address of the class A is `1.0.0.0`. The first byte is the network part in class A, so, as a simple binary calculation, we have 2^(8-1) networks, the minus 1 is due to the first bit that **must** be `0`, 2^7 = 128 networks that each can have 3 bytes of hosts, which correspond to 2^24 - 2 = 16 777 214 hosts. We remove 2 because of the network and broadcast addresses, that are reserved. The class A networks are the largest networks possible. 

To check if an IP address belongs to the class A, just check the first bit, if it is a `0`, then it does.

- Class B : The two first bits **must** be `10`

The range then goes from `1000 0000` (128) to `1011 1111` (191). The first address is `128.0.0.0` and the last one is `191.255.255.255`. The first two bytes are the network identificator, and the two last are the hosts identificator. We then have 2^14 (not 16 because of the 2 restricted prefix bits) networks (that is 16 384 compared to 128 in class A) that each can have 2^16 - 2 hosts (that is 65 534 compared to 16 777 214 as previously).

- Class C : The three first bits **must** be `110`

Class C addresses go from `1100 0000` (192) to `1101 1111`(223). The 3 first bytes are reserved to networks and the last byte is the one dedicated to hosts. Same calculations as before, 2^21 networks, which is 2 097 152 networks, and only 2^8 - 2 hosts, which is 254. The class C networks were the most common ones because we could have a lot of networks and still have a proper amount of hosts.

- D and E classes are not widely used, the class D is reserved to multicast (we will maybe talk about multicast on another post) and E class is dedicated to R&D.

A notion that is linked to classes is the network mask, also called netmask, that we use to differentiate the bits that are used by the network and the ones used for the hosts. This is where we are going to use a little binary arithmetic operation called **AND** bit-by-bit operation.

A netmask is formed using 4 bytes in the same format as an IP address, but the network part is only 1s and the host part only 0s. Meaning that an AND operation between a "real" IP address and a netmask gives us the network address.

```
         11111111.11111111.11111111.00000000 (255.255.255.0)
& (AND)  00111001.00000010.00000101.10000001 (57.2.5.129)
         -----------------------------------
         00111001.00000010.00000101.00000000 (57.2.5.0)
```

Netmasks came after the classes, and I will explain on the next paragraphes why, but we can associate a class to a specific netmask :

- Class A : 255.0.0.0
- Class B : 255.255.0.0
- Class C : 255.255.255.0
- Class D : 255.255.255.255

Classes were designed so that we can designate to which class belongs a network by only looking to the first bits, but they created netmask for the same purpose, but also widening the concept. As you can see, the structures of netmasks put in this exact order remembers us the pyramidal scale of the first bits of each class.

```
255.0.0.0           0000 0000
255.255.0.0         1000 0000
255.255.255.0       1100 0000
255.255.255.255     1110 0000
```

## **The problems begin**

Considering the number of hosts in total (2^32 = 4 294 967 296), we rapidly lack of addresses to attribute to each device on the world, as we count PCs, mobile phones, tablets, game consoles, in fact every device that is connected to the Internet, even a connected fridge, a smart watch, everything.

Network engineers then had to find fixes in order to continue using IPv4, considering upgrading would have been a huge cost to companies and organizations around the world, so they came to many solutions that are now widely used.

### **Variable-Length Subnet Mask**

Variable-Length Subnet Mask (VLSM) is born on the initiative to save IP addresses by divising the existing classes in subnets. As we saw before, until then, a byte of a netmask could either be `0` or `255`. With this principle, we can have many more networks by allowing other values for each byte.

VLSM uses the CIDR (Classless Inter-Domain Routing) notation to ease the writing of netmasks and the network addresses coupled.

Here are examples by transposing classes networks to CIDR notation :

- 10.0.0.0 (class A) : 10.0.0.0/8
- 191.100.0.0 (class B): 191.100.0.0/16
- 192.168.1.0 (class C): 192.168.1.0/24

The number indicated after the `/` is actually the number of 1s from the first bit of the netmask. For example, `10.0.0.0/8` is the network `10.0.0.0` associated with the netmask `255.0.0.0` (`1111 1111` is equal to 255). For `/16`, obviously, `255.255.0.0` is the associated netmask, and for `/24`, `255.255.255.0` is the associated netmask.

Now imagine you are being assigned the `52.2.0.0/16` (this network has already been divided by the ISP, how to know it ?!) network from an ISP, and you have multiple sites to address with this one and only network. To avoid too large IP broadcasting domains and vulnerabilities being spread really fast, you choose to divide this network into multiple `/24` networks, each for another physical site.

This example is pretty simple because we are converting one class into another, but it can be pretty when using non-perfect values (we will do another example later ☺️).

To go from 16 to 24 network bits, we just have to add 8 bits that we call subnetworks bits. These bits will identify the subnetworks we will be using. Each combinaison of the 8 will give us a subnetwork. A few examples :

```
52.2.0000 0000.0/16
->   0000 0001.0/24
->   0000 0010.0/24
     ...
```
You have, in total, a possibility of 255 subnetworks, which should be enough for our case. Of course, a subnetwork is a network, so each one of these have both a network and a broadcast address. `52.2.1.0/24` is the network address and we will compute the broadcast address very simply by changing the hosts bits value from 0 to 1, by doing so :
```
52.2.1.0000 0000/24 -> 52.2.1.1111 1111
                       52.2.1.255
52.2.2.0000 0000/24 -> 52.2.2.1111 1111
                       52.2.2.255
```
Aside those 2 addresses, you should have 254 addresses dedicated to your subnetworks hosts. And you have successfully designed subnetworks to split your network in case to use it smartly between your multiply physical sites.

Here is a quick diagram that abstracts all concepts we have seen : 

![Subnetworks of 52.2.0.0/16](https://dev-to-uploads.s3.amazonaws.com/i/8ftsassugu7ikosxmkxa.png)

<figcaption>Subnetworking the 52.2.0.0/16 network</figcaption>

Here you have your subnetworks !

Using a non-8 multiplicator for the netmask length is also possible and trickier to setup, but we will covering one of them in another post, where we will use all knowledge we have acquired to create a network on which we have services and users !

The Internet protocol is one big part of the networks, and we are not yet to cover it entirely !

The next part will be talking about routing and all its aspects. This chapter was a big part to the series and has quite complex notions.

Rendez-vous at the next part speaking about routing and relative concepts !