public static bool IpMatch(string ip1, string ip2) { string rgxString = @"^(((\d)|(\d\d)|((1)\d\d)|(2[0-4]\d)|(25[0-5])).){3}((\d)|(\d\d)|((1)\d\d)|(2[0-4]\d)|(25[0-5]))$"; Regex rgx = new Regex(rgxString); if (!rgx.IsMatch(ip1)) { throw new Exception("invalid argument: ip1 in IPMatch() function is not an IP address."); } IPNetwork ipNetwork2 = IPNetwork.Parse(ip2); if (!(ipNetwork2.AddressFamily == AddressFamily.InterNetwork || ipNetwork2.AddressFamily == AddressFamily.InterNetworkV6)) { throw new Exception("invalid argument: ip2 in IPMatch() function is neither an IP address nor a CIDR."); } IPNetwork ipNetwork1 = IPNetwork.Parse(ip1); if (ipNetwork1.Equals(ipNetwork2)) { return(true); } return(ipNetwork1.Netmask.Equals(IPAddress.Parse(ip2))); }
/// <summary> /// Gets all unique <c>pairs</c> of hosts with monitoring flag set and which share the same subnet.<br/> /// Subnets with less than or more than two hosts will be discarded. /// </summary> /// <param name="hamnetDbAccess">The handle to access the database.</param> /// <param name="subnet">The subnet to return data for.</param> /// <returns>The dictionary mapping a subnet to its unique monitored host pair.</returns> public static IReadOnlyDictionary <IHamnetDbSubnet, IHamnetDbHosts> UniqueMonitoredHostPairsInSubnet(this IHamnetDbAccess hamnetDbAccess, IPNetwork subnet) { if (hamnetDbAccess == null) { throw new ArgumentNullException(nameof(hamnetDbAccess), "hamnetDbAccess to work with is null"); } if (subnet == null) { throw new ArgumentNullException(nameof(subnet), "subnet search monitored hosts for is null"); } var directSupportHamnetAccess = hamnetDbAccess as IDirectSupportOfHamnetDbAccessExtensions; if (directSupportHamnetAccess != null) { return(directSupportHamnetAccess.UniqueMonitoredHostPairsInSubnet(subnet)); } var hosts = hamnetDbAccess.QueryMonitoredHosts(); var allSubnets = hamnetDbAccess.QuerySubnets(); // filter out parents for which we have nested subnets var subnets = allSubnets.Where(s => !allSubnets.Any(a => !object.ReferenceEquals(s.Subnet, a.Subnet) && s.Subnet.Contains(a.Subnet))); var association = subnets.AssociateHosts(hosts); var uniquePairs = association .Where(a => subnet.Contains(a.Key.Subnet) || subnet.Equals(a.Key.Subnet)) .Where(a => a.Value.Count == 2) .ToDictionary(k => k.Key, v => v.Value); return(uniquePairs); }
public bool Contains(IPNetworkProxy proxy) { if (proxy._isAddress && _isAddress) { return(_address.Equals(proxy._address)); } if (!proxy._isAddress && !_isAddress) { return(_network.Equals(this._network)); } // they are an address and we are a network if (proxy._isAddress && !_isAddress) { return(_network.Contains(proxy._address)); } return(false); // an ip address cannot contain a network }
/// <inheritdoc /> public IReadOnlyDictionary <IHamnetDbSubnet, IHamnetDbHosts> UniqueMonitoredHostPairsInSubnet(IPNetwork subnet) { if (subnet == null) { throw new ArgumentNullException(nameof(subnet), "subnet search monitored hosts for is null"); } lock (this.cacheRefreshLock) { var timeOfQuery = DateTime.UtcNow; var association = this.cacheDataStore.GetCacheDataSetOrNull <IReadOnlyDictionary <IHamnetDbSubnet, IHamnetDbHosts> >(DataTypes.UniqueMonitoredHostPairsInSubnet, timeOfQuery)?.Data ?? this.RefreshHostAssociations(timeOfQuery); var uniquePairs = association .Where(a => subnet.Contains(a.Key.Subnet) || subnet.Equals(a.Key.Subnet)) .Where(a => a.Value.Count == 2) .ToDictionary(k => k.Key, v => v.Value); return(uniquePairs); } }