47 lines
1.2 KiB
C#
47 lines
1.2 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|