/// <inheritdoc /> public bool ContainsIPAddressRange(IPAddressRange range) { foreach (IPAddress ipAddress in dnsServers) { if (range.Contains(ipAddress)) { return(true); } } return(false); }
/// <summary> /// Check if an ip address range is whitelisted. If any whitelist ip or range intersects, the range is whitelisted. /// </summary> /// <param name="range">Range</param> /// <returns>True if range is whitelisted, false otherwise</returns> public bool IsWhitelisted(IPAddressRange range) { foreach (System.Net.IPAddress ip in whiteList) { if (range.Contains(ip)) { return(true); } } foreach (IPAddressRange existingRange in whiteListRanges) { if (range.Contains(existingRange)) { return(true); } } // it's possible the whitelist other list or whitelist regex will match, but it's too performance // intensive to scan every range in the incoming range to check, oh well... return(false); }
private static bool IpAddressIsInRange(string ipAddress, string ipRange) { try { IPAddressRange range = IPAddressRange.Parse(ipRange); return(range.Contains(IPAddress.Parse(ipAddress))); /* * string[] parts = ipRange.Split('/'); * int IP_addr = BitConverter.ToInt32(IPAddress.Parse(parts[0]).GetAddressBytes(), 0); * int CIDR_addr = BitConverter.ToInt32(IPAddress.Parse(ipAddress).GetAddressBytes(), 0); * int CIDR_mask = IPAddress.HostToNetworkOrder(-1 << (32 - int.Parse(parts[1]))); * return ((IP_addr & CIDR_mask) == (CIDR_addr & CIDR_mask)); */ } catch { return(false); } }
/// <summary> /// Check if an ip address range is whitelisted. If any whitelist ip or range intersects, the range is whitelisted. /// </summary> /// <param name="range">Range</param> /// <returns>True if range is whitelisted, false otherwise</returns> public bool IsWhitelisted(IPAddressRange range) { // if we have a dns list and one of our dns servers is in the range, the range is whitelisted if (dnsList != null && dnsList.ContainsIPAddressRange(range)) { return(true); } // if the whitelist ip address set contains the range or // the whitelist range set contains the range, // the passed in range is considered whitelisted else if (whitelist.Any(i => range.Contains(i)) || whitelistRanges.Any(r => r.Contains(range))) { return(true); } // it's possible the whitelist other list or whitelist regex will match, but it's too performance // intensive to scan every range in the incoming range to check, oh well... return(false); }