• 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/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
    {
×
28
        if (!InternalToNetmask(false, cidr, family, out IPAddress? netmask))
×
29
        {
×
30
            throw new ArgumentException("Invalid CIDR.", nameof(cidr));
×
31
        }
32

33
        return netmask;
×
34
    }
×
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
    {
×
49
        return InternalToNetmask(true, cidr, family, out netmask);
×
50
    }
×
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
    {
×
61
        if (family != AddressFamily.InterNetwork
×
62
            && family != AddressFamily.InterNetworkV6)
×
63
        {
×
64
            if (!tryParse)
×
65
            {
×
66
                throw new ArgumentException("Invalid address family.", nameof(family));
×
67
            }
68

69
            netmask = null;
×
70
            return false;
×
71
        }
72

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

81
            netmask = null;
×
82
            return false;
×
83
        }
84

85
        BigInteger mask = ToUint(cidr, family);
×
86
        netmask = ToIPAddress(mask, family);
×
87
        return true;
×
88
    }
×
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