• 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

91.04
/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!
UNCOV
37
        {
×
NEW
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!
UNCOV
55
        {
×
NEW
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 (network == null)
2✔
74
        {
1✔
75
            throw new ArgumentNullException(nameof(network));
1✔
76
        }
77

78
        return InternalSubnet(true, network, cidr, out ipnetworkCollection);
1✔
79
    }
1✔
80

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

98
            ipnetworkCollection = null;
1✔
99
            return false;
1✔
100
        }
101

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

110
            ipnetworkCollection = null;
2✔
111
            return false;
2✔
112
        }
113

114
        if (cidr < network.Cidr)
783✔
115
        {
4✔
116
            if (!trySubnet)
4✔
117
            {
2✔
118
                throw new ArgumentException("CIDR is smaller than the network CIDR.", nameof(cidr));
2✔
119
            }
120

121
            ipnetworkCollection = null;
2✔
122
            return false;
2✔
123
        }
124

125
        ipnetworkCollection = new IPNetworkCollection(network, cidr);
779✔
126
        return true;
779✔
127
    }
784✔
128
}
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