-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
133 lines (116 loc) · 4.06 KB
/
Utils.cs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Collections;
namespace Dusty.Net
{
public static class Utils
{
public static bool IsInSameSubnet(SubnetIpAddress reference, IPAddress comparison)
{
if (reference.AddressFamily != comparison.AddressFamily)
{
throw new ArgumentException("reference and comparison IP addresses are not of same address family");
}
byte[] refBytes = reference.GetAddressBytes();
byte[] compBytes = comparison.GetAddressBytes();
//TODO
return true;
}
public static bool IsInSameSubnet(IPAddress reference, IPAddress comparison, SubnetMask subnetMask)
{
return IsInSameSubnet(new SubnetIpAddress(reference, subnetMask), comparison);
}
public static byte[] GetBytes(BitArray bits)
{
if (bits.Length % 8 != 0)
{
throw new ArgumentException("Bit array length is not an integer number of bytes");
}
int length = bits.Length / 8;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++)
{
int b = 0;
for (int j = 0; j < length; j++)
{
b = b & (bits[i*8 + j] ? 1 : 0);
b = b << 1;
}
bytes[i] = (byte)b;
}
return bytes;
}
public static BitArray GetNetworkBits(BitArray bits, int networkPrefixLength)
{
BitArray outbits = new BitArray(bits.Length);
for (int i = 0; i < networkPrefixLength; i++)
{
outbits[i] = bits[i];
}
for (int i = networkPrefixLength; i < bits.Length; i++)
{
outbits[i] = false;
}
return outbits;
}
public static BitArray GetHostBits(BitArray bits, int networkPrefixLength)
{
BitArray outbits = new BitArray(bits.Length);
for (int i = 0; i < networkPrefixLength; i++)
{
outbits[i] = false;
}
for (int i = networkPrefixLength; i < bits.Length; i++)
{
outbits[i] = bits[i];
}
return outbits;
}
public static bool CompareBits(BitArray reference, BitArray comparison)
{
if (reference.Length != comparison.Length) { return false; }
for (int i = 0; i < reference.Length; i++)
{
if (reference[i] != comparison[i]) { return false; }
}
return true;
}
public static bool CompareNetworkBits(BitArray reference, BitArray comparison, int networkPrefixLength)
{
if (reference.Length != comparison.Length) { return false; }
for (int i = 0; i < networkPrefixLength; i++)
{
if (reference[i] != comparison[i]) { return false; }
}
return true;
}
public static bool CompareHostBits(BitArray reference, BitArray comparison, int networkPrefixLength)
{
if (reference.Length != comparison.Length) { return false; }
for (int i = networkPrefixLength; i < reference.Length; i++)
{
if (reference[i] != comparison[i]) { return false; }
}
return true;
}
public static BitArray GetBits(byte[] bytes)
{
int length = bytes.Length * 8;
BitArray bitArray = new BitArray(length);
for (int i = 0; i < bytes.Length; i++)
{
int bitpos = 0x80;
for (int j = 0; j < 8; j++)
{
bitArray[i * 8 + j] = (bytes[i] & bitpos) != 0;
bitpos = bitpos >> 1;
}
}
return bitArray;
}
}
}