How to identify if two IP addresses are on the same network
Jotted down on 24 April 2022
446 words
The IP address is a logical address that we use to send data on the internet to the intended location. In this atomic, we will focus on identifying classes & network using an IP address.
IP address lives on layer 3 of the OSI model and consists of IP address class space plus the host address. Classful addressing divides IP into five layers, namely Class A, B, C, D, and E.
IP address takes the structure of xxx.xxx.xxx.xxx
.
1192.168.1.1
You are most probably familiar with this IP address, right? Your router management panel lives at this address.
Classful IP addresses
🌈 Quick math to calculate available IP addresses. Take the free bits and the result of 2^(free bits)
is the number of IP addresses at your disposal.
In Class A, we have 24 free bits (4 octets = 32 bits, 8 bits occupied for the network, we get the remaining 24 bits) so we have 2^24 IP addresses available. Whereas, in class C, we have only 256.
As you might have already noticed, the distribution of the address space is not equal at all. Class A enjoys an enormous range of IP addresses, while class B and class C get very little address space compared to class A.
You can ignore classes D and E as you won’t need them when building your web applications.
How to check if two IP addresses are on the same network
To check if two IP addresses are on the same network, we need to:
- Know what network the first IP is on, and then
- See if the second IP is also on the same network.
For example, let's check if 132.21.1.20
and 132.21.16.10
are on the same network.
Since these IPs belong to class B (refer table above), the subnet mask is 255.255.0.0
. This means the first two octets (132.21
) represent the network part while the last two represent the host part (1.20
and 16.10
).
For the purpose of the question, we can confidently say that these IPs belong to the same network.
And that's it! Hope you liked this atomic and it gave you some perspective on the seemingly random collection of numbers in an IP address.
Interested in TCP, how it works, and why connection-less protocol like UDP cannot replace it? You may like TCP connection establishment for Software Developers.