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

lduchosal / ipnetwork / 960

15 May 2026 07:18AM UTC coverage: 0.0% (-94.1%) from 94.053%
960

Pull #411

appveyor

web-flow
Merge ef6b054ce into 4cec191ae
Pull Request #411: Bump MSTest.TestFramework from 4.2.2 to 4.2.3

0 of 2438 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    {
×
31
        if (string.IsNullOrEmpty(ipaddress))
×
32
        {
×
33
            if (!tryParse)
×
34
            {
×
35
                throw new ArgumentNullException(nameof(ipaddress));
×
36
            }
37

38
            ipnetwork = null;
×
39
            return false;
×
40
        }
41

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

49
            ipnetwork = null;
×
50
            return false;
×
51
        }
52

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

60
            ipnetwork = null;
×
61
            return false;
×
62
        }
63

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

71
            ipnetwork = null;
×
72
            return false;
×
73
        }
74

75
        bool parsed = InternalParse(tryParse, ip, mask, out ipnetwork);
×
76
        return parsed;
×
77
    }
×
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
    {
×
92
        if (string.IsNullOrEmpty(network))
×
93
        {
×
94
            if (!tryParse)
×
95
            {
×
96
                throw new ArgumentNullException(nameof(network));
×
97
            }
98

99
            ipnetwork = null;
×
100
            return false;
×
101
        }
102

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

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

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

127
            ipnetwork = null;
×
128
            return false;
×
129
        }
130

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

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

148
        ipnetwork = null;
×
149
        return false;
×
150
    }
×
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
    {
×
168
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
169
        if (ipaddress == null)
×
170
        {
×
171
            if (!tryParse)
×
172
            {
×
173
                throw new ArgumentNullException(nameof(ipaddress));
×
174
            }
175

176
            ipnetwork = null;
×
177
            return false;
×
178
        }
179

180
        if (netmask == null)
×
181
        {
×
182
            if (!tryParse)
×
183
            {
×
184
                throw new ArgumentNullException(nameof(netmask));
×
185
            }
186

187
            ipnetwork = null;
×
188
            return false;
×
189
        }
190

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

200
            ipnetwork = null;
×
201
            return false;
×
202
        }
203

204
        var ipnet = new IPNetwork2(uintIpAddress, ipaddress.AddressFamily, cidr2);
×
205
        ipnetwork = ipnet;
×
206
        return true;
×
207
    }
×
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
    {
×
225
        if (string.IsNullOrEmpty(ipaddress))
×
226
        {
×
227
            if (!tryParse)
×
228
            {
×
229
                throw new ArgumentNullException(nameof(ipaddress));
×
230
            }
231

232
            ipnetwork = null;
×
233
            return false;
×
234
        }
235

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

243
            ipnetwork = null;
×
244
            return false;
×
245
        }
246

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

254
            ipnetwork = null;
×
255
            return false;
×
256
        }
257

258
        bool parsed = InternalParse(tryParse, ip, mask, out ipnetwork);
×
259
        return parsed;
×
260
    }
×
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