Ejemplo n.º 1
0
        /// <summary>
        /// Determines whether an address is in a subnet specified by an <see cref="IPAddress" /> and subnet mask.
        /// </summary>
        /// <param name="address">The <see cref="IPAddress" /> to test.</param>
        /// <param name="subnetAddress">An <see cref="IPAddress" /> that is in the desired subnet.</param>
        /// <param name="subnetMask">The subnet mask.</param>
        /// <returns><c>true</c> if the address is in the subnet indicated by the specified address and mask; otherwise, <c>false</c>.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="address" /> is null.
        /// <para>or</para>
        /// <paramref name="subnetAddress" /> is null.
        /// <para>or</para>
        /// <paramref name="subnetMask" /> is null.
        /// </exception>
        public static bool IsInSubnet(this IPAddress address, IPAddress subnetAddress, IPAddress subnetMask)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }

            if (subnetAddress == null)
            {
                throw new ArgumentNullException(nameof(subnetAddress));
            }

            if (subnetMask == null)
            {
                throw new ArgumentNullException(nameof(subnetMask));
            }

            IPAddress network1 = subnetAddress.ApplySubnetMask(subnetMask);
            IPAddress network2 = address.ApplySubnetMask(subnetMask);

            return(network1.Equals(network2));
        }