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

lduchosal / ipnetwork / 960

15 May 2026 07:18AM UTC coverage: 0.0% (-94.1%) from 94.053%
960

Pull #411

appveyor

web-flow
Merge ef6b054ce into 4cec191ae
Pull Request #411: Bump MSTest.TestFramework from 4.2.2 to 4.2.3

0 of 2438 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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 ();
×
17
    private readonly Lazy<int> cachedHashCode;
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
        {
×
32
            return this.ToString();
×
33
        }
×
34

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

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

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

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

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

93
            return ToIPAddress(this.InternalBroadcast, this.family);
×
94
        }
×
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
        {
×
105
            BigInteger first = this.InternalNetwork;
×
106
            if (this.family == AddressFamily.InterNetwork
×
107
                && this.cidr < 31)
×
108
            {
×
109
                first += 1;
×
110
            }
×
111

112
            return ToIPAddress(first, this.family);
×
113
        }
×
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
        {
×
124
            BigInteger last = this.InternalBroadcast;
×
125
            if (this.family == AddressFamily.InterNetwork
×
126
                && this.cidr < 31)
×
127
            {
×
128
                last -= 1;
×
129
            }
×
130

131
            return ToIPAddress(last, this.family);
×
132
        }
×
133
    }
134
    
135
    /// <summary>
136
    /// Gets first IPAddress in Network.
137
    /// </summary>
138
    public IPAddress First
139
    {
140
        get
141
        {
×
142
            BigInteger first = this.InternalNetwork;
×
143
            return ToIPAddress(first, this.family);
×
144
        }
×
145
    }
146
    
147
    /// <summary>
148
    /// Gets last IPAddress in Network.
149
    /// </summary>
150
    public IPAddress Last
151
    {
152
        get
153
        {
×
154
            BigInteger last = this.InternalBroadcast;
×
155
            return ToIPAddress(last, this.family);
×
156
        }
×
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
        {
×
172
            if (this.family == AddressFamily.InterNetworkV6)
×
173
            {
×
174
                return this.Total;
×
175
            }
176

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

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

209
    /// <summary>
210
    /// Gets the CIDR netmask notation.
211
    /// </summary>
212
    public byte Cidr
213
    {
214
        get
215
        {
×
216
            return this.cidr;
×
217
        }
×
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
        {
×
227
            var cached = this.cachedBroadcast;
×
228
            if (cached != null)
×
229
            {
×
230
                return cached.Value;
×
231
            }
232

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

241
                var network = this.InternalNetwork;
×
242
                var computed = CreateBroadcast(ref network, this.InternalNetmask, this.family);
×
243
                this.cachedBroadcast = computed;
×
244
                return computed;
×
245
            }
246
        }
×
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
        {
×
256
            BigInteger uintNetwork = this.ipaddress & this.InternalNetmask;
×
257
            return uintNetwork;
×
258
        }
×
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
        {
×
268
            return ToUint(this.cidr, this.family);
×
269
        }
×
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