IPNetwork  2.0.1
IPNetwork utility computes your ip, ipv4, ipv6, netmask, cidr, subnet, supernet and network
Public Member Functions | Static Public Member Functions | Properties | List of all members
System.Net.IPNetwork Class Reference

IP Network utility class. Use IPNetwork.Parse to create instances. More...

Inheritance diagram for System.Net.IPNetwork:

Public Member Functions

override string ToString ()
 
override int GetHashCode ()
 
Int32 CompareTo (IPNetwork other)
 
Int32 CompareTo (Object obj)
 
Boolean Equals (IPNetwork other)
 
override Boolean Equals (Object obj)
 

Static Public Member Functions

static IPNetwork Parse (string ipaddress, string netmask)
 192.168.168.100 - 255.255.255.0 More...
 
static IPNetwork Parse (string ipaddress, byte cidr)
 192.168.168.100/24 More...
 
static IPNetwork Parse (IPAddress ipaddress, IPAddress netmask)
 192.168.168.100 255.255.255.0 More...
 
static IPNetwork Parse (string network)
 192.168.0.1/24 192.168.0.1 255.255.255.0 More...
 
static bool TryParse (string ipaddress, string netmask, out IPNetwork ipnetwork)
 192.168.168.100 - 255.255.255.0 More...
 
static bool TryParse (string ipaddress, byte cidr, out IPNetwork ipnetwork)
 192.168.168.100/24 More...
 
static bool TryParse (string network, out IPNetwork ipnetwork)
 192.168.0.1/24 192.168.0.1 255.255.255.0 More...
 
static bool TryParse (IPAddress ipaddress, IPAddress netmask, out IPNetwork ipnetwork)
 192.168.0.1/24 192.168.0.1 255.255.255.0 More...
 
static BigInteger ToBigInteger (IPAddress ipaddress)
 Convert an ipadress to decimal 0.0.0.0 -> 0 0.0.1.0 -> 256 More...
 
static bool TryToBigInteger (IPAddress ipaddress, out BigInteger?uintIpAddress)
 Convert an ipadress to decimal 0.0.0.0 -> 0 0.0.1.0 -> 256 More...
 
static BigInteger ToUint (byte cidr, AddressFamily family)
 Convert a cidr to BigInteger netmask More...
 
static bool TryToUint (byte cidr, AddressFamily family, out BigInteger?uintNetmask)
 Convert a cidr to uint netmask More...
 
static byte ToCidr (IPAddress netmask)
 Convert netmask to CIDR 255.255.255.0 -> 24 255.255.0.0 -> 16 255.0.0.0 -> 8 More...
 
static bool TryToCidr (IPAddress netmask, out byte?cidr)
 Convert netmask to CIDR 255.255.255.0 -> 24 255.255.0.0 -> 16 255.0.0.0 -> 8 More...
 
static IPAddress ToNetmask (byte cidr, AddressFamily family)
 Convert CIDR to netmask 24 -> 255.255.255.0 16 -> 255.255.0.0 8 -> 255.0.0.0 More...
 
static bool TryToNetmask (byte cidr, AddressFamily family, out IPAddress netmask)
 Convert CIDR to netmask 24 -> 255.255.255.0 16 -> 255.255.0.0 8 -> 255.0.0.0 More...
 
static uint BitsSet (IPAddress netmask)
 Count bits set to 1 in netmask More...
 
static bool ValidNetmask (IPAddress netmask)
 return true if netmask is a valid netmask 255.255.255.0, 255.0.0.0, 255.255.240.0, ... More...
 
static IPAddress ToIPAddress (BigInteger ipaddress, AddressFamily family)
 Transform a uint ipaddress into IPAddress object More...
 
static bool Contains (IPNetwork network, IPAddress ipaddress)
 return true if ipaddress is contained in network More...
 
static bool Contains (IPNetwork network, IPNetwork network2)
 return true is network2 is fully contained in network More...
 
static bool Overlap (IPNetwork network, IPNetwork network2)
 return true is network2 overlap network More...
 
static bool IsIANAReserved (IPAddress ipaddress)
 return true if ipaddress is contained in IANA_ABLK_RESERVED1, IANA_BBLK_RESERVED1, IANA_CBLK_RESERVED1 More...
 
static bool IsIANAReserved (IPNetwork ipnetwork)
 return true if ipnetwork is contained in IANA_ABLK_RESERVED1, IANA_BBLK_RESERVED1, IANA_CBLK_RESERVED1 More...
 
static IPNetworkCollection Subnet (IPNetwork network, byte cidr)
 Subnet a network into multiple nets of cidr mask Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25 Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9 More...
 
static bool TrySubnet (IPNetwork network, byte cidr, out IPNetworkCollection ipnetworkCollection)
 Subnet a network into multiple nets of cidr mask Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25 Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9 More...
 
static IPNetwork Supernet (IPNetwork network1, IPNetwork network2)
 Supernet two consecutive cidr equal subnet into a single one 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24 More...
 
static bool TrySupernet (IPNetwork network1, IPNetwork network2, out IPNetwork supernet)
 Try to supernet two consecutive cidr equal subnet into a single one 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24 More...
 
static IPNetwork[] Supernet (IPNetwork[] ipnetworks)
 Supernet a list of subnet 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 192.168.0.0/24 + 192.168.1.0/24 + 192.168.2.0/24 + 192.168.3.0/24 = 192.168.0.0/22 More...
 
static bool TrySupernet (IPNetwork[] ipnetworks, out IPNetwork[] supernet)
 Supernet a list of subnet 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 192.168.0.0/24 + 192.168.1.0/24 + 192.168.2.0/24 + 192.168.3.0/24 = 192.168.0.0/22 More...
 
static bool InternalSupernet (bool trySupernet, IPNetwork[] ipnetworks, out IPNetwork[] supernet)
 
static IPNetwork WideSubnet (string start, string end)
 
static bool TryWideSubnet (IPNetwork[] ipnetworks, out IPNetwork ipnetwork)
 
static IPNetwork WideSubnet (IPNetwork[] ipnetworks)
 
static string Print (IPNetwork ipnetwork)
 Print an ipnetwork in a clear representation string More...
 
static bool TryGuessCidr (string ip, out byte cidr)
 
static bool TryParseCidr (string sidr, AddressFamily family, out byte?cidr)
 Try to parse cidr. Have to be >= 0 and <= 32 More...
 
static IPAddressCollection ListIPAddress (IPNetwork ipnetwork)
 
static Int32 Compare (IPNetwork left, IPNetwork right)
 
static Boolean Equals (IPNetwork left, IPNetwork right)
 
static Boolean operator== (IPNetwork left, IPNetwork right)
 
static Boolean operator!= (IPNetwork left, IPNetwork right)
 
static Boolean operator< (IPNetwork left, IPNetwork right)
 
static Boolean operator> (IPNetwork left, IPNetwork right)
 

Properties

IPAddress Network [get]
 Network address More...
 
AddressFamily AddressFamily [get]
 Address Family More...
 
IPAddress Netmask [get]
 Netmask More...
 
IPAddress Broadcast [get]
 Broadcast address More...
 
IPAddress FirstUsable [get]
 First usable IP adress in Network More...
 
IPAddress LastUsable [get]
 Last usable IP adress in Network More...
 
BigInteger Usable [get]
 Number of usable IP adress in Network More...
 
BigInteger Total [get]
 Number of IP adress in Network More...
 
byte Cidr [get]
 The CIDR netmask notation More...
 
static IPNetwork IANA_ABLK_RESERVED1 [get]
 10.0.0.0/8 More...
 
static IPNetwork IANA_BBLK_RESERVED1 [get]
 172.12.0.0/12 More...
 
static IPNetwork IANA_CBLK_RESERVED1 [get]
 192.168.0.0/16 More...
 

Detailed Description

IP Network utility class. Use IPNetwork.Parse to create instances.

Member Function Documentation

static uint System.Net.IPNetwork.BitsSet ( IPAddress  netmask)
inlinestatic

Count bits set to 1 in netmask

Parameters
netmask
Returns
static Int32 System.Net.IPNetwork.Compare ( IPNetwork  left,
IPNetwork  right 
)
inlinestatic

Need a better way to do it

#region TrySubstractNetwork

public static bool TrySubstractNetwork(IPNetwork[] ipnetworks, IPNetwork substract, out IEnumerable<IPNetwork> result) {

if (ipnetworks == null) {
    result = null;
    return false;
}
if (ipnetworks.Length <= 0) {
    result = null;
    return false;
}
if (substract == null) {
    result = null;
    return false;
}
var results = new List<IPNetwork>();
foreach (var ipn in ipnetworks) {
    if (!Overlap(ipn, substract)) {
        results.Add(ipn);
        continue;
    }

    var collection = IPNetwork.Subnet(ipn, substract.Cidr);
    var rtemp = new List<IPNetwork>();
    foreach(var subnet in collection) {
        if (subnet != substract) {
            rtemp.Add(subnet);
        }
    }
    var supernets = Supernet(rtemp.ToArray());
    results.AddRange(supernets);
}
result = results;
return true;

} #endregion

static bool System.Net.IPNetwork.Contains ( IPNetwork  network,
IPAddress  ipaddress 
)
inlinestatic

return true if ipaddress is contained in network

Parameters
network
ipaddress
Returns
static bool System.Net.IPNetwork.Contains ( IPNetwork  network,
IPNetwork  network2 
)
inlinestatic

return true is network2 is fully contained in network

Parameters
network
network2
Returns
static bool System.Net.IPNetwork.IsIANAReserved ( IPAddress  ipaddress)
inlinestatic

return true if ipaddress is contained in IANA_ABLK_RESERVED1, IANA_BBLK_RESERVED1, IANA_CBLK_RESERVED1

Parameters
ipaddress
Returns
static bool System.Net.IPNetwork.IsIANAReserved ( IPNetwork  ipnetwork)
inlinestatic

return true if ipnetwork is contained in IANA_ABLK_RESERVED1, IANA_BBLK_RESERVED1, IANA_CBLK_RESERVED1

Parameters
ipnetwork
Returns
static bool System.Net.IPNetwork.Overlap ( IPNetwork  network,
IPNetwork  network2 
)
inlinestatic

return true is network2 overlap network

Parameters
network
network2
Returns
static IPNetwork System.Net.IPNetwork.Parse ( string  ipaddress,
string  netmask 
)
inlinestatic

192.168.168.100 - 255.255.255.0

Network : 192.168.168.0 Netmask : 255.255.255.0 Cidr : 24 Start : 192.168.168.1 End : 192.168.168.254 Broadcast : 192.168.168.255

Parameters
ipaddress
netmask
Returns
static IPNetwork System.Net.IPNetwork.Parse ( string  ipaddress,
byte  cidr 
)
inlinestatic

192.168.168.100/24

Network : 192.168.168.0 Netmask : 255.255.255.0 Cidr : 24 Start : 192.168.168.1 End : 192.168.168.254 Broadcast : 192.168.168.255

Parameters
ipaddress
cidr
Returns
static IPNetwork System.Net.IPNetwork.Parse ( IPAddress  ipaddress,
IPAddress  netmask 
)
inlinestatic

192.168.168.100 255.255.255.0

Network : 192.168.168.0 Netmask : 255.255.255.0 Cidr : 24 Start : 192.168.168.1 End : 192.168.168.254 Broadcast : 192.168.168.255

Parameters
ipaddress
netmask
Returns
static IPNetwork System.Net.IPNetwork.Parse ( string  network)
inlinestatic

192.168.0.1/24 192.168.0.1 255.255.255.0

Network : 192.168.0.0 Netmask : 255.255.255.0 Cidr : 24 Start : 192.168.0.1 End : 192.168.0.254 Broadcast : 192.168.0.255

Parameters
network
Returns
static string System.Net.IPNetwork.Print ( IPNetwork  ipnetwork)
inlinestatic

Print an ipnetwork in a clear representation string

Parameters
ipnetwork
Returns
static IPNetworkCollection System.Net.IPNetwork.Subnet ( IPNetwork  network,
byte  cidr 
)
inlinestatic

Subnet a network into multiple nets of cidr mask Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25 Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9

Parameters
ipnetwork
cidr
Returns
static IPNetwork System.Net.IPNetwork.Supernet ( IPNetwork  network1,
IPNetwork  network2 
)
inlinestatic

Supernet two consecutive cidr equal subnet into a single one 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24

Parameters
network1
network2
Returns
static IPNetwork [] System.Net.IPNetwork.Supernet ( IPNetwork[]  ipnetworks)
inlinestatic

Supernet a list of subnet 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 192.168.0.0/24 + 192.168.1.0/24 + 192.168.2.0/24 + 192.168.3.0/24 = 192.168.0.0/22

Parameters
ipnetworks
supernet
Returns
static BigInteger System.Net.IPNetwork.ToBigInteger ( IPAddress  ipaddress)
inlinestatic

Convert an ipadress to decimal 0.0.0.0 -> 0 0.0.1.0 -> 256

Parameters
ipaddress
Returns
static byte System.Net.IPNetwork.ToCidr ( IPAddress  netmask)
inlinestatic

Convert netmask to CIDR 255.255.255.0 -> 24 255.255.0.0 -> 16 255.0.0.0 -> 8

Parameters
netmask
Returns
static IPAddress System.Net.IPNetwork.ToIPAddress ( BigInteger  ipaddress,
AddressFamily  family 
)
inlinestatic

Transform a uint ipaddress into IPAddress object

Parameters
ipaddress
Returns
static IPAddress System.Net.IPNetwork.ToNetmask ( byte  cidr,
AddressFamily  family 
)
inlinestatic

Convert CIDR to netmask 24 -> 255.255.255.0 16 -> 255.255.0.0 8 -> 255.0.0.0

http://snipplr.com/view/15557/cidr-class-for-ipv4/

Parameters
cidr
Returns
static BigInteger System.Net.IPNetwork.ToUint ( byte  cidr,
AddressFamily  family 
)
inlinestatic

Convert a cidr to BigInteger netmask

Parameters
cidr
Returns
static bool System.Net.IPNetwork.TryGuessCidr ( string  ip,
out byte  cidr 
)
inlinestatic

Class Leading bits Default netmask A (CIDR /8) 00 255.0.0.0 A (CIDR /8) 01 255.0.0.0 B (CIDR /16) 10 255.255.0.0 C (CIDR /24) 11 255.255.255.0

Parameters
ip
cidr
Returns
static bool System.Net.IPNetwork.TryParse ( string  ipaddress,
string  netmask,
out IPNetwork  ipnetwork 
)
inlinestatic

192.168.168.100 - 255.255.255.0

Network : 192.168.168.0 Netmask : 255.255.255.0 Cidr : 24 Start : 192.168.168.1 End : 192.168.168.254 Broadcast : 192.168.168.255

Parameters
ipaddress
netmask
Returns
static bool System.Net.IPNetwork.TryParse ( string  ipaddress,
byte  cidr,
out IPNetwork  ipnetwork 
)
inlinestatic

192.168.168.100/24

Network : 192.168.168.0 Netmask : 255.255.255.0 Cidr : 24 Start : 192.168.168.1 End : 192.168.168.254 Broadcast : 192.168.168.255

Parameters
ipaddress
cidr
Returns
static bool System.Net.IPNetwork.TryParse ( string  network,
out IPNetwork  ipnetwork 
)
inlinestatic

192.168.0.1/24 192.168.0.1 255.255.255.0

Network : 192.168.0.0 Netmask : 255.255.255.0 Cidr : 24 Start : 192.168.0.1 End : 192.168.0.254 Broadcast : 192.168.0.255

Parameters
network
ipnetwork
Returns
static bool System.Net.IPNetwork.TryParse ( IPAddress  ipaddress,
IPAddress  netmask,
out IPNetwork  ipnetwork 
)
inlinestatic

192.168.0.1/24 192.168.0.1 255.255.255.0

Network : 192.168.0.0 Netmask : 255.255.255.0 Cidr : 24 Start : 192.168.0.1 End : 192.168.0.254 Broadcast : 192.168.0.255

Parameters
ipaddress
netmask
ipnetwork
Returns
static bool System.Net.IPNetwork.TryParseCidr ( string  sidr,
AddressFamily  family,
out byte?  cidr 
)
inlinestatic

Try to parse cidr. Have to be >= 0 and <= 32

Parameters
sidr
cidr
Returns
static bool System.Net.IPNetwork.TrySubnet ( IPNetwork  network,
byte  cidr,
out IPNetworkCollection  ipnetworkCollection 
)
inlinestatic

Subnet a network into multiple nets of cidr mask Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25 Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9

Parameters
ipnetwork
cidr
Returns
static bool System.Net.IPNetwork.TrySupernet ( IPNetwork  network1,
IPNetwork  network2,
out IPNetwork  supernet 
)
inlinestatic

Try to supernet two consecutive cidr equal subnet into a single one 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24

Parameters
network1
network2
Returns
static bool System.Net.IPNetwork.TrySupernet ( IPNetwork[]  ipnetworks,
out IPNetwork[]  supernet 
)
inlinestatic

Supernet a list of subnet 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23 192.168.0.0/24 + 192.168.1.0/24 + 192.168.2.0/24 + 192.168.3.0/24 = 192.168.0.0/22

Parameters
ipnetworks
supernet
Returns
static bool System.Net.IPNetwork.TryToBigInteger ( IPAddress  ipaddress,
out BigInteger?  uintIpAddress 
)
inlinestatic

Convert an ipadress to decimal 0.0.0.0 -> 0 0.0.1.0 -> 256

Parameters
ipaddress
Returns
static bool System.Net.IPNetwork.TryToCidr ( IPAddress  netmask,
out byte?  cidr 
)
inlinestatic

Convert netmask to CIDR 255.255.255.0 -> 24 255.255.0.0 -> 16 255.0.0.0 -> 8

Parameters
netmask
Returns
static bool System.Net.IPNetwork.TryToNetmask ( byte  cidr,
AddressFamily  family,
out IPAddress  netmask 
)
inlinestatic

Convert CIDR to netmask 24 -> 255.255.255.0 16 -> 255.255.0.0 8 -> 255.0.0.0

http://snipplr.com/view/15557/cidr-class-for-ipv4/

Parameters
cidr
Returns
static bool System.Net.IPNetwork.TryToUint ( byte  cidr,
AddressFamily  family,
out BigInteger?  uintNetmask 
)
inlinestatic

Convert a cidr to uint netmask

Parameters
cidr
Returns
static bool System.Net.IPNetwork.ValidNetmask ( IPAddress  netmask)
inlinestatic

return true if netmask is a valid netmask 255.255.255.0, 255.0.0.0, 255.255.240.0, ...

http://www.actionsnip.com/snippets/tomo_atlacatl/calculate-if-a-netmask-is-valid--as2-

Parameters
netmask
Returns

Property Documentation

AddressFamily System.Net.IPNetwork.AddressFamily
get

Address Family

IPAddress System.Net.IPNetwork.Broadcast
get

Broadcast address

byte System.Net.IPNetwork.Cidr
get

The CIDR netmask notation

IPAddress System.Net.IPNetwork.FirstUsable
get

First usable IP adress in Network

IPNetwork System.Net.IPNetwork.IANA_ABLK_RESERVED1
staticget

10.0.0.0/8

Returns
IPNetwork System.Net.IPNetwork.IANA_BBLK_RESERVED1
staticget

172.12.0.0/12

Returns
IPNetwork System.Net.IPNetwork.IANA_CBLK_RESERVED1
staticget

192.168.0.0/16

Returns
IPAddress System.Net.IPNetwork.LastUsable
get

Last usable IP adress in Network

IPAddress System.Net.IPNetwork.Netmask
get

Netmask

IPAddress System.Net.IPNetwork.Network
get

Network address

BigInteger System.Net.IPNetwork.Total
get

Number of IP adress in Network

BigInteger System.Net.IPNetwork.Usable
get

Number of usable IP adress in Network


The documentation for this class was generated from the following file: