forked from ln-dotnet/ln.type
45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System;
|
|
using System.Net;
|
|
|
|
namespace ln.type
|
|
{
|
|
public class IPv4
|
|
{
|
|
public static readonly IPv4 ANY = new IPv4();
|
|
|
|
private UInt32 _ip;
|
|
private IPv4()
|
|
{
|
|
}
|
|
public IPv4(UInt32 ip)
|
|
{
|
|
_ip = ip;
|
|
}
|
|
|
|
public IPv4(byte[] ipbytes)
|
|
{
|
|
_ip = BitConverter.ToUInt32(ipbytes, 0);
|
|
}
|
|
|
|
public byte[] ToBytes => BitConverter.GetBytes(_ip);
|
|
|
|
public override bool Equals(Object obj) => (obj is IPv4 other) && _ip == other._ip;
|
|
public override int GetHashCode() => (int)_ip;
|
|
|
|
public override string ToString()
|
|
{
|
|
byte[] ipbytes = ToBytes;
|
|
return string.Format("{0}.{1}.{2}.{3}", ipbytes[3], ipbytes[2], ipbytes[1], ipbytes[0]);
|
|
}
|
|
|
|
public static IPv4 operator ++(IPv4 ip) => new IPv4(ip._ip + 1);
|
|
public static IPv4 operator --(IPv4 ip) => new IPv4(ip._ip - 1);
|
|
|
|
public static IPv4 operator +(IPv4 ip, int n) => new IPv4((uint)(ip._ip + n));
|
|
public static IPv4 operator -(IPv4 ip, int n) => new IPv4((uint)(ip._ip - n));
|
|
|
|
public static implicit operator IPAddress(IPv4 ipv4) => new IPAddress(ipv4.ToBytes);
|
|
public static implicit operator IPv4(IPAddress ipAddress) => new IPv4(ipAddress.GetAddressBytes());
|
|
|
|
}
|
|
} |