/Network/IPAddressExtensions.cs |
@@ -0,0 +1,58 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.Net; |
|
namespace wasSharpNET.Network |
{ |
/// <summary> |
/// IPAddress extensions. |
/// </summary> |
/// <remarks>https://blogs.msdn.microsoft.com/knom/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks/</remarks> |
public static class IPAddressExtensions |
{ |
public static IPAddress GetBroadcastAddress(this IPAddress address, IPAddress subnetMask) |
{ |
var ipAdressBytes = address.GetAddressBytes(); |
var subnetMaskBytes = subnetMask.GetAddressBytes(); |
|
if (ipAdressBytes.Length != subnetMaskBytes.Length) |
throw new ArgumentException("Lengths of IP address and subnet mask do not match."); |
|
var broadcastAddress = new byte[ipAdressBytes.Length]; |
for (var 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) |
{ |
var ipAdressBytes = address.GetAddressBytes(); |
var subnetMaskBytes = subnetMask.GetAddressBytes(); |
|
if (ipAdressBytes.Length != subnetMaskBytes.Length) |
throw new ArgumentException("Lengths of IP address and subnet mask do not match."); |
|
var broadcastAddress = new byte[ipAdressBytes.Length]; |
for (var 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) |
{ |
var network1 = address.GetNetworkAddress(subnetMask); |
var network2 = address2.GetNetworkAddress(subnetMask); |
|
return network1.Equals(network2); |
} |
} |
} |
/Network/SubnetMask.cs |
@@ -0,0 +1,64 @@ |
/////////////////////////////////////////////////////////////////////////// |
// Copyright (C) Wizardry and Steamworks 2016 - License: GNU GPLv3 // |
// Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
// rights of fair usage, the disclaimer and warranty conditions. // |
/////////////////////////////////////////////////////////////////////////// |
|
using System; |
using System.Net; |
|
namespace wasSharpNET.Network |
{ |
/// <summary> |
/// Subnet Mask. |
/// </summary> |
/// <remarks>https://blogs.msdn.microsoft.com/knom/2008/12/31/ip-address-calculations-with-c-subnetmasks-networks/</remarks> |
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) |
{ |
var hostPartLength = hostpartLength; |
var netPartLength = 32 - hostPartLength; |
|
if (netPartLength < 2) |
throw new ArgumentException("Number of hosts is to large for IPv4"); |
|
var binaryMask = new byte[4]; |
|
for (var i = 0; i < 4; i++) |
{ |
if (i*8 + 8 <= netPartLength) |
binaryMask[i] = 255; |
else if (i*8 > netPartLength) |
binaryMask[i] = 0; |
else |
{ |
var oneLength = netPartLength - i*8; |
var 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) |
{ |
var hostPartLength = 32 - netpartLength; |
return CreateByHostBitLength(hostPartLength); |
} |
|
public static IPAddress CreateByHostNumber(int numberOfHosts) |
{ |
var maxNumber = numberOfHosts + 1; |
|
var b = Convert.ToString(maxNumber, 2); |
|
return CreateByHostBitLength(b.Length); |
} |
} |
} |