• 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

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

5
namespace System.Net;
6

7
using System.Net.Sockets;
8
using System.Numerics;
9

10
/// <summary>
11
/// WideSubnet.
12
/// </summary>
13
public sealed partial class IPNetwork2
14
{
15
    /// <summary>
16
    /// Finds the widest subnet that can contain both the start and end IP addresses.
17
    /// </summary>
18
    /// <param name="start">The starting IP address.</param>
19
    /// <param name="end">The ending IP address.</param>
20
    /// <returns>The widest subnet that contains both the start and end IP addresses.</returns>
21
    /// <exception cref="ArgumentNullException">Thrown when either the start or end IP address is null or empty.</exception>
22
    /// <exception cref="ArgumentException">Thrown when the start or end IP addresses are not valid.</exception>
23
    /// <exception cref="NotSupportedException">Thrown when the start and end IP addresses have different address families.</exception>
24
    public static IPNetwork2 WideSubnet(string start, string end)
25
    {
1✔
26
        if (string.IsNullOrEmpty(start))
1✔
27
        {
×
28
            throw new ArgumentNullException(nameof(start));
×
29
        }
30

31
        if (string.IsNullOrEmpty(end))
1✔
32
        {
×
33
            throw new ArgumentNullException(nameof(end));
×
34
        }
35

36
        if (!IPAddress.TryParse(start, out IPAddress startIP))
1✔
37
        {
×
38
            throw new ArgumentException(nameof(start));
×
39
        }
40

41
        if (!IPAddress.TryParse(end, out IPAddress endIP))
1✔
42
        {
×
43
            throw new ArgumentException(nameof(end));
×
44
        }
45

46
        if (startIP.AddressFamily != endIP.AddressFamily)
1✔
47
        {
×
48
            throw new NotSupportedException(nameof(AddressFamily));
×
49
        }
50

51
        IPNetwork2 ipnetwork;
52

53
        // ReSharper disable once ConditionIsAlwaysTrueOrFalse
54
        for (byte cidr = 32; ; cidr--)
20✔
55
        {
20✔
56
            var wideSubnet = Parse(start, cidr);
20✔
57
            if (wideSubnet.Contains(endIP))
20✔
58
            {
1✔
59
                ipnetwork = wideSubnet;
1✔
60
                break;
1✔
61
            }
62
        }
19✔
63

64
        return ipnetwork;
1✔
65
    }
1✔
66

67
    /// <summary>
68
    /// Finds the widest subnet from an array of IP networks. </summary> <param name="ipnetworks">An array of IPNetwork2 objects representing the IP networks.</param> <returns>The widest subnet as an IPNetwork2 object.</returns>
69
    /// /
70
    public static IPNetwork2 WideSubnet(IPNetwork2[] ipnetworks)
71
    {
6✔
72
        bool parsed = InternalWideSubnet(false, ipnetworks, out IPNetwork2 ipn);
6✔
73
        if (!parsed)
3✔
74
        {
×
75
            throw new ArgumentException(nameof(ipnetworks));
×
76
        }
77
        return ipn;
3✔
78
    }
3✔
79

80
    /// <summary>
81
    /// Attempts to find the widest subnet that contains both the start and end IP addresses. objects.
82
    /// </summary>
83
    /// <param name="ipnetworks">An array of IPNetwork2 objects to wide subnet.</param>
84
    /// <param name="ipnetwork">When this method returns, contains the wide subnet of the IPNetwork2 objects, if wide subnet was successful; otherwise, null.</param>
85
    /// <returns>true if wide subnet was successful; otherwise, false.</returns>
86
    public static bool TryWideSubnet(IPNetwork2[] ipnetworks, out IPNetwork2 ipnetwork)
87
    {
6✔
88
        bool parsed = InternalWideSubnet(true, ipnetworks, out IPNetwork2 ipn);
6✔
89
        if (!parsed)
6✔
90
        {
2✔
91
            ipnetwork = null;
2✔
92
            return false;
2✔
93
        }
94
        
95
        ipnetwork = ipn;
4✔
96
        return true;
4✔
97
    }
6✔
98

99
    /// <summary>
100
    /// Attempts to find the widest subnet that includes all given IPNetwork2 instances.
101
    /// </summary>
102
    /// <param name="tryWide">If true, suppresses exceptions on invalid input; otherwise, throws.</param>
103
    /// <param name="ipnetworks">The array of IPNetwork2 instances to encompass within the widest subnet.</param>
104
    /// <param name="ipnetwork">The resulting widest IPNetwork2 subnet, or null if unsuccessful and tryWide is true.</param>
105
    /// <returns>true if successful, otherwise false.</returns>
106
    internal static bool InternalWideSubnet(bool tryWide, IPNetwork2[] ipnetworks, out IPNetwork2 ipnetwork)
107
    {
12✔
108
        if (ipnetworks == null)
12✔
109
        {
2✔
110
            if (!tryWide)
2✔
111
            {
1✔
112
                throw new ArgumentNullException(nameof(ipnetworks));
1✔
113
            }
114

115
            ipnetwork = null;
1✔
116
            return false;
1✔
117
        }
118

119
        IPNetwork2[] nnin = Array.FindAll(ipnetworks, ipnet => ipnet != null);
33✔
120

121
        if (nnin.Length <= 0)
10✔
122
        {
2✔
123
            if (!tryWide)
2✔
124
            {
1✔
125
                throw new ArgumentException(nameof(ipnetworks));
1✔
126
            }
127

128
            ipnetwork = null;
1✔
129
            return false;
1✔
130
        }
131

132
        if (nnin.Length == 1)
8✔
133
        {
1✔
134
            IPNetwork2 ipn0 = nnin[0];
1✔
135
            ipnetwork = ipn0;
1✔
136
            return true;
1✔
137
        }
138

139
        Array.Sort(nnin);
7✔
140
        IPNetwork2 nnin0 = nnin[0];
7✔
141
        BigInteger uintNnin0 = nnin0.ipaddress;
7✔
142

143
        IPNetwork2 nninX = nnin[nnin.Length - 1];
7✔
144
        IPAddress ipaddressX = nninX.Broadcast;
7✔
145

146
        AddressFamily family = ipnetworks[0].family;
7✔
147
        foreach (IPNetwork2 ipnx in ipnetworks)
64✔
148
        {
22✔
149
            if (ipnx.family != family)
22✔
150
            {
1✔
151
                if (!tryWide)
1✔
152
                {
1✔
153
                     throw new ArgumentException("MixedAddressFamily");
1✔
154
                }
155
                ipnetwork = null;
×
156
                return false;
×
157
            }
158
        }
21✔
159

160
        IPNetwork2 ipn;
161

162
        for (byte cidr = nnin0.cidr; ; cidr--)
132✔
163
        {
132✔
164
            var wideSubnet = new IPNetwork2(uintNnin0, family, cidr);
132✔
165
            if (wideSubnet.Contains(ipaddressX))
132✔
166
            {
6✔
167
                ipn = wideSubnet;
6✔
168
                break;
6✔
169
            }
170
        }
126✔
171

172
        ipnetwork = ipn;
6✔
173
        return true;
6✔
174
    }
9✔
175
}
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