diff --git a/src/.editorconfig b/src/.editorconfig new file mode 100644 index 0000000..d2741cf --- /dev/null +++ b/src/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS1591: Missing XML comment for publicly visible type or member +dotnet_diagnostic.CS1591.severity = suggestion diff --git a/src/Gnu.Getopt/Gnu.Getopt.csproj b/src/Gnu.Getopt/Gnu.Getopt.csproj index 359d0e5..0723bd9 100644 --- a/src/Gnu.Getopt/Gnu.Getopt.csproj +++ b/src/Gnu.Getopt/Gnu.Getopt.csproj @@ -9,5 +9,8 @@ false System.Net.IPNetwork.snk + + + \ No newline at end of file diff --git a/src/System.Net.IPNetwork/CidrClassFull.cs b/src/System.Net.IPNetwork/CidrClassFull.cs index db18345..bda8594 100644 --- a/src/System.Net.IPNetwork/CidrClassFull.cs +++ b/src/System.Net.IPNetwork/CidrClassFull.cs @@ -3,6 +3,9 @@ namespace System.Net { + /// + /// Class CidrClassFull tries to guess CIDR in a ClassFull way. + /// public sealed class CidrClassFull : ICidrGuess { /// diff --git a/src/System.Net.IPNetwork/CidrClassLess.cs b/src/System.Net.IPNetwork/CidrClassLess.cs index 71ccbbc..cc71733 100644 --- a/src/System.Net.IPNetwork/CidrClassLess.cs +++ b/src/System.Net.IPNetwork/CidrClassLess.cs @@ -2,7 +2,9 @@ namespace System.Net { - + /// + /// Try to guess a CIDR in a ClassLess way ie. ipv4 = 32, ipv6 = 128 + /// public sealed class CidrClassLess : ICidrGuess { /// diff --git a/src/System.Net.IPNetwork/CidrGuess.cs b/src/System.Net.IPNetwork/CidrGuess.cs index 441d8fa..f7c254e 100644 --- a/src/System.Net.IPNetwork/CidrGuess.cs +++ b/src/System.Net.IPNetwork/CidrGuess.cs @@ -1,12 +1,30 @@ namespace System.Net { + /// + /// + /// public interface ICidrGuess { + /// + /// + /// + /// + /// + /// bool TryGuessCidr(string ip, out byte cidr); } + /// + /// + /// public static class CidrGuess { + /// + /// + /// public static ICidrGuess ClassFull { get => _cidr_classfull.Value; } + /// + /// + /// public static ICidrGuess ClassLess { get => _cidr_classless.Value; } private static readonly Lazy _cidr_classless = new Lazy(() => new CidrClassLess()); diff --git a/src/System.Net.IPNetwork/IPAddressCollection.cs b/src/System.Net.IPNetwork/IPAddressCollection.cs index c840027..9edbda4 100644 --- a/src/System.Net.IPNetwork/IPAddressCollection.cs +++ b/src/System.Net.IPNetwork/IPAddressCollection.cs @@ -4,13 +4,15 @@ namespace System.Net { - public enum FilterEnum { All, Usable } + /// + /// + /// public class IPAddressCollection : IEnumerable, IEnumerator { private readonly IPNetwork _ipnetwork; @@ -82,12 +84,16 @@ public bool MoveNext() } return true; } - + /// + /// + /// public void Reset() { _enumerator = -1; } - + /// + /// + /// public void Dispose() { // nothing to dispose diff --git a/src/System.Net.IPNetwork/IPNetwork.cs b/src/System.Net.IPNetwork/IPNetwork.cs index 5ee4f64..2ee5ec8 100644 --- a/src/System.Net.IPNetwork/IPNetwork.cs +++ b/src/System.Net.IPNetwork/IPNetwork.cs @@ -27,6 +27,9 @@ public sealed class IPNetwork : IComparable, ISerializable { //private uint _usable; private byte _cidr; + /// + /// + /// [DataMember(Name="IPNetwork", IsRequired=true)] public string Value { @@ -192,7 +195,12 @@ public byte Cidr { { Init(ipaddress, family, cidr); } - + /// + /// + /// + /// + /// + /// public IPNetwork(IPAddress ipaddress, byte cidr) { if (ipaddress == null) throw new ArgumentNullException(nameof(ipaddress)); @@ -325,12 +333,12 @@ public static IPNetwork Parse(string network, ICidrGuess cidrGuess) { IPNetwork.InternalParse(false, network, cidrGuess, out ipnetwork); return ipnetwork; - } - -#endregion - -#region TryParse - + } + + #endregion + + #region TryParse + /// /// 192.168.168.100 - 255.255.255.0 /// @@ -342,7 +350,8 @@ public static IPNetwork Parse(string network, ICidrGuess cidrGuess) { /// Broadcast : 192.168.168.255 /// /// - /// + /// + /// /// public static bool TryParse(string ipaddress, string netmask, out IPNetwork ipnetwork) { @@ -352,10 +361,10 @@ public static bool TryParse(string ipaddress, string netmask, out IPNetwork ipne ipnetwork = ipnetwork2; return parsed; - } - - - + } + + + /// /// 192.168.168.100/24 /// @@ -367,7 +376,8 @@ public static bool TryParse(string ipaddress, string netmask, out IPNetwork ipne /// Broadcast : 192.168.168.255 /// /// - /// + /// + /// /// public static bool TryParse(string ipaddress, byte cidr, out IPNetwork ipnetwork) { @@ -426,13 +436,13 @@ public static bool TryParse(IPAddress ipaddress, IPAddress netmask, out IPNetwor ipnetwork = ipnetwork2; return parsed; - } - - -#endregion - -#region InternalParse - + } + + + #endregion + + #region InternalParse + /// /// 192.168.168.100 - 255.255.255.0 /// @@ -442,9 +452,11 @@ public static bool TryParse(IPAddress ipaddress, IPAddress netmask, out IPNetwor /// Start : 192.168.168.1 /// End : 192.168.168.254 /// Broadcast : 192.168.168.255 - /// + /// + /// /// - /// + /// + /// /// private static void InternalParse(bool tryParse, string ipaddress, string netmask, out IPNetwork ipnetwork) { @@ -525,10 +537,10 @@ private static void InternalParse(bool tryParse, string network, ICidrGuess cidr IPNetwork.InternalParse(tryParse, args[0], args[1], out ipnetwork); return; - } - - - + } + + + /// /// 192.168.168.100 255.255.255.0 /// @@ -538,9 +550,11 @@ private static void InternalParse(bool tryParse, string network, ICidrGuess cidr /// Start : 192.168.168.1 /// End : 192.168.168.254 /// Broadcast : 192.168.168.255 - /// + /// + /// /// - /// + /// + /// /// private static void InternalParse(bool tryParse, IPAddress ipaddress, IPAddress netmask, out IPNetwork ipnetwork) { @@ -576,10 +590,10 @@ private static void InternalParse(bool tryParse, IPAddress ipaddress, IPAddress ipnetwork = ipnet; return; - } - - - + } + + + /// /// 192.168.168.100/24 /// @@ -589,9 +603,11 @@ private static void InternalParse(bool tryParse, IPAddress ipaddress, IPAddress /// Start : 192.168.168.1 /// End : 192.168.168.254 /// Broadcast : 192.168.168.255 - /// + /// + /// /// - /// + /// + /// /// private static void InternalParse(bool tryParse, string ipaddress, byte cidr, out IPNetwork ipnetwork) { @@ -646,14 +662,15 @@ public static BigInteger ToBigInteger(IPAddress ipaddress) { IPNetwork.InternalToBigInteger(false, ipaddress, out uintIpAddress); return (BigInteger)uintIpAddress; - } - + } + /// /// Convert an ipadress to decimal /// 0.0.0.0 -> 0 /// 0.0.1.0 -> 256 /// - /// + /// + /// /// public static bool TryToBigInteger(IPAddress ipaddress, out BigInteger? uintIpAddress) { BigInteger? uintIpAddress2 = null; @@ -709,26 +726,29 @@ static void InternalToBigInteger(bool tryParse, IPAddress ipaddress, out BigInte Buffer.BlockCopy(bytes, 0, unsigned, 0, bytes.Length); uintIpAddress = new BigInteger(unsigned); #endif - } - - + } + + /// /// Convert a cidr to BigInteger netmask /// - /// + /// + /// /// public static BigInteger ToUint(byte cidr, AddressFamily family) { BigInteger? uintNetmask = null; IPNetwork.InternalToBigInteger(false, cidr, family, out uintNetmask); return (BigInteger)uintNetmask; - } - - + } + + /// /// Convert a cidr to uint netmask /// - /// + /// + /// + /// /// public static bool TryToUint(byte cidr, AddressFamily family, out BigInteger? uintNetmask) { @@ -737,12 +757,15 @@ public static bool TryToUint(byte cidr, AddressFamily family, out BigInteger? ui bool parsed = (uintNetmask2 != null); uintNetmask = uintNetmask2; return parsed; - } - + } + /// /// Convert a cidr to uint netmask - /// - /// + /// + /// + /// + /// + /// /// #if TRAVISCI public @@ -799,19 +822,22 @@ static void InternalToBigInteger(bool tryParse, byte cidr, AddressFamily family, uintNetmask = new BigInteger(bmask); - } - -#endregion - -#region ToCidr - + } + + #endregion + + #region ToCidr + /// /// Convert netmask to CIDR /// 255.255.255.0 -> 24 /// 255.255.0.0 -> 16 /// 255.0.0.0 -> 8 - /// - /// + /// + /// + /// + /// + /// /// private static void InternalToCidr(bool tryParse, BigInteger netmask, AddressFamily family, out byte? cidr) { @@ -840,15 +866,16 @@ public static byte ToCidr(IPAddress netmask) { byte? cidr = null; IPNetwork.InternalToCidr(false, netmask, out cidr); return (byte)cidr; - } - + } + /// /// Convert netmask to CIDR /// 255.255.255.0 -> 24 /// 255.255.0.0 -> 16 /// 255.0.0.0 -> 8 /// - /// + /// + /// /// public static bool TryToCidr(IPAddress netmask, out byte? cidr) { byte? cidr2 = null; @@ -870,15 +897,16 @@ private static void InternalToCidr(bool tryParse, IPAddress netmask, out byte? c BigInteger? uintNetmask2 = null; bool parsed = IPNetwork.TryToBigInteger(netmask, out uintNetmask2); - /// 20180217 lduchosal - /// impossible to reach code. - /// if (parsed == false) { - /// if (tryParse == false) { - /// throw new ArgumentException("netmask"); - /// } - /// cidr = null; - /// return; - /// } + // 20180217 lduchosal + // impossible to reach code. + // if (parsed == false) { + // if (tryParse == false) { + // throw new ArgumentException("netmask"); + // } + // cidr = null; + // return; + // } + BigInteger uintNetmask = (BigInteger)uintNetmask2; byte? cidr2 = null; @@ -887,37 +915,40 @@ private static void InternalToCidr(bool tryParse, IPAddress netmask, out byte? c return; - } - - -#endregion - -#region ToNetmask - + } + + + #endregion + + #region ToNetmask + /// /// Convert CIDR to netmask /// 24 -> 255.255.255.0 /// 16 -> 255.255.0.0 /// 8 -> 255.0.0.0 /// - /// - /// + /// + /// + /// /// public static IPAddress ToNetmask(byte cidr, AddressFamily family) { IPAddress netmask = null; IPNetwork.InternalToNetmask(false, cidr, family, out netmask); return netmask; - } - + } + /// /// Convert CIDR to netmask /// 24 -> 255.255.255.0 /// 16 -> 255.255.0.0 /// 8 -> 255.0.0.0 /// - /// - /// + /// + /// + /// + /// /// public static bool TryToNetmask(byte cidr, AddressFamily family, out IPAddress netmask) { @@ -945,16 +976,16 @@ static void InternalToNetmask(bool tryParse, byte cidr, AddressFamily family, ou return; } - /// 20180217 lduchosal - /// impossible to reach code, byte cannot be negative : - /// - /// if (cidr < 0) { - /// if (tryParse == false) { - /// throw new ArgumentOutOfRangeException("cidr"); - /// } - /// netmask = null; - /// return; - /// } + // 20180217 lduchosal + // impossible to reach code, byte cannot be negative : + // + // if (cidr < 0) { + // if (tryParse == false) { + // throw new ArgumentOutOfRangeException("cidr"); + // } + // netmask = null; + // return; + // } int maxCidr = family == Sockets.AddressFamily.InterNetwork ? 32 : 128; if (cidr > maxCidr) { @@ -970,21 +1001,22 @@ static void InternalToNetmask(bool tryParse, byte cidr, AddressFamily family, ou netmask = netmask2; return; - } - -#endregion - -#endregion - -#region utils - -#region BitsSet - + } + + #endregion + + #endregion + + #region utils + + #region BitsSet + /// /// Count bits set to 1 in netmask /// - /// - /// + /// + /// + /// /// private static byte BitsSet(BigInteger netmask, AddressFamily family) { @@ -1015,7 +1047,7 @@ public static uint BitsSet(IPAddress netmask) { /// return true if netmask is a valid netmask /// 255.255.255.0, 255.0.0.0, 255.255.240.0, ... /// - /// + /// /// /// public static bool ValidNetmask(IPAddress netmask) { @@ -1054,16 +1086,17 @@ static bool InternalValidNetmask(BigInteger netmask, AddressFamily family) { bool isNetmask = ((neg + 1) & neg) == 0; return isNetmask; - } - -#endregion - -#region ToIPAddress - + } + + #endregion + + #region ToIPAddress + /// /// Transform a uint ipaddress into IPAddress object /// - /// + /// + /// /// public static IPAddress ToIPAddress(BigInteger ipaddress, AddressFamily family) { @@ -1331,14 +1364,15 @@ public static IPNetworkCollection Subnet(IPNetwork network, byte cidr) { throw new ArgumentNullException("network"); } return network.Subnet(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 /// - /// + /// + /// /// public bool TrySubnet(byte cidr, out IPNetworkCollection ipnetworkCollection) { IPNetworkCollection inc = null; @@ -1419,15 +1453,16 @@ public IPNetwork Supernet(IPNetwork network2) { [Obsolete("static Supernet is deprecated, please use instance Supernet.")] public static IPNetwork Supernet(IPNetwork network, IPNetwork network2) { return network.Supernet(network2); - } - + } + /// /// 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 /// - /// + /// + /// /// public bool TrySupernet(IPNetwork network2, out IPNetwork supernet) { @@ -1491,13 +1526,12 @@ static void InternalSupernet(bool trySupernet, IPNetwork network1, IPNetwork net IPNetwork first = (network1._network < network2._network) ? network1 : network2; IPNetwork last = (network1._network > network2._network) ? network1 : network2; - /// Starting from here : - /// network1 and network2 have the same cidr, - /// network1 does not contain network2, - /// network2 does not contain network1, - /// first is the lower subnet - /// last is the higher subnet - + // Starting from here : + // network1 and network2 have the same cidr, + // network1 does not contain network2, + // network2 does not contain network1, + // first is the lower subnet + // last is the higher subnet if ((first._broadcast + 1) != last._network) { if (trySupernet == false) { @@ -1543,7 +1577,6 @@ public override int GetHashCode() { /// 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 /// /// - /// /// public static IPNetwork[] Supernet(IPNetwork[] ipnetworks) { @@ -1843,9 +1876,10 @@ public static bool TryGuessCidr(string ip, out byte cidr) } /// - /// Try to parse cidr. Have to be >= 0 and <= 32 or 128 + /// Try to parse cidr. Have to be >= 0 and <= 32 or 128 /// /// + /// /// /// public static bool TryParseCidr(string sidr, AddressFamily family, out byte? cidr) { @@ -1870,62 +1904,71 @@ public static bool TryParseCidr(string sidr, AddressFamily family, out byte? cid #region ListIPAddress + /// + /// List all ip addresses in a subnet + /// + /// + /// [Obsolete("static ListIPAddress is deprecated, please use instance ListIPAddress.")] public static IPAddressCollection ListIPAddress(IPNetwork ipnetwork) { return ipnetwork.ListIPAddress(); } + /// + /// List all ip addresses in a subnet + /// + /// + /// public IPAddressCollection ListIPAddress(FilterEnum filter = FilterEnum.All) { return new IPAddressCollection(this, filter); - } - - -#endregion - - /** - * Need a better way to do it - * -#region TrySubstractNetwork - - public static bool TrySubstractNetwork(IPNetwork[] ipnetworks, IPNetwork substract, out IEnumerable 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(); - foreach (var ipn in ipnetworks) { - if (!Overlap(ipn, substract)) { - results.Add(ipn); - continue; - } - - var collection = ipn.Subnet(substract.Cidr); - var rtemp = new List(); - foreach(var subnet in collection) { - if (subnet != substract) { - rtemp.Add(subnet); - } - } - var supernets = Supernet(rtemp.ToArray()); - results.AddRange(supernets); - } - result = results; - return true; - } -#endregion - * **/ - -#region IComparable Members - + } + + + #endregion + + // + // Need a better way to do it + // + // #region TrySubstractNetwork + // + // public static bool TrySubstractNetwork(IPNetwork[] ipnetworks, IPNetwork substract, out IEnumerable 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(); + // foreach (var ipn in ipnetworks) { + // if (!Overlap(ipn, substract)) { + // results.Add(ipn); + // continue; + // } + // + // var collection = ipn.Subnet(substract.Cidr); + // var rtemp = new List(); + // foreach(var subnet in collection) { + // if (subnet != substract) { + // rtemp.Add(subnet); + // } + // } + // var supernets = Supernet(rtemp.ToArray()); + // results.AddRange(supernets); + // } + // result = results; + // return true; + // } + // #endregion + + #region IComparable Members + public static Int32 Compare(IPNetwork left, IPNetwork right) { // two null IPNetworks are equal @@ -1998,22 +2041,41 @@ public override Boolean Equals(Object obj) #endregion #region Operators - + /// + /// + /// + /// + /// public static Boolean operator ==(IPNetwork left, IPNetwork right) { return Equals(left, right); } - + /// + /// + /// + /// + /// + /// public static Boolean operator !=(IPNetwork left, IPNetwork right) { return !Equals(left, right); } - + /// + /// + /// + /// + /// + /// public static Boolean operator <(IPNetwork left, IPNetwork right) { return Compare(left, right) < 0; } - + /// + /// + /// + /// + /// + /// public static Boolean operator >(IPNetwork left, IPNetwork right) { return Compare(left, right) > 0; diff --git a/src/ipnetwork.sln b/src/ipnetwork.sln index 50f5e61..c5ca6af 100644 --- a/src/ipnetwork.sln +++ b/src/ipnetwork.sln @@ -15,9 +15,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lib", "lib", "{4597D016-3B7 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F29C77A1-AA9F-47B4-A04F-A1BD121D9242}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication", "ConsoleApplication\ConsoleApplication.csproj", "{017AB983-2928-46D3-A05A-D7CF97FC6F7C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApplication", "ConsoleApplication\ConsoleApplication.csproj", "{017AB983-2928-46D3-A05A-D7CF97FC6F7C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestProject", "TestProject\TestProject.csproj", "{D4A8E6B7-7902-4EA4-8CD1-3733C2529582}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestProject", "TestProject\TestProject.csproj", "{D4A8E6B7-7902-4EA4-8CD1-3733C2529582}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1C792F68-1C82-4E74-8585-99CAEC1D77DB}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution