public static UInt48 ReadUInt48(this byte[] buffer, ref int offset, Endianity endianity) { UInt48 uint48 = ByteArrayExtensions.ReadUInt48(buffer, offset, endianity); offset += 6; return(uint48); }
public void UInt48Test() { UInt48 value = (UInt48)0x010203040506; byte[] buffer = new byte[UInt48.SizeOf]; buffer.Write(0, value, Endianity.Big); Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Big)); Assert.AreEqual(0x01, buffer[0]); Assert.AreEqual(0x02, buffer[1]); Assert.AreEqual(0x03, buffer[2]); Assert.AreEqual(0x04, buffer[3]); Assert.AreEqual(0x05, buffer[4]); Assert.AreEqual(0x06, buffer[5]); int offset = 0; buffer.Write(ref offset, value, Endianity.Big); Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Big)); Assert.AreEqual(6, offset); buffer.Write(0, value, Endianity.Small); Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Small)); Assert.AreEqual(0x06, buffer[0]); Assert.AreEqual(0x05, buffer[1]); Assert.AreEqual(0x04, buffer[2]); Assert.AreEqual(0x03, buffer[3]); Assert.AreEqual(0x02, buffer[4]); Assert.AreEqual(0x01, buffer[5]); offset = 0; buffer.Write(ref offset, value, Endianity.Small); Assert.AreEqual(value, buffer.ReadUInt48(0, Endianity.Small)); Assert.AreEqual(6, offset); }
public static List <byte> GetBytesList(this MacAddress address) { UInt48 value = address.ToValue(); byte[] array = BitConverter.GetBytes(value).Take(6).ToArray(); return(array.Reverse().ToList()); }
public DnsResourceDataTransactionSignature(DnsDomainName algorithm, UInt48 timeSigned, ushort fudge, DataSegment messageAuthenticationCode, ushort originalId, DnsResponseCode error, DataSegment other) { if (messageAuthenticationCode == null) { throw new ArgumentNullException("messageAuthenticationCode"); } if (other == null) { throw new ArgumentNullException("other"); } if (messageAuthenticationCode.Length > (int)ushort.MaxValue) { throw new ArgumentOutOfRangeException("messageAuthenticationCode", (object)messageAuthenticationCode.Length, string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Cannot be longer than {0}", new object[1] { (object)ushort.MaxValue })); } if (other.Length > (int)ushort.MaxValue) { throw new ArgumentOutOfRangeException("other", (object)other.Length, string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Cannot be longer than {0}", new object[1] { (object)ushort.MaxValue })); } this.Algorithm = algorithm; this.TimeSigned = timeSigned; this.Fudge = fudge; this.MessageAuthenticationCode = messageAuthenticationCode; this.OriginalId = originalId; this.Error = error; this.Other = other; }
/// <summary> /// Constructs an instance out of the algorithm time signed, fudge, message authentication code, original ID, error and other fields. /// </summary> /// <param name="algorithm">Name of the algorithm in domain name syntax.</param> /// <param name="timeSigned">Seconds since 1-Jan-70 UTC.</param> /// <param name="fudge">Seconds of error permitted in Time Signed.</param> /// <param name="messageAuthenticationCode">Defined by Algorithm Name.</param> /// <param name="originalId">Original message ID.</param> /// <param name="error">RCODE covering TSIG processing.</param> /// <param name="other">Empty unless Error == BADTIME.</param> public DnsResourceDataTransactionSignature(DnsDomainName algorithm, UInt48 timeSigned, ushort fudge, DataSegment messageAuthenticationCode, ushort originalId, DnsResponseCode error, DataSegment other) { if (messageAuthenticationCode == null) { throw new ArgumentNullException("messageAuthenticationCode"); } if (other == null) { throw new ArgumentNullException("other"); } if (messageAuthenticationCode.Length > ushort.MaxValue) { throw new ArgumentOutOfRangeException("messageAuthenticationCode", messageAuthenticationCode.Length, string.Format(CultureInfo.InvariantCulture, "Cannot be longer than {0}", ushort.MaxValue)); } if (other.Length > ushort.MaxValue) { throw new ArgumentOutOfRangeException("other", other.Length, string.Format(CultureInfo.InvariantCulture, "Cannot be longer than {0}", ushort.MaxValue)); } Algorithm = algorithm; TimeSigned = timeSigned; Fudge = fudge; MessageAuthenticationCode = messageAuthenticationCode; OriginalId = originalId; Error = error; Other = other; }
public void Should_SerializeUInt48() { UInt48 value = UInt48.MaxValue; // range 0 to 281,474,976,710,655 Assert.AreEqual(new byte[] { 255, 255, 255, 255, 255, 255 }, value.GetBytes()); Assert.AreEqual(new Bit[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, value.GetBits()); value = (UInt48)2; Assert.AreEqual(new byte[] { 2, 0, 0, 0, 0, 0 }, value.GetBytes()); Assert.AreEqual(new Bit[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, value.GetBits()); // test overflow value = (UInt48)281474981710655L; Assert.AreEqual(4999999, value); Assert.AreEqual(new byte[] { 63, 75, 76, 0, 0, 0 }, value.GetBytes()); Assert.AreEqual(new Bit[] { 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, value.GetBits()); }
public static void Write(this byte[] buffer, int offset, UInt48 value, Endianity endianity) { if (ByteArrayExtensions.IsWrongEndianity(endianity)) { value = ByteArrayExtensions.HostToNetworkOrder(value); } ByteArrayExtensions.Write(buffer, offset, value); }
public static byte[] GetBytes(this UInt48 uInt48) { Debug.Assert(BitConverter.IsLittleEndian); var ulongBytes = BitConverter.GetBytes((ulong)uInt48); var uInt48Bytes = ulongBytes.Take(6).ToArray(); return(uInt48Bytes); }
public static UInt48 ReadUInt48(this byte[] buffer, int offset, Endianity endianity) { UInt48 uint48 = ByteArrayExtensions.ReadUInt48(buffer, offset); if (ByteArrayExtensions.IsWrongEndianity(endianity)) { uint48 = ByteArrayExtensions.HostToNetworkOrder(uint48); } return(uint48); }
public MacAddress(string address) { if (address == null) { throw new ArgumentNullException("address"); } string[] strArray = address.Split(':'); if (strArray.Length != 6) { throw new ArgumentException("Failed parsing " + (object)address + " as mac address. Expected 6 hexes and got " + (string)(object)strArray.Length + " hexes", "address"); } this._value = BitSequence.Merge(Convert.ToByte(strArray[0], 16), Convert.ToByte(strArray[1], 16), Convert.ToByte(strArray[2], 16), Convert.ToByte(strArray[3], 16), Convert.ToByte(strArray[4], 16), Convert.ToByte(strArray[5], 16)); }
/// <summary> /// Constructs an instance out of the area address, system identifier and selector fields. /// </summary> /// <param name="areaAddress"> /// The combination of [IDP, HO-DSP] identify both the routing domain and the area within the routing domain. /// Hence the combination [IDP, HO-DSP] is called the "Area Address". /// All nodes within the area must have same Area address. /// </param> /// <param name="systemIdentifier">System Identifier.</param> /// <param name="selector">NSAP Selector.</param> public DnsResourceDataNetworkServiceAccessPoint(DataSegment areaAddress, UInt48 systemIdentifier, byte selector) { if (areaAddress == null) throw new ArgumentNullException("areaAddress"); if (areaAddress.Length < MinAreaAddressLength) throw new ArgumentOutOfRangeException("areaAddress", areaAddress.Length, string.Format(CultureInfo.InvariantCulture, "Area Address length must be at least {0}.", MinAreaAddressLength)); AreaAddress = areaAddress; SystemIdentifier = systemIdentifier; Selector = selector; }
internal override DnsResourceData CreateInstance(DataSegment data) { if (data.Length < 8) { return((DnsResourceData)null); } DataSegment areaAddress = data.Subsegment(0, 1 + data.Length - 8); int length = areaAddress.Length; UInt48 systemIdentifier = data.ReadUInt48(length, Endianity.Big); byte selector = data[length + 6]; return((DnsResourceData) new DnsResourceDataNetworkServiceAccessPoint(areaAddress, systemIdentifier, selector)); }
internal override DnsResourceData CreateInstance(DataSegment data) { if (data.Length < ConstantPartLength) return null; DataSegment areaAddress = data.Subsegment(Offset.AreaAddress, MinAreaAddressLength + data.Length - ConstantPartLength); int afterAreaOffset = areaAddress.Length; UInt48 systemIdentifier = data.ReadUInt48(afterAreaOffset + OffsetAfterArea.SystemIdentifier, Endianity.Big); byte selector = data[afterAreaOffset + OffsetAfterArea.Selector]; return new DnsResourceDataNetworkServiceAccessPoint(areaAddress, systemIdentifier, selector); }
private static unsafe UInt48 HostToNetworkOrder(UInt48 value) { UInt48 uint48; byte * numPtr1 = (byte *)&uint48; byte * numPtr2 = (byte *)&value; *numPtr1 = numPtr2[5]; numPtr1[1] = numPtr2[4]; numPtr1[2] = numPtr2[3]; numPtr1[3] = numPtr2[2]; numPtr1[4] = numPtr2[1]; numPtr1[5] = *numPtr2; return(uint48); }
internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length) { if (length < ConstantPartLength + DnsDomainName.RootLength) { return(null); } DnsDomainName algorithm; int algorithmLength; if (!DnsDomainName.TryParse(dns, offsetInDns, length - ConstantPartLength, out algorithm, out algorithmLength)) { return(null); } offsetInDns += algorithmLength; length -= algorithmLength; if (length < ConstantPartLength) { return(null); } UInt48 timeSigned = dns.ReadUInt48(offsetInDns + OffsetAfterAlgorithm.TimeSigned, Endianity.Big); ushort fudge = dns.ReadUShort(offsetInDns + OffsetAfterAlgorithm.Fudge, Endianity.Big); int messageAuthenticationCodeLength = dns.ReadUShort(offsetInDns + OffsetAfterAlgorithm.MessageAuthenticationCodeSize, Endianity.Big); if (length < ConstantPartLength + messageAuthenticationCodeLength) { return(null); } DataSegment messageAuthenticationCode = dns.Subsegment(offsetInDns + OffsetAfterAlgorithm.MessageAuthenticationCode, messageAuthenticationCodeLength); int totalReadAfterAlgorithm = OffsetAfterAlgorithm.MessageAuthenticationCode + messageAuthenticationCodeLength; offsetInDns += totalReadAfterAlgorithm; length -= totalReadAfterAlgorithm; ushort originalId = dns.ReadUShort(offsetInDns + OffsetAfterMessageAuthenticationCode.OriginalId, Endianity.Big); DnsResponseCode error = (DnsResponseCode)dns.ReadUShort(offsetInDns + OffsetAfterMessageAuthenticationCode.Error, Endianity.Big); int otherLength = dns.ReadUShort(offsetInDns + OffsetAfterMessageAuthenticationCode.OtherLength, Endianity.Big); if (length != OffsetAfterMessageAuthenticationCode.OtherData + otherLength) { return(null); } DataSegment other = dns.Subsegment(offsetInDns + OffsetAfterMessageAuthenticationCode.OtherData, otherLength); return(new DnsResourceDataTransactionSignature(algorithm, timeSigned, fudge, messageAuthenticationCode, originalId, error, other)); }
public DnsResourceDataNetworkServiceAccessPoint(DataSegment areaAddress, UInt48 systemIdentifier, byte selector) { if (areaAddress == null) { throw new ArgumentNullException("areaAddress"); } if (areaAddress.Length < 1) { throw new ArgumentOutOfRangeException("areaAddress", (object)areaAddress.Length, string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Area Address length must be at least {0}.", new object[1] { (object)1 })); } this.AreaAddress = areaAddress; this.SystemIdentifier = systemIdentifier; this.Selector = selector; }
// převod uint na macovku private PhysicalAddress UInt48ToHw(UInt48 inValue) { ulong value = (ulong)inValue; byte[] temp = new byte[6]; for (int i = 0; i < temp.Length; i++) { //string tmp = ""; int intTmp = 0; for (int j = 0; j < 8; j++) { intTmp += (int)((value % 2) * Math.Pow(2, j)); value = value >> 1; } temp[5 - i] = (byte)intTmp; } return(new PhysicalAddress(temp)); }
internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length) { if (length < 17) { return((DnsResourceData)null); } DnsDomainName domainName; int numBytesRead; if (!DnsDomainName.TryParse(dns, offsetInDns, length - 16, out domainName, out numBytesRead)) { return((DnsResourceData)null); } offsetInDns += numBytesRead; length -= numBytesRead; if (length < 16) { return((DnsResourceData)null); } UInt48 timeSigned = dns.ReadUInt48(offsetInDns, Endianity.Big); ushort fudge = dns.ReadUShort(offsetInDns + 6, Endianity.Big); int length1 = (int)dns.ReadUShort(offsetInDns + 8, Endianity.Big); if (length < 16 + length1) { return((DnsResourceData)null); } DataSegment messageAuthenticationCode = dns.Subsegment(offsetInDns + 10, length1); int num = 10 + length1; offsetInDns += num; length -= num; ushort originalId = dns.ReadUShort(offsetInDns, Endianity.Big); DnsResponseCode error = (DnsResponseCode)dns.ReadUShort(offsetInDns + 2, Endianity.Big); int length2 = (int)dns.ReadUShort(offsetInDns + 4, Endianity.Big); if (length != 6 + length2) { return((DnsResourceData)null); } DataSegment other = dns.Subsegment(offsetInDns + 6, length2); return((DnsResourceData) new DnsResourceDataTransactionSignature(domainName, timeSigned, fudge, messageAuthenticationCode, originalId, error, other)); }
public void ParseTest() { Random random = new Random(); for (int i = 0; i != 100; ++i) { UInt48 expected = (UInt48)random.NextLong(UInt48.MaxValue + 1); UInt48 actual = UInt48.Parse(expected.ToString(), NumberStyles.Integer, CultureInfo.InvariantCulture); Assert.AreEqual(expected, actual); actual = UInt48.Parse(expected.ToString(), NumberStyles.Integer); Assert.AreEqual(expected, actual); actual = UInt48.Parse(expected.ToString(), CultureInfo.InvariantCulture); Assert.AreEqual(expected, actual); actual = UInt48.Parse(expected.ToString()); Assert.AreEqual(expected, actual); } }
public void UInt48Test() { Random random = new Random(); for (int i = 0; i != 1000; ++i) { UInt48 value = random.NextUInt48(); Assert.AreEqual(value, value); Assert.AreNotEqual(value, "1"); // ReSharper disable EqualExpressionComparison Assert.IsTrue(value == value); Assert.IsFalse(value != value); // ReSharper restore EqualExpressionComparison Assert.IsNotNull(value.GetHashCode()); if (value < uint.MaxValue) { Assert.AreEqual(value, uint.Parse(value.ToString())); } Assert.AreEqual((byte)value, (byte)(value % 256)); } }
public void ParseTooBigTestEvenForUInt64() { UInt48 value = UInt48.Parse(ulong.MaxValue + "0"); Assert.IsNotNull(value); }
public void ParseTooBigTest() { UInt48 value = UInt48.Parse(ulong.MaxValue.ToString()); Assert.IsNotNull(value); }
public MacAddress(UInt48 value) { this._value = value; }
public static void AssertValue(this XElement element, UInt48 expectedValue) { element.AssertValue(expectedValue.ToString("x12")); }
public static void Write(this byte[] buffer, ref int offset, UInt48 value, Endianity endianity) { ByteArrayExtensions.Write(buffer, offset, value, endianity); offset += 6; }
/// <summary> /// Constructs the address from a 48 bit integer. /// </summary> /// <param name="value">The 48 bit integer to create the address from.</param> public MacAddress(UInt48 value) { _value = value; }
private static unsafe void Write(byte[] buffer, int offset, UInt48 value) { fixed(byte *numPtr = &buffer[offset]) * (UInt48 *)numPtr = value; }