public static bool TryParseNetwork(string network, out IPRange range, out Exception exception) { exception = null; range = null; if (network == null) { exception = new ArgumentNullException(nameof(network)); return(false); } network = network.Trim(); IPAddress singleAddress; if (IPAddress.TryParse(network, out singleAddress)) { range = new IPRange(singleAddress, singleAddress); return(true); } var pos = network.IndexOf('/'); if (pos < 0) { exception = new ArgumentException("Expected CIDR notation is missing network (correct example would be \"192.168.1.0/24\")", nameof(network)); return(false); } IPAddress networkIp; if (!IPAddress.TryParse(network.Substring(0, pos), out networkIp)) { exception = new ArgumentException("Cannot parse network part of IP address", nameof(network)); return(false); } byte cidr; if (!byte.TryParse(network.Substring(pos + 1), out cidr)) { exception = new ArgumentException("Cannot parse CIDR part of IP address", nameof(network)); return(false); } var subnetMask = networkIp.AddressFamily == AddressFamily.InterNetworkV6 ? IPAddressHelper.CreateSubnetMaskIPv6(cidr) : IPAddressHelper.CreateSubnetMaskIPv4(cidr); var fromIp = IPAddressHelper.GetNetworkAddress(networkIp, subnetMask); var toIp = IPAddressHelper.GetBroadcastAddress(networkIp, subnetMask); range = new IPRange(fromIp, toIp); return(true); }
private bool TryGetValueIpv6(IPAddress ipAddress, out T value) { if (ipAddress == null) { throw new ArgumentNullException(nameof(ipAddress)); } var number = IPAddressHelper.BigIntegerFromIpAddress(ipAddress); var keys = Ipv6Keys; var index = keys.BinarySearch(number); if (index < 0) { index = Math.Abs(index) - 2; } if (index >= 0 && index < keys.Count) { BigInteger key; try { key = keys[index]; } catch (IndexOutOfRangeException) { value = default(T); return(false); } SortedList <BigInteger, T> subDict; if (_dictIpv6.TryGetValue(key, out subDict)) { foreach (var pair in subDict) { if (number > pair.Key) { continue; } value = pair.Value; return(true); } } } value = default(T); return(false); }
private void AddIPv6(IPAddress fromIp, IPAddress toIp, T value) { if (fromIp == null) { throw new ArgumentNullException(nameof(fromIp)); } if (toIp == null) { throw new ArgumentNullException(nameof(toIp)); } var fromNumber = IPAddressHelper.BigIntegerFromIpAddress(fromIp); var toNumber = IPAddressHelper.BigIntegerFromIpAddress(toIp); if (fromNumber > toNumber) { var temp = fromNumber; fromNumber = toNumber; toNumber = temp; } SortedList <BigInteger, T> subDict; if (!_dictIpv6.TryGetValue(fromNumber, out subDict)) { subDict = new SortedList <BigInteger, T>(1); subDict[toNumber] = value; _dictIpv6[fromNumber] = subDict; } else { subDict[toNumber] = value; } _count = -1; _ipv6Keys = null; }