wasSharpNET – Diff between revs 11 and 27
?pathlinks?
Rev 11 | Rev 27 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | using System.Net; |
8 | using System.Net; |
|
9 | |
9 | |
|
10 | namespace wasSharpNET.Network |
10 | namespace wasSharpNET.Network |
|
11 | { |
11 | { |
|
12 | /// <summary> |
12 | /// <summary> |
|
13 | /// Subnet Mask. |
13 | /// Subnet Mask. |
|
14 | /// </summary> |
14 | /// </summary> |
|
15 | /// <remarks>https://blogs.msdn.microsoft.com/knom/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks/</remarks> |
15 | /// <remarks>https://blogs.msdn.microsoft.com/knom/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks/</remarks> |
|
16 | public static class SubnetMask |
16 | public static class SubnetMask |
|
17 | { |
17 | { |
|
18 | public static readonly IPAddress ClassA = IPAddress.Parse("255.0.0.0"); |
18 | public static readonly IPAddress ClassA = IPAddress.Parse("255.0.0.0"); |
|
19 | public static readonly IPAddress ClassB = IPAddress.Parse("255.255.0.0"); |
19 | public static readonly IPAddress ClassB = IPAddress.Parse("255.255.0.0"); |
|
20 | public static readonly IPAddress ClassC = IPAddress.Parse("255.255.255.0"); |
20 | public static readonly IPAddress ClassC = IPAddress.Parse("255.255.255.0"); |
|
21 | |
21 | |
|
22 | public static IPAddress CreateByHostBitLength(int hostpartLength) |
22 | public static IPAddress CreateByHostBitLength(int hostpartLength) |
|
23 | { |
23 | { |
|
24 | var hostPartLength = hostpartLength; |
24 | var hostPartLength = hostpartLength; |
|
25 | var netPartLength = 32 - hostPartLength; |
25 | var netPartLength = 32 - hostPartLength; |
|
26 | |
26 | |
|
27 | if (netPartLength < 2) |
27 | if (netPartLength < 2) |
|
28 | throw new ArgumentException("Number of hosts is to large for IPv4"); |
28 | throw new ArgumentException("Number of hosts is to large for IPv4"); |
|
29 | |
29 | |
|
30 | var binaryMask = new byte[4]; |
30 | var binaryMask = new byte[4]; |
|
31 | |
31 | |
|
32 | for (var i = 0; i < 4; i++) |
32 | for (var i = 0; i < 4; i++) |
|
33 | { |
- | ||
34 | if (i * 8 + 8 <= netPartLength) |
33 | if (i * 8 + 8 <= netPartLength) |
|
- | 34 | { |
||
35 | binaryMask[i] = 255; |
35 | binaryMask[i] = 255; |
|
- | 36 | } |
||
36 | else if (i * 8 > netPartLength) |
37 | else if (i * 8 > netPartLength) |
|
- | 38 | { |
||
37 | binaryMask[i] = 0; |
39 | binaryMask[i] = 0; |
|
- | 40 | } |
||
38 | else |
41 | else |
|
39 | { |
42 | { |
|
40 | var oneLength = netPartLength - i * 8; |
43 | var oneLength = netPartLength - i * 8; |
|
41 | var binaryDigit = |
44 | var binaryDigit = |
|
42 | string.Empty.PadLeft(oneLength, '1').PadRight(8, '0'); |
45 | string.Empty.PadLeft(oneLength, '1').PadRight(8, '0'); |
|
43 | binaryMask[i] = Convert.ToByte(binaryDigit, 2); |
46 | binaryMask[i] = Convert.ToByte(binaryDigit, 2); |
|
44 | } |
47 | } |
|
45 | } |
- | ||
46 | return new IPAddress(binaryMask); |
48 | return new IPAddress(binaryMask); |
|
47 | } |
49 | } |
|
48 | |
50 | |
|
49 | public static IPAddress CreateByNetBitLength(int netpartLength) |
51 | public static IPAddress CreateByNetBitLength(int netpartLength) |
|
50 | { |
52 | { |
|
51 | var hostPartLength = 32 - netpartLength; |
53 | var hostPartLength = 32 - netpartLength; |
|
52 | return CreateByHostBitLength(hostPartLength); |
54 | return CreateByHostBitLength(hostPartLength); |
|
53 | } |
55 | } |
|
54 | |
56 | |
|
55 | public static IPAddress CreateByHostNumber(int numberOfHosts) |
57 | public static IPAddress CreateByHostNumber(int numberOfHosts) |
|
56 | { |
58 | { |
|
57 | var maxNumber = numberOfHosts + 1; |
59 | var maxNumber = numberOfHosts + 1; |
|
58 | |
60 | |
|
59 | var b = Convert.ToString(maxNumber, 2); |
61 | var b = Convert.ToString(maxNumber, 2); |
|
60 | |
62 | |
|
61 | return CreateByHostBitLength(b.Length); |
63 | return CreateByHostBitLength(b.Length); |
|
62 | } |
64 | } |
|
63 | } |
65 | } |
|
64 | } |
66 | } |
|
65 | |
67 | |
|
- | 68 |
|
||
- | 69 | |
||
- | 70 | |
||
- | 71 | |