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

lduchosal / ipnetwork / 809

17 Aug 2025 08:25AM UTC coverage: 93.223% (-1.0%) from 94.226%
809

push

appveyor

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

1802 of 1933 relevant lines covered (93.22%)

726934.37 hits per line

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

97.12
/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 bool InternalParse(bool tryParse, string ipaddress, string netmask, out IPNetwork2 ipnetwork)
29
    {
81✔
30
        if (string.IsNullOrEmpty(ipaddress))
81✔
31
        {
5✔
32
            if (!tryParse)
5✔
33
            {
3✔
34
                throw new ArgumentNullException(nameof(ipaddress));
3✔
35
            }
36

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

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

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

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

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

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

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

76
        bool parsed = InternalParse(tryParse, ip, mask, out ipnetwork);
60✔
77
        return parsed;
58✔
78
    }
66✔
79

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

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

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

111
        StringSplitOptions splitOptions = sanitize ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None;
4,001,017✔
112
        string[] args = network.Split([' ', '/'], splitOptions);
4,001,017✔
113

114
        if (args.Length == 0)
4,001,017✔
115
        {
1✔
116
            if (!tryParse)
1✔
117
            {
×
118
                throw new ArgumentNullException(nameof(network));
×
119
            }
120

121
            ipnetwork = null;
1✔
122
            return false;
1✔
123
        }
124
        
125
        if (args.Length == 1)
4,001,016✔
126
        {
54✔
127
            string cidrlessNetwork = args[0];
54✔
128
            if (cidrGuess.TryGuessCidr(cidrlessNetwork, out byte cidr))
54✔
129
            {
49✔
130
                bool parsed = InternalParse(tryParse, cidrlessNetwork, cidr, out ipnetwork);
49✔
131
                return parsed;
49✔
132
            }
133

134
            if (!tryParse)
5✔
135
            {
2✔
136
                throw new ArgumentException("network");
2✔
137
            }
138

139
            ipnetwork = null;
3✔
140
            return false;
3✔
141
        }
142
        
143
        if (args.Length == 2)
4,000,962✔
144
        {
4,000,959✔
145
            if (byte.TryParse(args[1], out byte cidr1))
4,000,959✔
146
            {
4,000,914✔
147
                bool parsed2 = InternalParse(tryParse, args[0], cidr1, out ipnetwork);
4,000,914✔
148
                return parsed2;
4,000,914✔
149
            }
150

151
            bool parsed3 = InternalParse(tryParse, args[0], args[1], out ipnetwork);
45✔
152
            return parsed3;
43✔
153
        }
154
        else
155
        {
3✔
156
            if (!tryParse)
3✔
157
            {
×
158
                throw new ArgumentNullException(nameof(network));
×
159
            }
160
            ipnetwork = null;
3✔
161
            return false;
3✔
162
        }
163
    }
4,001,015✔
164

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

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

192
        if (netmask == null)
4,001,081✔
193
        {
4✔
194
            if (!tryParse)
4✔
195
            {
2✔
196
                throw new ArgumentNullException(nameof(netmask));
2✔
197
            }
198

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

203
        var uintIpAddress = ToBigInteger(ipaddress);
4,001,077✔
204
        bool parsed = TryToCidr(netmask, out byte cidr2);
4,001,077✔
205
        if (!parsed)
4,001,077✔
206
        {
4✔
207
            if (!tryParse)
4✔
208
            {
2✔
209
                throw new ArgumentException("netmask");
2✔
210
            }
211

212
            ipnetwork = null;
2✔
213
            return false;
2✔
214
        }
215

216
        var ipnet = new IPNetwork2(uintIpAddress, ipaddress.AddressFamily, cidr2);
4,001,073✔
217
        ipnetwork = ipnet;
4,001,073✔
218
        return true;
4,001,073✔
219
    }
4,001,079✔
220

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

244
            ipnetwork = null;
4✔
245
            return false;
4✔
246
        }
247

248
        bool ipaddressParsed = IPAddress.TryParse(ipaddress, out IPAddress ip);
4,001,055✔
249
        if (!ipaddressParsed)
4,001,055✔
250
        {
37✔
251
            if (!tryParse)
37✔
252
            {
1✔
253
                throw new ArgumentException("ipaddress");
1✔
254
            }
255

256
            ipnetwork = null;
36✔
257
            return false;
36✔
258
        }
259

260
        bool parsedNetmask = TryToNetmask(cidr, ip.AddressFamily, out IPAddress mask);
4,001,018✔
261
        if (!parsedNetmask)
4,001,018✔
262
        {
3✔
263
            if (!tryParse)
3✔
264
            {
1✔
265
                throw new ArgumentException("cidr");
1✔
266
            }
267

268
            ipnetwork = null;
2✔
269
            return false;
2✔
270
        }
271

272
        bool parsed = InternalParse(tryParse, ip, mask, out ipnetwork);
4,001,015✔
273
        return parsed;
4,001,015✔
274
    }
4,001,057✔
275
}
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

© 2025 Coveralls, Inc