以下方法實現判斷一個IP是否被一個IP區間所包含
有一些靜態方法可能引用了同名空間的自定義的類,
至於合並兩個相臨的IP段,可對其中的最大或最小IP+1
using System;
using System.Text.RegularExpressions;
namespace HKH.Common
{
/// <summary>
/// clsMacAndIP 的摘要說明。
/// </summary>
public class clsIPAddress
{
private clsIPAddress()
{
}
#region 判斷是否合法 IP
/// <summary>
/// 判斷是否合法 IP
/// </summary>
/// <param name="strIP"></param>
/// <returns></returns>
public static bool ValidateIPAddress(string strIP)
{
if ( null == strIP || "" == strIP.Trim() || Convert.IsDBNull(strIP) )
return false;
return Regex.IsMatch(strIP,@"^((/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.){3}(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])$");
}
#endregion
#region 轉換成數值
/// <summary>
/// 轉換為數值
/// </summary>
/// <param name="IPAddr"></param>
/// <returns></returns>
public static uint TransNum(string IPAddr)
{
if ( !ValidateIPAddress(IPAddr) )
throw new Exception("IP Address isn't Well Format!");
string[] IPStrArray = new string[4];
IPStrArray = IPAddr.Split('.');
return MAKELONG(MAKEWORD(byte.Parse(IPStrArray[3]),byte.Parse(IPStrArray[2])),MAKEWORD(byte.Parse(IPStrArray[1]),byte.Parse(IPStrArray[0])));
}
#endregion
#region 格式化
/// <summary>
/// 格式化
/// </summary>
/// <param name="IPAddr"></param>
/// <returns></returns>
public static string Format(string IPAddr)
{
if (IPAddr[0] == '.')
IPAddr = "0" + IPAddr;
if (IPAddr[IPAddr.Length - 1] == '.')
IPAddr += "0.0.0.";
else
IPAddr += ".0.0.0.";
IPAddr = IPAddr.Substring(0,clsString.IndexOf(IPAddr,".",4));
// //去掉無用的0
// IPAddr = Regex.Replace(IPAddr,"^0{1,2}","");
// IPAddr = Regex.Replace(IPAddr,@"/.+0{1,2}",".");
if ( ValidateIPAddress(IPAddr) )
return IPAddr;
return "0.0.0.0";
}
#endregion
#region 比較兩個IP的大小
/// <summary>
/// 比較兩個IP的大小
/// </summary>
/// <param name="IP1"></param>
/// <param name="IP2"></param>
/// <returns></returns>
public static int Compare(string IP1,string IP2)
{
if ( !(ValidateIPAddress(IP1) && ValidateIPAddress(IP2)))
throw new Exception("IP Address isn't Well Format!");
uint IPNum1 = TransNum(IP1);
uint IPNum2 = TransNum(IP2);
if (IPNum1 == IPNum2 )
return 0;
return IPNum1 > IPNum2 ? 1 : -1 ;
}
#endregion
#region 判斷一個IP是否包含在指定IP區間內
/// <summary>
/// 判斷一個IP是否包含在指定IP區間內
/// </summary>
/// <param name="IPRegion"></param>
/// <param name="IP"></param>
/// <returns></returns>
public static bool IsInclude(string[] IPRegion,string IP)
{
//驗證
if ( null == IPRegion || null == IP || 0 == IPRegion.Length )
return false;
if ( !ValidateIPAddress(IP) )
return false;
if ( 1 == IPRegion.Length )
{
if ( !ValidateIPAddress(IPRegion[0]) )
return false;
if ( 0 == Compare(IPRegion[0],IP) )
return true;
}
if ( !(ValidateIPAddress(IPRegion[0]) && ValidateIPAddress(IPRegion[1])) )
return false;
uint IPNum = TransNum(IP);
uint IPNum1 = TransNum(IPRegion[0]);
uint IPNum2 = TransNum(IPRegion[1]);
//比較
if ( Math.Min(IPNum1,IPNum2) <= IPNum && Math.Max(IPNum1,IPNum2) >= IPNum )
return true;
return false;
}
#endregion
#region 判斷兩個IP區間是否重疊,重疊則返回合並區間,否則返回 NULL
/// <summary>
/// 判斷兩個IP區間是否重疊,重疊則返回合並區間,否則返回 NULL
/// </summary>
/// <param name="IPRegion1"></param>
/// <param name="IPRegion2"></param>
/// <returns></returns>
public static string[] CheckOverlapAndMerge(string[] IPRegion1,string[] IPRegion2)
{
//驗證
if ( null == IPRegion1 || null == IPRegion2 || 0 == IPRegion1.Length || 0 == IPRegion2.Length )
return null;
//分數組長度進行處理 =1 和 > 1
if ( 1 == IPRegion1.Length )
{
if (!ValidateIPAddress(IPRegion1[0]))
throw new Exception("IP Address isn't Well Format!");