• 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/IPNetwork2Supernet.cs
1
// <copyright file="IPNetwork2Supernet.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.Numerics;
9

10
/// <summary>
11
/// supernet.
12
/// </summary>
13
public sealed partial class IPNetwork2
14
{
15
    /// <summary>
16
    /// Supernet two consecutive cidr equal subnet into a single one
17
    /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
18
    /// 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15
19
    /// 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24.
20
    /// </summary>
21
    /// <param name="network2">The network to supernet with.</param>
22
    /// <returns>A super netted IP Network.</returns>
23
    public IPNetwork2 Supernet(IPNetwork2 network2)
24
    {
×
25
        if (!InternalSupernet(false, this, network2, out IPNetwork2? supernet))
×
26
        {
×
27
            throw new ArgumentException("Failed to supernet networks.", nameof(network2));
×
28
        }
29

30
        return supernet;
×
31
    }
×
32

33
    /// <summary>
34
    /// Try to supernet two consecutive cidr equal subnet into a single one
35
    /// 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23
36
    /// 10.1.0.0/16 + 10.0.0.0/16 = 10.0.0.0/15
37
    /// 192.168.0.0/24 + 192.168.0.0/25 = 192.168.0.0/24.
38
    /// </summary>
39
    /// <param name="network2">The network to supernet with.</param>
40
    /// <param name="supernet">The resulting IPNetwork.</param>
41
    /// <returns>true if network2 was super netted successfully; otherwise, false.</returns>
42
    public bool TrySupernet(IPNetwork2 network2, [NotNullWhen(true)] out IPNetwork2? supernet)
43
    {
×
44
        return InternalSupernet(true, this, network2, out supernet);
×
45
    }
×
46

47
    /// <summary>
48
    /// Attempts to merge two adjacent IP networks with equal CIDR values into a single supernet.
49
    /// </summary>
50
    /// <param name="trySupernet">If true, suppresses exceptions on failure; otherwise, throws.</param>
51
    /// <param name="network1">The first IP network.</param>
52
    /// <param name="network2">The second IP network.</param>
53
    /// <param name="supernet">The resulting supernet if the merge is successful; otherwise, null.</param>
54
    internal static bool InternalSupernet(
55
        bool trySupernet,
56
        IPNetwork2 network1,
57
        IPNetwork2 network2,
58
        [NotNullWhen(true)] out IPNetwork2? supernet)
59
    {
×
60
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
61
        if (network1 == null)
×
62
        {
×
63
            if (!trySupernet)
×
64
            {
×
65
                throw new ArgumentNullException(nameof(network1));
×
66
            }
67

68
            supernet = null;
×
69
            return false;
×
70
        }
71

72
        if (network2 == null)
×
73
        {
×
74
            if (!trySupernet)
×
75
            {
×
76
                throw new ArgumentNullException(nameof(network2));
×
77
            }
78

79
            supernet = null;
×
80
            return false;
×
81
        }
82

83
        if (network1.Contains(network2))
×
84
        {
×
85
            supernet = new IPNetwork2(network1.InternalNetwork, network1.family, network1.Cidr);
×
86
            return true;
×
87
        }
88

89
        if (network2.Contains(network1))
×
90
        {
×
91
            supernet = new IPNetwork2(network2.InternalNetwork, network2.family, network2.Cidr);
×
92
            return true;
×
93
        }
94

95
        if (network1.cidr != network2.cidr)
×
96
        {
×
97
            if (!trySupernet)
×
98
            {
×
99
                throw new ArgumentException("Networks must have the same CIDR.", nameof(network1));
×
100
            }
101

102
            supernet = null;
×
103
            return false;
×
104
        }
105

106
        IPNetwork2 first = (network1.InternalNetwork < network2.InternalNetwork) ? network1 : network2;
×
107
        IPNetwork2 last = (network1.InternalNetwork > network2.InternalNetwork) ? network1 : network2;
×
108

109
        // Starting from here :
110
        // network1 and network2 have the same cidr,
111
        // network1 does not contain network2,
112
        // network2 does not contain network1,
113
        // first is the lower subnet
114
        // last is the higher subnet
115
        if ((first.InternalBroadcast + 1) != last.InternalNetwork)
×
116
        {
×
117
            if (!trySupernet)
×
118
            {
×
119
                throw new ArgumentOutOfRangeException(nameof(network1));
×
120
            }
121

122
            supernet = null;
×
123
            return false;
×
124
        }
125

126
        BigInteger uintSupernet = first.InternalNetwork;
×
127
        byte cidrSupernet = (byte)(first.cidr - 1);
×
128

129
        var networkSupernet = new IPNetwork2(uintSupernet, first.family, cidrSupernet);
×
130
        if (networkSupernet.InternalNetwork != first.InternalNetwork)
×
131
        {
×
132
            if (!trySupernet)
×
133
            {
×
134
                throw new ArgumentException("Networks are not contiguous.", nameof(network1));
×
135
            }
136

137
            supernet = null;
×
138
            return false;
×
139
        }
140

141
        supernet = networkSupernet;
×
142
        return true;
×
143
    }
×
144
}
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