Exemple #1
0
 // constructor - load the lists
 public Retriever()
 {
     using (var trieStream = new MemoryStream(Resources.geonet))
     {
         mTrie = Serializer.Deserialize <BitVectorTrie>(trieStream);
     }
 }
Exemple #2
0
 // constructor - load the lists
 public Retriever()
 {
     using (var trieStream = new MemoryStream(Resources.geonet))
     {
         mTrie = Serializer.Deserialize<BitVectorTrie>(trieStream);
     }
 }
Exemple #3
0
        // read from a nic stream into a trie - only used when
        // generating the binary file
        private static void Read(Stream ms, BitVectorTrie trie)
        {
            using (var nccin = new StreamReader(ms))
            {
                string line;
                while ((line = nccin.ReadLine()) != null)
                {
                    var data = line.Split('|');

                    // Make the following assumption:
                    // if 2nd entry is 2 chars, it's a country code,
                    // and 3rd entry is 4 chars (ipv4 / ipv6) we're good to look up
                    if ((data.Length <= 3))
                    {
                        continue;
                    }

                    // the country is always upper case, so don't waste time upper-casing
                    // the scheme is always lower case too
                    var country = data[1];
                    var scheme  = data[2];

                    if (country.Length != 2 || scheme.Length != 4)
                    {
                        continue;
                    }

                    var ip = data[3];
                    switch (scheme)
                    {
                    case "ipv4":
                        trie.Add(Ip4ToBitVector(ip), country);
                        break;

                    case "ipv6":
                        trie.Add(Ip6ToBitVector(ip), country);
                        break;
                    }
                }
            }
        }
Exemple #4
0
        // static utility to build the trie file
        public static string BuildProtoBufTrie(IEnumerable <byte[]> lists)
        {
            // initialize
            var trie = new BitVectorTrie();

            foreach (var list in lists)
            {
                using (var ms = new MemoryStream(list)) Read(ms, trie);
            }

            // serialize
            using (var trieStream = new MemoryStream())
            {
                Serializer.Serialize(trieStream, trie);
                using (var file = new FileStream("geonet.bin", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    trieStream.WriteTo(file);
                    return(file.Name);
                }
            }
            // don't forget to add it to the resources!
        }
Exemple #5
0
        // static utility to build the trie file
        public static string BuildProtoBufTrie(IEnumerable<byte[]> lists)
        {
            // initialize
            var trie = new BitVectorTrie();

            foreach (var list in lists)
            {
                using (var ms = new MemoryStream(list)) Read(ms, trie);
            }

            // serialize
            using (var trieStream = new MemoryStream())
            {
                Serializer.Serialize(trieStream, trie);
                using (var file = new FileStream("geonet.bin", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    trieStream.WriteTo(file);
                    return file.Name;
                }
            }
            // don't forget to add it to the resources!
        }
Exemple #6
0
        // read from a nic stream into a trie - only used when
        // generating the binary file
        private static void Read(Stream ms, BitVectorTrie trie)
        {
            using (var nccin = new StreamReader(ms))
            {

                string line;
                while ((line = nccin.ReadLine()) != null)
                {
                    var data = line.Split('|');

                    // Make the following assumption:
                    // if 2nd entry is 2 chars, it's a country code,
                    // and 3rd entry is 4 chars (ipv4 / ipv6) we're good to look up
                    if ((data.Length <= 3)) continue;

                    // the country is always upper case, so don't waste time upper-casing
                    // the scheme is always lower case too
                    var country = data[1];
                    var scheme = data[2];

                    if (country.Length != 2 || scheme.Length != 4) continue;

                    var ip = data[3];
                    switch (scheme)
                    {
                        case "ipv4":
                            trie.Add(Ip4ToBitVector(ip), country);
                            break;
                        case "ipv6":
                            trie.Add(Ip6ToBitVector(ip), country);
                            break;
                    }
                }
            }
        }