wasSharpNET – Diff between revs 5 and 11

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 5 Rev 11
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 { 33 {
34 if (i*8 + 8 <= netPartLength) 34 if (i * 8 + 8 <= netPartLength)
35 binaryMask[i] = 255; 35 binaryMask[i] = 255;
36 else if (i*8 > netPartLength) 36 else if (i * 8 > netPartLength)
37 binaryMask[i] = 0; 37 binaryMask[i] = 0;
38 else 38 else
39 { 39 {
40 var oneLength = netPartLength - i*8; 40 var oneLength = netPartLength - i * 8;
41 var binaryDigit = 41 var binaryDigit =
42 string.Empty.PadLeft(oneLength, '1').PadRight(8, '0'); 42 string.Empty.PadLeft(oneLength, '1').PadRight(8, '0');
43 binaryMask[i] = Convert.ToByte(binaryDigit, 2); 43 binaryMask[i] = Convert.ToByte(binaryDigit, 2);
44 } 44 }
45 } 45 }
46 return new IPAddress(binaryMask); 46 return new IPAddress(binaryMask);
47 } 47 }
48   48  
49 public static IPAddress CreateByNetBitLength(int netpartLength) 49 public static IPAddress CreateByNetBitLength(int netpartLength)
50 { 50 {
51 var hostPartLength = 32 - netpartLength; 51 var hostPartLength = 32 - netpartLength;
52 return CreateByHostBitLength(hostPartLength); 52 return CreateByHostBitLength(hostPartLength);
53 } 53 }
54   54  
55 public static IPAddress CreateByHostNumber(int numberOfHosts) 55 public static IPAddress CreateByHostNumber(int numberOfHosts)
56 { 56 {
57 var maxNumber = numberOfHosts + 1; 57 var maxNumber = numberOfHosts + 1;
58   58  
59 var b = Convert.ToString(maxNumber, 2); 59 var b = Convert.ToString(maxNumber, 2);
60   60  
61 return CreateByHostBitLength(b.Length); 61 return CreateByHostBitLength(b.Length);
62 } 62 }
63 } 63 }
64 } -  
65   64 }
-   65  
66
Generated by GNU Enscript 1.6.5.90.
-  
67   -  
68   -  
69   -