public static bool TryParseNetwork(string network, out IPRange range, out Exception exception) { exception = null; range = null; if (network == null) { exception = new ArgumentNullException("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 \"129.168.1.0/24\")", "network"); return(false); } IPAddress networkIp; if (!IPAddress.TryParse(network.Substring(0, pos), out networkIp)) { exception = new ArgumentException("Cannot parse network part of IP address", "network"); return(false); } byte cidr; if (!Byte.TryParse(network.Substring(pos + 1), out cidr)) { exception = new ArgumentException("Cannot parse CIDR part of IP address", "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("ipAddress"); } var number = IPAddressHelper.BigIntegerFromIpAddress(ipAddress); var keys = Ipv6Keys; int index = keys.BinarySearch(number); if (index < 0) { index = Math.Abs(index) - 2; } if (index >= 0 && index < keys.Count) { SortedList <BigInteger, T> subDict; if (_dictIpv6.TryGetValue(keys[index], out subDict)) { foreach (var pair in subDict) { if (number <= pair.Key) { 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("fromIp"); } if (toIp == null) { throw new ArgumentNullException("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; }