Skip to content

Commit 84cc98e

Browse files
authored
feat/tryparsesanitanize (#356)
* Feat: IPNetwork2 Subtract impl * Feat: Substract IPv6 * Fix: typos * Fix: warnings * Fix: formatting * Fix: sonarqube * Feat: ipnetwork logo * Fix: logo * Fix: upgrade nuget * Fix: missing TestClass * Fix: Assert.Throws * Fix: assert expecte order * Fix sonarqube issues * Feat: Subtract / console * Chore: added documentation to sanitanize parameter Fix: TryParse will fail if network is invalid and sanitanize is false #256
1 parent e160522 commit 84cc98e

File tree

6 files changed

+403
-349
lines changed

6 files changed

+403
-349
lines changed

src/ConsoleApplication/Program.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,10 @@ public static void Main(string[] args)
128128
case ActionEnum.ListIPAddress:
129129
ListIPAddress(ac);
130130
break;
131-
case ActionEnum.Usage:
132131
case ActionEnum.SubtractNetwork:
132+
SubtractNetwork(ac);
133+
break;
134+
case ActionEnum.Usage:
133135
default:
134136
Usage();
135137
break;
@@ -146,7 +148,18 @@ private static void ListIPAddress(ProgramContext ac)
146148
}
147149
}
148150
}
149-
151+
152+
private static void SubtractNetwork(ProgramContext ac)
153+
{
154+
foreach (IPNetwork2 ipnetwork in ac.Networks)
155+
{
156+
foreach (IPNetwork2 subtracted in ipnetwork.Subtract(ac.SubtractNetwork))
157+
{
158+
Console.WriteLine("{0}", subtracted);
159+
}
160+
}
161+
}
162+
150163
private static void ContainNetwork(ProgramContext ac)
151164
{
152165
foreach (IPNetwork2 ipnetwork in ac.Networks)
@@ -438,7 +451,7 @@ private static void Usage()
438451
string version = fvi.FileVersion;
439452

440453
Console.WriteLine(
441-
"Usage: ipnetwork [-inmcbflu] [-d cidr|-D] [-h|-s cidr|-S|-w|-W|-x|-C network|-o network] networks ...");
454+
"Usage: ipnetwork [-inmcbflu] [-d cidr|-D] [-h|-s cidr|-S network|-w|-W|-x|-C network|-o network] networks ...");
442455
Console.WriteLine("Version: {0}", version);
443456
Console.WriteLine();
444457
Console.WriteLine("Print options");
@@ -464,7 +477,7 @@ private static void Usage()
464477
Console.WriteLine("\t-x : list all ip adresses in networks");
465478
Console.WriteLine("\t-C network : network contain networks");
466479
Console.WriteLine("\t-o network : network overlap networks");
467-
Console.WriteLine("\t-S network : subtract network from subnet");
480+
Console.WriteLine("\t-S network : subtract network from networks");
468481
Console.WriteLine(string.Empty);
469482
Console.WriteLine("networks : one or more network addresses ");
470483
Console.WriteLine(

src/System.Net.IPNetwork/IPNetwork2InternalParse.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ private static void InternalParse(bool tryParse, string ipaddress, string netmas
8282
/// <param name="tryParse">Prevent exception.</param>
8383
/// <param name="network">The network to parse.</param>
8484
/// <param name="cidrGuess">The way to guess CIDR.</param>
85-
/// <param name="sanitanize">Clean up the network.</param>
85+
/// <param name="sanitize">If true, removes invalid characters and normalizes whitespace from the network string, keeping only valid network address characters (0-9, a-f, A-F, ., /, :, and spaces).</param>
8686
/// <param name="ipnetwork">The resulting IPNetwork.</param>
8787
/// <exception cref="ArgumentNullException">When network is null.</exception>
8888
/// <exception cref="ArgumentException">When network is not valid.</exception>
89-
private static void InternalParse(bool tryParse, string network, ICidrGuess cidrGuess, bool sanitanize, out IPNetwork2 ipnetwork)
89+
private static void InternalParse(bool tryParse, string network, ICidrGuess cidrGuess, bool sanitize, out IPNetwork2 ipnetwork)
9090
{
9191
if (string.IsNullOrEmpty(network))
9292
{
@@ -99,14 +99,14 @@ private static void InternalParse(bool tryParse, string network, ICidrGuess cidr
9999
return;
100100
}
101101

102-
if (sanitanize)
102+
if (sanitize)
103103
{
104104
network = Regex.Replace(network, @"[^0-9a-fA-F\.\/\s\:]+", string.Empty, RegexOptions.None, TimeSpan.FromMilliseconds(100));
105105
network = Regex.Replace(network, @"\s{2,}", " ", RegexOptions.None, TimeSpan.FromMilliseconds(100));
106106
network = network.Trim();
107107
}
108108

109-
StringSplitOptions splitOptions = sanitanize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None;
109+
StringSplitOptions splitOptions = sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None;
110110
string[] args = network.Split([' ', '/'], splitOptions);
111111

112112
if (args.Length == 0)
@@ -119,7 +119,7 @@ private static void InternalParse(bool tryParse, string network, ICidrGuess cidr
119119
ipnetwork = null;
120120
return;
121121
}
122-
122+
123123
if (args.Length == 1)
124124
{
125125
string cidrlessNetwork = args[0];
@@ -137,14 +137,26 @@ private static void InternalParse(bool tryParse, string network, ICidrGuess cidr
137137
ipnetwork = null;
138138
return;
139139
}
140+
141+
if (args.Length == 2)
142+
{
143+
if (byte.TryParse(args[1], out byte cidr1))
144+
{
145+
InternalParse(tryParse, args[0], cidr1, out ipnetwork);
146+
return;
147+
}
140148

141-
if (byte.TryParse(args[1], out byte cidr1))
149+
InternalParse(tryParse, args[0], args[1], out ipnetwork);
150+
}
151+
else
142152
{
143-
InternalParse(tryParse, args[0], cidr1, out ipnetwork);
153+
if (tryParse == false)
154+
{
155+
throw new ArgumentNullException(nameof(network));
156+
}
157+
ipnetwork = null;
144158
return;
145159
}
146-
147-
InternalParse(tryParse, args[0], args[1], out ipnetwork);
148160
}
149161

150162
/// <summary>

src/System.Net.IPNetwork/IPNetwork2Parse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static IPNetwork2 Parse(string network)
100100
/// Broadcast : 192.168.0.255.
101101
/// </summary>
102102
/// <param name="network">A string containing an ip network to convert.</param>
103-
/// <param name="sanitanize">Whether to sanitize network or not.</param>
103+
/// <param name="sanitanize">If true, removes invalid characters and normalizes whitespace from the network string, keeping only valid network address characters (0-9, a-f, A-F, ., /, :, and spaces).</param>
104104
/// <returns>An IPNetwork equivalent to the network contained in string network.</returns>
105105
public static IPNetwork2 Parse(string network, bool sanitanize)
106106
{
@@ -141,7 +141,7 @@ public static IPNetwork2 Parse(string network, ICidrGuess cidrGuess)
141141
/// </summary>
142142
/// <param name="network">A string containing an ip network to convert.</param>
143143
/// <param name="cidrGuess">A ICidrGuess implementation that will be used to guess CIDR during conversion.</param>
144-
/// <param name="sanitanize">Whether to sanitize network or not.</param>
144+
/// <param name="sanitanize">If true, removes invalid characters and normalizes whitespace from the network string, keeping only valid network address characters (0-9, a-f, A-F, ., /, :, and spaces).</param>
145145
/// <returns>An IPNetwork equivalent to the network contained in string network.</returns>
146146
public static IPNetwork2 Parse(string network, ICidrGuess cidrGuess, bool sanitanize)
147147
{

src/System.Net.IPNetwork/IPNetwork2TryParse.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static bool TryParse(string ipaddress, byte cidr, out IPNetwork2 ipnetwor
7171
/// <returns>true if network was converted successfully; otherwise, false.</returns>
7272
public static bool TryParse(string network, out IPNetwork2 ipnetwork)
7373
{
74-
InternalParse(true, network, CidrGuess.ClassFull, sanitanize: true, out IPNetwork2 ipnetwork2);
74+
InternalParse(true, network, CidrGuess.ClassFull, sanitize: true, out IPNetwork2 ipnetwork2);
7575
bool parsed = ipnetwork2 != null;
7676
ipnetwork = ipnetwork2;
7777

@@ -90,7 +90,7 @@ public static bool TryParse(string network, out IPNetwork2 ipnetwork)
9090
/// Broadcast : 192.168.0.255.
9191
/// </summary>
9292
/// <param name="network">A string containing an ip network to convert.</param>
93-
/// <param name="sanitanize">Whether to sanitize network or not.</param>
93+
/// <param name="sanitanize">If true, removes invalid characters and normalizes whitespace from the network string, keeping only valid network address characters (0-9, a-f, A-F, ., /, :, and spaces).</param>
9494
/// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
9595
/// <returns>true if network was converted successfully; otherwise, false.</returns>
9696
public static bool TryParse(string network, bool sanitanize, out IPNetwork2 ipnetwork)
@@ -163,7 +163,7 @@ public static bool TryParse(string network, ICidrGuess cidrGuess, out IPNetwork2
163163
/// </summary>
164164
/// <param name="network">A string containing an ip network to convert.</param>
165165
/// <param name="cidrGuess">A ICidrGuess implementation that will be used to guess CIDR during conversion.</param>
166-
/// <param name="sanitanize">Whether to sanitize network or not.</param>
166+
/// <param name="sanitanize">If true, removes invalid characters and normalizes whitespace from the network string, keeping only valid network address characters (0-9, a-f, A-F, ., /, :, and spaces).</param>
167167
/// <param name="ipnetwork">When this method returns, contains the IPNetwork value equivalent of the IPAddress contained in ipaddress with the netmask corresponding to cidr, if the conversion succeeded, or null if the conversion failed. The conversion fails if the s parameter is null or Empty, is not of the correct format, or represents an invalid ip address. This parameter is passed uninitialized; any value originally supplied in result will be overwritten.</param>
168168
/// <returns>true if network was converted successfully; otherwise, false.</returns>
169169
public static bool TryParse(string network, ICidrGuess cidrGuess, bool sanitanize, out IPNetwork2 ipnetwork)

0 commit comments

Comments
 (0)