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

lduchosal / ipnetwork / 808

17 Aug 2025 08:25AM UTC coverage: 93.223% (-1.0%) from 94.226%
808

push

appveyor

web-flow
Chore: cleanup, breaking changes, enum, tryparse, exception, static ListIPAddress (#363)

* Chore: huge cleanup, enum, tryparse, exception, static ListIPAddress, important changes : IPNetwork comparison and sort order have change to reflect expected behavoir
* Fix: obsolete enums
* Fix: network sorting and member comparison
* Chore: upgrade version number 3.3

1802 of 1933 relevant lines covered (93.22%)

726934.37 hits per line

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

94.52
/src/System.Net.IPNetwork/IPNetwork2ToUint.cs
1
// <copyright file="IPNetwork2ToUint.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
/// ToUint.
12
/// </summary>
13
public sealed partial class IPNetwork2
14
{
15
    /// <summary>
16
    /// Convert an IPAddress to decimal
17
    /// 0.0.0.0 -> 0
18
    /// 0.0.1.0 -> 256.
19
    /// </summary>
20
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
21
    /// <returns>A number representing the ipaddress.</returns>
22
    public static BigInteger ToBigInteger(IPAddress ipaddress)
23
    {
4,205,018✔
24
        bool parsed = InternalToBigInteger(false, ipaddress, out BigInteger uintIpAddress);
4,205,018✔
25
        if (!parsed)
4,205,016✔
26
        {
×
27
            throw new ArgumentOutOfRangeException(nameof(ipaddress));
×
28
        }
29
        return uintIpAddress;
4,205,016✔
30
    }
4,205,016✔
31

32
    /// <summary>
33
    /// Convert an IPAddress to decimal
34
    /// 0.0.0.0 -> 0
35
    /// 0.0.1.0 -> 256.
36
    /// </summary>
37
    /// <param name="ipaddress">A string containing an ip address to convert.</param>
38
    /// <param name="uintIpAddress">A number representing the IPAddress.</param>
39
    /// <returns>true if ipaddress was converted successfully; otherwise, false.</returns>
40
    public static bool TryToBigInteger(IPAddress ipaddress, out BigInteger uintIpAddress)
41
    {
4,001,151✔
42
        bool parsed = InternalToBigInteger(true, ipaddress, out uintIpAddress);
4,001,151✔
43
        return parsed;
4,001,151✔
44
    }
4,001,151✔
45

46
    /// <summary>
47
    /// Convert a cidr to BigInteger netmask.
48
    /// </summary>
49
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
50
    /// <param name="family">Either IPv4 or IPv6.</param>
51
    /// <returns>A number representing the netmask in CIDR form.</returns>
52
    public static BigInteger ToUint(byte cidr, AddressFamily family)
53
    {
17,307,058✔
54
        bool parsed = InternalToBigInteger(false, cidr, family, out BigInteger uintNetmask);
17,307,058✔
55
        if (!parsed)
17,307,057✔
56
        {
×
57
            throw new ArgumentException(nameof(cidr));
×
58
        }
59
        return uintNetmask;
17,307,057✔
60
    }
17,307,057✔
61

62
    /// <summary>
63
    /// Convert a cidr to uint netmask.
64
    /// </summary>
65
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
66
    /// <param name="family">Either IPv4 or IPv6.</param>
67
    /// <param name="uintNetmask">A number representing the netmask.</param>
68
    /// <returns>true if cidr was converted successfully; otherwise, false.</returns>
69
    public static bool TryToUint(byte cidr, AddressFamily family, out BigInteger uintNetmask)
70
    {
1✔
71
        bool parsed = InternalToBigInteger(true, cidr, family, out uintNetmask);
1✔
72
        return parsed;
1✔
73
    }
1✔
74

75
    /// <summary>
76
    /// Convert a cidr to uint netmask.
77
    /// </summary>
78
    /// <param name="tryParse">Whether to throw exception or not during conversion.</param>
79
    /// <param name="cidr">A byte representing the netmask in cidr format (/24).</param>
80
    /// <param name="family">Either IPv4 or IPv6.</param>
81
    /// <param name="uintNetmask">A number representing the netmask.</param>
82
    internal static bool InternalToBigInteger(bool tryParse, byte cidr, AddressFamily family, out BigInteger uintNetmask)
83
    {
17,307,064✔
84
        if ((family == AddressFamily.InterNetwork && cidr > 32)
17,307,064✔
85
            || (family == AddressFamily.InterNetworkV6 && cidr > 128))
17,307,064✔
86
        {
4✔
87
            if (!tryParse)
4✔
88
            {
2✔
89
                throw new ArgumentOutOfRangeException(nameof(cidr));
2✔
90
            }
91

92
            uintNetmask = 0;
2✔
93
            return false;
2✔
94
        }
95

96
        if (family != AddressFamily.InterNetwork
17,307,060✔
97
            && family != AddressFamily.InterNetworkV6)
17,307,060✔
98
        {
2✔
99
            if (!tryParse)
2✔
100
            {
1✔
101
                throw new NotSupportedException(family.ToString());
1✔
102
            }
103

104
            uintNetmask = 0;
1✔
105
            return false;
1✔
106
        }
107

108
        if (family == AddressFamily.InterNetwork)
17,307,058✔
109
        {
17,301,723✔
110
            uintNetmask = cidr == 0 ? 0 : 0xffffffff << (32 - cidr);
17,301,723✔
111
            return true;
17,301,723✔
112
        }
113

114
        var mask = new BigInteger([
5,335✔
115
            0xff, 0xff, 0xff, 0xff,
5,335✔
116
            0xff, 0xff, 0xff, 0xff,
5,335✔
117
            0xff, 0xff, 0xff, 0xff,
5,335✔
118
            0xff, 0xff, 0xff, 0xff,
5,335✔
119
            0x00
5,335✔
120
        ]);
5,335✔
121

122
        BigInteger masked = cidr == 0 ? 0 : mask << (128 - cidr);
5,335✔
123
        byte[] m = masked.ToByteArray();
5,335✔
124
        byte[] bmask = new byte[17];
5,335✔
125
        int copy = m.Length > 16 ? 16 : m.Length;
5,335✔
126
        Array.Copy(m, 0, bmask, 0, copy);
5,335✔
127
        uintNetmask = new BigInteger(bmask);
5,335✔
128
        return true;
5,335✔
129
    }
17,307,061✔
130

131
    /// <summary>
132
    /// Converts an IPAddress to a nullable BigInteger representation.
133
    /// </summary>
134
    /// <param name="tryParse">Indicates whether the method should handle errors silently (true) or throw exceptions (false).</param>
135
    /// <param name="ipaddress">The IPAddress to convert.</param>
136
    /// <param name="uintIpAddress">The resulting BigInteger representation of the IP address, or null if conversion fails and tryParse is true.</param>
137
    internal static bool InternalToBigInteger(bool tryParse, IPAddress ipaddress, out BigInteger uintIpAddress)
138
    {
8,206,169✔
139
        if (ipaddress == null)
8,206,169✔
140
        {
3✔
141
            if (!tryParse)
3✔
142
            {
2✔
143
                throw new ArgumentNullException(nameof(ipaddress));
2✔
144
            }
145

146
            uintIpAddress = default;
1✔
147
            return false;
1✔
148
        }
149

150
#if NETSTANDARD2_1
151
        byte[] bytes = ipaddress.AddressFamily == AddressFamily.InterNetwork ? new byte[4] : new byte[16];
152
        Span<byte> span = bytes.AsSpan();
153
        if (!ipaddress.TryWriteBytes(span, out _))
154
        {
155
            if (!tryParse)
156
            {
157
                throw new ArgumentException("ipaddress");
158
            }
159

160
            uintIpAddress = default;
161
            return false;
162
        }
163

164
        uintIpAddress = new BigInteger(span, isUnsigned: true, isBigEndian: true);
165
#elif NETSTANDARD20
166
        byte[] bytes = ipaddress.GetAddressBytes();
167
        bytes.AsSpan().Reverse();
168

169
        // add trailing 0 to make unsigned
170
        byte[] unsigned = new byte[bytes.Length + 1];
171
        Buffer.BlockCopy(bytes, 0, unsigned, 0, bytes.Length);
172
        uintIpAddress = new BigInteger(unsigned);
173
#else
174
        byte[] bytes = ipaddress.GetAddressBytes();
8,206,166✔
175
        Array.Reverse(bytes);
8,206,166✔
176

177
        // add trailing 0 to make unsigned
178
        byte[] unsigned = new byte[bytes.Length + 1];
8,206,166✔
179
        Buffer.BlockCopy(bytes, 0, unsigned, 0, bytes.Length);
8,206,166✔
180
        uintIpAddress = new BigInteger(unsigned);
8,206,166✔
181
#endif
182
        return true;
8,206,166✔
183
    }
8,206,167✔
184
}
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

© 2025 Coveralls, Inc