4

Does .NET have the notion of IP Address Ranges?

I need to test if a given IP address is within a range of addresses.

I could write some API that would give me something like

IPRange ipRange = IPRange.Parse("127.0.0.1-127.0.0.15");
ipRange.Contains(someAddress);

but I don't want to reinvent the wheel if there is already similar functionality built in.

Greg B
  • 14,597
  • 18
  • 87
  • 141
  • 4
    You can convert the IP address to a number and validate ranges that way. See http://stackoverflow.com/questions/461742/how-to-convert-an-ipv4-address-into-a-integer-in-c – George Johnston Jul 07 '11 at 14:03

2 Answers2

4

No, but here is how it can be done (VB since code tag not included in OP)

'test values
Dim rangeStart As Net.IPAddress = Net.IPAddress.Parse("192.168.133.1")
Dim rangeEnd As Net.IPAddress = Net.IPAddress.Parse("192.168.133.254")
Dim check As Net.IPAddress = Net.IPAddress.Parse("192.168.133.230")

'get the bytes of the address
Dim rbs() As Byte = rangeStart.GetAddressBytes
Dim rbe() As Byte = rangeEnd.GetAddressBytes
Dim cb() As Byte = check.GetAddressBytes

'reverse them for conversion
Array.Reverse(rbs)
Array.Reverse(rbe)
Array.Reverse(cb)

'convert them
Dim rs As UInt32 = BitConverter.ToUInt32(rbs, 0)
Dim re As UInt32 = BitConverter.ToUInt32(rbe, 0)
Dim chk As UInt32 = BitConverter.ToUInt32(cb, 0)

'check
If chk >= rs AndAlso chk <= re Then
    Debug.WriteLine("In Range")
Else
    Debug.WriteLine("Not In Range")
End If
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • 2
    It almost solved my problem, what if I defined the IP range like "192.168.*.*" and now I want to compare "192.168.133.230" – dIvYaNsH sInGh Oct 24 '12 at 00:25
  • 2
    so if you are trying to say all addresses in 192.168. then the start is 192.168.0.0 and the end is 192.168.255.255 – dbasnett Nov 01 '12 at 15:14
0

Here just a quick translation of dbasnett's answer to C#:

public static bool IsIPInRange(string ip, string ipStart, string ipEnd)
{
    var pIP = IPAddress.Parse(ip);
    var pIPStart = IPAddress.Parse(ipStart);
    var pIPEnd = IPAddress.Parse(ipEnd);

    var bIP = pIP.GetAddressBytes().Reverse().ToArray();
    var bIPStart = pIPStart.GetAddressBytes().Reverse().ToArray();
    var bIPEnd = pIPEnd.GetAddressBytes().Reverse().ToArray();

    var uIP = BitConverter.ToUInt32(bIP, 0);
    var uIPStart = BitConverter.ToUInt32(bIPStart, 0);
    var uIPEnd = BitConverter.ToUInt32(bIPEnd, 0);

    return uIP >= uIPStart && uIP <= uIPEnd;
}

Here is a full working .NET Fiddle with an example.

Hope this doesn't count as an off-topic answer.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Error at line: pIP.GetAddressBytes().Reverse().ToArray();. Message: Overload resolution failed because no accessible 'Reverse' accepts this number of arguments. – NoName Jul 13 '17 at 10:55
  • @PhucNguyen Just add a `using System.Linq;` to call [this method](https://msdn.microsoft.com/en-us/library/bb358497.aspx) should help. – Uwe Keim Jul 13 '17 at 14:05