static void loadIpCountryTable(Root root, string fileName)
    {
        FileStream   fs = new FileStream(fileName, FileMode.Open);
        StreamReader sr = new StreamReader(fs);
        string       line;

        while ((line = sr.ReadLine()) != null)
        {
            int sep1 = line.IndexOf('|');
            if (sep1 >= 0)
            {
                int sep2 = line.IndexOf('|', sep1 + 1);
                int sep3 = line.IndexOf('|', sep2 + 1);
                int sep4 = line.IndexOf('|', sep3 + 1);
                if (sep2 > sep1 && sep4 > sep3)
                {
                    String iso = line.Substring(sep1 + 1, sep2 - sep1 - 1).ToUpper();
                    String ip  = line.Substring(sep3 + 1, sep4 - sep3 - 1);
                    if (ip.IndexOf('.') > 0 && iso.Length == 2)
                    {
                        Country c = (Country)root.countries[iso];
                        if (c == null)
                        {
                            Console.WriteLine("Unknown country code: " + iso);
                        }
                        else
                        {
                            root.trie.Add(PatriciaTrieKey.FromIpAddress(ip), c);
                        }
                    }
                }
            }
        }
    }
Ejemplo n.º 2
0
    public static void Main(string[] args)
    {
        IDatabase db = DatabaseFactory.CreateDatabase();

        db.Open("ipcountry.dbs", PagePoolSize);
        Root root = (Root)db.Root;

        if (root == null)
        {
            root           = new Root();
            root.countries = db.CreateIndex <string, Country>(IndexType.Unique);
            root.trie      = db.CreatePatriciaTrie <Country>();
            loadCountries(root.countries);
            db.Root = root;
        }
        for (int i = 0; i < args.Length; i++)
        {
            loadIpCountryTable(root, args[i]);
        }

        string ip;

        while ((ip = Console.ReadLine()) != null)
        {
            Country country = root.trie.FindBestMatch(PatriciaTrieKey.FromIpAddress(ip));
            if (country != null)
            {
                Console.WriteLine(ip + "->" + country.name);
            }
        }
        db.Close();
    }
    public static void Main(string[] args)
    {
        Storage db = StorageFactory.Instance.CreateStorage();

        db.Open("ipcountry.dbs", PagePoolSize);
        Root root = (Root)db.Root;

        if (root == null)
        {
            root = new Root();
#if USE_GENERICS
            root.countries = db.CreateIndex <string, Country>(true);
            root.trie      = db.CreatePatriciaTrie <Country>();
#else
            root.countries = db.CreateIndex(typeof(string), true);
            root.trie      = db.CreatePatriciaTrie();
#endif
            loadCountries(root.countries);
            db.Root = root;
        }
        for (int i = 0; i < args.Length; i++)
        {
            loadIpCountryTable(root, args[i]);
        }

        string ip;
        while ((ip = Console.ReadLine()) != null)
        {
#if USE_GENERICS
            Country country = root.trie.FindBestMatch(PatriciaTrieKey.FromIpAddress(ip));
#else
            Country country = (Country)root.trie.FindBestMatch(PatriciaTrieKey.FromIpAddress(ip));
#endif
            if (country != null)
            {
                Console.WriteLine(ip + "->" + country.name);
            }
        }
        db.Close();
    }