Beispiel #1
0
        private IPLocation GetIPInfo(FileStream fs, BinaryReader reader, long offset, IPAddress ipToLoc, Byte[] ipBytes)
        {
            fs.Position = offset;
            //To confirm that the given ip is within the range of record IP range
            byte[] endIP    = reader.ReadBytes(4);
            uint   endIpVal = BitConverter.ToUInt32(endIP, 0);
            uint   ipVal    = BitConverter.ToUInt32(ipBytes, 0);

            if (endIpVal < ipVal)
            {
                return(null);
            }

            string country;
            string zone;
            //Read the Redirection pattern byte
            Byte pattern = reader.ReadByte();

            if (pattern == RedirectMode.Mode_1)
            {
                Byte[] countryOffsetBytes = reader.ReadBytes(IPFormat.RecOffsetLength);
                uint   countryOffset      = IPFormat.ToUint(countryOffsetBytes);

                if (countryOffset == 0)
                {
                    return(GetUnknownLocation(ipToLoc));
                }

                fs.Position = countryOffset;
                if (fs.ReadByte() == RedirectMode.Mode_2)
                {
                    return(ReadMode2Record(fs, reader, ipToLoc));
                }
                else
                {
                    fs.Position--;
                    country = ReadString(reader);
                    zone    = ReadZone(fs, reader, Convert.ToUInt32(fs.Position));
                }
            }
            else if (pattern == RedirectMode.Mode_2)
            {
                return(ReadMode2Record(fs, reader, ipToLoc));
            }
            else
            {
                fs.Position--;
                country = ReadString(reader);
                zone    = ReadZone(fs, reader, Convert.ToUInt32(fs.Position));
            }
            return(new IPLocation(ipToLoc, country, zone));
        }
Beispiel #2
0
        //When it is in Mode 2
        private IPLocation ReadMode2Record(FileStream fs, BinaryReader reader, IPAddress ip)
        {
            uint countryOffset = IPFormat.ToUint(reader.ReadBytes(IPFormat.RecOffsetLength));
            uint curOffset     = Convert.ToUInt32(fs.Position);

            if (countryOffset == 0)
            {
                return(GetUnknownLocation(ip));
            }
            fs.Position = countryOffset;
            string country = ReadString(reader);
            string zone    = ReadZone(fs, reader, curOffset);

            return(new IPLocation(ip, country, zone));
        }
Beispiel #3
0
        //Retrieve the zone info
        private string ReadZone(FileStream fs, BinaryReader reader, uint offset)
        {
            fs.Position = offset;
            byte b = reader.ReadByte();

            if (b == RedirectMode.Mode_1 || b == RedirectMode.Mode_2)
            {
                uint zoneOffset = IPFormat.ToUint(reader.ReadBytes(3));
                if (zoneOffset == 0)
                {
                    return(IPFormat.UnknownZone);
                }
                return(ReadZone(fs, reader, zoneOffset));
            }
            else
            {
                fs.Position--;
                return(ReadString(reader));
            }
        }