• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lduchosal / ipnetwork / 777

13 Aug 2025 05:47AM UTC coverage: 93.478% (-0.04%) from 93.519%
777

push

appveyor

web-flow
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

1634 of 1748 relevant lines covered (93.48%)

793858.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.08
/src/System.Net.IPNetwork/IPNetwork2InternalParse.cs
1
// <copyright file="IPNetwork2InternalParse.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using System.Text.RegularExpressions;
8

9
/// <summary>
10
/// The InternalParse methodes.
11
/// </summary>
12
public partial class IPNetwork2
13
{
14
    /// <summary>
15
    /// 192.168.168.100 - 255.255.255.0
16
    ///
17
    /// Network   : 192.168.168.0
18
    /// Netmask   : 255.255.255.0
19
    /// Cidr      : 24
20
    /// Start     : 192.168.168.1
21
    /// End       : 192.168.168.254
22
    /// Broadcast : 192.168.168.255.
23
    /// </summary>
24
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
25
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
26
    /// <param name="netmask">A string containing a netmask to convert (255.255.255.0).</param>
27
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
28
    private static void InternalParse(bool tryParse, string ipaddress, string netmask, out IPNetwork2 ipnetwork)
29
    {
81✔
30
        if (string.IsNullOrEmpty(ipaddress))
81✔
31
        {
5✔
32
            if (tryParse == false)
5✔
33
            {
3✔
34
                throw new ArgumentNullException(nameof(ipaddress));
3✔
35
            }
36

37
            ipnetwork = null;
2✔
38
            return;
2✔
39
        }
40

41
        if (string.IsNullOrEmpty(netmask))
76✔
42
        {
6✔
43
            if (tryParse == false)
6✔
44
            {
4✔
45
                throw new ArgumentNullException(nameof(netmask));
4✔
46
            }
47

48
            ipnetwork = null;
2✔
49
            return;
2✔
50
        }
51

52
        bool ipaddressParsed = IPAddress.TryParse(ipaddress, out IPAddress ip);
70✔
53
        if (ipaddressParsed == false)
70✔
54
        {
6✔
55
            if (tryParse == false)
6✔
56
            {
4✔
57
                throw new ArgumentException("ipaddress");
4✔
58
            }
59

60
            ipnetwork = null;
2✔
61
            return;
2✔
62
        }
63

64
        bool netmaskParsed = IPAddress.TryParse(netmask, out IPAddress mask);
64✔
65
        if (netmaskParsed == false)
64✔
66
        {
4✔
67
            if (tryParse == false)
4✔
68
            {
2✔
69
                throw new ArgumentException("netmask");
2✔
70
            }
71

72
            ipnetwork = null;
2✔
73
            return;
2✔
74
        }
75

76
        InternalParse(tryParse, ip, mask, out ipnetwork);
60✔
77
    }
66✔
78

79
    /// <summary>
80
    /// Internal parse an IPNetwork2.
81
    /// </summary>
82
    /// <param name="tryParse">Prevent exception.</param>
83
    /// <param name="network">The network to parse.</param>
84
    /// <param name="cidrGuess">The way to guess CIDR.</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>
86
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
87
    /// <exception cref="ArgumentNullException">When network is null.</exception>
88
    /// <exception cref="ArgumentException">When network is not valid.</exception>
89
    private static void InternalParse(bool tryParse, string network, ICidrGuess cidrGuess, bool sanitize, out IPNetwork2 ipnetwork)
90
    {
4,000,777✔
91
        if (string.IsNullOrEmpty(network))
4,000,777✔
92
        {
5✔
93
            if (tryParse == false)
5✔
94
            {
3✔
95
                throw new ArgumentNullException(nameof(network));
3✔
96
            }
97

98
            ipnetwork = null;
2✔
99
            return;
2✔
100
        }
101

102
        if (sanitize)
4,000,772✔
103
        {
4,000,765✔
104
            network = Regex.Replace(network, @"[^0-9a-fA-F\.\/\s\:]+", string.Empty, RegexOptions.None, TimeSpan.FromMilliseconds(100));
4,000,765✔
105
            network = Regex.Replace(network, @"\s{2,}", " ", RegexOptions.None, TimeSpan.FromMilliseconds(100));
4,000,765✔
106
            network = network.Trim();
4,000,765✔
107
        }
4,000,765✔
108

109
        StringSplitOptions splitOptions = sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None;
4,000,772✔
110
        string[] args = network.Split([' ', '/'], splitOptions);
4,000,772✔
111

112
        if (args.Length == 0)
4,000,772✔
113
        {
1✔
114
            if (tryParse == false)
1✔
115
            {
×
116
                throw new ArgumentNullException(nameof(network));
×
117
            }
118

119
            ipnetwork = null;
1✔
120
            return;
1✔
121
        }
122
        
123
        if (args.Length == 1)
4,000,771✔
124
        {
49✔
125
            string cidrlessNetwork = args[0];
49✔
126
            if (cidrGuess.TryGuessCidr(cidrlessNetwork, out byte cidr))
49✔
127
            {
44✔
128
                InternalParse(tryParse, cidrlessNetwork, cidr, out ipnetwork);
44✔
129
                return;
44✔
130
            }
131

132
            if (tryParse == false)
5✔
133
            {
2✔
134
                throw new ArgumentException("network");
2✔
135
            }
136

137
            ipnetwork = null;
3✔
138
            return;
3✔
139
        }
140
        
141
        if (args.Length == 2)
4,000,722✔
142
        {
4,000,719✔
143
            if (byte.TryParse(args[1], out byte cidr1))
4,000,719✔
144
            {
4,000,674✔
145
                InternalParse(tryParse, args[0], cidr1, out ipnetwork);
4,000,674✔
146
                return;
4,000,674✔
147
            }
148

149
            InternalParse(tryParse, args[0], args[1], out ipnetwork);
45✔
150
        }
43✔
151
        else
152
        {
3✔
153
            if (tryParse == false)
3✔
154
            {
×
155
                throw new ArgumentNullException(nameof(network));
×
156
            }
157
            ipnetwork = null;
3✔
158
            return;
3✔
159
        }
160
    }
4,000,770✔
161

162
    /// <summary>
163
    /// 192.168.168.100 255.255.255.0
164
    ///
165
    /// Network   : 192.168.168.0
166
    /// Netmask   : 255.255.255.0
167
    /// Cidr      : 24
168
    /// Start     : 192.168.168.1
169
    /// End       : 192.168.168.254
170
    /// Broadcast : 192.168.168.255.
171
    /// </summary>
172
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
173
    /// <param name="ipaddress">An ip address to convert.</param>
174
    /// <param name="netmask">A netmask to convert (255.255.255.0).</param>
175
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
176
    private static void InternalParse(bool tryParse, IPAddress ipaddress, IPAddress netmask, out IPNetwork2 ipnetwork)
177
    {
4,000,840✔
178
        if (ipaddress == null)
4,000,840✔
179
        {
4✔
180
            if (tryParse == false)
4✔
181
            {
2✔
182
                throw new ArgumentNullException(nameof(ipaddress));
2✔
183
            }
184

185
            ipnetwork = null;
2✔
186
            return;
2✔
187
        }
188

189
        if (netmask == null)
4,000,836✔
190
        {
4✔
191
            if (tryParse == false)
4✔
192
            {
2✔
193
                throw new ArgumentNullException(nameof(netmask));
2✔
194
            }
195

196
            ipnetwork = null;
2✔
197
            return;
2✔
198
        }
199

200
        var uintIpAddress = ToBigInteger(ipaddress);
4,000,832✔
201
        bool parsed = TryToCidr(netmask, out byte? cidr2);
4,000,832✔
202
        if (parsed == false)
4,000,832✔
203
        {
4✔
204
            if (tryParse == false)
4✔
205
            {
2✔
206
                throw new ArgumentException("netmask");
2✔
207
            }
208

209
            ipnetwork = null;
2✔
210
            return;
2✔
211
        }
212

213
        byte cidr = (byte)cidr2!;
4,000,828✔
214

215
        var ipnet = new IPNetwork2(uintIpAddress, ipaddress.AddressFamily, cidr);
4,000,828✔
216
        ipnetwork = ipnet;
4,000,828✔
217
    }
4,000,834✔
218

219
    /// <summary>
220
    /// 192.168.168.100/24
221
    ///
222
    /// Network   : 192.168.168.0
223
    /// Netmask   : 255.255.255.0
224
    /// Cidr      : 24
225
    /// Start     : 192.168.168.1
226
    /// End       : 192.168.168.254
227
    /// Broadcast : 192.168.168.255.
228
    /// </summary>
229
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
230
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
231
    /// <param name="cidr">A byte representing the CIDR to be used in conversion (/24).</param>
232
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
233
    private static void InternalParse(bool tryParse, string ipaddress, byte cidr, out IPNetwork2 ipnetwork)
234
    {
4,000,816✔
235
        if (string.IsNullOrEmpty(ipaddress))
4,000,816✔
236
        {
6✔
237
            if (tryParse == false)
6✔
238
            {
2✔
239
                throw new ArgumentNullException(nameof(ipaddress));
2✔
240
            }
241

242
            ipnetwork = null;
4✔
243
            return;
4✔
244
        }
245

246
        bool ipaddressParsed = IPAddress.TryParse(ipaddress, out IPAddress ip);
4,000,810✔
247
        if (ipaddressParsed == false)
4,000,810✔
248
        {
37✔
249
            if (tryParse == false)
37✔
250
            {
1✔
251
                throw new ArgumentException("ipaddress");
1✔
252
            }
253

254
            ipnetwork = null;
36✔
255
            return;
36✔
256
        }
257

258
        bool parsedNetmask = TryToNetmask(cidr, ip.AddressFamily, out IPAddress mask);
4,000,773✔
259
        if (parsedNetmask == false)
4,000,773✔
260
        {
3✔
261
            if (tryParse == false)
3✔
262
            {
1✔
263
                throw new ArgumentException("cidr");
1✔
264
            }
265

266
            ipnetwork = null;
2✔
267
            return;
2✔
268
        }
269

270
        InternalParse(tryParse, ip, mask, out ipnetwork);
4,000,770✔
271
    }
4,000,812✔
272
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc