73 lines
1.7 KiB
C#
73 lines
1.7 KiB
C#
using System;
|
|
using System.Globalization;
|
|
namespace ln.types.net
|
|
{
|
|
public class MAC : IComparable<MAC>
|
|
{
|
|
readonly byte[] mac;
|
|
|
|
public byte[] Bytes => mac.Slice(0);
|
|
|
|
public MAC()
|
|
{
|
|
mac = new byte[6];
|
|
}
|
|
public MAC(byte[] macBytes)
|
|
:this()
|
|
{
|
|
if (macBytes.Length != 6)
|
|
throw new ArgumentOutOfRangeException(nameof(macBytes), "MAC bytes need to be exactly 6 bytes");
|
|
|
|
Array.Copy(macBytes, 0, this.mac, 0, 6);
|
|
}
|
|
public MAC(string smac)
|
|
:this()
|
|
{
|
|
string[] sbytes = null;
|
|
if (smac.Contains(":"))
|
|
sbytes = smac.Split(':');
|
|
else
|
|
sbytes = smac.Split('-');
|
|
|
|
if (sbytes.Length != 6)
|
|
throw new ArgumentException(nameof(smac), "MAC needs 6 bytes");
|
|
|
|
for (int n = 0; n < 6; n++)
|
|
mac[n] = Convert.ToByte(sbytes[n], 16);
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return BitConverter.ToString(mac);
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int hash = 0;
|
|
foreach (byte mb in mac)
|
|
hash = (hash << 16) ^ mb;
|
|
return hash;
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj is MAC)
|
|
{
|
|
return mac.AreEqual((obj as MAC).mac);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int CompareTo(MAC other)
|
|
{
|
|
for (int n=0;n<6;n++)
|
|
{
|
|
int d = mac[n] - other.mac[n];
|
|
if (d != 0)
|
|
return d;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
}
|