|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Net; |
|
|
|
//SOURCE: http://blogs.msdn.com/b/knom/archive/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks.aspx |
|
|
|
namespace Knom.Helpers.Net |
|
{ |
|
public static class SubnetMask |
|
{ |
|
public static readonly IPAddress ClassA = IPAddress.Parse("255.0.0.0"); |
|
public static readonly IPAddress ClassB = IPAddress.Parse("255.255.0.0"); |
|
public static readonly IPAddress ClassC = IPAddress.Parse("255.255.255.0"); |
|
|
|
public static IPAddress CreateByHostBitLength(int hostpartLength) |
|
{ |
|
int hostPartLength = hostpartLength; |
|
int netPartLength = 32 - hostPartLength; |
|
|
|
if (netPartLength < 2) |
|
throw new ArgumentException("Number of hosts is to large for IPv4"); |
|
|
|
Byte[] binaryMask = new byte[4]; |
|
|
|
for (int i = 0; i < 4; i++) |
|
{ |
|
if (i * 8 + 8 <= netPartLength) |
|
binaryMask[i] = (byte)255; |
|
else if (i * 8 > netPartLength) |
|
binaryMask[i] = (byte)0; |
|
else |
|
{ |
|
int oneLength = netPartLength - i * 8; |
|
string binaryDigit = |
|
String.Empty.PadLeft(oneLength, '1').PadRight(8, '0'); |
|
binaryMask[i] = Convert.ToByte(binaryDigit, 2); |
|
} |
|
} |
|
return new IPAddress(binaryMask); |
|
} |
|
|
|
public static IPAddress CreateByNetBitLength(int netpartLength) |
|
{ |
|
int hostPartLength = 32 - netpartLength; |
|
return CreateByHostBitLength(hostPartLength); |
|
} |
|
|
|
public static IPAddress CreateByHostNumber(int numberOfHosts) |
|
{ |
|
int maxNumber = numberOfHosts + 1; |
|
|
|
string b = Convert.ToString(maxNumber, 2); |
|
|
|
return CreateByHostBitLength(b.Length); |
|
} |
|
} |
|
|
|
public static class IPAddressExtensions |
|
{ |
|
public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask) |
|
{ |
|
byte[] ipAdressBytes = address.GetAddressBytes(); |
|
byte[] subnetMaskBytes = subnetMask.GetAddressBytes(); |
|
|
|
if (ipAdressBytes.Length != subnetMaskBytes.Length) |
|
throw new ArgumentException("Lengths of IP address and subnet mask do not match."); |
|
|
|
byte[] broadcastAddress = new byte[ipAdressBytes.Length]; |
|
for (int i = 0; i < broadcastAddress.Length; i++) |
|
{ |
|
broadcastAddress[i] = (byte)(ipAdressBytes[i] | (subnetMaskBytes[i] ^ 255)); |
|
} |
|
return new IPAddress(broadcastAddress); |
|
} |
|
|
|
public static IPAddress GetNetworkAddress(this IPAddress address, IPAddress subnetMask) |
|
{ |
|
byte[] ipAdressBytes = address.GetAddressBytes(); |
|
byte[] subnetMaskBytes = subnetMask.GetAddressBytes(); |
|
|
|
if (ipAdressBytes.Length != subnetMaskBytes.Length) |
|
throw new ArgumentException("Lengths of IP address and subnet mask do not match."); |
|
|
|
byte[] broadcastAddress = new byte[ipAdressBytes.Length]; |
|
for (int i = 0; i < broadcastAddress.Length; i++) |
|
{ |
|
broadcastAddress[i] = (byte)(ipAdressBytes[i] & (subnetMaskBytes[i])); |
|
} |
|
return new IPAddress(broadcastAddress); |
|
} |
|
|
|
public static bool IsInSameSubnet(this IPAddress address2, IPAddress address, IPAddress subnetMask) |
|
{ |
|
IPAddress network1 = address.GetNetworkAddress(subnetMask); |
|
IPAddress network2 = address2.GetNetworkAddress(subnetMask); |
|
|
|
return network1.Equals(network2); |
|
} |
|
} |
|
} |