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

lduchosal / ipnetwork / 752

05 Apr 2025 05:56AM UTC coverage: 93.417% (-0.2%) from 93.641%
752

push

appveyor

web-flow
Update README.md

1561 of 1671 relevant lines covered (93.42%)

825349.07 hits per line

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

95.29
/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,430✔
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,135,887✔
55
            return ToIPAddress(this.InternalNetwork, this.family);
4,135,887✔
56
        }
4,135,887✔
57
    }
58

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

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

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

93
            return ToIPAddress(this.InternalBroadcast, this.family);
66,779✔
94
        }
66,780✔
95
    }
96

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

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

129
    /// <summary>
130
    /// Gets number of usable IP adress in Network.
131
    /// </summary>
132
    public BigInteger Usable
133
    {
134
        get
135
        {
2,940✔
136
            if (this.family == AddressFamily.InterNetworkV6)
2,940✔
137
            {
×
138
                return this.Total;
×
139
            }
140

141
            byte[] mask = [0xff, 0xff, 0xff, 0xff, 0x00];
2,940✔
142
            var bmask = new BigInteger(mask);
2,940✔
143
            BigInteger usableIps = (this.cidr > 30) ? 0 : ((bmask >> this.cidr) - 1);
2,940✔
144
            return usableIps;
2,940✔
145
        }
2,940✔
146
    }
147

148
    /// <summary>
149
    /// Gets number of IP adress in Network.
150
    /// </summary>
151
    public BigInteger Total
152
    {
153
        get
154
        {
2,562✔
155
            int max = this.family == AddressFamily.InterNetwork ? 32 : 128;
2,562✔
156
            var count = BigInteger.Pow(2, max - this.cidr);
2,562✔
157
            return count;
2,562✔
158
        }
2,562✔
159
    }
160

161
    /// <summary>
162
    /// Gets the CIDR netmask notation.
163
    /// </summary>
164
    public byte Cidr
165
    {
166
        get
167
        {
4,204,623✔
168
            return this.cidr;
4,204,623✔
169
        }
4,204,623✔
170
    }
171

172
    /// <summary>
173
    /// Gets the broadcast address calculated from the network address and the netmask.
174
    /// </summary>
175
    internal BigInteger InternalBroadcast
176
    {
177
        get
178
        {
662,960✔
179
            var cached = this.cachedBroadcast;
662,960✔
180
            if (cached != null)
662,960✔
181
            {
529,222✔
182
                return cached.Value;
529,222✔
183
            }
184

185
            lock (this.sync)
133,738✔
186
            {
133,738✔
187
                var cached2 = this.cachedBroadcast;
133,738✔
188
                if (cached2 != null)
133,738✔
189
                {
×
190
                    return cached2.Value;
×
191
                }
192

193
                var network = this.InternalNetwork;
133,738✔
194
                var computed = CreateBroadcast(ref network, this.InternalNetmask, this.family);
133,738✔
195
                this.cachedBroadcast = computed;
133,738✔
196
                return computed;
133,738✔
197
            }
198
        }
662,960✔
199
    }
200

201
    /// <summary>
202
    /// Gets the network address calculated by applying the subnet mask to the IP address.
203
    /// </summary>
204
    internal BigInteger InternalNetwork
205
    {
206
        get
207
        {
13,166,975✔
208
            BigInteger uintNetwork = this.ipaddress & this.InternalNetmask;
13,166,975✔
209
            return uintNetwork;
13,166,975✔
210
        }
13,166,975✔
211
    }
212

213
    /// <summary>
214
    /// Gets the netmask as a BigInteger representation based on the CIDR and address family.
215
    /// </summary>
216
    internal BigInteger InternalNetmask
217
    {
218
        get
219
        {
13,301,252✔
220
            return ToUint(this.cidr, this.family);
13,301,252✔
221
        }
13,301,252✔
222
    }
223
}
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