• 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

93.18
/src/System.Net.IPNetwork/IPNetwork2ToNetmask.cs
1
// <copyright file="IPNetwork2ToNetmask.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
/// ToNetmask.
13
/// </summary>
14
public sealed partial class IPNetwork2
15
{
16
    /// <summary>
17
    /// Convert CIDR to netmask
18
    ///  24 -> 255.255.255.0
19
    ///  16 -> 255.255.0.0
20
    ///  8 -> 255.0.0.0.
21
    /// </summary>
22
    /// <see href="http://snipplr.com/view/15557/cidr-class-for-ipv4/"/>
23
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
24
    /// <param name="family">Either IPv4 or IPv6.</param>
25
    /// <returns>An IPAddress representing cidr.</returns>
26
    public static IPAddress ToNetmask(byte cidr, AddressFamily family)
27
    {
15✔
28
        if (!InternalToNetmask(false, cidr, family, out IPAddress? netmask))
15!
NEW
29
        {
×
NEW
30
            throw new ArgumentException("Invalid CIDR.", nameof(cidr));
×
31
        }
32

33
        return netmask;
11✔
34
    }
11✔
35

36
    /// <summary>
37
    /// Convert CIDR to netmask
38
    ///  24 -> 255.255.255.0
39
    ///  16 -> 255.255.0.0
40
    ///  8 -> 255.0.0.0.
41
    /// </summary>
42
    /// <see href="http://snipplr.com/view/15557/cidr-class-for-ipv4/"/>
43
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
44
    /// <param name="family">Either IPv4 or IPv6.</param>
45
    /// <param name="netmask">The resulting netmask.</param>
46
    /// <returns>true if cidr was converted successfully; otherwise, false.</returns>
47
    public static bool TryToNetmask(byte cidr, AddressFamily family, [NotNullWhen(true)] out IPAddress? netmask)
48
    {
4,001,103✔
49
        return InternalToNetmask(true, cidr, family, out netmask);
4,001,103✔
50
    }
4,001,103✔
51

52
    /// <summary>
53
    /// Converts a CIDR value to its corresponding IPAddress netmask.
54
    /// </summary>
55
    /// <param name="tryParse">If true, handles errors silently; otherwise, throws exceptions.</param>
56
    /// <param name="cidr">The CIDR value to convert.</param>
57
    /// <param name="family">The address family (IPv4 or IPv6).</param>
58
    /// <param name="netmask">The resulting IPAddress netmask.</param>
59
    internal static bool InternalToNetmask(bool tryParse, byte cidr, AddressFamily family, [NotNullWhen(true)] out IPAddress? netmask)
60
    {
4,001,119✔
61
        if (family != AddressFamily.InterNetwork
4,001,119✔
62
            && family != AddressFamily.InterNetworkV6)
4,001,119✔
63
        {
2✔
64
            if (!tryParse)
2✔
65
            {
1✔
66
                throw new ArgumentException("Invalid address family.", nameof(family));
1✔
67
            }
68

69
            netmask = null;
1✔
70
            return false;
1✔
71
        }
72

73
        int maxCidr = family == AddressFamily.InterNetwork ? 32 : 128;
4,001,117✔
74
        if (cidr > maxCidr)
4,001,117✔
75
        {
9✔
76
            if (!tryParse)
9✔
77
            {
3✔
78
                throw new ArgumentOutOfRangeException(nameof(cidr));
3✔
79
            }
80

81
            netmask = null;
6✔
82
            return false;
6✔
83
        }
84

85
        BigInteger mask = ToUint(cidr, family);
4,001,108✔
86
        netmask = ToIPAddress(mask, family);
4,001,108✔
87
        return true;
4,001,108✔
88
    }
4,001,115✔
89
}
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