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

lduchosal / ipnetwork / 795

15 Aug 2025 01:50PM UTC coverage: 93.543% (+0.007%) from 93.536%
795

push

appveyor

web-flow
Fix: usable count according to CIDR reference (#360)

* Fix: usable count according to CIDR reference

1695 of 1812 relevant lines covered (93.54%)

765871.66 hits per line

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

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

5
namespace System.Net;
6

7
using System.Numerics;
8
using System.Net.Sockets;
9
using System.Runtime.Serialization;
10

11
/// <summary>
12
/// Properties and members of IPNetwork2.
13
/// </summary>
14
public partial class IPNetwork2
15
{
16
    private readonly object sync = new ();
8,136,610✔
17
    private readonly int hashCode;
18
    private BigInteger ipaddress;
19
    private byte cidr;
20
    private BigInteger? cachedBroadcast;
21

22
    private AddressFamily family;
23

24
    /// <summary>
25
    /// Gets or sets a value indicating whether gets or sets the value of the IPNetwork property.
26
    /// </summary>
27
    [DataMember(Name = "IPNetwork", IsRequired = true)]
28
    public string Value
29
    {
30
        get
31
        {
2,000,002✔
32
            return this.ToString();
2,000,002✔
33
        }
2,000,002✔
34

35
        set
36
        {
2,000,004✔
37
            var ipnetwork = Parse(value);
2,000,004✔
38
            this.ipaddress = ipnetwork.ipaddress;
2,000,004✔
39
            this.family = ipnetwork.family;
2,000,004✔
40
            this.cidr = ipnetwork.cidr;
2,000,004✔
41
            lock (this.sync)
2,000,004✔
42
            {
2,000,004✔
43
                this.cachedBroadcast = null;
2,000,004✔
44
            }
2,000,004✔
45
        }
2,000,004✔
46
    }
47

48
    /// <summary>
49
    /// Gets network address.
50
    /// </summary>
51
    public IPAddress Network
52
    {
53
        get
54
        {
4,138,184✔
55
            return ToIPAddress(this.InternalNetwork, this.family);
4,138,184✔
56
        }
4,138,184✔
57
    }
58

59
    /// <summary>
60
    /// Gets address Family.
61
    /// </summary>
62
    public AddressFamily AddressFamily
63
    {
64
        get
65
        {
135,736✔
66
            return this.family;
135,736✔
67
        }
135,736✔
68
    }
69

70
    /// <summary>
71
    /// Gets netmask.
72
    /// </summary>
73
    public IPAddress Netmask
74
    {
75
        get
76
        {
367✔
77
            return ToIPAddress(this.InternalNetmask, this.family);
367✔
78
        }
367✔
79
    }
80

81
    /// <summary>
82
    /// Gets broadcast address.
83
    /// </summary>
84
    public IPAddress Broadcast
85
    {
86
        get
87
        {
67,266✔
88
            if (this.family == AddressFamily.InterNetworkV6)
67,266✔
89
            {
31✔
90
                return null;
31✔
91
            }
92

93
            return ToIPAddress(this.InternalBroadcast, this.family);
67,235✔
94
        }
67,266✔
95
    }
96

97
    /// <summary>
98
    /// Gets first usable IPAddress in Network.
99
    /// </summary>
100
    public IPAddress FirstUsable
101
    {
102
        get
103
        {
2,355✔
104
            BigInteger first = this.family == AddressFamily.InterNetworkV6
2,355✔
105
                ? this.InternalNetwork
2,355✔
106
                : (this.Usable <= 1)
2,355✔
107
                    ? this.InternalNetwork
2,355✔
108
                    : this.InternalNetwork + 1;
2,355✔
109
            return ToIPAddress(first, this.family);
2,355✔
110
        }
2,355✔
111
    }
112

113
    /// <summary>
114
    /// Gets last usable IPAddress in Network.
115
    /// </summary>
116
    public IPAddress LastUsable
117
    {
118
        get
119
        {
2,874✔
120
            BigInteger last = this.family == AddressFamily.InterNetworkV6
2,874✔
121
                ? this.InternalBroadcast
2,874✔
122
                : (this.Usable <= 1)
2,874✔
123
                    ? this.InternalNetwork
2,874✔
124
                    : this.InternalBroadcast - 1;
2,874✔
125
            return ToIPAddress(last, this.family);
2,874✔
126
        }
2,874✔
127
    }
128

129
    /// <summary>
130
    /// Gets number of usable IPAddress in Network.
131
    /// 
132
    /// According to : https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#IPv4_CIDR_blocks
133
    /// Address         Mask                        Total         Usable         Network         Broadcast         FirstUsable         LastUsable         Typical use
134
    /// 1.0.0.1/32         255.255.255.255         1         **1**         1.0.0.1         1.0.0.1         1.0.0.1         1.0.0.1         Host route
135
    /// 1.0.0.1/31         255.255.255.254         2         **2**         1.0.0.1         1.0.0.2         1.0.0.1         1.0.0.2         Point-to-point links (RFC 3021)
136
    /// ...
137
    /// </summary>
138
    public BigInteger Usable
139
    {
140
        get
141
        {
2,972✔
142
            if (this.family == AddressFamily.InterNetworkV6)
2,972✔
143
            {
32✔
144
                return this.Total;
32✔
145
            }
146

147
            byte[] mask = [0xff, 0xff, 0xff, 0xff, 0x00];
2,940✔
148
            var bmask = new BigInteger(mask);
2,940✔
149
            BigInteger usableIps = (this.cidr == 32)? 1
2,940✔
150
                : (this.cidr == 31)? 2
2,940✔
151
                : ((bmask >> this.cidr)- 1);
2,940✔
152
            return usableIps;
2,940✔
153
        }
2,972✔
154
    }
155

156
    /// <summary>
157
    /// Gets number of IPAddress in Network.
158
    /// </summary>
159
    public BigInteger Total
160
    {
161
        get
162
        {
2,601✔
163
            int max = this.family == AddressFamily.InterNetwork ? 32 : 128;
2,601✔
164
            var count = BigInteger.Pow(2, max - this.cidr);
2,601✔
165
            return count;
2,601✔
166
        }
2,601✔
167
    }
168

169
    /// <summary>
170
    /// Gets the CIDR netmask notation.
171
    /// </summary>
172
    public byte Cidr
173
    {
174
        get
175
        {
4,208,155✔
176
            return this.cidr;
4,208,155✔
177
        }
4,208,155✔
178
    }
179

180
    /// <summary>
181
    /// Gets the broadcast address calculated from the network address and the netmask.
182
    /// </summary>
183
    internal BigInteger InternalBroadcast
184
    {
185
        get
186
        {
666,435✔
187
            var cached = this.cachedBroadcast;
666,435✔
188
            if (cached != null)
666,435✔
189
            {
531,784✔
190
                return cached.Value;
531,784✔
191
            }
192

193
            lock (this.sync)
134,651✔
194
            {
134,651✔
195
                var cached2 = this.cachedBroadcast;
134,651✔
196
                if (cached2 != null)
134,651✔
197
                {
×
198
                    return cached2.Value;
×
199
                }
200

201
                var network = this.InternalNetwork;
134,651✔
202
                var computed = CreateBroadcast(ref network, this.InternalNetmask, this.family);
134,651✔
203
                this.cachedBroadcast = computed;
134,651✔
204
                return computed;
134,651✔
205
            }
206
        }
666,435✔
207
    }
208

209
    /// <summary>
210
    /// Gets the network address calculated by applying the subnet mask to the IP address.
211
    /// </summary>
212
    internal BigInteger InternalNetwork
213
    {
214
        get
215
        {
13,174,124✔
216
            BigInteger uintNetwork = this.ipaddress & this.InternalNetmask;
13,174,124✔
217
            return uintNetwork;
13,174,124✔
218
        }
13,174,124✔
219
    }
220

221
    /// <summary>
222
    /// Gets the netmask as a BigInteger representation based on the CIDR and address family.
223
    /// </summary>
224
    internal BigInteger InternalNetmask
225
    {
226
        get
227
        {
13,309,344✔
228
            return ToUint(this.cidr, this.family);
13,309,344✔
229
        }
13,309,344✔
230
    }
231
}
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