Class IpRangeOrderedList
Represents a sorted set of IP ranges of both families, in which the ranges of a family that overlap or touch are merged into one.
public class IpRangeOrderedList : IOrderedEnumerable<IpRange>, IReadOnlyList<IpRange>, IReadOnlyCollection<IpRange>, IEnumerable<IpRange>, IEnumerable
- Inheritance
-
IpRangeOrderedList
- Implements
- Inherited Members
Remarks
The list cannot be changed: Union(IEnumerable<IpRange>), Exclude(IpRangeOrderedList), Intersect(IpRangeOrderedList) and Invert(bool, bool) return another list, or this one where nothing changes. The ranges are in the order of their first addresses, the IPv4 ranges before the IPv6 ones, and Contains(IpAddressValue) finds an address by binary search.
An IPv4-mapped IPv6 address is an IPv4 address in IPv6's form: IpRange makes a range of such
addresses the range of their IPv4 addresses, and Contains(IpAddressValue) asks about one as its IPv4
address. A range that only reaches into the mapped addresses, as that of ::/80 does, is a range of IPv6
addresses like any other, which holds none of them for a lookup. So two IPv6 ranges with nothing between them but
mapped addresses are one range, and what an intersection would leave wholly among them is left out.
Constructors
IpRangeOrderedList()
Initializes a new instance of the IpRangeOrderedList class that holds no range.
public IpRangeOrderedList()
IpRangeOrderedList(IEnumerable<IpRange>)
Initializes a new instance of the IpRangeOrderedList class that holds the given ranges, sorted, with those of a family that overlap or touch merged.
public IpRangeOrderedList(IEnumerable<IpRange> ipRanges)
Parameters
ipRangesIEnumerable<IpRange>The ranges, in any order.
Exceptions
- ArgumentNullException
ipRangesis null.
Properties
Count
Gets the number of ranges, after merging.
public int Count { get; }
Property Value
Empty
Gets an empty list.
public static IpRangeOrderedList Empty { get; }
Property Value
this[int]
Gets the range at an index.
public IpRange this[int index] { get; }
Parameters
Property Value
Exceptions
- ArgumentOutOfRangeException
indexis negative, or not below Count.
Methods
Contains(IPAddress)
Determines whether an address is in one of the ranges.
public bool Contains(IPAddress ipAddress)
Parameters
ipAddressIPAddressThe address. An IPv4-mapped IPv6 address is looked up as its IPv4 address, and a scope id is ignored.
Returns
Exceptions
- ArgumentNullException
ipAddressis null.
Contains(IpAddressValue)
Determines whether an address is in one of the ranges, with no object made.
public bool Contains(IpAddressValue ipAddress)
Parameters
ipAddressIpAddressValueThe address. An IPv4-mapped IPv6 address is looked up as its IPv4 address, and a scope id is ignored; the default value is in no range.
Returns
CreateOrderedEnumerable<TKey>(Func<IpRange, TKey>, IComparer<TKey>?, bool)
Adds a key to the list's order, for ThenBy and ThenByDescending.
public IOrderedEnumerable<IpRange> CreateOrderedEnumerable<TKey>(Func<IpRange, TKey> keySelector, IComparer<TKey>? comparer, bool descending)
Parameters
keySelectorFunc<IpRange, TKey>The function that gets a range's key.
comparerIComparer<TKey>The comparer of keys, or null for the default one.
descendingbooltrue to sort by the key in descending order; false for ascending.
Returns
- IOrderedEnumerable<IpRange>
The ranges, in this list's order, then the key's.
Type Parameters
TKeyThe type of the key.
Remarks
As with any ordered sequence, this list's order comes first, and the key orders only what it leaves equal;
the list's ranges never start at the same address, so list.ThenBy(key) keeps the list's order.
Exceptions
- ArgumentNullException
keySelectoris null.
Deserialize(Stream)
Reads ranges that Serialize(Stream) wrote.
public static IpRangeOrderedList Deserialize(Stream stream)
Parameters
streamStreamThe stream, at the start of the data. It is left open.
Returns
- IpRangeOrderedList
The list, sorted and merged again.
Exceptions
- ArgumentNullException
streamis null.- EndOfStreamException
The stream ends before the last range's end.
- InvalidDataException
The number of ranges is negative, or an address is neither 4 nor 16 bytes long.
- ArgumentException
streamcannot be read, or a range's addresses are of different families, or out of order.
DeserializeRaw(Stream)
Reads ranges that Serialize(Stream) wrote as their addresses' bytes, with no IpRange or IPAddress made, and no sorting.
public static IEnumerable<(byte[] Start, byte[] End)> DeserializeRaw(Stream stream)
Parameters
streamStreamThe stream, at the start of the data. It is left open.
Returns
- IEnumerable<(byte[] Start, byte[] End)>
Each range's first and last address bytes, 4 for IPv4 and 16 for IPv6, in the stream's order, which Serialize(Stream) wrote sorted and merged. Nothing is read until the result is enumerated.
Remarks
For a caller that only stores the ranges, such as a bulk insert into a database.
Exceptions
- ArgumentNullException
streamis null, when the enumeration starts.- ArgumentException
streamcannot be read, when the enumeration starts.- EndOfStreamException
The stream ends before the last range's end.
- InvalidDataException
The number of ranges is negative, or an address is neither 4 nor 16 bytes long.
Exclude(IEnumerable<IpRange>)
Returns a list of this list's addresses outside other ranges.
public IpRangeOrderedList Exclude(IEnumerable<IpRange> ipRanges)
Parameters
ipRangesIEnumerable<IpRange>The ranges to leave out, in any order.
Returns
- IpRangeOrderedList
The new list, which may be this list itself when
ipRangesis empty.
Exceptions
- ArgumentNullException
ipRangesis null.
Exclude(IPAddress)
Returns a list of this list's addresses but one.
public IpRangeOrderedList Exclude(IPAddress ipAddress)
Parameters
ipAddressIPAddressThe address to leave out.
Returns
- IpRangeOrderedList
The new list.
Exclude(IpRange)
Returns a list of this list's addresses outside a range.
public IpRangeOrderedList Exclude(IpRange ipRange)
Parameters
ipRangeIpRangeThe range to leave out.
Returns
- IpRangeOrderedList
The new list.
Exclude(IpRangeOrderedList)
Returns a list of this list's addresses outside another list.
public IpRangeOrderedList Exclude(IpRangeOrderedList ipRanges)
Parameters
ipRangesIpRangeOrderedListThe ranges to leave out.
Returns
- IpRangeOrderedList
The new list, which may be this list itself when
ipRangesis empty.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<IpRange> GetEnumerator()
Returns
- IEnumerator<IpRange>
An enumerator that can be used to iterate through the collection.
Intersect(IEnumerable<IpRange>)
Returns a list of the addresses that this list and other ranges both hold.
public IpRangeOrderedList Intersect(IEnumerable<IpRange> ipRanges)
Parameters
ipRangesIEnumerable<IpRange>The other ranges, in any order.
Returns
- IpRangeOrderedList
The new list; this list itself when the other ranges hold every address and this list does not.
Exceptions
- ArgumentNullException
ipRangesis null.
Intersect(IpRangeOrderedList)
Returns a list of the addresses that this list and another both hold.
public IpRangeOrderedList Intersect(IpRangeOrderedList ipRanges)
Parameters
ipRangesIpRangeOrderedListThe other list.
Returns
- IpRangeOrderedList
The new list;
ipRangesitself when this list holds every address, or else this list itself whenipRangesdoes.
Invert(bool, bool)
Returns a list of the addresses this list does not hold.
public IpRangeOrderedList Invert(bool includeIPv4 = true, bool includeIPv6 = true)
Parameters
includeIPv4booltrue to include the IPv4 addresses outside this list; false to leave IPv4 out of the result.
includeIPv6booltrue to include the IPv6 addresses outside this list; false to leave IPv6 out of the result.
Returns
- IpRangeOrderedList
The new list. A family this list holds no range of comes back whole, when included.
IsAll()
Determines whether the list holds every address, IPv4 and IPv6.
public bool IsAll()
Returns
IsAllV4()
Determines whether the list holds every IPv4 address and no IPv6 one.
public bool IsAllV4()
Returns
IsAllV6()
Determines whether the list holds every IPv6 address and no IPv4 one.
public bool IsAllV6()
Returns
IsNone()
Determines whether the list holds no range.
public bool IsNone()
Returns
Serialize(Stream)
Writes the ranges to a stream in a compact binary form, which Deserialize(Stream) reads.
public void Serialize(Stream stream)
Parameters
streamStreamThe stream, which is left open.
Remarks
The form is the number of ranges, as a 32-bit little-endian integer, then for each range the length (4 or 16) and bytes of its first address, then those of its last. Scope ids are not written.
Exceptions
- ArgumentNullException
streamis null.- ArgumentException
streamcannot be written.
ToIpNetworks()
Converts each range to the fewest networks that hold its addresses.
public IOrderedEnumerable<IpNetwork> ToIpNetworks()
Returns
- IOrderedEnumerable<IpNetwork>
The networks, in the order of their first addresses, the IPv4 networks before the IPv6 ones.
Union(IEnumerable<IpRange>)
Returns a list of this list's addresses and those of other ranges.
public IpRangeOrderedList Union(IEnumerable<IpRange> ipRanges)
Parameters
ipRangesIEnumerable<IpRange>The ranges to add, in any order.
Returns
- IpRangeOrderedList
The new list; this list itself when
ipRangesis empty.
Exceptions
- ArgumentNullException
ipRangesis null.
Union(IPAddress)
Returns a list of this list's addresses and one more.
public IpRangeOrderedList Union(IPAddress ipAddress)
Parameters
ipAddressIPAddressThe address to add.
Returns
- IpRangeOrderedList
The new list.
Union(IpRange)
Returns a list of this list's addresses and those of a range.
public IpRangeOrderedList Union(IpRange ipRange)
Parameters
ipRangeIpRangeThe range to add.
Returns
- IpRangeOrderedList
The new list.