Add Minimalistic IPv4 class
ln.build - build0.l--n.de build job pending Details

master
Harald Wolff 2021-07-10 01:31:18 +02:00
parent 7510e7296a
commit db015ed6db
2 changed files with 44 additions and 0 deletions

43
ln.type/IPv4.cs 100644
View File

@ -0,0 +1,43 @@
using System;
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));
}
}

View File

@ -8,6 +8,7 @@
<Company>l--n.de</Company>
<AssemblyVersion>0.0.1.0</AssemblyVersion>
<FileVersion>0.0.1.0</FileVersion>
<PackageVersion>0.1.8-dev</PackageVersion>
</PropertyGroup>
<ItemGroup>