示例#1
0
    public void Enumerate_IPv6()
    {
        var ips = IPAddressRange.Parse("fe80::d503:4ee:3882:c586/120").AsEnumerable().ToArray();

        ips.Length.Is(256);
        ips.First().Is(IPAddress.Parse("fe80::d503:4ee:3882:c500"));
        ips.Last().Is(IPAddress.Parse("fe80::d503:4ee:3882:c5ff"));
    }
    public void ContainsTest_IPv6_with_ScopeId()
    {
        var range = IPAddressRange.Parse("FE80::%eth0/10");

        range.Contains(IPAddress.Parse("::1")).Is(false);
        range.Contains(IPAddress.Parse("fe80::d503:4ee:3882:c586")).Is(true);
        range.Contains(IPAddress.Parse("fe80::d503:4ee:3882:c586%4")).Is(true);
    }
    public void ContainsTest_IPv6()
    {
        var range = IPAddressRange.Parse("FE80::/10");

        range.Contains(IPAddress.Parse("::1")).Is(false);
        range.Contains(IPAddress.Parse("fe80::d503:4ee:3882:c586")).Is(true);
        range.Contains(IPAddress.Parse("fe80::d503:4ee:3882:c586%3")).Is(true);
    }
示例#4
0
    public void ParseTest_IPv4_Bitmask()
    {
        var range = IPAddressRange.Parse("192.168.1.0/255.255.255.0");

        range.Begin.AddressFamily.Is(AddressFamily.InterNetwork);
        range.Begin.ToString().Is("192.168.1.0");
        range.End.AddressFamily.Is(AddressFamily.InterNetwork);
        range.End.ToString().Is("192.168.1.255");
    }
示例#5
0
    public void ParseTest_IPv4_Begin_to_End()
    {
        var range = IPAddressRange.Parse("192.168.60.26-192.168.60.37");

        range.Begin.AddressFamily.Is(AddressFamily.InterNetwork);
        range.Begin.ToString().Is("192.168.60.26");
        range.End.AddressFamily.Is(AddressFamily.InterNetwork);
        range.End.ToString().Is("192.168.60.37");
    }
示例#6
0
    public void ParseTest_IPv6_CIDR()
    {
        var range = IPAddressRange.Parse("fe80::/10");

        range.Begin.AddressFamily.Is(AddressFamily.InterNetworkV6);
        range.Begin.ToString().Is("fe80::");
        range.End.AddressFamily.Is(AddressFamily.InterNetworkV6);
        range.End.ToString().Is("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
    }
示例#7
0
    public void ParseTest_IPv4_Uniaddress()
    {
        var range = IPAddressRange.Parse("192.168.60.13");

        range.Begin.AddressFamily.Is(AddressFamily.InterNetwork);
        range.Begin.ToString().Is("192.168.60.13");
        range.End.AddressFamily.Is(AddressFamily.InterNetwork);
        range.End.ToString().Is("192.168.60.13");
    }
示例#8
0
        /// <summary>
        /// Determines whether or not an IP address lies within a private IP range
        /// </summary>
        /// <param name="ipAddress">The IP address to check</param>
        /// <returns>True if the IP address is a private IP address. False otherwise</returns>
        public static bool IsPrivateIpAddress(IPAddress ipAddress)
        {
            if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
            {
                //IPv4 Loopback
                var rangeIpv4Loopback = IPAddressRange.Parse("127.0.0.0/8");
                //IPv4 Private
                var rangeIpv4Priv1 = IPAddressRange.Parse("10.0.0.0/8");
                var rangeIpv4Priv2 = IPAddressRange.Parse("172.16.0.0/12");
                var rangeIpv4Priv3 = IPAddressRange.Parse("192.168.0.0/16");
                //IPv4 Link Local
                var rangeIpv4Local = IPAddressRange.Parse("169.254.0.0/16");

                //Loopback
                if (rangeIpv4Loopback.Contains(ipAddress))
                {
                    return(true);
                }
                //Private
                if (rangeIpv4Priv1.Contains(ipAddress) || rangeIpv4Priv2.Contains(ipAddress) || rangeIpv4Priv3.Contains(ipAddress))
                {
                    return(true);
                }
                //Link Local
                if (rangeIpv4Local.Contains(ipAddress))
                {
                    return(true);
                }
            }
            else if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
            {
                //IPv6 Loopback
                var rangeIpv6Loopback = IPAddressRange.Parse("::1/128");
                //IPv6 Unique Local
                var rangeIpv6Priv = IPAddressRange.Parse("fc00::/7");
                //IPv6 Link Local
                var rangeIpv6Local = IPAddressRange.Parse("fe80::/10");

                //Loopback
                if (rangeIpv6Loopback.Contains(ipAddress))
                {
                    return(true);
                }
                //Unique Local
                if (rangeIpv6Priv.Contains(ipAddress))
                {
                    return(true);
                }
                //Link Local
                if (rangeIpv6Local.Contains(ipAddress))
                {
                    return(true);
                }
            }

            return(false);
        }
示例#9
0
    public void ParseTest_IPv4_CIDR()
    {
        var range = IPAddressRange.Parse("219.165.64.0/19");

        range.Begin.AddressFamily.Is(AddressFamily.InterNetwork);
        range.Begin.ToString().Is("219.165.64.0");
        range.End.AddressFamily.Is(AddressFamily.InterNetwork);
        range.End.ToString().Is("219.165.95.255");
    }
示例#10
0
    public void ParseTest_IPv4_CIDR_Max()
    {
        var range = IPAddressRange.Parse("219.165.64.73/32");

        range.Begin.AddressFamily.Is(AddressFamily.InterNetwork);
        range.Begin.ToString().Is("219.165.64.73");
        range.End.AddressFamily.Is(AddressFamily.InterNetwork);
        range.End.ToString().Is("219.165.64.73");
    }
示例#11
0
        public void ParseSucceeds(string input, string expectedBegin, string expectedEnd)
        {
            Console.WriteLine("TestCase: \"{0}\", Expected Begin: {1}, End: {2}", input, expectedBegin, expectedEnd);
            var range = IPAddressRange.Parse(input);

            range.IsNot(null);
            Console.WriteLine("  Result: Begin: {0}, End: {1}", range.Begin, range.End);
            range.Begin.ToString().Is(expectedBegin);
            range.End.ToString().Is(expectedEnd);
        }
示例#12
0
 public void Equals_SameRange_ReturnsFalse()
 {
     TestContext.Run((string input1, string input2) =>
     {
         Console.WriteLine("TestCase: \"{0}\" and \"{1}\"", input1, input2);
         var range1 = IPAddressRange.Parse(input1);
         var range2 = IPAddressRange.Parse(input2);
         range1.Equals(range2).IsFalse();
     });
 }
示例#13
0
 public void GetHashCode_DifferentRanges_HashCodesAreDifferent()
 {
     TestContext.Run((string input1, string input2) =>
     {
         Console.WriteLine("TestCase: \"{0}\" and \"{1}\"", input1, input2);
         var range1 = IPAddressRange.Parse(input1);
         var range2 = IPAddressRange.Parse(input2);
         range1.GetHashCode().IsNot(range2.GetHashCode());
     });
 }
示例#14
0
 public void GetHashCode_SameRange_HashCodesAreSame()
 {
     TestContext.Run((string input) =>
     {
         Console.WriteLine("TestCase: \"{0}\"", input);
         var range1 = IPAddressRange.Parse(input);
         var range2 = IPAddressRange.Parse(input);
         range1.GetHashCode().Is(range2.GetHashCode());
     });
 }
示例#15
0
        public bool IsInRange(string address, string subnet)
        {
            if (address == null)
            {
                return(false);
            }
            var range = IPAddressRange.Parse(subnet);

            return(range.Contains(IPAddress.Parse(address)));
        }
示例#16
0
        private void IPAddressTest()
        {
            var ip      = "13.68.136.211/8";
            var address = IPAddressRange.Parse(ip);

            ConsoleDisplay.ShowObject(address, nameof(address));
            var shouldPerformSSO = ShouldPerformSSO("::1");

            ConsoleDisplay.ShowObject(shouldPerformSSO, nameof(shouldPerformSSO));
        }
 public void ToCidrString_Output()
 {
     TestContext.Run((string input, string expected) =>
     {
         Console.WriteLine("TestCase: \"{0}\", Expected: \"{1}\"", input, expected);
         var output = IPAddressRange.Parse(input).ToCidrString();
         Console.WriteLine("  Result: \"{0}\"", output);
         output.Is(expected);
     });
 }
示例#18
0
    public void ContainsTest_with_IPV4andv6_is_False_ever()
    {
        var fullRangeIPv6 = IPAddressRange.Parse("::-fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");

        fullRangeIPv6.Contains(IPAddressRange.Parse("192.168.0.0/24")).Is(false);

        var fullRangeIPv4 = IPAddressRange.Parse("0.0.0.0-255.255.255.255");

        fullRangeIPv4.Contains(IPAddressRange.Parse("::1-::2")).Is(false);
    }
 public void GetPrefixLength_Success()
 {
     TestContext.Run((string input, int expected) =>
     {
         Console.WriteLine("TestCase: \"{0}\", Expected: \"{1}\"", input, expected);
         var output = IPAddressRange.Parse(input).GetPrefixLength();
         Console.WriteLine("  Result: \"{0}\"", output);
         output.Is(expected);
     });
 }
    private static void ContainsTest_RewriteProperties_IPv6(IPAddressRange range)
    {
        range.Begin = IPAddress.Parse("fe80::");
        range.End   = IPAddress.Parse("fe80::d503:4ee:3882:c586");

        range.Contains(IPAddress.Parse("::1")).Is(false);
        range.Contains(IPAddress.Parse("fe80::d503:4ee:3882:c586")).Is(true);
        range.Contains(IPAddress.Parse("fe80::d503:4ee:3882:c586%3")).Is(true);

        range = IPAddressRange.Parse("::/0");
        range.Contains(IPAddress.Parse("::1")).Is(true);
    }
示例#21
0
        public void Parse_SingleAddress()
        {
            //arrange
            IPAddress address = IPAddress.Parse("192.168.1.1");

            //action
            IPAddressRange actual = IPAddressRange.Parse(address.ToString());

            //assert
            Assert.AreEqual(address, actual.Lower);
            Assert.AreEqual(address, actual.Upper);
        }
示例#22
0
    public void ContainsTest_IPv4()
    {
        var range = IPAddressRange.Parse("192.168.60.26-192.168.60.37");

        range.Contains(IPAddress.Parse("192.168.60.25")).Is(false);
        range.Contains(IPAddress.Parse("192.168.60.26")).Is(true);
        range.Contains(IPAddress.Parse("192.168.60.27")).Is(true);

        range.Contains(IPAddress.Parse("192.168.60.36")).Is(true);
        range.Contains(IPAddress.Parse("192.168.60.37")).Is(true);
        range.Contains(IPAddress.Parse("192.168.60.38")).Is(false);
    }
示例#23
0
 public bool IsValid(string ipNetwork)
 {
     try
     {
         var range = IPAddressRange.Parse(ipNetwork);
         return(range != null);
     }
     catch
     {
         return(false);
     }
 }
示例#24
0
        public void ToString_Output(string input, string expected)
        {
            Console.WriteLine("TestCase: \"{0}\", Expected: \"{1}\"", input, expected);
            var output = IPAddressRange.Parse(input).ToString();

            Console.WriteLine("  Result: \"{0}\"", output);
            output.Is(expected);

            var parsed = IPAddressRange.Parse(output).ToString();

            parsed.Is(expected, "Output of ToString() should be usable by Parse() and result in the same output");
        }
示例#25
0
        public override void Execute(TriggerBase trigger)
        {
            var ip     = trigger.Get <string>("ip");
            var reason = trigger.Get <string>("reason");

            if (!IPCAccessor.Instance.IsConnected)
            {
                trigger.ReplyError("IPC service not operational !");
                return;
            }

            try
            {
                IPAddressRange.Parse(ip);
            }
            catch
            {
                trigger.ReplyError("IP format '{0}' incorrect", ip);
                return;
            }

            var message = new BanIPMessage
            {
                IPRange   = ip,
                BanReason = reason,
            };

            var source = trigger.GetSource() as WorldClient;

            if (source != null)
            {
                message.BannerAccountId = source.Account.Id;
            }

            if (trigger.IsArgumentDefined("time"))
            {
                message.BanEndDate = DateTime.Now + TimeSpan.FromMinutes(trigger.Get <int>("time"));
            }
            else if (trigger.IsArgumentDefined("life"))
            {
                message.BanEndDate = null;
            }
            else
            {
                trigger.ReplyError("No ban duration given");
                return;
            }

            IPCAccessor.Instance.SendRequest(message,
                                             ok => trigger.Reply("IP {0} banned", ip),
                                             error => trigger.ReplyError("IP {0} not banned : {1}", ip, error.Message));
        }
示例#26
0
 public static bool TryParse(string ipRangeString, out IPAddressRange ipRange)
 {
     try
     {
         ipRange = IPAddressRange.Parse(ipRangeString);
         return(true);
     }
     catch (Exception)
     {
         ipRange = null;
         return(false);
     }
 }
        public void ParseSucceeds_IPV4_Cipdr_Begin_To_End()
        {
            var range = IPAddressRange.Parse("192.168.60.26-192.168.60.37");

            range.IsNotNull();

            // with "dash (–)" (0x2013) is also support.
            var range2 = IPAddressRange.Parse("192.168.60.26–192.168.60.37");

            range2.IsNotNull();
            range2.Begin.ToString().Is("192.168.60.26");
            range2.End.ToString().Is("192.168.60.37");
        }
示例#28
0
    public void ContainsTest_Range_is_False_IPv6()
    {
        var range             = IPAddressRange.Parse("fe80::d503:4ee:3882:c586-fe80::d504:4ee:3882:c586");
        var range1_overLeft   = IPAddressRange.Parse("fe80::d502:4ee:3882:c586-fe80::d503:4ee:3882:c586");
        var range2_overRight  = IPAddressRange.Parse("fe80::d503:4ef:3882:c586-fe80::d505:4ee:3882:c586");
        var range3_outOfLeft  = IPAddressRange.Parse("fe80::d501:4ee:3882:c586-fe80::d502:4ee:3882:c586");
        var range4_outOfRight = IPAddressRange.Parse("fe80::d505:4ee:3882:c586-fe80::d506:4ee:3882:c586");

        range.Contains(range1_overLeft).Is(false);
        range.Contains(range2_overRight).Is(false);
        range.Contains(range3_outOfLeft).Is(false);
        range.Contains(range4_outOfRight).Is(false);
    }
示例#29
0
    public void ContainsTest_Range_is_True_IPv6()
    {
        var range            = IPAddressRange.Parse("fe80::/10");
        var range1_same      = IPAddressRange.Parse("fe80::/10");
        var range2_samestart = IPAddressRange.Parse("fe80::-fe80::d503:4ee:3882:c586");
        var range3_sameend   = IPAddressRange.Parse("fe80::d503:4ee:3882:c586-febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
        var range4_subset    = IPAddressRange.Parse("fe80::d503:4ee:3882:c586-fe80::d504:4ee:3882:c586");

        range.Contains(range1_same).Is(true);
        range.Contains(range2_samestart).Is(true);
        range.Contains(range3_sameend).Is(true);
        range.Contains(range4_subset).Is(true);
    }
示例#30
0
    public void ContainsTest_Range_is_False_IPv4()
    {
        var range             = IPAddressRange.Parse("192.168.60.29-192.168.60.32");
        var range1_overLeft   = IPAddressRange.Parse("192.168.60.26-192.168.70.1");
        var range2_overRight  = IPAddressRange.Parse("192.168.50.1-192.168.60.37");
        var range3_outOfLeft  = IPAddressRange.Parse("192.168.50.30-192.168.50.31");
        var range4_outOfRight = IPAddressRange.Parse("192.168.70.30-192.168.70.31");

        range.Contains(range1_overLeft).Is(false);
        range.Contains(range2_overRight).Is(false);
        range.Contains(range3_outOfLeft).Is(false);
        range.Contains(range4_outOfRight).Is(false);
    }