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

lduchosal / ipnetwork / 878

07 Mar 2026 04:31PM UTC coverage: 92.392% (-1.0%) from 93.419%
878

Pull #383

appveyor

web-flow
Merge 0f21b91fc into 4178c9692
Pull Request #383: feat/v4-nullable

1931 of 2090 relevant lines covered (92.39%)

573792.98 hits per line

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

98.43
/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.Diagnostics.CodeAnalysis;
8
using System.Text.RegularExpressions;
9

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

38
            ipnetwork = null;
2✔
39
            return false;
2✔
40
        }
41

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

49
            ipnetwork = null;
2✔
50
            return false;
2✔
51
        }
52

53
        if (!IPAddress.TryParse(ipaddress, out IPAddress? ip))
70✔
54
        {
6✔
55
            if (!tryParse)
6✔
56
            {
4✔
57
                throw new ArgumentException("Invalid IP address.", nameof(ipaddress));
4✔
58
            }
59

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

64
        if (!IPAddress.TryParse(netmask, out IPAddress? mask))
64✔
65
        {
4✔
66
            if (!tryParse)
4✔
67
            {
2✔
68
                throw new ArgumentException("Invalid netmask.", nameof(netmask));
2✔
69
            }
70

71
            ipnetwork = null;
2✔
72
            return false;
2✔
73
        }
74

75
        bool parsed = InternalParse(tryParse, ip, mask, out ipnetwork);
60✔
76
        return parsed;
58✔
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
    /// <returns>true if parsed, otherwise false</returns>
90
    private static bool InternalParse(bool tryParse, string network, ICidrGuess cidrGuess, bool sanitize, [NotNullWhen(true)] out IPNetwork2? ipnetwork)
91
    {
4,001,093✔
92
        if (string.IsNullOrEmpty(network))
4,001,093✔
93
        {
5✔
94
            if (!tryParse)
5✔
95
            {
3✔
96
                throw new ArgumentNullException(nameof(network));
3✔
97
            }
98

99
            ipnetwork = null;
2✔
100
            return false;
2✔
101
        }
102

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

110
        StringSplitOptions splitOptions = sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None;
4,001,088✔
111
        string[] args = network.Split([' ', '/'], splitOptions);
4,001,088✔
112
        
113
        if (args.Length == 1)
4,001,088✔
114
        {
54✔
115
            string cidrlessNetwork = args[0];
54✔
116
            if (cidrGuess.TryGuessCidr(cidrlessNetwork, out byte cidr))
54✔
117
            {
49✔
118
                bool parsed = InternalParse(tryParse, cidrlessNetwork, cidr, out ipnetwork);
49✔
119
                return parsed;
49✔
120
            }
121

122
            if (!tryParse)
5✔
123
            {
2✔
124
                throw new ArgumentException("Invalid network.", nameof(network));
2✔
125
            }
126

127
            ipnetwork = null;
3✔
128
            return false;
3✔
129
        }
130

131
        if (args.Length == 2)
4,001,034✔
132
        {
4,001,030✔
133
            if (byte.TryParse(args[1], out byte cidr1))
4,001,030✔
134
            {
4,000,985✔
135
                bool parsed2 = InternalParse(tryParse, args[0], cidr1, out ipnetwork);
4,000,985✔
136
                return parsed2;
4,000,985✔
137
            }
138

139
            bool parsed3 = InternalParse(tryParse, args[0], args[1], out ipnetwork);
45✔
140
            return parsed3;
43✔
141
        }
142
        
143
        if (!tryParse)
4✔
144
        {
×
145
            throw new ArgumentNullException(nameof(network));
×
146
        }
147

148
        ipnetwork = null;
4✔
149
        return false;
4✔
150
    }
4,001,086✔
151

152
    /// <summary>
153
    /// 192.168.168.100 255.255.255.0
154
    ///
155
    /// Network   : 192.168.168.0
156
    /// Netmask   : 255.255.255.0
157
    /// Cidr      : 24
158
    /// Start     : 192.168.168.1
159
    /// End       : 192.168.168.254
160
    /// Broadcast : 192.168.168.255.
161
    /// </summary>
162
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
163
    /// <param name="ipaddress">An ip address to convert.</param>
164
    /// <param name="netmask">A netmask to convert (255.255.255.0).</param>
165
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
166
    private static bool InternalParse(bool tryParse, IPAddress ipaddress, IPAddress netmask, [NotNullWhen(true)] out IPNetwork2? ipnetwork)
167
    {
4,001,158✔
168
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
169
        if (ipaddress == null)
4,001,158✔
170
        {
4✔
171
            if (!tryParse)
4✔
172
            {
2✔
173
                throw new ArgumentNullException(nameof(ipaddress));
2✔
174
            }
175

176
            ipnetwork = null;
2✔
177
            return false;
2✔
178
        }
179

180
        if (netmask == null)
4,001,154✔
181
        {
4✔
182
            if (!tryParse)
4✔
183
            {
2✔
184
                throw new ArgumentNullException(nameof(netmask));
2✔
185
            }
186

187
            ipnetwork = null;
2✔
188
            return false;
2✔
189
        }
190

191
        var uintIpAddress = ToBigInteger(ipaddress);
4,001,150✔
192
        bool parsed = TryToCidr(netmask, out byte cidr2);
4,001,150✔
193
        if (!parsed)
4,001,150✔
194
        {
4✔
195
            if (!tryParse)
4✔
196
            {
2✔
197
                throw new ArgumentException("Invalid netmask.", nameof(netmask));
2✔
198
            }
199

200
            ipnetwork = null;
2✔
201
            return false;
2✔
202
        }
203

204
        var ipnet = new IPNetwork2(uintIpAddress, ipaddress.AddressFamily, cidr2);
4,001,146✔
205
        ipnetwork = ipnet;
4,001,146✔
206
        return true;
4,001,146✔
207
    }
4,001,152✔
208

209
    /// <summary>
210
    /// 192.168.168.100/24
211
    ///
212
    /// Network   : 192.168.168.0
213
    /// Netmask   : 255.255.255.0
214
    /// Cidr      : 24
215
    /// Start     : 192.168.168.1
216
    /// End       : 192.168.168.254
217
    /// Broadcast : 192.168.168.255.
218
    /// </summary>
219
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
220
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
221
    /// <param name="cidr">A byte representing the CIDR to be used in conversion (/24).</param>
222
    /// <param name="ipnetwork">The resulting IPNetwork.</param>
223
    private static bool InternalParse(bool tryParse, string ipaddress, byte cidr, [NotNullWhen(true)] out IPNetwork2? ipnetwork)
224
    {
4,001,134✔
225
        if (string.IsNullOrEmpty(ipaddress))
4,001,134✔
226
        {
6✔
227
            if (!tryParse)
6✔
228
            {
2✔
229
                throw new ArgumentNullException(nameof(ipaddress));
2✔
230
            }
231

232
            ipnetwork = null;
4✔
233
            return false;
4✔
234
        }
235

236
        if (!IPAddress.TryParse(ipaddress, out IPAddress? ip))
4,001,128✔
237
        {
37✔
238
            if (!tryParse)
37✔
239
            {
1✔
240
                throw new ArgumentException("Invalid IP address.", nameof(ipaddress));
1✔
241
            }
242

243
            ipnetwork = null;
36✔
244
            return false;
36✔
245
        }
246

247
        if (!TryToNetmask(cidr, ip.AddressFamily, out IPAddress? mask))
4,001,091✔
248
        {
3✔
249
            if (!tryParse)
3✔
250
            {
1✔
251
                throw new ArgumentException("Invalid CIDR.", nameof(cidr));
1✔
252
            }
253

254
            ipnetwork = null;
2✔
255
            return false;
2✔
256
        }
257

258
        bool parsed = InternalParse(tryParse, ip, mask, out ipnetwork);
4,001,088✔
259
        return parsed;
4,001,088✔
260
    }
4,001,130✔
261
}
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