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

lduchosal / ipnetwork / 825

17 Aug 2025 07:49PM UTC coverage: 93.655% (-0.04%) from 93.698%
825

Pull #365

appveyor

web-flow
Merge ba0929df1 into e4829c18d
Pull Request #365: feat/operator-add-int

1963 of 2096 relevant lines covered (93.65%)

668522.94 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,984✔
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,137,486✔
55
            return ToIPAddress(this.InternalNetwork, this.family);
4,137,486✔
56
        }
4,137,486✔
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
        {
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,260✔
88
            if (this.family == AddressFamily.InterNetworkV6)
67,260✔
89
            {
31✔
90
                return null;
31✔
91
            }
92

93
            return ToIPAddress(this.InternalBroadcast, this.family);
67,229✔
94
        }
67,260✔
95
    }
96
    
97
    /// <summary>
98
    /// Gets first usable IPAddress in Network.
99
    /// </summary>
100
    public IPAddress FirstUsable
101
    {
102
        get
103
        {
1,355✔
104
            BigInteger first = this.InternalNetwork;
1,355✔
105
            if (this.family == AddressFamily.InterNetwork
1,355✔
106
                && this.Usable > 1)
1,355✔
107
            {
287✔
108
                first+= 1;
287✔
109
            }
287✔
110
            return ToIPAddress(first, this.family);
1,355✔
111
        }
1,355✔
112
    }
113

114
    /// <summary>
115
    /// Gets last usable IPAddress in Network.
116
    /// </summary>
117
    public IPAddress LastUsable
118
    {
119
        get
120
        {
1,874✔
121
            BigInteger last = this.InternalBroadcast;
1,874✔
122
            if (this.family == AddressFamily.InterNetwork
1,874✔
123
                && this.Usable > 1)
1,874✔
124
            {
287✔
125
                last -= 1;
287✔
126
            }
287✔
127
            return ToIPAddress(last, this.family);
1,874✔
128
        }
1,874✔
129
    }
130
    
131
    /// <summary>
132
    /// Gets first IPAddress in Network.
133
    /// </summary>
134
    public IPAddress First
135
    {
136
        get
137
        {
38✔
138
            BigInteger first = this.InternalNetwork;
38✔
139
            return ToIPAddress(first, this.family);
38✔
140
        }
38✔
141
    }
142
    
143
    /// <summary>
144
    /// Gets last IPAddress in Network.
145
    /// </summary>
146
    public IPAddress Last
147
    {
148
        get
149
        {
38✔
150
            BigInteger last = this.InternalBroadcast;
38✔
151
            return ToIPAddress(last, this.family);
38✔
152
        }
38✔
153
    }
154
    
155
    /// <summary>
156
    /// Gets number of usable IPAddress in Network.
157
    /// 
158
    /// According to : https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#IPv4_CIDR_blocks
159
    /// Address         Mask                        Total         Usable         Network         Broadcast         FirstUsable         LastUsable         Typical use
160
    /// 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
161
    /// 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)
162
    /// ...
163
    /// </summary>
164
    public BigInteger Usable
165
    {
166
        get
167
        {
2,972✔
168
            if (this.family == AddressFamily.InterNetworkV6)
2,972✔
169
            {
32✔
170
                return this.Total;
32✔
171
            }
172

173
            byte[] mask = [0xff, 0xff, 0xff, 0xff, 0x00];
2,940✔
174
            var bmask = new BigInteger(mask);
2,940✔
175
            BigInteger usableIps;
176
            if (this.cidr == 32)
2,940✔
177
            {
2,073✔
178
                usableIps = 1;
2,073✔
179
            }
2,073✔
180
            else if (this.cidr == 31)
867✔
181
            {
1✔
182
                usableIps = 2;
1✔
183
            }
1✔
184
            else
185
            {
866✔
186
                usableIps = ((bmask >> this.cidr)- 1);
866✔
187
            }
866✔
188
            return usableIps;
2,940✔
189
        }
2,972✔
190
    }
191

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

205
    /// <summary>
206
    /// Gets the CIDR netmask notation.
207
    /// </summary>
208
    public byte Cidr
209
    {
210
        get
211
        {
4,206,314✔
212
            return this.cidr;
4,206,314✔
213
        }
4,206,314✔
214
    }
215

216
    /// <summary>
217
    /// Gets the broadcast address calculated from the network address and the netmask.
218
    /// </summary>
219
    internal BigInteger InternalBroadcast
220
    {
221
        get
222
        {
666,581✔
223
            var cached = this.cachedBroadcast;
666,581✔
224
            if (cached != null)
666,581✔
225
            {
531,858✔
226
                return cached.Value;
531,858✔
227
            }
228

229
            lock (this.sync)
134,723✔
230
            {
134,723✔
231
                var cached2 = this.cachedBroadcast;
134,723✔
232
                if (cached2 != null)
134,723✔
233
                {
×
234
                    return cached2.Value;
×
235
                }
236

237
                var network = this.InternalNetwork;
134,723✔
238
                var computed = CreateBroadcast(ref network, this.InternalNetmask, this.family);
134,723✔
239
                this.cachedBroadcast = computed;
134,723✔
240
                return computed;
134,723✔
241
            }
242
        }
666,581✔
243
    }
244

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

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