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

lduchosal / ipnetwork / 853

29 Jan 2026 01:30PM UTC coverage: 93.741%. Remained the same
853

push

appveyor

web-flow
fix/369-firstusable-lastusable-31-network (#370)

* fix: ipnetwork / first last usable / inverted issue

1962 of 2093 relevant lines covered (93.74%)

669469.56 hits per line

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

98.1
/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,135,990✔
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 a 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,137,489✔
55
            return ToIPAddress(this.InternalNetwork, this.family);
4,137,489✔
56
        }
4,137,489✔
57
    }
58

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

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

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

93
            return ToIPAddress(this.InternalBroadcast, this.family);
67,231✔
94
        }
67,262✔
95
    }
96
    
97
    /// <summary>
98
    /// Gets first usable IPAddress in Network.
99
    /// Per RFC 3021, /31 networks have no reserved network address.
100
    /// </summary>
101
    public IPAddress FirstUsable
102
    {
103
        get
104
        {
1,360✔
105
            BigInteger first = this.InternalNetwork;
1,360✔
106
            if (this.family == AddressFamily.InterNetwork
1,360✔
107
                && this.cidr < 31)
1,360✔
108
            {
288✔
109
                first += 1;
288✔
110
            }
288✔
111

112
            return ToIPAddress(first, this.family);
1,360✔
113
        }
1,360✔
114
    }
115

116
    /// <summary>
117
    /// Gets last usable IPAddress in Network.
118
    /// Per RFC 3021, /31 networks have no reserved broadcast address.
119
    /// </summary>
120
    public IPAddress LastUsable
121
    {
122
        get
123
        {
1,879✔
124
            BigInteger last = this.InternalBroadcast;
1,879✔
125
            if (this.family == AddressFamily.InterNetwork
1,879✔
126
                && this.cidr < 31)
1,879✔
127
            {
288✔
128
                last -= 1;
288✔
129
            }
288✔
130

131
            return ToIPAddress(last, this.family);
1,879✔
132
        }
1,879✔
133
    }
134
    
135
    /// <summary>
136
    /// Gets first IPAddress in Network.
137
    /// </summary>
138
    public IPAddress First
139
    {
140
        get
141
        {
39✔
142
            BigInteger first = this.InternalNetwork;
39✔
143
            return ToIPAddress(first, this.family);
39✔
144
        }
39✔
145
    }
146
    
147
    /// <summary>
148
    /// Gets last IPAddress in Network.
149
    /// </summary>
150
    public IPAddress Last
151
    {
152
        get
153
        {
39✔
154
            BigInteger last = this.InternalBroadcast;
39✔
155
            return ToIPAddress(last, this.family);
39✔
156
        }
39✔
157
    }
158
    
159
    /// <summary>
160
    /// Gets number of usable IPAddress in Network.
161
    /// 
162
    /// According to : https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#IPv4_CIDR_blocks
163
    /// Address         Mask                        Total         Usable         Network         Broadcast         FirstUsable         LastUsable         Typical use
164
    /// 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
165
    /// 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)
166
    /// ...
167
    /// </summary>
168
    public BigInteger Usable
169
    {
170
        get
171
        {
340✔
172
            if (this.family == AddressFamily.InterNetworkV6)
340✔
173
            {
32✔
174
                return this.Total;
32✔
175
            }
176

177
            byte[] mask = [0xff, 0xff, 0xff, 0xff, 0x00];
308✔
178
            var bmask = new BigInteger(mask);
308✔
179
            BigInteger usableIps;
180
            if (this.cidr == 32)
308✔
181
            {
12✔
182
                usableIps = 1;
12✔
183
            }
12✔
184
            else if (this.cidr == 31)
296✔
185
            {
3✔
186
                usableIps = 2;
3✔
187
            }
3✔
188
            else
189
            {
293✔
190
                usableIps = ((bmask >> this.cidr)- 1);
293✔
191
            }
293✔
192
            return usableIps;
308✔
193
        }
340✔
194
    }
195

196
    /// <summary>
197
    /// Gets number of IPAddress in Network.
198
    /// </summary>
199
    public BigInteger Total
200
    {
201
        get
202
        {
1,628✔
203
            int max = this.family == AddressFamily.InterNetwork ? 32 : 128;
1,628✔
204
            var count = BigInteger.Pow(2, max - this.cidr);
1,628✔
205
            return count;
1,628✔
206
        }
1,628✔
207
    }
208

209
    /// <summary>
210
    /// Gets the CIDR netmask notation.
211
    /// </summary>
212
    public byte Cidr
213
    {
214
        get
215
        {
4,206,334✔
216
            return this.cidr;
4,206,334✔
217
        }
4,206,334✔
218
    }
219

220
    /// <summary>
221
    /// Gets the broadcast address calculated from the network address and the netmask.
222
    /// </summary>
223
    internal BigInteger InternalBroadcast
224
    {
225
        get
226
        {
666,589✔
227
            var cached = this.cachedBroadcast;
666,589✔
228
            if (cached != null)
666,589✔
229
            {
531,862✔
230
                return cached.Value;
531,862✔
231
            }
232

233
            lock (this.sync)
134,727✔
234
            {
134,727✔
235
                var cached2 = this.cachedBroadcast;
134,727✔
236
                if (cached2 != null)
134,727✔
237
                {
×
238
                    return cached2.Value;
×
239
                }
240

241
                var network = this.InternalNetwork;
134,727✔
242
                var computed = CreateBroadcast(ref network, this.InternalNetmask, this.family);
134,727✔
243
                this.cachedBroadcast = computed;
134,727✔
244
                return computed;
134,727✔
245
            }
246
        }
666,589✔
247
    }
248

249
    /// <summary>
250
    /// Gets the network address calculated by applying the subnet mask to the IP address.
251
    /// </summary>
252
    internal BigInteger InternalNetwork
253
    {
254
        get
255
        {
13,171,028✔
256
            BigInteger uintNetwork = this.ipaddress & this.InternalNetmask;
13,171,028✔
257
            return uintNetwork;
13,171,028✔
258
        }
13,171,028✔
259
    }
260

261
    /// <summary>
262
    /// Gets the netmask as a BigInteger representation based on the CIDR and address family.
263
    /// </summary>
264
    internal BigInteger InternalNetmask
265
    {
266
        get
267
        {
13,306,325✔
268
            return ToUint(this.cidr, this.family);
13,306,325✔
269
        }
13,306,325✔
270
    }
271
}
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