When a IP-Range is written as aaa.bbb.ccc.ddd/netmask (CIDR Notation) I need to calculate the first and the last included ip address in this range with C#.
Example:
Input: 192.168.0.1/25
Result: 192.168.0.1 - 192.168.0.126
When a IP-Range is written as aaa.bbb.ccc.ddd/netmask (CIDR Notation) I need to calculate the first and the last included ip address in this range with C#.
Example:
Input: 192.168.0.1/25
Result: 192.168.0.1 - 192.168.0.126
my good friend Alessandro have a nice post regarding bit operators in C#, you should read about it so you know what to do.
It's pretty easy. If you break down the IP given to you to binary, the network address is the ip address where all of the host bits (the 0's in the subnet mask) are 0,and the last address, the broadcast address, is where all the host bits are 1.
For example:
ip 192.168.33.72 mask 255.255.255.192
11111111.11111111.11111111.11000000 (subnet mask)
11000000.10101000.00100001.01001000 (ip address)
The bolded parts is the HOST bits (the rest are network bits). If you turn all the host bits to 0 on the IP, you get the first possible IP:
11000000.10101000.00100001.01000000 (192.168.33.64)
If you turn all the host bits to 1's, then you get the last possible IP (aka the broadcast address):
11000000.10101000.00100001.01111111 (192.168.33.127)
So for my example:
the network is "192.168.33.64/26":
Network address: 192.168.33.64
First usable: 192.168.33.65 (you can use the network address, but generally this is considered bad practice)
Last useable: 192.168.33.126
Broadcast address: 192.168.33.127
I'll just post the code:
IPAddress ip = new IPAddress(new byte[] { 192, 168, 0, 1 });
int bits = 25;
uint mask = ~(uint.MaxValue >> bits);
// Convert the IP address to bytes.
byte[] ipBytes = ip.GetAddressBytes();
// BitConverter gives bytes in opposite order to GetAddressBytes().
byte[] maskBytes = BitConverter.GetBytes(mask).Reverse().ToArray();
byte[] startIPBytes = new byte[ipBytes.Length];
byte[] endIPBytes = new byte[ipBytes.Length];
// Calculate the bytes of the start and end IP addresses.
for (int i = 0; i < ipBytes.Length; i++)
{
startIPBytes[i] = (byte)(ipBytes[i] & maskBytes[i]);
endIPBytes[i] = (byte)(ipBytes[i] | ~maskBytes[i]);
}
// Convert the bytes to IP addresses.
IPAddress startIP = new IPAddress(startIPBytes);
IPAddress endIP = new IPAddress(endIPBytes);
Invert mask (XOR with ones), AND it with IP. Add 1. This will be the starting range. OR IP with mask. This will be the ending range.
I learned this shortcut from working at the network deployment position. It helped me so much, I figured I will share this secret with everyone. So far, I have not able to find an easier way online that I know of.
For example a network 192.115.103.64 /27, what is the range?
just remember that subnet mask is 0, 128, 192, 224, 240, 248, 252, 254, 255
255.255.255.255 11111111.11111111.11111111.11111111 /32
255.255.255.254 11111111.11111111.11111111.11111110 /31
255.255.255.252 11111111.11111111.11111111.11111100 /30
255.255.255.248 11111111.11111111.11111111.11111000 /29
255.255.255.240 11111111.11111111.11111111.11110000 /28
255.255.255.224 11111111.11111111.11111111.11100000 /27
255.255.255.192 11111111.11111111.11111111.11000000 /26
255.255.255.128 11111111.11111111.11111111.10000000 /25
255.255.255.0 11111111.11111111.11111111.00000000 /24
from /27 we know that (11111111.11111111.11111111.11100000). Counting from the left, it is the third number from the last octet, which equal 255.255.255.224 subnet mask. (Don't count 0, 0 is /24) so 128, 192, 224..etc
Here where the math comes in:
use the subnet mask - subnet mask of the previous listed subnet mask in this case 224-192=32
We know 192.115.103.64 is the network: 64 + 32 = 96 (the next network for /27)
which means we have .0 .32. 64. 96. 128. 160. 192. 224. (Can't use 256 because it is .255)
Here is the range 64 -- 96.
network is 64.
first host is 65.(first network +1)
Last host is 94. (broadcast -1)
broadcast is 95. (last network -1)
You might already know this, but to check that you're getting this stuff right have a look at http://www.subnet-calculator.com/ - you can see there how the bits represent the network and host portions of the address.
I know this is an older question, but I found this nifty library on nuget that seems to do just the trick for me:
http://nuget.org/packages/TakeIo.NetworkAddress/
I would recommend the use of IPNetwork Library https://github.com/lduchosal/ipnetwork. As of version 2, it supports IPv4 and IPv6 as well.
IPv4
IPNetwork ipnetwork = IPNetwork.Parse("192.168.0.1/25");
Console.WriteLine("Network : {0}", ipnetwork.Network);
Console.WriteLine("Netmask : {0}", ipnetwork.Netmask);
Console.WriteLine("Broadcast : {0}", ipnetwork.Broadcast);
Console.WriteLine("FirstUsable : {0}", ipnetwork.FirstUsable);
Console.WriteLine("LastUsable : {0}", ipnetwork.LastUsable);
Console.WriteLine("Usable : {0}", ipnetwork.Usable);
Console.WriteLine("Cidr : {0}", ipnetwork.Cidr);
Output
Network : 192.168.0.0
Netmask : 255.255.255.128
Broadcast : 192.168.0.127
FirstUsable : 192.168.0.1
LastUsable : 192.168.0.126
Usable : 126
Cidr : 25
Have fun !
Input: 192.168.0.1/25
The mask is this part: /25
To find the network address do the following:
Subtract the mask from the ip length (32 - mask) = 32 - 25 = 7 and take those bits from the right
In the given ip address
I.e: 192.168.0.1
in binary is:
11111111 11111111 00000000 00000001
Now, taking 7 bits from right '0'
1111111 11111111 00000000 00000000
Which in decimal is:
192.168.0.0
(this is the network address)
To find first valid/usable ip address add +1 to network address I.e: 192.168.0.1
To find the last/broadcast address the procedure is same as that of finding network address but here you have to make (32-mask) bits from right to '1'
I.e: 11111111 11111111 00000000 01111111
Which in decimal is 192.168.0.127
To find the last valid/usable ip address subtract 1 from the broadcast address
I.e: 192.168.0.126