Skip to content

Commit 0d02478

Browse files
authored
Chore: cleanup, breaking changes, enum, tryparse, exception, static ListIPAddress (#363)
* Chore: huge cleanup, enum, tryparse, exception, static ListIPAddress, important changes : IPNetwork comparison and sort order have change to reflect expected behavoir * Fix: obsolete enums * Fix: network sorting and member comparison * Chore: upgrade version number 3.3
1 parent 90baade commit 0d02478

40 files changed

+807
-547
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '3.2.{build}'
1+
version: '3.3.{build}'
22
image: Visual Studio 2022
33

44
assembly_info:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace System.Net;
77
/// <summary>
88
/// Switch and actions.
99
/// </summary>
10-
public enum ActionEnum
10+
public enum Action
1111
{
1212
/// <summary>
1313
/// Prints usage.
@@ -52,5 +52,5 @@ public enum ActionEnum
5252
/// <summary>
5353
/// Substract networks.
5454
/// </summary>
55-
SubtractNetwork,
55+
SubtractNetwork
5656
}

src/ConsoleApplication/ArgParsed.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ArgParsed
1212
/// <summary>
1313
/// Gets or sets position.
1414
/// </summary>
15-
public int Arg { get; set; }
15+
public int Arg { get; }
1616

1717
private event ArgParsedDelegate OnArgParsed;
1818

src/ConsoleApplication/CidrParseTypeEnum.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace System.Net;
77
/// <summary>
88
/// CidrParseEnum.
99
/// </summary>
10-
public enum CidrParseEnum
10+
public enum CidrParse
1111
{
1212
/// <summary>
1313
/// Default value,
@@ -17,5 +17,5 @@ public enum CidrParseEnum
1717
/// <summary>
1818
/// The other.
1919
/// </summary>
20-
Value,
20+
Value
2121
}

src/ConsoleApplication/Program.cs

Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -29,70 +29,70 @@ public static class Program
2929
new ArgParsed('l', (ac, _) => { ac.LastUsable = true; }),
3030
new ArgParsed('u', (ac, _) => { ac.Usable = true; }),
3131
new ArgParsed('t', (ac, _) => { ac.Total = true; }),
32-
new ArgParsed('w', (ac, _) => { ac.Action = ActionEnum.Supernet; }),
33-
new ArgParsed('W', (ac, _) => { ac.Action = ActionEnum.WideSupernet; }),
34-
new ArgParsed('h', (ac, _) => { ac.Action = ActionEnum.Usage; }),
35-
new ArgParsed('x', (ac, _) => { ac.Action = ActionEnum.ListIPAddress; }),
32+
new ArgParsed('w', (ac, _) => { ac.Action = Action.Supernet; }),
33+
new ArgParsed('W', (ac, _) => { ac.Action = Action.WideSupernet; }),
34+
new ArgParsed('h', (ac, _) => { ac.Action = Action.Usage; }),
35+
new ArgParsed('x', (ac, _) => { ac.Action = Action.ListIPAddress; }),
3636
new ArgParsed('?', (_, _) => { }),
37-
new ArgParsed('D', (ac, _) => { ac.CidrParse = CidrParseEnum.Default; }),
37+
new ArgParsed('D', (ac, _) => { ac.CidrParse = CidrParse.Default; }),
3838
new ArgParsed('d', (ac, arg) =>
3939
{
4040
if (!IPNetwork2.TryParseCidr(arg, Sockets.AddressFamily.InterNetwork, out byte? cidr))
4141
{
4242
Console.WriteLine("Invalid cidr {0}", cidr);
43-
ac.Action = ActionEnum.Usage;
43+
ac.Action = Action.Usage;
4444
return;
4545
}
4646

47-
ac.CidrParse = CidrParseEnum.Value;
47+
ac.CidrParse = CidrParse.Value;
4848
ac.CidrParsed = (byte)cidr!;
4949
}),
5050
new ArgParsed('s', (ac, arg) =>
5151
{
5252
if (!IPNetwork2.TryParseCidr(arg, Sockets.AddressFamily.InterNetwork, out byte? cidr))
5353
{
5454
Console.WriteLine("Invalid cidr {0}", cidr);
55-
ac.Action = ActionEnum.Usage;
55+
ac.Action = Action.Usage;
5656
return;
5757
}
5858

59-
ac.Action = ActionEnum.Subnet;
59+
ac.Action = Action.Subnet;
6060
ac.SubnetCidr = (byte)cidr!;
6161
}),
6262
new ArgParsed('C', (ac, arg) =>
6363
{
6464
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
6565
{
66-
Console.WriteLine("Unable to parse ipnetwork {0}", arg);
67-
ac.Action = ActionEnum.Usage;
66+
Console.WriteLine($"Unable to parse ipnetwork {arg}", arg);
67+
ac.Action = Action.Usage;
6868
return;
6969
}
7070

71-
ac.Action = ActionEnum.ContainNetwork;
71+
ac.Action = Action.ContainNetwork;
7272
ac.ContainNetwork = ipnetwork;
7373
}),
7474
new ArgParsed('o', (ac, arg) =>
7575
{
7676
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
7777
{
7878
Console.WriteLine("Unable to parse ipnetwork {0}", arg);
79-
ac.Action = ActionEnum.Usage;
79+
ac.Action = Action.Usage;
8080
return;
8181
}
8282

83-
ac.Action = ActionEnum.OverlapNetwork;
83+
ac.Action = Action.OverlapNetwork;
8484
ac.OverlapNetwork = ipnetwork;
8585
}),
8686
new ArgParsed('S', (ac, arg) =>
8787
{
8888
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2 ipnetwork))
8989
{
9090
Console.WriteLine("Unable to parse ipnetwork {0}", arg);
91-
ac.Action = ActionEnum.Usage;
91+
ac.Action = Action.Usage;
9292
return;
9393
}
9494

95-
ac.Action = ActionEnum.SubtractNetwork;
95+
ac.Action = Action.SubtractNetwork;
9696
ac.SubtractNetwork = ipnetwork;
9797
})
9898
];
@@ -107,31 +107,30 @@ public static void Main(string[] args)
107107

108108
switch (ac.Action)
109109
{
110-
case ActionEnum.Subnet:
110+
case Action.Subnet:
111111
SubnetNetworks(ac);
112112
break;
113-
case ActionEnum.Supernet:
113+
case Action.Supernet:
114114
SupernetNetworks(ac);
115115
break;
116-
case ActionEnum.WideSupernet:
116+
case Action.WideSupernet:
117117
WideSupernetNetworks(ac);
118118
break;
119-
case ActionEnum.PrintNetworks:
119+
case Action.PrintNetworks:
120120
PrintNetworks(ac);
121121
break;
122-
case ActionEnum.ContainNetwork:
122+
case Action.ContainNetwork:
123123
ContainNetwork(ac);
124124
break;
125-
case ActionEnum.OverlapNetwork:
125+
case Action.OverlapNetwork:
126126
OverlapNetwork(ac);
127127
break;
128-
case ActionEnum.ListIPAddress:
128+
case Action.ListIPAddress:
129129
ListIPAddress(ac);
130130
break;
131-
case ActionEnum.SubtractNetwork:
131+
case Action.SubtractNetwork:
132132
SubtractNetwork(ac);
133133
break;
134-
case ActionEnum.Usage:
135134
default:
136135
Usage();
137136
break;
@@ -198,17 +197,6 @@ private static void SupernetNetworks(ProgramContext ac)
198197
PrintNetworks(ac, supernet, supernet.Length);
199198
}
200199

201-
private static void PrintNetworks(ProgramContext ac, IEnumerable<IPNetwork2> ipnetworks, BigInteger networkLength)
202-
{
203-
int i = 0;
204-
foreach (IPNetwork2 ipn in ipnetworks)
205-
{
206-
i++;
207-
PrintNetwork(ac, ipn);
208-
PrintSeparator(networkLength, i);
209-
}
210-
}
211-
212200
private static void SubnetNetworks(ProgramContext ac)
213201
{
214202
BigInteger i = 0;
@@ -228,19 +216,25 @@ private static void SubnetNetworks(ProgramContext ac)
228216
}
229217
}
230218

231-
// private static void PrintSeparator(Array network, int index) {
232-
// if (network.Length > 1 && index != network.Length) {
233-
// Console.WriteLine("--");
234-
// }
235-
// }
236219
private static void PrintSeparator(BigInteger max, BigInteger index)
237220
{
238221
if (max > 1 && index != max)
239222
{
240223
Console.WriteLine("--");
241224
}
242225
}
243-
226+
227+
private static void PrintNetworks(ProgramContext ac, IEnumerable<IPNetwork2> ipnetworks, BigInteger networkLength)
228+
{
229+
int i = 0;
230+
foreach (IPNetwork2 ipn in ipnetworks)
231+
{
232+
i++;
233+
PrintNetwork(ac, ipn);
234+
PrintSeparator(networkLength, i);
235+
}
236+
}
237+
244238
private static void PrintNetworks(ProgramContext ac)
245239
{
246240
int i = 0;
@@ -338,21 +332,21 @@ private static ProgramContext ParseArgs(string[] args)
338332
if (ac.Networks.Length == 0)
339333
{
340334
Console.WriteLine("Provide at least one ipnetwork");
341-
ac.Action = ActionEnum.Usage;
335+
ac.Action = Action.Usage;
342336
}
343337

344-
if (ac.Action == ActionEnum.Supernet
338+
if (ac.Action == Action.Supernet
345339
&& ipnetworks.Count < 2)
346340
{
347341
Console.WriteLine("Supernet action required at least two ipnetworks");
348-
ac.Action = ActionEnum.Usage;
342+
ac.Action = Action.Usage;
349343
}
350344

351-
if (ac.Action == ActionEnum.WideSupernet
345+
if (ac.Action == Action.WideSupernet
352346
&& ipnetworks.Count < 2)
353347
{
354348
Console.WriteLine("WideSupernet action required at least two ipnetworks");
355-
ac.Action = ActionEnum.Usage;
349+
ac.Action = Action.Usage;
356350
}
357351

358352
if (PrintNoValue(ac))
@@ -385,23 +379,21 @@ private static void ParseIPNetworks(ProgramContext ac)
385379
ac.Networks = ipnetworks.ToArray();
386380
}
387381

388-
private static bool TryParseIPNetwork(string ip, CidrParseEnum cidrParseEnum, byte cidr, out IPNetwork2 ipn)
382+
private static bool TryParseIPNetwork(string ip, CidrParse cidrParse, byte cidr, out IPNetwork2 ipn)
389383
{
390384
IPNetwork2 ipnetwork = null;
391-
switch (cidrParseEnum)
385+
switch (cidrParse)
392386
{
393-
case CidrParseEnum.Default when !IPNetwork2.TryParse(ip, out ipnetwork):
387+
case CidrParse.Default when !IPNetwork2.TryParse(ip, out ipnetwork):
394388
ipn = null;
395389
return false;
396-
case CidrParseEnum.Value:
390+
case CidrParse.Value:
397391
{
398-
if (!IPNetwork2.TryParse(ip, cidr, out ipnetwork))
392+
if (!IPNetwork2.TryParse(ip, cidr, out ipnetwork)
393+
&& !IPNetwork2.TryParse(ip, out ipnetwork))
399394
{
400-
if (!IPNetwork2.TryParse(ip, out ipnetwork))
401-
{
402-
ipn = null;
403-
return false;
404-
}
395+
ipn = null;
396+
return false;
405397
}
406398

407399
break;
@@ -416,15 +408,15 @@ private static bool PrintNoValue(ProgramContext ac)
416408
{
417409
ArgumentNullException.ThrowIfNull(ac);
418410

419-
return ac.IPNetwork == false
420-
&& ac.Network == false
421-
&& ac.Netmask == false
422-
&& ac.Cidr == false
423-
&& ac.Broadcast == false
424-
&& ac.FirstUsable == false
425-
&& ac.LastUsable == false
426-
&& ac.Total == false
427-
&& ac.Usable == false;
411+
return !ac.IPNetwork
412+
&& !ac.Network
413+
&& !ac.Netmask
414+
&& !ac.Cidr
415+
&& !ac.Broadcast
416+
&& !ac.FirstUsable
417+
&& !ac.LastUsable
418+
&& !ac.Total
419+
&& !ac.Usable;
428420
}
429421

430422
private static void PrintAll(ProgramContext ac)

src/ConsoleApplication/ProgramContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class ProgramContext
5757
/// <summary>
5858
/// Gets or sets a value indicating whether gets or sets the CidrParse.
5959
/// </summary>
60-
public CidrParseEnum CidrParse { get; set; } = CidrParseEnum.Value;
60+
public CidrParse CidrParse { get; set; } = CidrParse.Value;
6161

6262
/// <summary>
6363
/// Gets or sets a value indicating whether gets or sets the CidrParsed.
@@ -82,7 +82,7 @@ public class ProgramContext
8282
/// <summary>
8383
/// Gets or sets a value indicating whether gets or sets the Action.
8484
/// </summary>
85-
public ActionEnum Action { get; set; } = ActionEnum.PrintNetworks;
85+
public Action Action { get; set; } = Action.PrintNetworks;
8686

8787
/// <summary>
8888
/// Gets or sets a value indicating whether gets or sets the SubnetCidr.

src/System.Net.IPNetwork/CidrClassFull.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public sealed class CidrClassFull : ICidrGuess
3030
public bool TryGuessCidr(string ip, out byte cidr)
3131
{
3232
bool parsed = IPAddress.TryParse($"{ip}", out var ipaddress);
33-
if (parsed == false)
33+
if (!parsed)
3434
{
3535
cidr = 0;
3636
return false;
@@ -43,7 +43,7 @@ public bool TryGuessCidr(string ip, out byte cidr)
4343
}
4444

4545
var uintIPAddress = IPNetwork2.ToBigInteger(ipaddress);
46-
uintIPAddress = uintIPAddress >> 30;
46+
uintIPAddress >>= 30;
4747
if (uintIPAddress <= 1)
4848
{
4949
cidr = 8;

src/System.Net.IPNetwork/CidrNetworkAware.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,17 @@ public bool TryGuessCidr(string ip, out byte cidr)
7575
if (!IPAddress.TryParse(ip.Trim(), out var ipAddress))
7676
return false;
7777

78-
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
78+
switch (ipAddress.AddressFamily)
7979
{
80-
cidr = GuessIpv4(ipAddress);
81-
return true;
80+
case AddressFamily.InterNetwork:
81+
cidr = GuessIpv4(ipAddress);
82+
return true;
83+
case AddressFamily.InterNetworkV6:
84+
cidr = GuessIpv6(ipAddress);
85+
return true;
86+
default:
87+
return false;
8288
}
83-
else if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
84-
{
85-
cidr = GuessIpv6(ipAddress);
86-
return true;
87-
}
88-
89-
return false;
9089
}
9190

9291
private static byte GuessIpv4(IPAddress ip)

0 commit comments

Comments
 (0)