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

lduchosal / ipnetwork / 809

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

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

97.94
/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,798✔
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,350✔
55
            return ToIPAddress(this.InternalNetwork, this.family);
4,137,350✔
56
        }
4,137,350✔
57
    }
58

59
    /// <summary>
60
    /// Gets address Family.
61
    /// </summary>
62
    public AddressFamily AddressFamily
63
    {
64
        get
65
        {
135,835✔
66
            return this.family;
135,835✔
67
        }
135,835✔
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,259✔
88
            if (this.family == AddressFamily.InterNetworkV6)
67,259✔
89
            {
31✔
90
                return null;
31✔
91
            }
92

93
            return ToIPAddress(this.InternalBroadcast, this.family);
67,228✔
94
        }
67,259✔
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 number of usable IPAddress in Network.
133
    /// 
134
    /// According to : https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#IPv4_CIDR_blocks
135
    /// Address         Mask                        Total         Usable         Network         Broadcast         FirstUsable         LastUsable         Typical use
136
    /// 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
137
    /// 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)
138
    /// ...
139
    /// </summary>
140
    public BigInteger Usable
141
    {
142
        get
143
        {
2,972✔
144
            if (this.family == AddressFamily.InterNetworkV6)
2,972✔
145
            {
32✔
146
                return this.Total;
32✔
147
            }
148

149
            byte[] mask = [0xff, 0xff, 0xff, 0xff, 0x00];
2,940✔
150
            var bmask = new BigInteger(mask);
2,940✔
151
            BigInteger usableIps;
152
            if (this.cidr == 32)
2,940✔
153
            {
2,073✔
154
                usableIps = 1;
2,073✔
155
            }
2,073✔
156
            else if (this.cidr == 31)
867✔
157
            {
1✔
158
                usableIps = 2;
1✔
159
            }
1✔
160
            else
161
            {
866✔
162
                usableIps = ((bmask >> this.cidr)- 1);
866✔
163
            }
866✔
164
            return usableIps;
2,940✔
165
        }
2,972✔
166
    }
167

168
    /// <summary>
169
    /// Gets number of IPAddress in Network.
170
    /// </summary>
171
    public BigInteger Total
172
    {
173
        get
174
        {
1,628✔
175
            int max = this.family == AddressFamily.InterNetwork ? 32 : 128;
1,628✔
176
            var count = BigInteger.Pow(2, max - this.cidr);
1,628✔
177
            return count;
1,628✔
178
        }
1,628✔
179
    }
180

181
    /// <summary>
182
    /// Gets the CIDR netmask notation.
183
    /// </summary>
184
    public byte Cidr
185
    {
186
        get
187
        {
4,206,178✔
188
            return this.cidr;
4,206,178✔
189
        }
4,206,178✔
190
    }
191

192
    /// <summary>
193
    /// Gets the broadcast address calculated from the network address and the netmask.
194
    /// </summary>
195
    internal BigInteger InternalBroadcast
196
    {
197
        get
198
        {
666,529✔
199
            var cached = this.cachedBroadcast;
666,529✔
200
            if (cached != null)
666,529✔
201
            {
531,855✔
202
                return cached.Value;
531,855✔
203
            }
204

205
            lock (this.sync)
134,674✔
206
            {
134,674✔
207
                var cached2 = this.cachedBroadcast;
134,674✔
208
                if (cached2 != null)
134,674✔
209
                {
×
210
                    return cached2.Value;
×
211
                }
212

213
                var network = this.InternalNetwork;
134,674✔
214
                var computed = CreateBroadcast(ref network, this.InternalNetmask, this.family);
134,674✔
215
                this.cachedBroadcast = computed;
134,674✔
216
                return computed;
134,674✔
217
            }
218
        }
666,529✔
219
    }
220

221
    /// <summary>
222
    /// Gets the network address calculated by applying the subnet mask to the IP address.
223
    /// </summary>
224
    internal BigInteger InternalNetwork
225
    {
226
        get
227
        {
13,170,574✔
228
            BigInteger uintNetwork = this.ipaddress & this.InternalNetmask;
13,170,574✔
229
            return uintNetwork;
13,170,574✔
230
        }
13,170,574✔
231
    }
232

233
    /// <summary>
234
    /// Gets the netmask as a BigInteger representation based on the CIDR and address family.
235
    /// </summary>
236
    internal BigInteger InternalNetmask
237
    {
238
        get
239
        {
13,305,817✔
240
            return ToUint(this.cidr, this.family);
13,305,817✔
241
        }
13,305,817✔
242
    }
243
}
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