Class IpNetwork
Represents an IP network in CIDR notation, such as 10.0.0.0/8 or 2001:db8::/32: a prefix, and the
number of its leading bits that every address of the network shares.
[JsonConverter(typeof(IpNetworkConverter))]
public class IpNetwork
- Inheritance
-
IpNetwork
- Inherited Members
Remarks
Its properties are read-only, and the addresses they return are its own, which a caller must not change. The prefix
is kept as given, host bits included, so 10.0.0.5/8 prints as such while its FirstIpAddress is
10.0.0.0; and two networks are equal when they hold the same addresses, so 10.0.0.5/8 equals
10.0.0.0/8.
A network of IPv4-mapped IPv6 addresses is the IPv4 network of those addresses, and is made so:
::ffff:10.0.0.0/104 is 10.0.0.0/8. A network that only reaches into the mapped addresses, as
::/80 does, is an IPv6 network like any other; and an address asked about is asked as its IPv4 address
when it is mapped, which such a network does not hold.
JSON reads and writes a network as its text, through IpNetworkConverter.
Constructors
IpNetwork(IPAddress)
Initializes a new instance of the IpNetwork class that holds one address: a /32 for IPv4, a /128 for IPv6.
public IpNetwork(IPAddress prefix)
Parameters
prefixIPAddressThe address.
IpNetwork(IPAddress, int)
Initializes a new instance of the IpNetwork class.
public IpNetwork(IPAddress prefix, int prefixLength)
Parameters
prefixIPAddressAn address of the network. Its host bits are left out of the network's addresses but kept in Prefix.
prefixLengthintThe number of leading bits the network's addresses share: 0 to 32 for IPv4, 0 to 128 for IPv6.
Remarks
An IPv4-mapped prefix with a length of 96 or more makes the IPv4 network of its IPv4 address, with a length 96 shorter.
Exceptions
- ArgumentOutOfRangeException
prefixLengthis negative, above 32 for an IPv4 prefix, or above 128 for an IPv6 one.
Properties
AddressFamily
Gets the network's address family: InterNetwork or InterNetworkV6.
public AddressFamily AddressFamily { get; }
Property Value
All
public static IReadOnlyList<IpNetwork> All { get; }
Property Value
AllGlobalUnicastV6
Gets the IPv6 global unicast space, 2000::/3, which holds every public IPv6 address assigned today.
public static IpNetwork AllGlobalUnicastV6 { get; }
Property Value
AllV4
Gets the network of every IPv4 address, 0.0.0.0/0.
public static IpNetwork AllV4 { get; }
Property Value
AllV6
Gets the network of every IPv6 address, ::/0.
public static IpNetwork AllV6 { get; }
Property Value
FirstIpAddress
Gets the network's first address: the prefix with its host bits cleared.
public IPAddress FirstIpAddress { get; }
Property Value
IsV4
Gets whether the network is an IPv4 network.
public bool IsV4 { get; }
Property Value
IsV6
Gets whether the network is an IPv6 network.
public bool IsV6 { get; }
Property Value
LastIpAddress
Gets the network's last address: the prefix with its host bits set.
public IPAddress LastIpAddress { get; }
Property Value
LinkLocalNetworkV4
Gets the IPv4 link-local network, 169.254.0.0/16: the addresses an interface gives itself when no DHCP
server answers (RFC 3927). They are never routed.
public static IpNetwork LinkLocalNetworkV4 { get; }
Property Value
LinkLocalNetworkV6
Gets the IPv6 link-local network, fe80::/10, which every IPv6 interface has an address in, with no
configuration. Its addresses are never routed.
public static IpNetwork LinkLocalNetworkV6 { get; }
Property Value
LocalNetworks
Gets LocalNetworksV4 followed by LocalNetworksV6.
public static IReadOnlyList<IpNetwork> LocalNetworks { get; }
Property Value
LocalNetworksV4
Gets the IPv4 networks of local networks: the private 10.0.0.0/8, 172.16.0.0/12 and
192.168.0.0/16 (RFC 1918), and the link-local LinkLocalNetworkV4.
public static IReadOnlyList<IpNetwork> LocalNetworksV4 { get; }
Property Value
Remarks
Neither the loopback network 127.0.0.0/8 nor the carriers' shared space 100.64.0.0/10 is in it:
NonPublicNetworks holds every network that is not public.
LocalNetworksV6
Gets the IPv6 networks outside the global unicast space AllGlobalUnicastV6: ::/3,
4000::/2 and 8000::/1.
public static IReadOnlyList<IpNetwork> LocalNetworksV6 { get; }
Property Value
Remarks
So it holds the loopback, unique local, link-local and multicast addresses, and the space no registry assigns yet, where LocalNetworksV4 holds the private and link-local networks alone.
LoopbackNetworkV4
Gets the IPv4 loopback network, 127.0.0.0/8.
public static IpNetwork LoopbackNetworkV4 { get; }
Property Value
LoopbackNetworkV6
Gets the IPv6 loopback address as a network, ::1/128.
public static IpNetwork LoopbackNetworkV6 { get; }
Property Value
LoopbackNetworks
Gets LoopbackNetworkV4 and LoopbackNetworkV6.
public static IReadOnlyList<IpNetwork> LoopbackNetworks { get; }
Property Value
MulticastNetworkV4
Gets the IPv4 multicast network, 224.0.0.0/4.
public static IpNetwork MulticastNetworkV4 { get; }
Property Value
MulticastNetworkV6
Gets the IPv6 multicast network, ff00::/8.
public static IpNetwork MulticastNetworkV6 { get; }
Property Value
MulticastNetworks
Gets MulticastNetworkV4 and MulticastNetworkV6.
public static IReadOnlyList<IpNetwork> MulticastNetworks { get; }
Property Value
NonPublicNetworks
Gets the networks of both families whose addresses are not public: those the registries set aside and do not count as reachable from the internet, the multicast networks, and every IPv6 address outside the global unicast space AllGlobalUnicastV6.
public static IReadOnlyList<IpNetwork> NonPublicNetworks { get; }
Property Value
Remarks
It is the list for a guard that must keep a destination someone else names away from what only the machine or its network should reach: a server's own loopback, its local network, its provider's services. What a device takes as its local network is LocalNetworks.
IPv4: 0.0.0.0/8; the private 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16; the
carriers' shared space 100.64.0.0/10; the loopback 127.0.0.0/8; the link-local
169.254.0.0/16; the protocol assignments 192.0.0.0/24; the documentation networks
192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24; the benchmarking network
198.18.0.0/15; the multicast 224.0.0.0/4; and the reserved 240.0.0.0/4, the broadcast
address with it.
IPv6: ::/3, 4000::/2 and 8000::/1, which hold the unspecified and loopback addresses, the
NAT64 prefix 64:ff9b::/96, the unique local, link-local and multicast networks and the space no
registry assigns yet; and, inside the global unicast space, Teredo's 2001::/32, the benchmarking network
2001:2::/48, the old ORCHID network 2001:10::/28, the documentation networks
2001:db8::/32 and 3fff::/20, and 6to4's 2002::/16.
The rest of the protocol assignments 2001::/23 is not in it, as the registry counts its other parts
as reachable. The two anycast addresses it counts so inside 192.0.0.0/24 are not left out; and where a
network translates the NAT64 prefix, its addresses stand for public IPv4 hosts all the same.
None
Gets an empty list of networks.
public static IReadOnlyList<IpNetwork> None { get; }
Property Value
Prefix
Gets the address the network was made with, host bits included.
public IPAddress Prefix { get; }
Property Value
PrefixLength
Gets the number of leading bits the network's addresses share.
public int PrefixLength { get; }
Property Value
SubnetMask
Gets the subnet mask of the prefix length, such as 255.255.255.0 for a /24.
public IPAddress SubnetMask { get; }
Property Value
Exceptions
- ArgumentOutOfRangeException
PrefixLength is negative.
Total
Gets the number of addresses in the network: 2 to the power of its host bits.
public BigInteger Total { get; }
Property Value
Methods
Contains(IPAddress)
Determines whether an address is in the network.
public bool Contains(IPAddress ipAddress)
Parameters
ipAddressIPAddressThe address. An IPv4-mapped IPv6 address is asked about as its IPv4 address, and a scope id is ignored.
Returns
Exceptions
- ArgumentNullException
ipAddressis null.
Contains(IpAddressValue)
Determines whether an address is in the network, with no object made.
public bool Contains(IpAddressValue ipAddress)
Parameters
ipAddressIpAddressValueThe 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 network.
Returns
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
Remarks
Two networks are equal when they hold the same addresses; the prefix's host bits do not count.
FromRange(IPAddress, IPAddress)
Gets the fewest networks that hold exactly the addresses of a range.
public static IEnumerable<IpNetwork> FromRange(IPAddress firstIpAddress, IPAddress lastIpAddress)
Parameters
firstIpAddressIPAddressThe range's first address.
lastIpAddressIPAddressThe range's last address, of the same family.
Returns
- IEnumerable<IpNetwork>
The networks, from the lowest address up; none when
lastIpAddressis belowfirstIpAddress.
Remarks
The networks are made, and the families checked, as the result is enumerated. Two IPv4-mapped IPv6 addresses give the networks of their IPv4 addresses; a range that only reaches into the mapped addresses gives no network that lies wholly among them, as such a network holds no address a lookup asks about.
Exceptions
- ArgumentException
The two addresses are of different families.
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
Invert()
Gets the networks of the addresses outside this network, within its own address family.
public IOrderedEnumerable<IpNetwork> Invert()
Returns
- IOrderedEnumerable<IpNetwork>
The fewest networks that hold those addresses, from the lowest address up; none for a /0 network.
Parse(string)
Converts the text form of a network, such as 10.0.0.0/8, to an IpNetwork.
public static IpNetwork Parse(string value)
Parameters
valuestringAn address, a slash and a prefix length.
Returns
- IpNetwork
The network.
Exceptions
- FormatException
valueis not an address, one slash and a prefix length of digits alone, or the length is above the family's bits.
ToIpRange()
Converts the network to the range from its first to its last address.
public IpRange ToIpRange()
Returns
- IpRange
The range.
ToString()
Returns the network's text form: the prefix as given, a slash and the prefix length, such as
10.0.0.0/8.
public override string ToString()
Returns
- string
The text.