判断一个IP区间(或IP)是否被另一个IP区间所包含
以下方法实现判断一个IP是否被一个IP区间所包含
有一些静态方法可能引用了同名空间的自定义的类,
至于合并两个相临的IP段,可对其中的最大或最小IP+1
using System;
using System.Text.RegularExpressions;
namespace HKH.Common
{
///
/// clsMacAndIP 的摘要说明。
///
public class clsIPAddress
{
private clsIPAddress()
{
}
#region 判断是否合法 IP
///
/// 判断是否合法 IP
///
///
///
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 转换成数值
///
///转换为数值
///
///
///
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 格式化
///
/// 格式化
///
///
///
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的大小
///
/// 比较两个IP的大小
///
///
///
///
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区间内
///
/// 判断一个IP是否包含在指定IP区间内
///
///
///
///
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
///
/// 判断两个IP区间是否重叠,重叠则返回合并区间,否则返回 NULL
///
///
///
///
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!");
if ( 1 == IPRegion2.Length )
{
if (!ValidateIPAddress(IPRegion2[0]))
throw new Exception("IP Address isn't Well Format!");
//相等
if ( Equals(IPRegion1[0],IPRegion2[0]) )
return IPRegion1;
}
// > 1
else
{
if ( !(ValidateIPAddress(IPRegion2[0]) && ValidateIPAddress(IPRegion2[1])) )
throw new Exception("IP Address isn't Well Format!");
if ( IsInclude(IPRegion2,IPRegion1[0]) )
return IPRegion2;
}
}
// >1
else
{
if ( !(ValidateIPAddress(IPRegion1[0]) && ValidateIPAddress(IPRegion1[1])) )
throw new Exception("IP Address isn't Well Format!");
if ( 1 == IPRegion2.Length )
{
if (!ValidateIPAddress(IPRegion2[0]))
throw new Exception("IP Address isn't Well Format!");
if ( IsInclude(IPRegion1,IPRegion2[0]) )
return IPRegion1;
}
// > 1
else
{
if ( !(ValidateIPAddress(IPRegion2[0]) && ValidateIPAddress(IPRegion2[1])) )
throw new Exception("IP Address isn't Well Format!");
uint IPNum1_1 = TransNum(IPRegion1[0]);
uint IPNum1_2 = TransNum(IPRegion1[1]);
uint IPNum2_1 = TransNum(IPRegion2[0]);
uint IPNum2_2 = TransNum(IPRegion2[1]);
#region 大小格式整理 IP[0] <= IP[1]
if ( IPNum1_1 > IPNum1_2 )
{
string stemp = IPRegion1[0];
IPRegion1[0] = IPRegion1[1];
IPRegion1[1] = stemp;
uint utemp = IPNum1_1;
IPNum1_1 = IPNum1_2;
IPNum1_2 = utemp;
}
if ( IPNum2_1 > IPNum2_2 )
{
string stemp = IPRegion2[0];
IPRegion2[0] = IPRegion2[1];
IPRegion2[1] = stemp;
uint utemp = IPNum2_1;
IPNum2_1 = IPNum2_2;
IPNum2_2 = utemp;
}
#endregion
#region 判断合并
//互不包含
if ( IPNum1_1 > IPNum2_2 || IPNum1_2 < IPNum2_1 )
return null;
//合并
string[] result = new string[2];
result[0] = IPNum1_1 < IPNum2_1 ? IPRegion1[0] : IPRegion2[0] ;
result[1] = IPNum1_2 > IPNum2_2 ? IPRegion1[1] : IPRegion2[1] ;
return result;
#endregion
}
}
return null;
}
#endregion
#region 私有方法
///
/// 移位转换_8
///
///
///
///
private static ushort MAKEWORD(byte bLow,byte bHigh)
{
return ((ushort) (((byte) (bLow)) | ((ushort) ((byte) (bHigh))) << 8));
}
///
/// 移位转换_16
///
///
/// 【判断一个IP区间(或IP)是否被另一个IP区间所包含】
///
private static uint MAKELONG(ushort bLow,ushort bHigh)
{
return ((uint) (((ushort) (bLow)) | ((uint) ((ushort) (bHigh))) << 16));
}
#endregion
}
}
推荐阅读
- 一个人的旅行,三亚
- 一个小故事,我的思考。
- 一个人的碎碎念
- 七年之痒之后
- 我从来不做坏事
- 异地恋中,逐渐适应一个人到底意味着什么()
- 迷失的世界(二十七)
- live|live to inspire 一个普通上班族的流水账0723
- 遗憾是生活的常态,但孝顺这件事,我希望每一个人都不留遗憾
- NO.38|NO.38 我不是嫁不出去,而是不想嫁