Esempio n. 1
0
        /// <summary>
        /// Attempts to parse a subnet from CIDR notation in the form of <i>ip-address</i>/<i>prefix</i>,
        /// where <i>prefix</i> is the network prefix length in bits.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <param name="cidr">The parsed <see cref="NetworkCidr"/>.</param>
        /// <returns><c>true</c> if the operation was successful.</returns>
        public static bool TryParse(string input, out NetworkCidr cidr)
        {
            cidr = null;

            if (string.IsNullOrEmpty(input))
            {
                return(false);
            }

            var slashPos = input.IndexOf('/');

            if (slashPos <= 0)
            {
                return(false);
            }

            if (!IPAddress.TryParse(input.Substring(0, slashPos), out var address))
            {
                return(false);
            }

            if (!int.TryParse(input.Substring(slashPos + 1), out var prefixLength) || prefixLength < 0 || prefixLength > 32)
            {
                return(false);
            }

            cidr = new NetworkCidr();

            cidr.Initialize(address, prefixLength);

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Parses a subnet from CIDR notation in the form of <i>ip-address</i>/<i>prefix</i>,
        /// where <i>prefix</i> is the network prefix length in bits.
        /// </summary>
        /// <param name="input">The input string.</param>
        /// <returns>The parsed <see cref="NetworkCidr"/>.</returns>
        /// <exception cref="ArgumentException">Thrown if the input is not correctly formatted.</exception>
        public static NetworkCidr Parse(string input)
        {
            Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(input));

            var slashPos = input.IndexOf('/');

            if (slashPos <= 0)
            {
                throw new ArgumentException($"Invalid CIDR [{input}].");
            }

            if (!IPAddress.TryParse(input.Substring(0, slashPos), out var address))
            {
                throw new ArgumentException($"Invalid CIDR [{input}].");
            }

            if (!int.TryParse(input.Substring(slashPos + 1), out var prefixLength) || prefixLength < 0 || prefixLength > 32)
            {
                throw new ArgumentException($"Invalid CIDR [{input}].");
            }

            var cidr = new NetworkCidr();

            cidr.Initialize(address, prefixLength);

            return(cidr);
        }
Esempio n. 3
0
        /// <summary>
        /// Determines whether this subnet overlaps another.
        /// </summary>
        /// <param name="subnet">The subnet being tested.</param>
        /// <returns><c>true</c> if the subnets overlap.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="subnet"/> is <c>null</c>.</exception>
        public bool Overlaps(NetworkCidr subnet)
        {
            Covenant.Requires <ArgumentNullException>(subnet != null);

            var first       = NetHelper.AddressToUint(this.FirstAddress);
            var last        = NetHelper.AddressToUint(this.LastAddress);
            var subnetFirst = NetHelper.AddressToUint(subnet.FirstAddress);
            var subnetLast  = NetHelper.AddressToUint(subnet.LastAddress);

            if (last < subnetFirst)
            {
                return(false);
            }
            else if (first > subnetLast)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Determines whether a subnet is completely contained within the current subnet.
        /// </summary>
        /// <param name="subnet">The subnet being tested.</param>
        /// <returns><c>true</c> if <paramref name="subnet"/> is fully contained.</returns>
        public bool Contains(NetworkCidr subnet)
        {
            Covenant.Requires <ArgumentNullException>(subnet != null);

            return(Contains(subnet.FirstAddress) && Contains(subnet.LastAddress));
        }