/// <summary> /// Retuns true if the ip address is one of the following /// IANA-reserved private IPv4 network ranges (from http://en.wikipedia.org/wiki/IP_address) /// Start End /// 10.0.0.0 10.255.255.255 /// 172.16.0.0 172.31.255.255 /// 192.168.0.0 192.168.255.255 /// </summary> /// <returns></returns> public static bool IsOnIntranet(this IPAddress ipAddress) { if (empty.Equals(ipAddress)) { return(false); } bool onIntranet = IPAddress.IsLoopback(ipAddress); onIntranet = onIntranet || ipAddress.Equals(ipAddress.And(intranetMask1)); //10.255.255.255 onIntranet = onIntranet || ipAddress.Equals(ipAddress.And(intranetMask4)); ////192.168.255.255 onIntranet = onIntranet || (intranetMask2.Equals(ipAddress.And(intranetMask2)) && ipAddress.Equals(ipAddress.And(intranetMask3))); return(onIntranet); }
/// <summary> /// Determines whether the IP address is in the same subnet as the /// specified IP address. /// </summary> /// <param name="address">The IPAddress instance this is being called /// for.</param> /// <param name="other">The IP address to determine whether it is in the same /// subnet.</param> /// <param name="netmask">The subnetmask to apply.</param> /// <returns>true if both IP address are located in the same subnet; Otherwise /// false.</returns> /// <exception cref="ArgumentNullException">The other parameter or the netmask /// parameter is null.</exception> /// <exception cref="ArgumentException">The netmask and IP address must be /// of the same address family.</exception> /// <remarks>This is an extension method for the IPAddress class.</remarks> public static bool InSameSubnet(this IPAddress address, IPAddress other, IPAddress netmask) { other.ThrowIfNull("other"); netmask.ThrowIfNull("netmask"); return(address.And(netmask).Equals(other.And(netmask))); }