diff options
| author | LogicalPhallacy <44458166+LogicalPhallacy@users.noreply.github.com> | 2019-01-23 00:31:35 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-23 00:31:35 -0800 |
| commit | 404bd04cbc17dc8c8bf4a5c9aa3ca9c5cd85aa68 (patch) | |
| tree | 3d267c6ceef9439a034c113095e10e4d619e7c70 /Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs | |
| parent | 8ff89fdc0c30f595a171ffc550f907ef22b6212a (diff) | |
| parent | e05e002b8bb4d13eb2b80b56a0aad8903ddb701e (diff) | |
Merge pull request #8 from jellyfin/master
rebase to latest master
Diffstat (limited to 'Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs')
| -rw-r--r-- | Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs | 348 |
1 files changed, 143 insertions, 205 deletions
diff --git a/Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs b/Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs index 8d0fb7997..16f39daf7 100644 --- a/Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs +++ b/Emby.Server.Implementations/Networking/IPNetwork/IPNetwork.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using System.Net.Sockets; using System.Numerics; @@ -7,7 +7,7 @@ using System.Text.RegularExpressions; namespace System.Net { /// <summary> - /// IP Network utility class. + /// IP Network utility class. /// Use IPNetwork.Parse to create instances. /// </summary> public class IPNetwork : IComparable<IPNetwork> @@ -33,7 +33,7 @@ namespace System.Net { get { - BigInteger uintNetwork = this._ipaddress & this._netmask; + var uintNetwork = this._ipaddress & this._netmask; return uintNetwork; } } @@ -41,44 +41,19 @@ namespace System.Net /// <summary> /// Network address /// </summary> - public IPAddress Network - { - get - { - - return IPNetwork.ToIPAddress(this._network, this._family); - } - } + public IPAddress Network => IPNetwork.ToIPAddress(this._network, this._family); /// <summary> /// Address Family /// </summary> - public AddressFamily AddressFamily - { - get - { - return this._family; - } - } + public AddressFamily AddressFamily => this._family; - private BigInteger _netmask - { - get - { - return IPNetwork.ToUint(this._cidr, this._family); - } - } + private BigInteger _netmask => IPNetwork.ToUint(this._cidr, this._family); /// <summary> /// Netmask /// </summary> - public IPAddress Netmask - { - get - { - return IPNetwork.ToIPAddress(this._netmask, this._family); - } - } + public IPAddress Netmask => IPNetwork.ToIPAddress(this._netmask, this._family); private BigInteger _broadcast { @@ -86,7 +61,7 @@ namespace System.Net { int width = this._family == Sockets.AddressFamily.InterNetwork ? 4 : 16; - BigInteger uintBroadcast = this._network + this._netmask.PositiveReverse(width); + var uintBroadcast = this._network + this._netmask.PositiveReverse(width); return uintBroadcast; } } @@ -113,7 +88,7 @@ namespace System.Net { get { - BigInteger fisrt = this._family == Sockets.AddressFamily.InterNetworkV6 + var fisrt = this._family == Sockets.AddressFamily.InterNetworkV6 ? this._network : (this.Usable <= 0) ? this._network : this._network + 1; return IPNetwork.ToIPAddress(fisrt, this._family); @@ -127,7 +102,7 @@ namespace System.Net { get { - BigInteger last = this._family == Sockets.AddressFamily.InterNetworkV6 + var last = this._family == Sockets.AddressFamily.InterNetworkV6 ? this._broadcast : (this.Usable <= 0) ? this._network : this._broadcast - 1; return IPNetwork.ToIPAddress(last, this._family); @@ -147,8 +122,8 @@ namespace System.Net return this.Total; } byte[] mask = new byte[] { 0xff, 0xff, 0xff, 0xff, 0x00 }; - BigInteger bmask = new BigInteger(mask); - BigInteger usableIps = (_cidr > 30) ? 0 : ((bmask >> _cidr) - 1); + var bmask = new BigInteger(mask); + var usableIps = (_cidr > 30) ? 0 : ((bmask >> _cidr) - 1); return usableIps; } } @@ -162,7 +137,7 @@ namespace System.Net { int max = this._family == Sockets.AddressFamily.InterNetwork ? 32 : 128; - BigInteger count = BigInteger.Pow(2, (max - _cidr)); + var count = BigInteger.Pow(2, (max - _cidr)); return count; } } @@ -171,13 +146,7 @@ namespace System.Net /// <summary> /// The CIDR netmask notation /// </summary> - public byte Cidr - { - get - { - return this._cidr; - } - } + public byte Cidr => this._cidr; #endregion @@ -195,7 +164,7 @@ namespace System.Net int maxCidr = family == Sockets.AddressFamily.InterNetwork ? 32 : 128; if (cidr > maxCidr) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } this._ipaddress = ipaddress; @@ -210,7 +179,7 @@ namespace System.Net /// <summary> /// 192.168.168.100 - 255.255.255.0 - /// + /// /// Network : 192.168.168.0 /// Netmask : 255.255.255.0 /// Cidr : 24 @@ -231,7 +200,7 @@ namespace System.Net /// <summary> /// 192.168.168.100/24 - /// + /// /// Network : 192.168.168.0 /// Netmask : 255.255.255.0 /// Cidr : 24 @@ -253,7 +222,7 @@ namespace System.Net /// <summary> /// 192.168.168.100 255.255.255.0 - /// + /// /// Network : 192.168.168.0 /// Netmask : 255.255.255.0 /// Cidr : 24 @@ -276,7 +245,7 @@ namespace System.Net /// <summary> /// 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 @@ -303,7 +272,7 @@ namespace System.Net /// <summary> /// 192.168.168.100 - 255.255.255.0 - /// + /// /// Network : 192.168.168.0 /// Netmask : 255.255.255.0 /// Cidr : 24 @@ -329,7 +298,7 @@ namespace System.Net /// <summary> /// 192.168.168.100/24 - /// + /// /// Network : 192.168.168.0 /// Netmask : 255.255.255.0 /// Cidr : 24 @@ -354,7 +323,7 @@ namespace System.Net /// <summary> /// 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 @@ -379,7 +348,7 @@ namespace System.Net /// <summary> /// 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 @@ -409,7 +378,7 @@ namespace System.Net /// <summary> /// 192.168.168.100 - 255.255.255.0 - /// + /// /// Network : 192.168.168.0 /// Netmask : 255.255.255.0 /// Cidr : 24 @@ -427,7 +396,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } ipnetwork = null; return; @@ -437,7 +406,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("netmask"); + throw new ArgumentNullException(nameof(netmask)); } ipnetwork = null; return; @@ -477,7 +446,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("network"); + throw new ArgumentNullException(nameof(network)); } ipnetwork = null; return; @@ -520,7 +489,7 @@ namespace System.Net /// <summary> /// 192.168.168.100 255.255.255.0 - /// + /// /// Network : 192.168.168.0 /// Netmask : 255.255.255.0 /// Cidr : 24 @@ -538,7 +507,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } ipnetwork = null; return; @@ -548,15 +517,14 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("netmask"); + throw new ArgumentNullException(nameof(netmask)); } ipnetwork = null; return; } - BigInteger uintIpAddress = IPNetwork.ToBigInteger(ipaddress); - byte? cidr2 = null; - bool parsed = IPNetwork.TryToCidr(netmask, out cidr2); + var uintIpAddress = IPNetwork.ToBigInteger(ipaddress); + bool parsed = IPNetwork.TryToCidr(netmask, out var cidr2); if (parsed == false) { if (tryParse == false) @@ -568,7 +536,7 @@ namespace System.Net } byte cidr = (byte)cidr2; - IPNetwork ipnet = new IPNetwork(uintIpAddress, ipaddress.AddressFamily, cidr); + var ipnet = new IPNetwork(uintIpAddress, ipaddress.AddressFamily, cidr); ipnetwork = ipnet; return; @@ -578,7 +546,7 @@ namespace System.Net /// <summary> /// 192.168.168.100/24 - /// + /// /// Network : 192.168.168.0 /// Netmask : 255.255.255.0 /// Cidr : 24 @@ -596,7 +564,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } ipnetwork = null; return; @@ -646,8 +614,7 @@ namespace System.Net /// <returns></returns> public static BigInteger ToBigInteger(IPAddress ipaddress) { - BigInteger? uintIpAddress = null; - IPNetwork.InternalToBigInteger(false, ipaddress, out uintIpAddress); + IPNetwork.InternalToBigInteger(false, ipaddress, out var uintIpAddress); return (BigInteger)uintIpAddress; } @@ -661,8 +628,7 @@ namespace System.Net /// <returns></returns> public static bool TryToBigInteger(IPAddress ipaddress, out BigInteger? uintIpAddress) { - BigInteger? uintIpAddress2 = null; - IPNetwork.InternalToBigInteger(true, ipaddress, out uintIpAddress2); + IPNetwork.InternalToBigInteger(true, ipaddress, out var uintIpAddress2); bool parsed = (uintIpAddress2 != null); uintIpAddress = uintIpAddress2; return parsed; @@ -680,7 +646,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } uintIpAddress = null; return; @@ -712,9 +678,7 @@ namespace System.Net /// <returns></returns> public static BigInteger ToUint(byte cidr, AddressFamily family) { - - BigInteger? uintNetmask = null; - IPNetwork.InternalToBigInteger(false, cidr, family, out uintNetmask); + IPNetwork.InternalToBigInteger(false, cidr, family, out var uintNetmask); return (BigInteger)uintNetmask; } @@ -726,9 +690,7 @@ namespace System.Net /// <returns></returns> public static bool TryToUint(byte cidr, AddressFamily family, out BigInteger? uintNetmask) { - - BigInteger? uintNetmask2 = null; - IPNetwork.InternalToBigInteger(true, cidr, family, out uintNetmask2); + IPNetwork.InternalToBigInteger(true, cidr, family, out var uintNetmask2); bool parsed = (uintNetmask2 != null); uintNetmask = uintNetmask2; return parsed; @@ -751,7 +713,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } uintNetmask = null; return; @@ -761,7 +723,7 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } uintNetmask = null; return; @@ -785,7 +747,7 @@ namespace System.Net return; } - BigInteger mask = new BigInteger(new byte[] { + var mask = new BigInteger(new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -793,7 +755,7 @@ namespace System.Net 0x00 }); - BigInteger masked = cidr == 0 ? 0 : mask << (128 - cidr); + var masked = cidr == 0 ? 0 : mask << (128 - cidr); byte[] m = masked.ToByteArray(); byte[] bmask = new byte[17]; int copy = m.Length > 16 ? 16 : m.Length; @@ -843,8 +805,7 @@ namespace System.Net /// <returns></returns> public static byte ToCidr(IPAddress netmask) { - byte? cidr = null; - IPNetwork.InternalToCidr(false, netmask, out cidr); + IPNetwork.InternalToCidr(false, netmask, out var cidr); return (byte)cidr; } @@ -858,8 +819,7 @@ namespace System.Net /// <returns></returns> public static bool TryToCidr(IPAddress netmask, out byte? cidr) { - byte? cidr2 = null; - IPNetwork.InternalToCidr(true, netmask, out cidr2); + IPNetwork.InternalToCidr(true, netmask, out var cidr2); bool parsed = (cidr2 != null); cidr = cidr2; return parsed; @@ -872,13 +832,13 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentNullException("netmask"); + throw new ArgumentNullException(nameof(netmask)); } cidr = null; return; } - BigInteger? uintNetmask2 = null; - bool parsed = IPNetwork.TryToBigInteger(netmask, out uintNetmask2); + + bool parsed = IPNetwork.TryToBigInteger(netmask, out var uintNetmask2); /// 20180217 lduchosal /// impossible to reach code. @@ -889,10 +849,9 @@ namespace System.Net /// cidr = null; /// return; /// } - BigInteger uintNetmask = (BigInteger)uintNetmask2; + var uintNetmask = (BigInteger)uintNetmask2; - byte? cidr2 = null; - IPNetwork.InternalToCidr(tryParse, uintNetmask, netmask.AddressFamily, out cidr2); + IPNetwork.InternalToCidr(tryParse, uintNetmask, netmask.AddressFamily, out var cidr2); cidr = cidr2; return; @@ -962,7 +921,7 @@ namespace System.Net /// 20180217 lduchosal /// impossible to reach code, byte cannot be negative : - /// + /// /// if (cidr < 0) { /// if (tryParse == false) { /// throw new ArgumentOutOfRangeException("cidr"); @@ -976,14 +935,14 @@ namespace System.Net { if (tryParse == false) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } netmask = null; return; } - BigInteger mask = IPNetwork.ToUint(cidr, family); - IPAddress netmask2 = IPNetwork.ToIPAddress(mask, family); + var mask = IPNetwork.ToUint(cidr, family); + var netmask2 = IPNetwork.ToIPAddress(mask, family); netmask = netmask2; return; @@ -1021,7 +980,7 @@ namespace System.Net /// <returns></returns> public static uint BitsSet(IPAddress netmask) { - BigInteger uintNetmask = IPNetwork.ToBigInteger(netmask); + var uintNetmask = IPNetwork.ToBigInteger(netmask); uint bits = IPNetwork.BitsSet(uintNetmask, netmask.AddressFamily); return bits; } @@ -1042,9 +1001,9 @@ namespace System.Net if (netmask == null) { - throw new ArgumentNullException("netmask"); + throw new ArgumentNullException(nameof(netmask)); } - BigInteger uintNetmask = IPNetwork.ToBigInteger(netmask); + var uintNetmask = IPNetwork.ToBigInteger(netmask); bool valid = IPNetwork.InternalValidNetmask(uintNetmask, netmask.AddressFamily); return valid; } @@ -1073,7 +1032,7 @@ namespace System.Net 0x00 }); - BigInteger neg = ((~netmask) & (mask)); + var neg = ((~netmask) & (mask)); bool isNetmask = ((neg + 1) & neg) == 0; return isNetmask; @@ -1099,7 +1058,7 @@ namespace System.Net Array.Reverse(bytes2); byte[] sized = Resize(bytes2, family); - IPAddress ip = new IPAddress(sized); + var ip = new IPAddress(sized); return ip; } @@ -1145,7 +1104,7 @@ namespace System.Net if (ipaddress == null) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } if (AddressFamily != ipaddress.AddressFamily) @@ -1153,9 +1112,9 @@ namespace System.Net return false; } - BigInteger uintNetwork = _network; - BigInteger uintBroadcast = _broadcast; - BigInteger uintAddress = IPNetwork.ToBigInteger(ipaddress); + var uintNetwork = _network; + var uintBroadcast = _broadcast; + var uintAddress = IPNetwork.ToBigInteger(ipaddress); bool contains = (uintAddress >= uintNetwork && uintAddress <= uintBroadcast); @@ -1174,14 +1133,14 @@ namespace System.Net if (network2 == null) { - throw new ArgumentNullException("network2"); + throw new ArgumentNullException(nameof(network2)); } - BigInteger uintNetwork = _network; - BigInteger uintBroadcast = _broadcast; + var uintNetwork = _network; + var uintBroadcast = _broadcast; - BigInteger uintFirst = network2._network; - BigInteger uintLast = network2._broadcast; + var uintFirst = network2._network; + var uintLast = network2._broadcast; bool contains = (uintFirst >= uintNetwork && uintLast <= uintBroadcast); @@ -1203,14 +1162,14 @@ namespace System.Net if (network2 == null) { - throw new ArgumentNullException("network2"); + throw new ArgumentNullException(nameof(network2)); } - BigInteger uintNetwork = _network; - BigInteger uintBroadcast = _broadcast; + var uintNetwork = _network; + var uintBroadcast = _broadcast; - BigInteger uintFirst = network2._network; - BigInteger uintLast = network2._broadcast; + var uintFirst = network2._network; + var uintLast = network2._broadcast; bool overlap = (uintFirst >= uintNetwork && uintFirst <= uintBroadcast) @@ -1242,40 +1201,22 @@ namespace System.Net /// 10.0.0.0/8 /// </summary> /// <returns></returns> - public static IPNetwork IANA_ABLK_RESERVED1 - { - get - { - return _iana_ablock_reserved.Value; - } - } + public static IPNetwork IANA_ABLK_RESERVED1 => _iana_ablock_reserved.Value; /// <summary> /// 172.12.0.0/12 /// </summary> /// <returns></returns> - public static IPNetwork IANA_BBLK_RESERVED1 - { - get - { - return _iana_bblock_reserved.Value; - } - } + public static IPNetwork IANA_BBLK_RESERVED1 => _iana_bblock_reserved.Value; /// <summary> /// 192.168.0.0/16 /// </summary> /// <returns></returns> - public static IPNetwork IANA_CBLK_RESERVED1 - { - get - { - return _iana_cblock_reserved.Value; - } - } + public static IPNetwork IANA_CBLK_RESERVED1 => _iana_cblock_reserved.Value; /// <summary> - /// return true if ipaddress is contained in + /// return true if ipaddress is contained in /// IANA_ABLK_RESERVED1, IANA_BBLK_RESERVED1, IANA_CBLK_RESERVED1 /// </summary> /// <param name="ipaddress"></param> @@ -1285,7 +1226,7 @@ namespace System.Net if (ipaddress == null) { - throw new ArgumentNullException("ipaddress"); + throw new ArgumentNullException(nameof(ipaddress)); } return IPNetwork.IANA_ABLK_RESERVED1.Contains(ipaddress) @@ -1294,7 +1235,7 @@ namespace System.Net } /// <summary> - /// return true if ipnetwork is contained in + /// return true if ipnetwork is contained in /// IANA_ABLK_RESERVED1, IANA_BBLK_RESERVED1, IANA_CBLK_RESERVED1 /// </summary> /// <returns></returns> @@ -1356,7 +1297,7 @@ namespace System.Net { if (trySubnet == false) { - throw new ArgumentNullException("network"); + throw new ArgumentNullException(nameof(network)); } ipnetworkCollection = null; return; @@ -1367,7 +1308,7 @@ namespace System.Net { if (trySubnet == false) { - throw new ArgumentOutOfRangeException("cidr"); + throw new ArgumentOutOfRangeException(nameof(cidr)); } ipnetworkCollection = null; return; @@ -1395,9 +1336,9 @@ namespace System.Net /// <summary> /// 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 + /// 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 + /// 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24 /// </summary> /// <param name="network2"></param> /// <returns></returns> @@ -1410,9 +1351,9 @@ namespace System.Net /// <summary> /// 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 + /// 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 + /// 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24 /// </summary> /// <param name="network2"></param> /// <returns></returns> @@ -1438,7 +1379,7 @@ namespace System.Net { if (trySupernet == false) { - throw new ArgumentNullException("network1"); + throw new ArgumentNullException(nameof(network1)); } supernet = null; return; @@ -1448,7 +1389,7 @@ namespace System.Net { if (trySupernet == false) { - throw new ArgumentNullException("network2"); + throw new ArgumentNullException(nameof(network2)); } supernet = null; return; @@ -1477,8 +1418,8 @@ namespace System.Net return; } - IPNetwork first = (network1._network < network2._network) ? network1 : network2; - IPNetwork last = (network1._network > network2._network) ? network1 : network2; + var first = (network1._network < network2._network) ? network1 : network2; + var last = (network1._network > network2._network) ? network1 : network2; /// Starting from here : /// network1 and network2 have the same cidr, @@ -1492,16 +1433,16 @@ namespace System.Net { if (trySupernet == false) { - throw new ArgumentOutOfRangeException("network"); + throw new ArgumentOutOfRangeException(nameof(trySupernet), "TrySupernet was false while the first and last networks are not adjacent."); } supernet = null; return; } - BigInteger uintSupernet = first._network; + var uintSupernet = first._network; byte cidrSupernet = (byte)(first._cidr - 1); - IPNetwork networkSupernet = new IPNetwork(uintSupernet, first._family, cidrSupernet); + var networkSupernet = new IPNetwork(uintSupernet, first._family, cidrSupernet); if (networkSupernet._network != first._network) { if (trySupernet == false) @@ -1536,13 +1477,11 @@ namespace System.Net /// 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 /// </summary> - /// <param name="ipnetworks"></param> - /// <param name="supernet"></param> + /// <param name="ipnetworks">The IP networks</param> /// <returns></returns> public static IPNetwork[] Supernet(IPNetwork[] ipnetworks) { - IPNetwork[] supernet; - InternalSupernet(false, ipnetworks, out supernet); + InternalSupernet(false, ipnetworks, out var supernet); return supernet; } @@ -1573,7 +1512,7 @@ namespace System.Net { if (trySupernet == false) { - throw new ArgumentNullException("ipnetworks"); + throw new ArgumentNullException(nameof(ipnetworks)); } supernet = null; return false; @@ -1585,9 +1524,9 @@ namespace System.Net return true; } - List<IPNetwork> supernetted = new List<IPNetwork>(); - List<IPNetwork> ipns = IPNetwork.Array2List(ipnetworks); - Stack<IPNetwork> current = IPNetwork.List2Stack(ipns); + var supernetted = new List<IPNetwork>(); + var ipns = IPNetwork.Array2List(ipnetworks); + var current = IPNetwork.List2Stack(ipns); int previousCount = 0; int currentCount = current.Count; @@ -1597,8 +1536,8 @@ namespace System.Net supernetted.Clear(); while (current.Count > 1) { - IPNetwork ipn1 = current.Pop(); - IPNetwork ipn2 = current.Peek(); + var ipn1 = current.Pop(); + var ipn2 = current.Peek(); IPNetwork outNetwork = null; bool success = ipn1.TrySupernet(ipn2, out outNetwork); @@ -1628,7 +1567,7 @@ namespace System.Net private static Stack<IPNetwork> List2Stack(List<IPNetwork> list) { - Stack<IPNetwork> stack = new Stack<IPNetwork>(); + var stack = new Stack<IPNetwork>(); list.ForEach(new Action<IPNetwork>( delegate (IPNetwork ipn) { @@ -1640,7 +1579,7 @@ namespace System.Net private static List<IPNetwork> Array2List(IPNetwork[] array) { - List<IPNetwork> ipns = new List<IPNetwork>(); + var ipns = new List<IPNetwork>(); ipns.AddRange(array); IPNetwork.RemoveNull(ipns); ipns.Sort(new Comparison<IPNetwork>( @@ -1684,22 +1623,20 @@ namespace System.Net if (string.IsNullOrEmpty(start)) { - throw new ArgumentNullException("start"); + throw new ArgumentNullException(nameof(start)); } if (string.IsNullOrEmpty(end)) { - throw new ArgumentNullException("end"); + throw new ArgumentNullException(nameof(end)); } - IPAddress startIP; - if (!IPAddress.TryParse(start, out startIP)) + if (!IPAddress.TryParse(start, out var startIP)) { throw new ArgumentException("start"); } - IPAddress endIP; - if (!IPAddress.TryParse(end, out endIP)) + if (!IPAddress.TryParse(end, out var endIP)) { throw new ArgumentException("end"); } @@ -1709,10 +1646,10 @@ namespace System.Net throw new NotSupportedException("MixedAddressFamily"); } - IPNetwork ipnetwork = new IPNetwork(0, startIP.AddressFamily, 0); + var ipnetwork = new IPNetwork(0, startIP.AddressFamily, 0); for (byte cidr = 32; cidr >= 0; cidr--) { - IPNetwork wideSubnet = IPNetwork.Parse(start, cidr); + var wideSubnet = IPNetwork.Parse(start, cidr); if (wideSubnet.Contains(endIP)) { ipnetwork = wideSubnet; @@ -1750,15 +1687,16 @@ namespace System.Net { if (tryWide == false) { - throw new ArgumentNullException("ipnetworks"); + throw new ArgumentNullException(nameof(ipnetworks)); } ipnetwork = null; return; } - IPNetwork[] nnin = Array.FindAll<IPNetwork>(ipnetworks, new Predicate<IPNetwork>( - delegate (IPNetwork ipnet) { + IPNetwork[] nnin = Array.FindAll(ipnetworks, new Predicate<IPNetwork>( + delegate (IPNetwork ipnet) + { return ipnet != null; } )); @@ -1775,19 +1713,19 @@ namespace System.Net if (nnin.Length == 1) { - IPNetwork ipn0 = nnin[0]; + var ipn0 = nnin[0]; ipnetwork = ipn0; return; } - Array.Sort<IPNetwork>(nnin); - IPNetwork nnin0 = nnin[0]; - BigInteger uintNnin0 = nnin0._ipaddress; + Array.Sort(nnin); + var nnin0 = nnin[0]; + var uintNnin0 = nnin0._ipaddress; - IPNetwork nninX = nnin[nnin.Length - 1]; - IPAddress ipaddressX = nninX.Broadcast; + var nninX = nnin[nnin.Length - 1]; + var ipaddressX = nninX.Broadcast; - AddressFamily family = ipnetworks[0]._family; + var family = ipnetworks[0]._family; foreach (var ipnx in ipnetworks) { if (ipnx._family != family) @@ -1796,10 +1734,10 @@ namespace System.Net } } - IPNetwork ipn = new IPNetwork(0, family, 0); + var ipn = new IPNetwork(0, family, 0); for (byte cidr = nnin0._cidr; cidr >= 0; cidr--) { - IPNetwork wideSubnet = new IPNetwork(uintNnin0, family, cidr); + var wideSubnet = new IPNetwork(uintNnin0, family, cidr); if (wideSubnet.Contains(ipaddressX)) { ipn = wideSubnet; @@ -1822,7 +1760,7 @@ namespace System.Net public string Print() { - StringWriter sw = new StringWriter(); + var sw = new StringWriter(); sw.WriteLine("IPNetwork : {0}", ToString()); sw.WriteLine("Network : {0}", Network); @@ -1841,13 +1779,13 @@ namespace System.Net #region TryGuessCidr /// <summary> - /// - /// 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 - /// + /// + /// 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 + /// /// </summary> /// <param name="ip"></param> /// <param name="cidr"></param> @@ -1868,7 +1806,7 @@ namespace System.Net cidr = 64; return true; } - BigInteger uintIPAddress = IPNetwork.ToBigInteger(ipaddress); + var uintIPAddress = IPNetwork.ToBigInteger(ipaddress); uintIPAddress = uintIPAddress >> 29; if (uintIPAddress <= 3) { @@ -1931,7 +1869,7 @@ namespace System.Net /** * Need a better way to do it - * + * #region TrySubstractNetwork public static bool TrySubstractNetwork(IPNetwork[] ipnetworks, IPNetwork substract, out IEnumerable<IPNetwork> result) { @@ -1973,7 +1911,7 @@ namespace System.Net #region IComparable<IPNetwork> Members - public static Int32 Compare(IPNetwork left, IPNetwork right) + public static int Compare(IPNetwork left, IPNetwork right) { // two null IPNetworks are equal if (ReferenceEquals(left, null) && ReferenceEquals(right, null)) return 0; @@ -1994,12 +1932,12 @@ namespace System.Net return result; } - public Int32 CompareTo(IPNetwork other) + public int CompareTo(IPNetwork other) { return Compare(this, other); } - public Int32 CompareTo(Object obj) + public int CompareTo(object obj) { // null is at less if (obj == null) return 1; @@ -2012,7 +1950,7 @@ namespace System.Net { throw new ArgumentException( "The supplied parameter is an invalid type. Please supply an IPNetwork type.", - "obj"); + nameof(obj)); } // perform the comparision @@ -2023,17 +1961,17 @@ namespace System.Net #region IEquatable<IPNetwork> Members - public static Boolean Equals(IPNetwork left, IPNetwork right) + public static bool Equals(IPNetwork left, IPNetwork right) { return Compare(left, right) == 0; } - public Boolean Equals(IPNetwork other) + public bool Equals(IPNetwork other) { return Equals(this, other); } - public override Boolean Equals(Object obj) + public override bool Equals(object obj) { return Equals(this, obj as IPNetwork); } @@ -2042,22 +1980,22 @@ namespace System.Net #region Operators - public static Boolean operator ==(IPNetwork left, IPNetwork right) + public static bool operator ==(IPNetwork left, IPNetwork right) { return Equals(left, right); } - public static Boolean operator !=(IPNetwork left, IPNetwork right) + public static bool operator !=(IPNetwork left, IPNetwork right) { return !Equals(left, right); } - public static Boolean operator <(IPNetwork left, IPNetwork right) + public static bool operator <(IPNetwork left, IPNetwork right) { return Compare(left, right) < 0; } - public static Boolean operator >(IPNetwork left, IPNetwork right) + public static bool operator >(IPNetwork left, IPNetwork right) { return Compare(left, right) > 0; } |
