判断一个IP区间(或IP)是否被另一个IP区间所包含

以下方法实现判断一个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!");

    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 私有方法

  /// <summary>
  /// 移位转换_8
  /// </summary>
  /// <param name="bLow"></param>
  /// <param name="bHigh"></param>
  /// <returns></returns>
  private static ushort MAKEWORD(byte bLow,byte bHigh)
  {
   return ((ushort) (((byte) (bLow)) | ((ushort) ((byte) (bHigh))) << 8));
  }

  /// <summary>
  /// 移位转换_16
  /// </summary>
  /// <param name="bLow"></param>
  /// <param name="bHigh"></param>
  /// <returns></returns>
  private static uint MAKELONG(ushort bLow,ushort bHigh)
  {
   return ((uint) (((ushort) (bLow)) | ((uint) ((ushort) (bHigh))) << 16));
  }
  
  #endregion
 }
}




Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1539209

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值