Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. public static class SubnetMask
  2. {
  3. public static readonly IPAddress ClassA = IPAddress.Parse("255.0.0.0");
  4. public static readonly IPAddress ClassB = IPAddress.Parse("255.255.0.0");
  5. public static readonly IPAddress ClassC = IPAddress.Parse("255.255.255.0");
  6.  
  7. public static IPAddress CreateByHostBitLength(int hostpartLength)
  8. {
  9. int hostPartLength = hostpartLength;
  10. int netPartLength = 32 - hostPartLength;
  11.  
  12. if (netPartLength < 2)
  13. throw new ArgumentException("Number of hosts is to large for IPv4");
  14.  
  15. Byte[] binaryMask = new byte[4];
  16.  
  17. for (int i = 0; i < 4; i++)
  18. {
  19. if (i * 8 + 8 <= netPartLength)
  20. binaryMask[i] = (byte)255;
  21. else if (i * 8 > netPartLength)
  22. binaryMask[i] = (byte)0;
  23. else
  24. {
  25. int oneLength = netPartLength - i * 8;
  26. string binaryDigit =
  27. String.Empty.PadLeft(oneLength, '1').PadRight(8, '0');
  28. binaryMask[i] = Convert.ToByte(binaryDigit, 2);
  29. }
  30. }
  31. return new IPAddress(binaryMask);
  32. }
  33.  
  34. public static IPAddress CreateByNetBitLength(int netpartLength)
  35. {
  36. int hostPartLength = 32 - netpartLength;
  37. return CreateByHostBitLength(hostPartLength);
  38. }
  39.  
  40. public static IPAddress CreateByHostNumber(int numberOfHosts)
  41. {
  42. int maxNumber = numberOfHosts + 1;
  43.  
  44. string b = Convert.ToString(maxNumber, 2);
  45.  
  46. return CreateByHostBitLength(b.Length);
  47. }
  48. }
  49.  
  50. public static class IPAddressExtensions
  51. {
  52. public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask)
  53. {
  54. byte[] ipAdressBytes = address.GetAddressBytes();
  55. byte[] subnetMaskBytes = subnetMask.GetAddressBytes();
  56.  
  57. if (ipAdressBytes.Length != subnetMaskBytes.Length)
  58. throw new ArgumentException("Lengths of IP address and subnet mask do not match.");
  59.  
  60. byte[] broadcastAddress = new byte[ipAdressBytes.Length];
  61. for (int i = 0; i < broadcastAddress.Length; i++)
  62. {
  63. broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255));
  64. }
  65. return new IPAddress(broadcastAddress);
  66. }
  67.  
  68. public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask)
  69. {
  70. byte[] ipAdressBytes = address.GetAddressBytes();
  71. byte[] subnetMaskBytes = subnetMask.GetAddressBytes();
  72.  
  73. if (ipAdressBytes.Length != subnetMaskBytes.Length)
  74. throw new ArgumentException("Lengths of IP address and subnet mask do not match.");
  75.  
  76. byte[] broadcastAddress = new byte[ipAdressBytes.Length];
  77. for (int i = 0; i < broadcastAddress.Length; i++)
  78. {
  79. broadcastAddress[i] = (byte)(ipAdressBytes[i] & (subnetMaskBytes[i]));
  80. }
  81. return new IPAddress(broadcastAddress);
  82. }
  83.  
  84. public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask)
  85. {
  86. IPAddress network1 = address.GetNetworkAddress(subnetMask);
  87. IPAddress network2 = address2.GetNetworkAddress(subnetMask);
  88.  
  89. return network1.Equals(network2);
  90. }
  91.  
  92. }
  93.  
  94. private void Form1_Load(object sender, EventArgs e)
  95. {
  96. string hostName = Dns.GetHostName();
  97. string myIP = Dns.GetHostByName(hostName).AddressList[0].ToString();
  98. lblip.Text = myIP;
  99.  
  100. lblmac.Text = DeviceInfo.GetMacAddress()[0].ToString();
  101. **lblmask.Text = SubnetMask.CreateByHostBitLength();**esta es la que no sale
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement