• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

lduchosal / ipnetwork / 738

29 Mar 2025 10:31PM UTC coverage: 93.524% (+0.2%) from 93.279%
738

push

appveyor

web-flow
feat/code-quality (#350)

* Chore: file scoped namespace

* Feat: sign assembly
Fix: documentation on public methodes

* Fix: documentation and warnings

* Chore: split IPNetwork2 into multiple partial classes

* Chore: split IPNetwork2

* Chore: document partial classes

* Fix: order methodes

* Chore: documentd

* Chore: cleanup

1603 of 1714 relevant lines covered (93.52%)

825934.22 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 System.Net.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(string.Format("{0}", 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
            else if (uintIPAddress <= 2)
20✔
53
            {
4✔
54
                cidr = 16;
4✔
55
                return true;
4✔
56
            }
57
            else if (uintIPAddress <= 3)
16✔
58
            {
16✔
59
                cidr = 24;
16✔
60
                return true;
16✔
61
            }
62

63
            cidr = 0;
×
64
            return false;
×
65
        }
56✔
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