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

lduchosal / ipnetwork / 743

30 Mar 2025 12:27AM UTC coverage: 93.641% (+0.1%) from 93.516%
743

push

appveyor

lduchosal
Chore: last cleanups

1561 of 1667 relevant lines covered (93.64%)

820053.65 hits per line

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

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

5
namespace System.Net;
6

7
using Sockets;
8

9
/// <summary>
10
/// Class <c>CidrClassFull</c> tries to guess CIDR in a ClassFull way.
11
/// </summary>
12
public sealed class CidrClassFull : ICidrGuess
13
{
14
    /// <summary>
15
    ///
16
    /// IPV4 :
17
    ///
18
    /// Class              Leading bits    Default netmask
19
    ///     A (CIDR /8)        00           255.0.0.0
20
    ///     A (CIDR /8)        01           255.0.0.0
21
    ///     B (CIDR /16)       10           255.255.0.0
22
    ///     C (CIDR /24)       11           255.255.255.0
23
    ///
24
    /// IPV6 : 64.
25
    ///
26
    /// </summary>
27
    /// <param name="ip">A string representing the CIDR to convert.</param>
28
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
29
    /// <returns>true if ip was converted successfully; otherwise, false.</returns>
30
    public bool TryGuessCidr(string ip, out byte cidr)
31
    {
56✔
32
            bool parsed = IPAddress.TryParse($"{ip}", out var ipaddress);
56✔
33
            if (parsed == false)
56✔
34
            {
9✔
35
                cidr = 0;
9✔
36
                return false;
9✔
37
            }
38

39
            if (ipaddress.AddressFamily == AddressFamily.InterNetworkV6)
47✔
40
            {
20✔
41
                cidr = 64;
20✔
42
                return true;
20✔
43
            }
44

45
            var uintIPAddress = IPNetwork2.ToBigInteger(ipaddress);
27✔
46
            uintIPAddress = uintIPAddress >> 30;
27✔
47
            if (uintIPAddress <= 1)
27✔
48
            {
7✔
49
                cidr = 8;
7✔
50
                return true;
7✔
51
            }
52

53
            if (uintIPAddress <= 2)
20✔
54
            {
4✔
55
                cidr = 16;
4✔
56
                return true;
4✔
57
            }
58

59
            if (uintIPAddress <= 3)
16✔
60
            {
16✔
61
                cidr = 24;
16✔
62
                return true;
16✔
63
            }
64

65
            cidr = 0;
×
66
            return false;
×
67
        }
56✔
68
}
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