CIDR - what the heck is it? 
Tags: aws, azure, tutorial, hacktoberfest
Published: 2020-10-22 20:30:07

So you may or may not have heard about this CIDR - Class Inter Domain routing , ever wonder what is it or why is it even needed? In this post i have tried to oversimplify it by using my own formulas and stuff and this can get really messy if you are not a network admin or hearing it for the first time! 

<b> What is CIDR <b>

from wiki

<i>"Classless Inter-Domain Routing (CIDR /ˈsaɪdər, ˈsɪ-/) is a method for allocating IP addresses and for IP routing. The Internet Engineering Task Force introduced CIDR in 1993 to replace the previous classful network addressing architecture on the Internet. Its goal was to slow the growth of routing tables on routers across the Internet, and to help slow the rapid exhaustion of IPv4 addresses.[1][2]" </i>

<b> IP in CIDR Notation <b>

<p>So CIDR is a method to write IP addresses, right! ok, so lets take an ip address, Like 8.8.8.8, 
now this is a singleton ip address, meaning its just <i>an<i> ip address, however in CIDR you will use this notation  - 8.8.8.8/32.. the first part "8.8.8.8" is called a prefix and the "/32" is a suffix.. 

But, what the heck is this 32? and what's the relevance here? 

<p>So  all the ips have 4 parts (or 3 dots) and each are 8 bits, overall 32 bits , now watch this 32 which will be used later.. The point to note here is that a "/32" suffix means, its a single ip address and anything less than that like a "/31" or "/16" or "/23" are a range of ip addresses. 

More info - 
![](https://www.networkcomputing.com/sites/default/files/fig%2013.png?raw=true)

Before that we need to touch base on few items like what are the classes of IP addresses. 

Per [techrepublic](https://www.techrepublic.com/article/build-your-skills-ip-address-classes-overview/#:~:text=If%20the%20address%20is%20written%20in%20decimal%20format%2C%20the%20first,the%20address%20is%20class%20C)

<b> IP Address Classes
There are five classes of IP addresses: A, B, C, D, and E. In the following sections, I’ll discuss each class. 

    Class A addresses range from 0.0.0.0 to 127.255.255.255.
    Class B network addresses range from 128.0.0.0 to 191.255.255.255.
    Class C network addresses range from 192.0.0.0 to 223.255.255.255.
    Class D IP addresses are reserved for multicasting. 
    Class E addresses are reserved for special use on the Internet.


or - 

<p>Now we can ignore D and E , as we will concentrate only on A, B and C. And since we are not talking about Network in general , we can jump to CIDR in full flow! 

<b> CIDR notation

So , any ipv4 address or <i><b>range</b></i> in this world can be depicted via CIDR using a prefix and suffix, so why is it needed? 

<p> The previous classes of IP addresses presented more problems as the networks grew exponentially (or expanded) making it harder for organizations to adapt to IP address classes!  , lets take an example of an large company, with wide range of servers in its network and all of them requiring an ip address and a class C ip address may not even fit. 

Enter CIDR for efficient subnetting , Example if the company has three tiers, each needing different sets of IP address, the CIDR notation might looks something like this

    Web - 10.1.0.0/20
    App- 10.1.0.0/23
    DB- 10.1.0.0/25

<b>Now how many IP address does each have?</b>

Lets take web tier, we could see that this has a "/20" suffix, meaning the range is 4096 ip addresses.. wait what?! how? 

<i>well, here is how :)</i>

The way <i>i</i> calculate the IP address from a CIDR range is like this..  

     1) Subtract  20 from 32 = 12
     2) What is 2^12 = 4096

Or if you take /20 as x, do this

    1) x =20
    2) y =32-x
         
    <b>Answer = 2^y = 4096 </b>

<i>tada</i>, there are 4096 ip addresses in /20 CIDR, but what are those ip address and what's the range.. 

<b> OK , part 2 - </b>
How many total ipv4 ips are there? like ex when we say 0.0.0.0/0, how many ips does it have ? /0	has 4,294,967,294 IP addresses = 2^32 ;) 

![](https://www.ripe.net/images/IPv4CIDRChart_2015.jpg?raw=true)

So if someone asks the range of /20, here is how <i><b>i</b></i> calculate that

    1) IP = 10.1.0.0/20 = 4096 ips
    2) Now take this 4096/256 = 16 (256 is a constant - 0-255 ips)
    3) 16-1 =15 
    4) The range is 10.1.0.0 - 10.1.15.255

So the ranges are like 
    10.1.0.0 - 10.1.0.255
    10.1.1.0 - 10.1.1.255
    10.1.2.0 - 10.1.2.255
    .
    .
    .
    .
    10.1.15.0 - 10.1.15.255

[check the full listhere](http://magic-cookie.co.uk/iplist.html)

<b> Formula </b>

    1) x=4096  
    2) y=256 
    3) z=(4096/256)-1

    Answer = 15  = range

(note that this is a constant on my calculation and what if , say the x is lesser than y ;), check bottom of post)

Now lets throw any ips and check..

    App- 10.1.0.0/23 = 512 ip addresses / range 10.1.0.0 - 10.1.1.255

    DB- 10.1.0.0/25  = 128 = 10.1.0.0 - 10.1.0.127

So , if the list of IP addresses are greater than 256 we use the formula of y/x-1, if its below 256 , we use the calculation of Ip address -1 


Like 10.1.0.0/25  = 128 ip addresses and range is 10.1.0.(128-1)=10.1.0.127 

Point to note, thanks to {% user liptanbiswas %} for this comment

<i>
Just a note to go with this article.
For a network 10.1.0.0/20 having 4096 IP address, The actual number of nodes the network can have is 4094. The first and last address of network can not be assigned to any node. It is called network and broadcast address respectively. Read more
</i>