You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.4 KiB
48 lines
1.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EasyBL
|
|
{
|
|
public class IPAddressRange
|
|
{
|
|
readonly AddressFamily addressFamily;
|
|
readonly byte[] lowerBytes;
|
|
readonly byte[] upperBytes;
|
|
|
|
public IPAddressRange(IPAddress lowerInclusive, IPAddress upperInclusive)
|
|
{
|
|
addressFamily = lowerInclusive.AddressFamily;
|
|
lowerBytes = lowerInclusive.GetAddressBytes();
|
|
upperBytes = upperInclusive.GetAddressBytes();
|
|
}
|
|
|
|
public bool IsInRange(IPAddress address)
|
|
{
|
|
if (address.AddressFamily != addressFamily)
|
|
{
|
|
return false;
|
|
}
|
|
var addressBytes = address.GetAddressBytes();
|
|
|
|
bool lowerBoundary = true, upperBoundary = true;
|
|
|
|
for (var i = 0; i < 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;
|
|
}
|
|
}
|
|
}
|