Calculate IP range by subnet mask

If I have a subnet mask, for example. 255.255.255.0 and ip address 192.168.1.5 , is there an easy way to determine all the possible IP addresses on this subnet?

In this case:

 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 ... ... 192.168.1.252 192.168.1.253 192.168.1.254 192.168.1.255 

All I have found so far are the heavy overloaded .net libraries. Is there any native way to solve this using default namespaces?

+6
source share
4 answers

To determine the address range, follow these steps:

1) Take the subnet mask (here 255.255.255.0) and convert it to a binary file:

 11111111.11111111.11111111.00000000 ( 8 + 8 + 8 + 0 = 24 -> So you can write your ip adresse like this : 192.168.1.x/24 because you are in a /24 network) 

2) You have 256-2 = 254 available IP addresses for the host on the / 24 network (one for the network address (the first in your range) and the other for the broadcast address (the last in your range)).

3) To get your range, just get your network address (the first ip address according to your subnet mask) and get the following 255 IP addresses, and you will have your own range.

Network address:

In binary, the last octet must be null:

 xxxxxxxx.xxxxxxxx.xxxxxxxx.00000000 

Your broadcast address:

In binary, the last octet must be 1:

 xxxxxxxx.xxxxxxxx.xxxxxxxx.11111111 

Here your IP address is 192.168.1.5. In binary expression we get:

 11000000.10101000.00000000.00000101 
  • Your network address: 11000000.10101000.00000000.00000000 ↔ 192.168.1.0
  • Your broadcast address: 11000000.10101000.000000000.11111111 ↔ 192.168.1.255
  • First usable IP address: 192.168.1.1

  • Last used IP address: 192.168.1.254

I hope you enjoyed reading bad English. Tell me if you have a question, Loris

+7
source

10 minutes of coding and are NOT fully tested:

 class IPSegment { private UInt32 _ip; private UInt32 _mask; public IPSegment(string ip, string mask) { _ip = ip.ParseIp(); _mask = mask.ParseIp(); } public UInt32 NumberOfHosts { get { return ~_mask+1; } } public UInt32 NetworkAddress { get { return _ip & _mask; } } public UInt32 BroadcastAddress { get { return NetworkAddress + ~_mask; } } public IEnumerable<UInt32> Hosts(){ for (var host = NetworkAddress+1; host < BroadcastAddress; host++) { yield return host; } } } public static class IpHelpers { public static string ToIpString(this UInt32 value) { var bitmask = 0xff000000; var parts = new string[4]; for (var i = 0; i < 4; i++) { var masked = (value & bitmask) >> ((3-i)*8); bitmask >>= 8; parts[i] = masked.ToString(CultureInfo.InvariantCulture); } return String.Join(".", parts); } public static UInt32 ParseIp(this string ipAddress) { var splitted = ipAddress.Split('.'); UInt32 ip = 0; for (var i = 0; i < 4; i++) { ip = (ip << 8) + UInt32.Parse(splitted[i]); } return ip; } } 

Using:

  static void Main(string[] args) { IPSegment ip = new IPSegment("192.168.1.1","255.255.255.248"); Console.WriteLine(ip.NumberOfHosts); Console.WriteLine(ip.NetworkAddress.ToIpString()); Console.WriteLine(ip.BroadcastAddress.ToIpString()); Console.WriteLine("==="); foreach (var host in ip.Hosts()) { Console.WriteLine(host.ToIpString()); } Console.ReadLine(); } 
+8
source

yup, convert everything to a 32-bit representation (assuming IPv4). if your mask is M and IP is IP, then your IP range is (M & IP) +1, (M & IP) +2, ..., (M & IP) + (~ M) -1. where is bitwise And and ~ bitwise.

to convert things to a 32-bit representation, each place in ip abcd is an 8-bit number.

0
source

Nice call. Do not use additional libraries for this. It is pretty simple. Here is how I do it:

 public static uint[] GetIpRange(string ip, IPAddress subnet) { uint ip2 = Utils.IPv4ToUInt(ip); uint sub = Utils.IPv4ToUInt(subnet); uint first = ip2 & sub; uint last = first | (0xffffffff & ~sub); return new uint[] { first, last }; } 

Note:
You will need to do some more work on converting IP addresses to uint and vice versa (should be quite simple). You can then iterate over all the IP addresses with a simple to loop between the first and last .

Something like this (not verified):

 byte[] bytes = subnet.GetAddressBytes(); uint sub = (uint)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]); IPAddress ip1 = IPAddress.Parse(ip); bytes = ip1.GetAddressBytes(); uint ip2 = (uint)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]); 
0
source

All Articles