-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathIPNetwork2Contains.cs
More file actions
78 lines (64 loc) · 2.28 KB
/
IPNetwork2Contains.cs
File metadata and controls
78 lines (64 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// <copyright file="IPNetwork2contains.cs" company="IPNetwork">
// Copyright (c) IPNetwork. All rights reserved.
// </copyright>
namespace System.Net;
using System.Net.Sockets;
using System.Numerics;
/// <summary>
/// Contains.
/// </summary>
public sealed partial class IPNetwork2
{
/// <summary>
/// return true if ipaddress is contained in network.
/// </summary>
/// <param name="contains">A string containing an ip address to convert.</param>
/// <returns>true if ipaddress is contained into the IP Network; otherwise, false.</returns>
[CLSCompliant(false)]
public bool Contains(IPAddress contains)
{
if (contains == null)
{
throw new ArgumentNullException(nameof(contains));
}
if (this.AddressFamily != contains.AddressFamily)
{
return false;
}
BigInteger uintNetwork = this.InternalNetwork;
BigInteger
uintBroadcast = this.InternalBroadcast;
var uintAddress = ToBigInteger(contains);
bool result = uintAddress >= uintNetwork
&& uintAddress <= uintBroadcast;
return result;
}
/// <summary>
/// return true is network2 is fully contained in network.
/// </summary>
/// <param name="contains">The network to test.</param>
/// <returns>It returns the boolean value. If network2 is in IPNetwork then it returns True, otherwise returns False.</returns>
public bool Contains(IPNetwork2 contains)
{
if (contains == null)
{
throw new ArgumentNullException(nameof(contains));
}
BigInteger uintNetwork = this.InternalNetwork;
BigInteger
uintBroadcast = this.InternalBroadcast;
BigInteger uintFirst = contains.InternalNetwork;
BigInteger
uintLast = contains
.InternalBroadcast;
bool result = uintFirst >= uintNetwork
&& uintLast <= uintBroadcast;
return result;
}
private static BigInteger CreateBroadcast(ref BigInteger network, BigInteger netmask, AddressFamily family)
{
int width = family == AddressFamily.InterNetwork ? 4 : 16;
BigInteger uintBroadcast = network + netmask.PositiveReverse(width);
return uintBroadcast;
}
}