Added IPAddress.Increment/Decrement; Added ParseNetwork, ExpandNetwork
This commit is contained in:
46
AMWD.Common/Extensions/IPAddressExtensions.cs
Normal file
46
AMWD.Common/Extensions/IPAddressExtensions.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
namespace System.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extension methods for <see cref="IPAddress"/>es.
|
||||
/// </summary>
|
||||
public static class IPAddressExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Increments the <see cref="IPAddress"/> by one and returns a new instance.
|
||||
/// </summary>
|
||||
/// <param name="address">The <see cref="IPAddress"/> to increment.</param>
|
||||
public static IPAddress Increment(this IPAddress address)
|
||||
{
|
||||
byte[] bytes = address.GetAddressBytes();
|
||||
int bytePos = bytes.Length - 1;
|
||||
|
||||
while (bytes[bytePos] == byte.MaxValue)
|
||||
{
|
||||
bytes[bytePos] = 0;
|
||||
bytePos--;
|
||||
}
|
||||
bytes[bytePos]++;
|
||||
|
||||
return new IPAddress(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrements the <see cref="IPAddress"/> by one and returns a new instance.
|
||||
/// </summary>
|
||||
/// <param name="address">The <see cref="IPAddress"/> to decrement.</param>
|
||||
public static IPAddress Decrement(this IPAddress address)
|
||||
{
|
||||
byte[] bytes = address.GetAddressBytes();
|
||||
int bytePos = bytes.Length - 1;
|
||||
|
||||
while (bytes[bytePos] == 0)
|
||||
{
|
||||
bytes[bytePos] = byte.MaxValue;
|
||||
bytePos--;
|
||||
}
|
||||
bytes[bytePos]--;
|
||||
|
||||
return new IPAddress(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user