예제 #1
0
    protected void Button_Query_Click(object sender, EventArgs e)
    {
        EZGoal.CZIPDB db = new EZGoal.CZIPDB(Server.MapPath("~/App_Data/qqwry.dat"));


        EZGoal.IPRecord data = db.Query(TextBox_IP.Text.Trim());

        Label_Result.Text = string.Format("{0} {1} {2}", data.IP, data.Location1, data.Location2);
    }
예제 #2
0
        public IPRecord Query(string ip)
        {
            if (!regex.IsMatch(ip))
            {
                throw new ArgumentException("IP Address Format Error");
            }
            IPRecord ipLocation = new IPRecord()
            {
                IP = ip
            };
            UInt32 ipInt = IpStrToInt(ip);

            if ((ipInt >> 22) == 0x191)
            {
                //100.64.0.0 - 100.127.255.255
                ipLocation.Location1 = "Carrier-Grade NAT";
                ipLocation.Location2 = "";
            }
            else if ((ipInt >> 24) == 0x7F)
            {
                //127.0.0.0 - 127.255.255.255
                ipLocation.Location1 = "Loopback Network";
                ipLocation.Location2 = "";
            }
            else if ((ipInt >> 24) == 0x0A || (ipInt >> 20) == 0xAC1 || (ipInt >> 16) == 0xC0A8)
            {
                //10.0.0.0 - 10.255.255.255, 172.16.0.0 - 172.31.255.255, 192.168.0.0 - 192.168.255.255
                ipLocation.Location1 = "Local Area Network";
                ipLocation.Location2 = "";
            }
            else
            {
                uint lowerIndex    = 0;
                uint upperIndex    = ipCount;
                uint middle        = 0;
                uint lowerIp       = 0;
                uint upperIp       = 0;
                uint upperIpOffset = 0;
                byte flag          = 0;
                while (lowerIndex < (upperIndex - 1))
                {
                    middle  = (upperIndex + lowerIndex) >> 1;
                    lowerIp = GetLowerIp(middle, out upperIpOffset);
                    if (ipInt == lowerIp)
                    {
                        lowerIndex = middle;
                        break;
                    }
                    if (ipInt > lowerIp)
                    {
                        lowerIndex = middle;
                    }
                    else
                    {
                        upperIndex = middle;
                    }
                }
                lowerIp = GetLowerIp(lowerIndex, out upperIpOffset);
                upperIp = GetUpperIp(upperIpOffset, out flag);
                if ((lowerIp <= ipInt) && (upperIp >= ipInt))
                {
                    string local;
                    ipLocation.Location1 = GetLocation(upperIpOffset, flag, out local);
                    ipLocation.Location2 = local;
                }
                else
                {
                    ipLocation.Location1 = "Unknown";
                    ipLocation.Location2 = "";
                }
            }
            return(ipLocation);
        }