Range of Host IPs in the network

I am able to get the IP address and Subnet Mask programatically. Is there a way we could generate all addressable host IPs with these two information?

I am just trying to develop a simple Ping Sweep module for my application.

Thanks.

PS: I am using C# .NET 3.5.

Answers


Calculate the first and last host IP addresses and then iterate over them sequentially. The first IP address is (address & mask) + 1. The last is (address | ~mask) - 1.

So if the network is 192.168.132.0 and the netmask is 255.255.255.0, the first eligible host IP is:

(address & mask) + 1
(192.168.132.0 & 255.255.255.0) + 1
192.168.132.0 + 1
192.168.132.1

The last eligible host IP is:

(address | ~mask) - 1
(192.168.132.0 | ~(255.255.255.0)) - 1
(192.168.1.32.0 | (0.0.0.255)) - 1
192.168.132.255 - 1
192.168.132.254

So the valid host addresses are 192.168.132.1 through 192.168.132.254 inclusive.


Need Your Help