Table of Contents

Class IpRange

Namespace
VpnHood.Net.Toolkit.Net
Assembly
VpnHood.Net.Toolkit.dll

Represents a range of IP addresses of one family, from a first to a last address, both included.

[JsonConverter(typeof(IpRangeConverter))]
public class IpRange
Inheritance
IpRange
Inherited Members

Remarks

Its properties are read-only, and the addresses they return are its own, which a caller must not change. Its text form is 10.0.0.1-10.0.0.9, or the address alone for a range of one address, and JSON reads and writes a range as that text, through IpRangeConverter. IpRangeOrderedList holds a sorted set of ranges.

An IPv4-mapped IPv6 address, such as ::ffff:10.0.0.1, is an IPv4 address in IPv6's form. A range of such addresses is the range of their IPv4 addresses, and is made so: ::ffff:10.0.0.1-::ffff:10.0.0.9 is 10.0.0.1-10.0.0.9. A range that only reaches into them, as that of ::/80 does, is a range of IPv6 addresses like any other; and an address asked about is asked as its IPv4 address, which such a range does not hold.

Constructors

IpRange(long, long)

Initializes a new instance of the IpRange class from IPv4 addresses given as numbers, as ToLong(IPAddress) gives them: 10.0.0.1 is 0x0A000001.

public IpRange(long firstIpAddress, long lastIpAddress)

Parameters

firstIpAddress long

The first address. Only its low 32 bits are read.

lastIpAddress long

The last address. Only its low 32 bits are read.

Exceptions

ArgumentException

The last address is below the first.

IpRange(IPAddress)

Initializes a new instance of the IpRange class that holds one address.

public IpRange(IPAddress ipAddress)

Parameters

ipAddress IPAddress

The address.

IpRange(IPAddress, IPAddress)

Initializes a new instance of the IpRange class.

public IpRange(IPAddress firstIpAddress, IPAddress lastIpAddress)

Parameters

firstIpAddress IPAddress

The first address.

lastIpAddress IPAddress

The last address, of the same family.

Remarks

Two IPv4-mapped IPv6 addresses make the range of their IPv4 addresses. Any other two are put in order by number, so a range from ::ffff:10.0.0.5 to ::1 is refused.

Exceptions

ArgumentException

The two addresses are of different families, or lastIpAddress comes before firstIpAddress.

Properties

AddressFamily

Gets the address family of the range's addresses.

public AddressFamily AddressFamily { get; }

Property Value

AddressFamily

FirstIpAddress

Gets the range's first address.

public IPAddress FirstIpAddress { get; }

Property Value

IPAddress

LastIpAddress

Gets the range's last address.

public IPAddress LastIpAddress { get; }

Property Value

IPAddress

Total

Gets the number of addresses in the range.

public BigInteger Total { get; }

Property Value

BigInteger

Methods

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

Remarks

Two ranges are equal when their ends are, as Equals(object) compares them.

FromIpAddress(IPAddress)

Creates a range that holds one address.

public static IpRange FromIpAddress(IPAddress ipAddress)

Parameters

ipAddress IPAddress

The address.

Returns

IpRange

The range.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

IsInRange(IPAddress)

Determines whether an address is in the range.

public bool IsInRange(IPAddress ipAddress)

Parameters

ipAddress IPAddress

The address. An IPv4-mapped IPv6 address is asked about as its IPv4 address, and a scope id is ignored.

Returns

bool

true if the range holds the address; otherwise, false.

Exceptions

ArgumentNullException

ipAddress is null.

IsInRange(IpAddressValue)

Determines whether an address is in the range, with no object made.

public bool IsInRange(IpAddressValue ipAddress)

Parameters

ipAddress IpAddressValue

The address. An IPv4-mapped IPv6 address is asked about as its IPv4 address, and a scope id is ignored; the default value is in no range.

Returns

bool

true if the range holds the address; otherwise, false.

Parse(string)

Converts the text form of a range to an IpRange.

public static IpRange Parse(string value)

Parameters

value string

An address (10.0.0.1); two addresses joined by - or to (10.0.0.1-10.0.0.9), white space around them allowed; or a network (10.0.0.0/24), as Parse(string) reads it.

Returns

IpRange

The range.

Exceptions

FormatException

value is none of these forms, or an address in it is not valid.

ArgumentException

The two addresses are of different families, or the second comes before the first.

ToIpNetworks()

Gets the fewest networks that hold exactly the range's addresses.

public IEnumerable<IpNetwork> ToIpNetworks()

Returns

IEnumerable<IpNetwork>

The networks, from the lowest address up, made as the result is enumerated.

ToString()

Returns the range's text form: 10.0.0.1-10.0.0.9, or the address alone when the range holds one address.

public override string ToString()

Returns

string

The text.