public static uint GenerateToken(IPEndPoint address, long seed) { var buffer = new byte[24 + sizeof(long)]; // sizeof(address raw) + sizeof(long) var bytes = new Span <byte>(buffer); // TODO //if (pAddr->type & NETTYPE_LINK_BROADCAST) // return GenerateToken(&NullAddr, Seed); if (address != null) { var port = address.Port; address.Port = 0; address.Raw().CopyTo(bytes); address.Port = port; } BitConverter.TryWriteBytes(bytes.Slice(24), seed); var result = NetworkHelper.Hash(buffer) & TokenMask; if (result == TokenNone) { result--; } return(result); }
public void TestHash() { var endPoint1 = new IPEndPoint(IPAddress.Parse("192.168.42.5"), 65202); var buf2 = endPoint1.Raw(); var hash = Network.NetworkHelper.Hash(buf2); Assert.AreEqual(hash, 1578705565u); }
public void TestEndPointRaw() { var endPoint1 = new IPEndPoint(IPAddress.Parse("192.168.137.106"), 51850); var actual = endPoint1.Raw(); var expected = new byte[] { 1, 0, 0, 0, 192, 168, 137, 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138, 202, 0, 0 }; CollectionAssert.AreEqual(expected, actual); }
public void TestIPv4EndPointSerialize() { var endPoint1 = new IPEndPoint(IPAddress.Parse("192.168.42.5"), 65202); var buf1 = new byte[] { 1, 0, 0, 0, 192, 168, 42, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 178, 254, 0, 0 }; var buf2 = endPoint1.Raw(); CollectionAssert.AreEqual(buf1, buf2); // TODO for IPv6 //var endPoint2 = new IPEndPoint(IPAddress.Parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), 65202); // var buf3 = endPoint2.Raw(); }