Exemplo n.º 1
0
        /// <summary>
        ///     Determines whether a string is a valid IP address range.
        /// </summary>
        /// <returns>
        ///     <see langword="true" /> if <paramref name="str" /> is a valid IP address range; otherwise, <see langword="false" />
        ///     .
        /// </returns>
        /// <param name="str">The string to validate.</param>
        /// <param name="addressRange">The <see cref="IPRange" /> version of the string.</param>
        public static bool TryParse(string str, out IPRange addressRange)
        {
            var ips = str.Split('-');

            if (ips.Length == 2)
            {
                IPAddress address1;
                IPAddress address2;
                if (IPAddress.TryParse(ips[0], out address1) && IPAddress.TryParse(ips[1], out address2))
                {
                    if (!address1.Equals(address2))
                    {
                        addressRange = new IPRange(address1, address2);
                        return(true);
                    }
                }
            }
            addressRange = null;
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Determines whether a string is a valid IP address.
        /// </summary>
        /// <returns>
        ///     true if <paramref name="ipString" /> is a valid IP address; otherwise, false.
        /// </returns>
        /// <param name="ipString">The string to validate.</param>
        /// <param name="address">The <see cref="SingleIP" /> version of the string.</param>
        public static bool TryParse(string ipString, out SingleIP address)
        {
            // Check if this is "Any Address" special string
            if (ipString.Trim() == "*")
            {
                address = Any;

                return(true);
            }

            // Check if this is a valid IPAddress
            if (IPAddress.TryParse(ipString, out var ipAddress))
            {
                address = new SingleIP(ipAddress);

                return(true);
            }

            // Check if this is a IP range with only one IP
            if (IPRange.TryParse(ipString, out var ipRange) &&
                ipRange.StartAddress.Equals(ipRange.EndAddress))
            {
                address = new SingleIP(ipRange.StartAddress);

                return(true);
            }

            // Check if this is a single IP NetworkAddress
            if (NetworkAddress.TryParse(ipString, out var networkAddress) &&
                networkAddress.StartAddress.Equals(networkAddress.EndAddress))
            {
                address = new SingleIP(networkAddress.Address);

                return(true);
            }

            address = null;

            return(false);
        }
Exemplo n.º 3
0
 /// <summary>
 ///     Compares two IP address ranges.
 /// </summary>
 /// <returns>
 ///     <see langword="true" /> if the two address ranges are equal; otherwise, <see langword="false" />.
 /// </returns>
 /// <param name="comparand">An <see cref="IPRange" /> instance to compare to the current instance. </param>
 private bool Equals(IPRange comparand)
 {
     return(StartAddress.Equals(comparand.StartAddress) && EndAddress.Equals(comparand.EndAddress));
 }