Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a input IP fall in a specific IP range

Tags:

c#

asp.net

If we let users input a couple of ip ranges, e.g., 172.16.11.5 - 100, how could I write a function to check if a IP (172.16.11.50) falls in the ranges?

Is there any existing library in .NET to leverage?

like image 265
Ricky Avatar asked Jan 26 '10 10:01

Ricky


People also ask

How do you check if an IP address is in a range with Java?

Using Java's InetAddress Class for IPv4 We can check if it falls under the given range. Java's InetAddress class represents an IP address and provides methods to get the IP for any given hostnames. An instance of InetAddress represents the IP address with its corresponding hostname.

How do I enter an IP range?

Click IP Address Manager > IP Addresses > Manage Subnets & IP Addresses. In the network tree pane on the left, click the subnet to which you want to add your new IP address range. Click Add IP Range. Enter the starting IP address and the ending IP address of your IP address range.

How many IP addresses are in a range?

For IPv4, this pool is 32-bits (232) in size and contains 4,294,967,296 IPv4 addresses. The IPv6 address space is 128-bits (2128) in size, containing 340,282,366,920,938,463,463,374,607,431,768,211,456 IPv6 addresses. A bit is a digit in the binary numeral system, the basic unit for storing information.


1 Answers

There's nothing built into the framework, but it wouldn't take much effort to create an IPAddressRange class.

You'd compare the ranges by calling IPAddress.GetAddressBytes on the lower address, upper address and comparison address. Starting at the first byte, check if the comparison address is in the range of the upper/lower address.

This method works for both IPv4 and IPv6 addresses.

public class IPAddressRange {     readonly AddressFamily addressFamily;     readonly byte[] lowerBytes;     readonly byte[] upperBytes;      public IPAddressRange(IPAddress lowerInclusive, IPAddress upperInclusive)     {         // Assert that lower.AddressFamily == upper.AddressFamily          this.addressFamily = lowerInclusive.AddressFamily;         this.lowerBytes = lowerInclusive.GetAddressBytes();         this.upperBytes = upperInclusive.GetAddressBytes();     }      public bool IsInRange(IPAddress address)     {         if (address.AddressFamily != addressFamily)         {             return false;         }          byte[] addressBytes = address.GetAddressBytes();          bool lowerBoundary = true, upperBoundary = true;          for (int i = 0; i < this.lowerBytes.Length &&              (lowerBoundary || upperBoundary); i++)         {             if ((lowerBoundary && addressBytes[i] < lowerBytes[i]) ||                 (upperBoundary && addressBytes[i] > upperBytes[i]))             {                 return false;             }              lowerBoundary &= (addressBytes[i] == lowerBytes[i]);             upperBoundary &= (addressBytes[i] == upperBytes[i]);         }          return true;     } } 

NB: The above code could be extended to add public static factory methods FromCidr(IPAddress address, int bits)

like image 144
Richard Szalay Avatar answered Oct 11 '22 12:10

Richard Szalay