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!
What is CIDR
"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."
IP in CIDR Notation
So CIDR is a method to write IP addresses, right! ok, so lets take an ip address, Like 188.8.131.52,
now this is a singleton ip address, meaning its just an ip address, however in CIDR you will use this notation - 184.108.40.206/32.. the first part "220.127.116.11" is called a prefix and the "/32" is a suffix..
But, what the heck is this 32? and what's the relevance here?
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.
Before that we need to touch base on few items like what are the classes of IP addresses.
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 18.104.22.168 to 22.214.171.124. Class C network addresses range from 192.0.0.0 to 126.96.36.199. Class D IP addresses are reserved for multicasting. Class E addresses are reserved for special use on the Internet.
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!
So , any ipv4 address or range in this world can be depicted via CIDR using a prefix and suffix, so why is it needed?
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
Now how many IP address does each have?
Lets take web tier, we could see that this has a "/20" suffix, meaning the range is 4096 ip addresses.. wait what?! how?
well, here is how :)
The way 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>
tada, there are 4096 ip addresses in /20 CIDR, but what are those ip address and what's the range..
OK , part 2 -
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 ;)
So if someone asks the range of /20, here is how 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
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
for this comment
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