DEV Community

Vivek Siva
Vivek Siva

Posted on • Updated on

CIDR - what the heck is it?

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

from wiki

"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]"

IP in CIDR Notation

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 an 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?

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 -

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

Per techrepublic

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.
Enter fullscreen mode Exit fullscreen mode

or -

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!

CIDR notation

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Or if you take /20 as x, do this

1) x =20
2) y =32-x

<b>Answer = 2^y = 4096 </b>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Formula

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

Answer = 15  = range
Enter fullscreen mode Exit fullscreen mode

(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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (7)

Collapse
 
liptanbiswas profile image
Liptan Biswas

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

Collapse
 
viv profile image
Vivek Siva

I will add this point, with your permission ofcourse :)

Collapse
 
liptanbiswas profile image
Liptan Biswas • Edited

sure, please go ahead.

Collapse
 
leob profile image
leob

But, will this become obsolete when we (finally) switch to IPv6 some day? (funny that I rarely hear about IPv6 anymore after the commotion a few years ago over IPv4 address shortages)

Collapse
 
lexplt profile image
Alexandre Plt

Nearly. Luckily IP address classes are already obsolete and we only use IP/mask

Collapse
 
viv profile image
Vivek Siva

Yeah, but some are betting that ipv4 will be there for a decade atleast and CIDR for ipv6 is a whole different ball game!!

mediawiki.org/wiki/Help:Range_bloc...

Collapse
 
leob profile image
leob

Yeah funny that we don't hear about the IPv4 address shortage anymore, I guess NAT and similar techniques got better and more wider implemented ...