41 lines
967 B
C#
41 lines
967 B
C#
using System.Net;
|
|
|
|
namespace SddpViewer;
|
|
|
|
public class IpRangeHelper
|
|
{
|
|
public static IEnumerable<IPAddress> GetIPRange(IPAddress startIp, IPAddress endIp)
|
|
{
|
|
uint start = IpToUint(startIp);
|
|
uint end = IpToUint(endIp);
|
|
|
|
if (start > end)
|
|
{
|
|
(start, end) = (end, start);
|
|
}
|
|
|
|
for (uint current = start; current <= end; current++)
|
|
{
|
|
yield return UintToIp(current);
|
|
}
|
|
}
|
|
|
|
private static uint IpToUint(IPAddress ip)
|
|
{
|
|
byte[] bytes = ip.GetAddressBytes();
|
|
if (bytes.Length != 4)
|
|
throw new ArgumentException("Nur IPv4 wird unterstützt.");
|
|
return (uint)((bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]);
|
|
}
|
|
|
|
private static IPAddress UintToIp(uint ip)
|
|
{
|
|
byte[] bytes = new byte[4];
|
|
bytes[0] = (byte)((ip >> 24) & 0xFF);
|
|
bytes[1] = (byte)((ip >> 16) & 0xFF);
|
|
bytes[2] = (byte)((ip >> 8) & 0xFF);
|
|
bytes[3] = (byte)(ip & 0xFF);
|
|
return new IPAddress(bytes);
|
|
}
|
|
}
|