IPNetwork  2.1.2
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

bool Contains (IPAddress ipaddress)
 return true if ipaddress is contained in network More...
 
bool Contains (IPNetwork network2)
 return true is network2 is fully contained in network More...
 
bool Overlap (IPNetwork network2)
 return true is network2 overlap network More...
 
override string ToString ()
 
bool IsIANAReserved ()
 return true if ipnetwork is contained in IANA_ABLK_RESERVED1, IANA_BBLK_RESERVED1, IANA_CBLK_RESERVED1 More...
 
IPNetworkCollection Subnet (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...
 
bool TrySubnet (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...
 
IPNetwork Supernet (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...
 
bool TrySupernet (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...
 
override int GetHashCode ()
 
string Print ()
 Print an ipnetwork in a clear representation string More...
 
IPAddressCollection ListIPAddress ()
 
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)
 
static bool Contains (IPNetwork network, IPNetwork network2)
 
static bool Overlap (IPNetwork network, IPNetwork network2)
 
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)
 
static IPNetworkCollection Subnet (IPNetwork network, byte cidr)
 
static bool TrySubnet (IPNetwork network, byte cidr, out IPNetworkCollection ipnetworkCollection)
 
static IPNetwork Supernet (IPNetwork network, IPNetwork network2)
 
static bool TrySupernet (IPNetwork network, IPNetwork network2, out IPNetwork supernet)
 
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 IPNetwork WideSubnet (string start, string end)
 
static bool TryWideSubnet (IPNetwork[] ipnetworks, out IPNetwork ipnetwork)
 
static IPNetwork WideSubnet (IPNetwork[] ipnetworks)
 
static string Print (IPNetwork ipnetwork)
 
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 or 128 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

◆ BitsSet()

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

Count bits set to 1 in netmask

Parameters
netmask
Returns

◆ Compare()

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 = ipn.Subnet(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

◆ Contains() [1/2]

bool System.Net.IPNetwork.Contains ( IPAddress  ipaddress)
inline

return true if ipaddress is contained in network

Parameters
ipaddress
Returns

◆ Contains() [2/2]

bool System.Net.IPNetwork.Contains ( IPNetwork  network2)
inline

return true is network2 is fully contained in network

Parameters
network2
Returns

◆ IsIANAReserved() [1/2]

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

◆ IsIANAReserved() [2/2]

bool System.Net.IPNetwork.IsIANAReserved ( )
inline

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

Returns

◆ Overlap()

bool System.Net.IPNetwork.Overlap ( IPNetwork  network2)
inline

return true is network2 overlap network

Parameters
network2
Returns

◆ Parse() [1/4]

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

◆ Parse() [2/4]

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

◆ Parse() [3/4]

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

◆ Parse() [4/4]

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

◆ Print()

string System.Net.IPNetwork.Print ( )
inline

Print an ipnetwork in a clear representation string

Returns

◆ Subnet()

IPNetworkCollection System.Net.IPNetwork.Subnet ( byte  cidr)
inline

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
cidr
Returns

◆ Supernet() [1/2]

IPNetwork System.Net.IPNetwork.Supernet ( IPNetwork  network2)
inline

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
network2
Returns

◆ Supernet() [2/2]

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

◆ ToBigInteger()

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

◆ ToCidr()

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

◆ ToIPAddress()

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

Transform a uint ipaddress into IPAddress object

Parameters
ipaddress
Returns

◆ ToNetmask()

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

◆ ToUint()

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

Convert a cidr to BigInteger netmask

Parameters
cidr
Returns

◆ TryGuessCidr()

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

◆ TryParse() [1/4]

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

◆ TryParse() [2/4]

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

◆ TryParse() [3/4]

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

◆ TryParse() [4/4]

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

◆ TryParseCidr()

static bool System.Net.IPNetwork.TryParseCidr ( string  sidr,
AddressFamily  family,
out byte?  cidr 
)
inlinestatic

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

Parameters
sidr
cidr
Returns

◆ TrySubnet()

bool System.Net.IPNetwork.TrySubnet ( byte  cidr,
out IPNetworkCollection  ipnetworkCollection 
)
inline

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
cidr
Returns

◆ TrySupernet() [1/2]

bool System.Net.IPNetwork.TrySupernet ( IPNetwork  network2,
out IPNetwork  supernet 
)
inline

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
network2
Returns

◆ TrySupernet() [2/2]

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

◆ TryToBigInteger()

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

◆ TryToCidr()

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

◆ TryToNetmask()

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

◆ TryToUint()

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

Convert a cidr to uint netmask

Parameters
cidr
Returns

◆ ValidNetmask()

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

AddressFamily System.Net.IPNetwork.AddressFamily
get

Address Family

◆ Broadcast

IPAddress System.Net.IPNetwork.Broadcast
get

Broadcast address

◆ Cidr

byte System.Net.IPNetwork.Cidr
get

The CIDR netmask notation

◆ FirstUsable

IPAddress System.Net.IPNetwork.FirstUsable
get

First usable IP adress in Network

◆ IANA_ABLK_RESERVED1

IPNetwork System.Net.IPNetwork.IANA_ABLK_RESERVED1
staticget

10.0.0.0/8

Returns

◆ IANA_BBLK_RESERVED1

IPNetwork System.Net.IPNetwork.IANA_BBLK_RESERVED1
staticget

172.12.0.0/12

Returns

◆ IANA_CBLK_RESERVED1

IPNetwork System.Net.IPNetwork.IANA_CBLK_RESERVED1
staticget

192.168.0.0/16

Returns

◆ LastUsable

IPAddress System.Net.IPNetwork.LastUsable
get

Last usable IP adress in Network

◆ Netmask

IPAddress System.Net.IPNetwork.Netmask
get

Netmask

◆ Network

IPAddress System.Net.IPNetwork.Network
get

Network address

◆ Total

BigInteger System.Net.IPNetwork.Total
get

Number of IP adress in Network

◆ Usable

BigInteger System.Net.IPNetwork.Usable
get

Number of usable IP adress in Network


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