public void ContainsTest_Range_is_False_IPv6()
    {
        var range             = new IPAddressRange("fe80::d503:4ee:3882:c586-fe80::d504:4ee:3882:c586");
        var range1_overLeft   = new IPAddressRange("fe80::d502:4ee:3882:c586-fe80::d503:4ee:3882:c586");
        var range2_overRight  = new IPAddressRange("fe80::d503:4ef:3882:c586-fe80::d505:4ee:3882:c586");
        var range3_outOfLeft  = new IPAddressRange("fe80::d501:4ee:3882:c586-fe80::d502:4ee:3882:c586");
        var range4_outOfRight = new IPAddressRange("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);
    }
    public void ContainsTest_Range_is_True_IPv4()
    {
        var range            = new IPAddressRange("192.168.60.26-192.168.60.37");
        var range1_same      = new IPAddressRange("192.168.60.26-192.168.60.37");
        var range2_samestart = new IPAddressRange("192.168.60.26-192.168.60.30");
        var range3_sameend   = new IPAddressRange("192.168.60.36-192.168.60.37");
        var range4_subset    = new IPAddressRange("192.168.60.29-192.168.60.32");

        range.Contains(range1_same).Is(true);
        range.Contains(range2_samestart).Is(true);
        range.Contains(range3_sameend).Is(true);
        range.Contains(range4_subset).Is(true);
    }
    public void ContainsTest_Range_is_True_IPv6()
    {
        var range            = new IPAddressRange("fe80::/10");
        var range1_same      = new IPAddressRange("fe80::/10");
        var range2_samestart = new IPAddressRange("fe80::-fe80::d503:4ee:3882:c586");
        var range3_sameend   = new IPAddressRange("fe80::d503:4ee:3882:c586-febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
        var range4_subset    = new IPAddressRange("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);
    }
    public void ContainsTest_Range_is_False_IPv4()
    {
        var range             = new IPAddressRange("192.168.60.29-192.168.60.32");
        var range1_overLeft   = new IPAddressRange("192.168.60.26-192.168.70.1");
        var range2_overRight  = new IPAddressRange("192.168.50.1-192.168.60.37");
        var range3_outOfLeft  = new IPAddressRange("192.168.50.30-192.168.50.31");
        var range4_outOfRight = new IPAddressRange("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);
    }
Ejemplo n.º 5
0
        /// <summary>
        /// Returns true if the ip is in the whitelist.
        /// </summary>
        /// <param name="ipStr"></param>
        /// <param name="whitelist"></param>
        /// <returns></returns>
        public static bool IsWhitelisted(string ipStr, string whitelist)
        {
            if (string.IsNullOrWhiteSpace(whitelist))
            {
                return(true);
            }
            IPAddress ip = IPAddress.Parse(ipStr);

            string[] parts = whitelist.Split(',');
            foreach (string rangeStr in parts)
            {
                try
                {
                    IPAddressRange range = IPAddressRange.Parse(rangeStr);
                    if (range.Contains(ip))
                    {
                        return(true);
                    }
                }
                catch
                {
                }
            }
            return(parts.Length == 0);
        }
        public bool Contains(IPAddress ipaddress)
        {
            if (_wildCard)
            {
                return(true);
            }

            return(_base.Contains(ipaddress));
        }
Ejemplo n.º 7
0
    public void ContainsTest_with_IPV4andv6_is_False_ever()
    {
        var fullRangeIPv6 = new IPAddressRange("::-fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");

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

        var fullRangeIPv4 = new IPAddressRange("0.0.0.0-255.255.255.255");

        fullRangeIPv4.Contains(new IPAddressRange("::1-::2")).Is(false);
    }
Ejemplo n.º 8
0
        public void CtorTest_MaskLength()
        {
            var range = new IPAddressRange(IPAddress.Parse("192.168.0.80"), 24);

            range.Begin.AddressFamily.Is(AddressFamily.InterNetwork);
            range.Begin.ToString().Is("192.168.0.0");
            range.End.AddressFamily.Is(AddressFamily.InterNetwork);
            range.End.ToString().Is("192.168.0.255");
            range.Contains(range.Begin).Is(true);
        }
Ejemplo n.º 9
0
        public void CtorTest_Single()
        {
            var range = new IPAddressRange(IPAddress.Parse("192.168.0.88"));

            range.Begin.AddressFamily.Is(AddressFamily.InterNetwork);
            range.Begin.ToString().Is("192.168.0.88");
            range.End.AddressFamily.Is(AddressFamily.InterNetwork);
            range.End.ToString().Is("192.168.0.88");
            range.Contains(range.Begin).Is(true);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Checks validate string as valid ip address/Subnet mask/ Range
        /// </summary>
        /// <param name="addressToValidate">Address string to validate</param>
        /// <returns>Boolean response indicating valid or invalid</returns>
        public bool Contains(System.Net.IPAddress addressToValidate)
        {
            if (!IsValidNow())
            {
                return(true);
            }

            switch (entryType)
            {
            case IPFenceEntryType.IPAddress:
                return(address.Equals(addressToValidate));

            case IPFenceEntryType.IPRange:
                return(range.Contains(addressToValidate));

            case IPFenceEntryType.IPSubnet:
                return(range.Contains(addressToValidate));
            }
            return(false);
        }
Ejemplo n.º 11
0
        public void CtorTest_IPv6_BeginEndAddresses_with_ScopeId()
        {
            var range = new IPAddressRange(
                begin: IPAddress.Parse("ff80::56%23"),
                end: IPAddress.Parse("ff80::789%23"));

            range.Begin.AddressFamily.Is(AddressFamily.InterNetworkV6);
            range.Begin.ToString().Is("ff80::56");
            range.End.AddressFamily.Is(AddressFamily.InterNetworkV6);
            range.End.ToString().Is("ff80::789");
            range.Contains(range.Begin).Is(true);
        }
Ejemplo n.º 12
0
        public static bool ContainsIp(List<string> ipRules, string clientIp)
        {
            var ip = ParseIp(clientIp);
            if (ipRules != null && ipRules.Any())
            {
                foreach (var rule in ipRules)
                {
                    var range = new IPAddressRange(rule);
                    if (range.Contains(ip))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
Ejemplo n.º 13
0
    public void ContainsTest_IPv4()
    {
        var range = new IPAddressRange("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);
    }
Ejemplo n.º 14
0
        public static bool ContainsIp(List <string> ipRules, string clientIp)
        {
            var ip = ParseIp(clientIp);

            if (ipRules != null && ipRules.Any())
            {
                foreach (var rule in ipRules)
                {
                    var range = new IPAddressRange(rule);
                    if (range.Contains(ip))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
    private static void ContainsTest_RewriteProperties_IPv4(IPAddressRange range)
    {
        range.Begin = IPAddress.Parse("192.168.60.26");
        range.End   = IPAddress.Parse("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);
    }
Ejemplo n.º 16
0
        public static bool ContainsIp(List <string> ipRules, string clientIp, out string rule)
        {
            rule = null;
            var ip = ParseIp(clientIp);

            if (ipRules?.Any() == true)
            {
                foreach (var r in ipRules)
                {
                    var range = new IPAddressRange(r);
                    if (range.Contains(ip))
                    {
                        rule = r;
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 17
0
 public static bool IsPrivateIpv4(this IPAddress ip)
 {
     if (ip == null)
     {
         return(false);
     }
     if (ip.AddressFamily != System.Net.Sockets.AddressFamily.InterNetwork)
     {
         return(false);
     }
     if (ClassCRange.Contains(ip) || ClassARange.Contains(ip) || ClassBRange.Contains(ip) || LinkLocalRange.Contains(ip))
     {
         return(true);
     }
     if (ip.Equals(BroadcastIPv4))
     {
         return(true);
     }
     if (ip.Equals(AnyIPv4))
     {
         return(true);
     }
     return(false);
 }
    /// <summary>
    /// Determine whether this IP address is part of the range/subnet
    /// </summary>
    /// <param name="ipBegin">Beginning IP address of range</param>
    /// <param name="ipEnd">Ending IP address of range</param>
    /// <returns></returns>
    public static bool IsInRange(this IPAddress thisIP, IPAddress ipBegin, IPAddress ipEnd)
    {
        IPAddressRange range = new IPAddressRange(ipBegin, ipEnd);

        return(range.Contains(thisIP));
    }
    /// <summary>
    /// Determine whether this IP address is part of the range/subnet
    /// </summary>
    /// <param name="range">Can be specified in CIDR/UNI (ex: 192.168.10.0/24) </param>
    /// <returns></returns>
    public static bool IsInRange(this IPAddress thisIP, string rangeIP)
    {
        IPAddressRange range = IPAddressRange.Parse(rangeIP);

        return(range.Contains(thisIP));
    }
 /// <summary>
 /// Determine whether this IP address is part of the range/subnet
 /// </summary>
 /// <param name="range">A range of IPAddresses</param>
 /// <returns></returns>
 public static bool IsInRange(this IPAddress thisIP, IPAddressRange range)
 {
     return(range.Contains(thisIP));
 }
Ejemplo n.º 21
0
    public void ContainsTest_TestIPv6_to_IPv4Range()
    {
        var range = new IPAddressRange("192.168.60.26-192.168.60.37");

        range.Contains(IPAddress.Parse("c0a8:3c1a::")).Is(false);
    }