예제 #1
0
        public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
        {
            IPAddress network1 = address.GetNetworkAddress(subnetMask);
            IPAddress network2 = address2.GetNetworkAddress(subnetMask);

            return network1.Equals(network2);
        }
예제 #2
0
        public static bool IsInSameSubnet(this IPAddress address, IPAddress check, byte[] subnetMask)
        {
            if (address.AddressFamily != check.AddressFamily)
                return false;

            IPAddress network1 = address.GetNetworkAddress(subnetMask);
            IPAddress network2 = check.GetNetworkAddress(subnetMask);
            return network1.IsEqual(network2);
        }
        /// <summary>
        /// Compares if 2 IPv6 addresses are within the same Subnet using the Subnet Mask
        /// </summary>
        /// <param name="address1">The first IPv4 address to compare the network IDs for.</param>
        /// <param name="address2">The second IPv4 address to compare the network IDs for.</param>
        /// <param name="subnetBits">The number of MSBs to use for comparing the subnet.</param>
        /// <returns>True if they are part of the same subnet</returns>
        public static bool SameSubnetAs(this IPAddress address1, IPAddress address2, byte subnetBits = 64)
        {
            if (address1.AddressFamily != AddressFamily.InterNetworkV6)
                throw new ArgumentException("must be an IPv6 (InterNetworkV6) address family.", "address1");

            if (address2.AddressFamily != AddressFamily.InterNetworkV6)
                throw new ArgumentException("must be an IPv6 (InterNetworV6) address family.", "address2");

            if (subnetBits > 128)
                throw new ArgumentException("must be between 0 and 128", "subnetBits");

            if (subnetBits != 0)
            {
                IPAddress lIPNet1 = address1.GetNetworkAddress(IPAddressFromBitsSet(128, subnetBits));
                IPAddress lIPNet2 = address2.GetNetworkAddress(IPAddressFromBitsSet(128, subnetBits));

                return lIPNet1.Equals(lIPNet2);
            }
            else
                return address1.Equals(address2);
        }
예제 #4
0
        public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
        {
            try
            {
                if (address.ToString().Contains(".") && address2.ToString().Contains(".") && subnetMask.ToString().Contains("."))
                {
                    IPAddress network1 = address.GetNetworkAddress(subnetMask);
                    IPAddress network2 = address2.GetNetworkAddress(subnetMask);

                    return network1.Equals(network2);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
                return false;
            }

        }
        public static bool IsInSameIpv4Subnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
        {
            if (address2.AddressFamily != AddressFamily.InterNetwork || address.AddressFamily != AddressFamily.InterNetwork)
            {
                throw new ArgumentException("Both IPAddress must be IPV4 addresses");
            }
            var network1 = address.GetNetworkAddress(subnetMask);
            var network2 = address2.GetNetworkAddress(subnetMask);

            return network1.Equals(network2);
        }
        /// <summary>
        /// Compares if 2 IPv4 addresses are within the same Subnet using the Subnet Mask
        /// </summary>
        /// <param name="address1">The first IPv4 address to compare the network IDs for.</param>
        /// <param name="address2">The second IPv4 address to compare the network IDs for.</param>
        /// <param name="subnetMask">The IPv4 subnet mask to apply to the addreses.</param>
        /// <returns>True if they are part of the same subnet</returns>
        public static bool SameSubnetAs(this IPAddress address1, IPAddress address2, IPAddress subnetMask)
        {
            if (address1.AddressFamily != AddressFamily.InterNetwork)
                throw new ArgumentException("must be an IPv4 (InterNetwork) address family.", "address1");

            if (address2.AddressFamily != AddressFamily.InterNetwork)
                throw new ArgumentException("must be an IPv4 (InterNetwork) address family.", "address2");

            if (subnetMask != null)
            {
                if (subnetMask.AddressFamily != AddressFamily.InterNetwork)
                    throw new ArgumentException("must be null or an IPv4 (InterNetwork) address family.", "subnetMask");

                IPAddress lIPNet1 = address1.GetNetworkAddress(subnetMask);
                IPAddress lIPNet2 = address2.GetNetworkAddress(subnetMask);

                return lIPNet1.Equals(lIPNet2);
            }
            else
                return address1.Equals(address2);
        }
        public static bool IsInSameNetworkAs(
            this IPAddress firstAddress,
            IPAddress secondAddress,
            IPAddress mask)
        {
            Contract.Requires<ArgumentNullException>(firstAddress != null);
            Contract.Requires<ArgumentNullException>(secondAddress != null);
            Contract.Requires<ArgumentNullException>(mask != null);

            return
                firstAddress
                .GetNetworkAddress(mask)
                .Equals(
                    secondAddress
                    .GetNetworkAddress(mask));
        }
예제 #8
0
        private static bool? IsIp6Match(string domain, IPAddress ipAddress, int? prefix6)
        {
            if (prefix6.HasValue)
            {
                ipAddress = ipAddress.GetNetworkAddress(prefix6.Value);
            }

            DnsMessage dnsMessage = DnsClient.Default.Resolve(domain, RecordType.Aaaa);
            if ((dnsMessage == null) || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
                return null;

            foreach (AaaaRecord dnsRecord in dnsMessage.AnswerRecords.Where(record => record.RecordType == RecordType.Aaaa).Cast<AaaaRecord>())
            {
                if (prefix6.HasValue)
                {
                    if (ipAddress.Equals(dnsRecord.Address.GetNetworkAddress(prefix6.Value)))
                        return true;
                }
                else
                {
                    if (ipAddress.Equals(dnsRecord.Address))
                        return true;
                }
            }

            return false;
        }
예제 #9
0
 internal static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
 {
     IPAddress networkAddress = address.GetNetworkAddress(subnetMask);
     IPAddress address4 = address2.GetNetworkAddress(subnetMask);
     return networkAddress.Equals(address4);
 }