Пример #1
0
 public IPLocation Query(string ip)
 {
     if (!regex.Match(ip).Success)
     {
         ip = "300.300.300.300";
     }
     IPLocation ipLocation = new IPLocation() { IP = ip };
     long intIP = IpToInt(ip);
     if ((intIP >= IpToInt("127.0.0.1") && (intIP <= IpToInt("127.255.255.255"))))
     {
         ipLocation.Country = "本机内部环回地址";
         ipLocation.Local = "";
     }
     else
     {
         if ((((intIP >= IpToInt("0.0.0.0")) && (intIP <= IpToInt("2.255.255.255"))) || ((intIP >= IpToInt("64.0.0.0")) && (intIP <= IpToInt("126.255.255.255")))) ||
         ((intIP >= IpToInt("58.0.0.0")) && (intIP <= IpToInt("60.255.255.255"))))
         {
             ipLocation.Country = "网络保留地址";
             ipLocation.Local = "";
         }
     }
     long right = ipCount;
     long left = 0L;
     long middle = 0L;
     long startIp = 0L;
     long endIpOff = 0L;
     long endIp = 0L;
     int countryFlag = 0;
     while (left < (right - 1L))
     {
         middle = (right + left) / 2L;
         startIp = GetStartIp(middle, out endIpOff);
         if (intIP == startIp)
         {
             left = middle;
             break;
         }
         if (intIP > startIp)
         {
             left = middle;
         }
         else
         {
             right = middle;
         }
     }
     startIp = GetStartIp(left, out endIpOff);
     endIp = GetEndIp(endIpOff, out countryFlag);
     if ((startIp <= intIP) && (endIp >= intIP))
     {
         string local;
         ipLocation.Country = GetCountry(endIpOff, countryFlag, out local);
         ipLocation.Local = local;
     }
     else
     {
         ipLocation.Country = "未知的IP地址";
         ipLocation.Local = "";
     }
     return ipLocation;
 }
Пример #2
0
        public IPLocation Query(string ip)
        {
            if (!regex.Match(ip).Success)
            {
                ip = "300.300.300.300";
            }
            IPLocation ipLocation = new IPLocation()
            {
                IP = ip
            };
            long intIP = IpToInt(ip);

            if ((intIP >= IpToInt("127.0.0.1") && (intIP <= IpToInt("127.255.255.255"))))
            {
                ipLocation.Country = "本机内部环回地址";
                ipLocation.Local   = "";
            }
            else
            {
                if ((((intIP >= IpToInt("0.0.0.0")) && (intIP <= IpToInt("2.255.255.255"))) || ((intIP >= IpToInt("64.0.0.0")) && (intIP <= IpToInt("126.255.255.255")))) ||
                    ((intIP >= IpToInt("58.0.0.0")) && (intIP <= IpToInt("60.255.255.255"))))
                {
                    ipLocation.Country = "网络保留地址";
                    ipLocation.Local   = "";
                }
            }
            long right       = ipCount;
            long left        = 0L;
            long middle      = 0L;
            long startIp     = 0L;
            long endIpOff    = 0L;
            long endIp       = 0L;
            int  countryFlag = 0;

            while (left < (right - 1L))
            {
                middle  = (right + left) / 2L;
                startIp = GetStartIp(middle, out endIpOff);
                if (intIP == startIp)
                {
                    left = middle;
                    break;
                }
                if (intIP > startIp)
                {
                    left = middle;
                }
                else
                {
                    right = middle;
                }
            }
            startIp = GetStartIp(left, out endIpOff);
            endIp   = GetEndIp(endIpOff, out countryFlag);
            if ((startIp <= intIP) && (endIp >= intIP))
            {
                string local;
                ipLocation.Country = GetCountry(endIpOff, countryFlag, out local);
                ipLocation.Local   = local;
            }
            else
            {
                ipLocation.Country = "未知的IP地址";
                ipLocation.Local   = "";
            }
            return(ipLocation);
        }