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

lduchosal / ipnetwork / 881

07 Mar 2026 04:55PM UTC coverage: 92.371% (-1.0%) from 93.419%
881

Pull #383

appveyor

web-flow
Merge 72bf930e1 into 4178c9692
Pull Request #383: feat/v4-nullable

1901 of 2058 relevant lines covered (92.37%)

580908.3 hits per line

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

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

5
namespace System.Net;
6

7
using System.Diagnostics.CodeAnalysis;
8

9
/// <summary>
10
/// Subnet.
11
/// </summary>
12
public sealed partial class IPNetwork2
13
{
14
    /// <summary>
15
    /// Subnet a network into multiple nets of cidr mask
16
    /// Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25
17
    /// Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9.
18
    /// </summary>
19
    /// <param name="cidr1">A byte representing the CIDR to be used to subnet the current IPNetwork.</param>
20
    /// <param name="ipnetworkCollection">The resulting subnetted IPNetwork.</param>
21
    /// <returns>true if network was split successfully; otherwise, false.</returns>
22
    public bool TrySubnet(byte cidr1, out IPNetworkCollection? ipnetworkCollection)
23
    {
9✔
24
        return InternalSubnet(true, this, cidr1, out ipnetworkCollection);
9✔
25
    }
9✔
26

27
    /// <summary>
28
    /// Subnet a network into multiple nets of cidr mask
29
    /// Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25
30
    /// Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9.
31
    /// </summary>
32
    /// <param name="cidr1">A byte representing the CIDR to be used to subnet the current IPNetwork.</param>
33
    /// <returns>A IPNetworkCollection split by CIDR.</returns>
34
    public IPNetworkCollection Subnet(byte cidr1)
35
    {
776✔
36
        if (!InternalSubnet(false, this, cidr1, out IPNetworkCollection? ipnetworkCollection))
776✔
37
        {
×
38
            throw new ArgumentException("Invalid CIDR.", nameof(cidr1));
×
39
        }
40

41
        return ipnetworkCollection;
772✔
42
    }
772✔
43

44
    /// <summary>
45
    /// Subnet a network into multiple nets of cidr mask
46
    /// Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25
47
    /// Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9.
48
    /// </summary>
49
    /// <param name="network">The network to subnet.</param>
50
    /// <param name="cidr">A byte representing the CIDR to be used to subnet the network.</param>
51
    /// <returns>A IPNetworkCollection split by CIDR.</returns>
52
    public static IPNetworkCollection Subnet(IPNetwork2 network, byte cidr)
53
    {
2✔
54
        if (!InternalSubnet(false, network, cidr, out IPNetworkCollection? ipnetworkCollection))
2✔
55
        {
×
56
            throw new ArgumentException("Invalid CIDR.", nameof(cidr));
×
57
        }
58

59
        return ipnetworkCollection;
1✔
60
    }
1✔
61

62
    /// <summary>
63
    /// Subnet a network into multiple nets of cidr mask
64
    /// Subnet 192.168.0.0/24 into cidr 25 gives 192.168.0.0/25, 192.168.0.128/25
65
    /// Subnet 10.0.0.0/8 into cidr 9 gives 10.0.0.0/9, 10.128.0.0/9.
66
    /// </summary>
67
    /// <param name="network">The network to subnet.</param>
68
    /// <param name="cidr">A byte representing the CIDR to be used to subnet the network.</param>
69
    /// <param name="ipnetworkCollection">The resulting subnetted IPNetwork.</param>
70
    /// <returns>true if network was split successfully; otherwise, false.</returns>
71
    public static bool TrySubnet(IPNetwork2 network, byte cidr, out IPNetworkCollection? ipnetworkCollection)
72
    {
2✔
73
#if NETSTANDARD2_1
74
        if (network == null)
75
        {
76
            throw new ArgumentNullException(nameof(network));
77
        }
78
#else
79
        ArgumentNullException.ThrowIfNull(network);
2✔
80
#endif
81

82
        return InternalSubnet(true, network, cidr, out ipnetworkCollection);
1✔
83
    }
1✔
84

85
    /// <summary>
86
    /// Splits a given IP network into smaller subnets of the specified CIDR size.
87
    /// </summary>
88
    /// <param name="trySubnet">Indicates whether to throw exceptions or return null on failure.</param>
89
    /// <param name="network">The IP network to be subnetted.</param>
90
    /// <param name="cidr">The CIDR value used to define the new subnet size.</param>
91
    /// <param name="ipnetworkCollection">The resulting collection of subnets, or null if the operation fails and trySubnet is true.</param>
92
    internal static bool InternalSubnet(bool trySubnet, IPNetwork2 network, byte cidr, [NotNullWhen(true)] out IPNetworkCollection? ipnetworkCollection)
93
    {
790✔
94
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
95
        if (network == null)
790✔
96
        {
3✔
97
            if (!trySubnet)
3✔
98
            {
2✔
99
                throw new ArgumentNullException(nameof(network));
2✔
100
            }
101

102
            ipnetworkCollection = null;
1✔
103
            return false;
1✔
104
        }
105

106
        int maxCidr = network.family == Sockets.AddressFamily.InterNetwork ? 32 : 128;
787✔
107
        if (cidr > maxCidr)
787✔
108
        {
4✔
109
            if (!trySubnet)
4✔
110
            {
2✔
111
                throw new ArgumentOutOfRangeException(nameof(cidr));
2✔
112
            }
113

114
            ipnetworkCollection = null;
2✔
115
            return false;
2✔
116
        }
117

118
        if (cidr < network.Cidr)
783✔
119
        {
4✔
120
            if (!trySubnet)
4✔
121
            {
2✔
122
                throw new ArgumentException("CIDR is smaller than the network CIDR.", nameof(cidr));
2✔
123
            }
124

125
            ipnetworkCollection = null;
2✔
126
            return false;
2✔
127
        }
128

129
        ipnetworkCollection = new IPNetworkCollection(network, cidr);
779✔
130
        return true;
779✔
131
    }
784✔
132
}
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