示例#1
0
        public void run_example()
        {
            // get regions from resource
            var regions = IPRangesParser.ParseFromResources().SelectMany(x => x.Regions);

            // create regions dictionary
            var dictionary = new IPRangeDictionary <IPRangesRegion>();

            foreach (var region in regions)
            {
                foreach (var range in region.Ranges)
                {
                    dictionary.Add(range, region);
                }
            }

            var ipAddress = IPAddress.Parse("23.20.123.123");

            // test if IP address is within any region
            var foundRegion = dictionary[ipAddress]; // throws KeyNotFoundException if not found

            Console.WriteLine("IP address '{0}' is in region '{1}'", ipAddress, foundRegion);

            // or better (to avoid KeyNotFoundException)
            IPRangesRegion value;

            if (dictionary.TryGetValue(ipAddress, out value))
            {
                Console.WriteLine("IP address '{0}' is in region '{1}'", ipAddress, foundRegion);
            }
        }
示例#2
0
        public void describe_dictionary_with_many_nonoverlapping_IPv6_ranges_try_get_value_for_ip_in_range(string ipAddress, string expected)
        {
            var dict = new IPRangeDictionary <string>();

            dict.Add(IPAddress.Parse("fe80:0:0:0:0010::1"), IPAddress.Parse("fe80:0:0:0:0015::1"), "A");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0020::1"), IPAddress.Parse("fe80:0:0:0:0025::1"), "B");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0030::1"), IPAddress.Parse("fe80:0:0:0:0035::1"), "C");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0040::1"), IPAddress.Parse("fe80:0:0:0:0045::1"), "D");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0050::1"), IPAddress.Parse("fe80:0:0:0:0055::1"), "E");

            string value;

            Assert.AreEqual(true, dict.TryGetValue(IPAddress.Parse(ipAddress), out value));
            Assert.AreEqual(expected, value);
        }
示例#3
0
        public void describe_dictionary_with_many_nonoverlapping_IPv4_ranges_try_get_value_for_ip_in_range(string ipAddress, string expected)
        {
            var dict = new IPRangeDictionary <string>();

            dict.Add(IPAddress.Parse("192.168.1.10"), IPAddress.Parse("192.168.1.15"), "A");
            dict.Add(IPAddress.Parse("192.168.1.20"), IPAddress.Parse("192.168.1.25"), "B");
            dict.Add(IPAddress.Parse("192.168.1.30"), IPAddress.Parse("192.168.1.35"), "C");
            dict.Add(IPAddress.Parse("192.168.1.40"), IPAddress.Parse("192.168.1.45"), "D");
            dict.Add(IPAddress.Parse("192.168.1.50"), IPAddress.Parse("192.168.1.55"), "E");

            string value;

            Assert.AreEqual(true, dict.TryGetValue(IPAddress.Parse(ipAddress), out value));
            Assert.AreEqual(expected, value);
        }
示例#4
0
        public void PerformanceTestIpv6()
        {
            const long count = 1000000;
            var        dict  = new IPRangeDictionary <string>();

            for (var i = 1; i <= 100; i++)
            {
                for (var j = 1; j <= 100; j++)
                {
                    dict.Add(IPAddress.Parse("fe80:" + i.ToString("X4") + ":" + j.ToString("X4") + ":0:0::1"), IPAddress.Parse("fe80:" + i.ToString("X4") + ":" + j.ToString("X4") + ":0:0::1"), null);
                }
            }

            string value;
            var    stopwatch = Stopwatch.StartNew();

            for (var i = 0; i < count; i++)
            {
                dict.TryGetValue(IPAddress.IPv6Any, out value);
            }
            stopwatch.Stop();
            Console.WriteLine("Elapsed in " + stopwatch.ElapsedMilliseconds + "ms, " + count * 1000 / stopwatch.ElapsedMilliseconds);
        }
        public void run_example()
        {
            // get regions from resource
            var regions = IPRangesParser.ParseFromResources().SelectMany(x => x.Regions);

            // create regions dictionary
            var dictionary = new IPRangeDictionary<IPRangesRegion>();
            foreach (var region in regions)
                foreach (var range in region.Ranges)
                    dictionary.Add(range, region);

            var ipAddress = IPAddress.Parse("23.20.123.123");

            // test if IP address is within any region
            var foundRegion = dictionary[ipAddress]; // throws KeyNotFoundException if not found
            Console.WriteLine("IP address '{0}' is in region '{1}'", ipAddress, foundRegion);

            // or better (to avoid KeyNotFoundException)
            IPRangesRegion value;
            if (dictionary.TryGetValue(ipAddress, out value))
                Console.WriteLine("IP address '{0}' is in region '{1}'", ipAddress, foundRegion);
        }
        public void PerformanceTestIpv6()
        {
            const long count = 1000000;
            var dict = new IPRangeDictionary<string>();
            for (var i = 1; i <= 100; i++)
                for (var j = 1; j <= 100; j++)
                    dict.Add(IPAddress.Parse("fe80:" + i.ToString("X4") + ":" + j.ToString("X4") + ":0:0::1"), IPAddress.Parse("fe80:" + i.ToString("X4") + ":" + j.ToString("X4") + ":0:0::1"), null);

            string value;
            var stopwatch = Stopwatch.StartNew();
            for (var i = 0; i < count; i++)
                dict.TryGetValue(IPAddress.IPv6Any, out value);
            stopwatch.Stop();
            Console.WriteLine("Elapsed in " + stopwatch.ElapsedMilliseconds + "ms, " + count * 1000 / stopwatch.ElapsedMilliseconds);
        }
        public void describe_dictionary_with_many_nonoverlapping_IPv6_ranges_try_get_value_for_ip_in_range(string ipAddress, string expected)
        {
            var dict = new IPRangeDictionary<string>();
            dict.Add(IPAddress.Parse("fe80:0:0:0:0010::1"), IPAddress.Parse("fe80:0:0:0:0015::1"), "A");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0020::1"), IPAddress.Parse("fe80:0:0:0:0025::1"), "B");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0030::1"), IPAddress.Parse("fe80:0:0:0:0035::1"), "C");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0040::1"), IPAddress.Parse("fe80:0:0:0:0045::1"), "D");
            dict.Add(IPAddress.Parse("fe80:0:0:0:0050::1"), IPAddress.Parse("fe80:0:0:0:0055::1"), "E");

            string value;
            Assert.Equal(true, dict.TryGetValue(IPAddress.Parse(ipAddress), out value));
            Assert.Equal(expected, value);
        }
        public void describe_dictionary_with_many_nonoverlapping_IPv4_ranges_try_get_value_for_ip_in_range(string ipAddress, string expected)
        {
            var dict = new IPRangeDictionary<string>();
            dict.Add(IPAddress.Parse("192.168.1.10"), IPAddress.Parse("192.168.1.15"), "A");
            dict.Add(IPAddress.Parse("192.168.1.20"), IPAddress.Parse("192.168.1.25"), "B");
            dict.Add(IPAddress.Parse("192.168.1.30"), IPAddress.Parse("192.168.1.35"), "C");
            dict.Add(IPAddress.Parse("192.168.1.40"), IPAddress.Parse("192.168.1.45"), "D");
            dict.Add(IPAddress.Parse("192.168.1.50"), IPAddress.Parse("192.168.1.55"), "E");

            string value;
            Assert.Equal(true, dict.TryGetValue(IPAddress.Parse(ipAddress), out value));
            Assert.Equal(expected, value);
        }