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

lduchosal / ipnetwork / 22803352265

07 Mar 2026 05:07PM UTC coverage: 91.848% (-1.3%) from 93.118%
22803352265

push

travis-pro

web-flow
Merge pull request #383 from lduchosal/feat/v4-nullable

feat/v4-nullable

651 of 721 branches covered (90.29%)

Branch coverage included in aggregate %.

111 of 147 new or added lines in 26 files covered. (75.51%)

5 existing lines in 3 files now uncovered.

1929 of 2088 relevant lines covered (92.39%)

574342.5 hits per line

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

82.79
/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.Diagnostics.CodeAnalysis;
8
using System.Net.Sockets;
9
using System.Numerics;
10

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

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

37
        if (!IPAddress.TryParse(start, out IPAddress? startIP))
1!
38
        {
×
NEW
39
            throw new ArgumentException("Invalid start IP address.", nameof(start));
×
40
        }
41

42
        if (!IPAddress.TryParse(end, out IPAddress? endIP))
1!
43
        {
×
NEW
44
            throw new ArgumentException("Invalid end IP address.", nameof(end));
×
45
        }
46

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

52
        IPNetwork2 ipnetwork;
53

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

65
        return ipnetwork;
1✔
66
    }
1✔
67

68
    /// <summary>
69
    /// 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>
70
    /// /
71
    public static IPNetwork2 WideSubnet(IPNetwork2[] ipnetworks)
72
    {
7✔
73
        if (!InternalWideSubnet(false, ipnetworks, out IPNetwork2? ipn))
7!
UNCOV
74
        {
×
NEW
75
            throw new ArgumentException("Invalid IP networks.", nameof(ipnetworks));
×
76
        }
77

78
        return ipn;
4✔
79
    }
4✔
80

81
    /// <summary>
82
    /// Attempts to find the widest subnet that contains both the start and end IP addresses. objects.
83
    /// </summary>
84
    /// <param name="ipnetworks">An array of IPNetwork2 objects to wide subnet.</param>
85
    /// <param name="ipnetwork">When this method returns, contains the wide subnet of the IPNetwork2 objects, if wide subnet was successful; otherwise, null.</param>
86
    /// <returns>true if wide subnet was successful; otherwise, false.</returns>
87
    public static bool TryWideSubnet(IPNetwork2[] ipnetworks, [NotNullWhen(true)] out IPNetwork2? ipnetwork)
88
    {
6✔
89
        if (!InternalWideSubnet(true, ipnetworks, out IPNetwork2? ipn))
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, [NotNullWhen(true)] out IPNetwork2? ipnetwork)
107
    {
13✔
108
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
109
        if (ipnetworks == null)
13✔
110
        {
2✔
111
            if (!tryWide)
2✔
112
            {
1✔
113
                throw new ArgumentNullException(nameof(ipnetworks));
1✔
114
            }
115

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

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

122
        if (nnin.Length <= 0)
11✔
123
        {
2✔
124
            if (!tryWide)
2✔
125
            {
1✔
126
                throw new ArgumentException("No valid IP networks provided.", nameof(ipnetworks));
1✔
127
            }
128

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

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

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

144
        IPNetwork2 nninX = nnin[^1];
8✔
145
        IPAddress ipaddressX = nninX.Last;
8✔
146

147
        AddressFamily family = ipnetworks[0].family;
8✔
148
        foreach (IPNetwork2 ipnx in ipnetworks)
71✔
149
        {
24✔
150
            if (ipnx.family != family)
24✔
151
            {
1✔
152
                if (!tryWide)
1!
153
                {
1✔
154
                     throw new ArgumentException("All networks must have the same address family.", nameof(ipnetworks));
1✔
155
                }
156
                ipnetwork = null;
×
157
                return false;
×
158
            }
159
        }
23✔
160

161
        IPNetwork2 ipn;
162

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

173
        ipnetwork = ipn;
7✔
174
        return true;
7✔
175
    }
10✔
176
}
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