• 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/IPNetwork2ValidNetmask.cs
1
// <copyright file="IPNetwork2ValidNetmask.cs" company="IPNetwork">
2
// Copyright (c) IPNetwork. All rights reserved.
3
// </copyright>
4

5
namespace System.Net;
6

7
using System.Net.Sockets;
8
using System.Numerics;
9

10
/// <summary>
11
/// ValidNetmask.
12
/// </summary>
13
public sealed partial class IPNetwork2
14
{
15
    /// <summary>
16
    /// return true if netmask is a valid netmask
17
    /// 255.255.255.0, 255.0.0.0, 255.255.240.0, ...
18
    /// </summary>
19
    /// <see href="http://www.actionsnip.com/snippets/tomo_atlacatl/calculate-if-a-netmask-is-valid--as2-"/>
20
    /// <param name="netmask">A number representing the netmask to validate.</param>
21
    /// <returns>true if netmask is a valid IP Netmask; otherwise, false.</returns>
22
    public static bool ValidNetmask(IPAddress netmask)
23
    {
×
24
        if (netmask == null)
×
25
        {
×
26
            throw new ArgumentNullException(nameof(netmask));
×
27
        }
28

29
        var uintNetmask = ToBigInteger(netmask);
×
30
        bool valid = InternalValidNetmask(uintNetmask, netmask.AddressFamily);
×
31

32
        return valid;
×
33
    }
×
34

35
    /// <summary>
36
    /// Determines whether a given BigInteger netmask is valid for the specified address family.
37
    /// </summary>
38
    /// <param name="netmask">The netmask represented as a BigInteger.</param>
39
    /// <param name="family">The address family (IPv4 or IPv6).</param>
40
    /// <returns>
41
    /// <c>true</c> if the netmask is valid; otherwise, <c>false</c>.
42
    /// </returns>
43
    internal static bool InternalValidNetmask(BigInteger netmask, AddressFamily family)
44
    {
×
45
        if (family != AddressFamily.InterNetwork
×
46
            && family != AddressFamily.InterNetworkV6)
×
47
        {
×
48
            throw new ArgumentException("Invalid address family.", nameof(family));
×
49
        }
50

51
        BigInteger mask = family == AddressFamily.InterNetwork
×
52
            ? new BigInteger(0x0ffffffff)
×
53
            : new BigInteger([
×
54
                0xff, 0xff, 0xff, 0xff,
×
55
                0xff, 0xff, 0xff, 0xff,
×
56
                0xff, 0xff, 0xff, 0xff,
×
57
                0xff, 0xff, 0xff, 0xff,
×
58
                0x00
×
59
            ]);
×
60

61
        BigInteger neg = (~netmask) & mask;
×
62
        bool isNetmask = ((neg + 1) & neg) == 0;
×
63

64
        return isNetmask;
×
65
    }
×
66
}
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