コード例 #1
0
ファイル: TokenHelper.cs プロジェクト: Matodor/TeeSharp
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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();
        }