Example #1
0
        public void GetIpV4BytesTest()
        {
            XorMappedAddress target = new XorMappedAddress(TurnMessageRfc.Rfc3489)
            {
                IpAddress = IPAddress.Parse("192.168.1.1"),
                Port      = 0x15b3,
            };

            byte[] expected = new byte[]
            {
                0xee, 0xee, 0xee, 0xee,
                0x00, 0x20, 0x00, 0x08,
                0x00, 0x01, 0x34, 0xa1,
                0xe1, 0xba, 0xa5, 0x43,
            };

            byte[] actual = new byte[]
            {
                0xee, 0xee, 0xee, 0xee,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
            };

            int startIndex = 4;

            target.GetBytes(actual, ref startIndex, GetTransactionId());

            Assert.AreEqual(16, startIndex);
            Helpers.AreArrayEqual(expected, actual);
        }
Example #2
0
        /// <summary>
        /// TODO: Documentation CreatePermission
        /// </summary>
        /// <param name="xorPeerAddress"></param>
        /// <param name="allocation"></param>
        public void CreatePermission(XorMappedAddress xorPeerAddress, TurnAllocation allocation)
        {
            StunMessage msg = new StunMessage(StunMethodType.CreatePermission, StunMethodClass.Request, StunUtilities.NewTransactionId);

            msg.Turn.XorPeerAddress = xorPeerAddress;
            msg.Stun.Username       = new UTF8Attribute(StunAttributeType.Username, allocation.Username);
            msg.Stun.Realm          = new UTF8Attribute(StunAttributeType.Realm, allocation.Realm);
            msg.Stun.Nonce          = new UTF8Attribute(StunAttributeType.Nonce, allocation.Nonce);

            msg.AddMessageIntegrity(allocation.Password, true);

            this.StunClient.BeginSendMessage(msg, allocation);
        }
Example #3
0
        /// <summary>
        /// TODO: Documentation SendIndication
        /// </summary>
        /// <param name="xorPeerAddress"></param>
        /// <param name="data"></param>
        /// <param name="allocation"></param>
        public void SendIndication(XorMappedAddress xorPeerAddress, byte[] data, TurnAllocation allocation)
        {
            StunMessage msg = new StunMessage(StunMethodType.Send, StunMethodClass.Indication, StunUtilities.NewTransactionId);

            msg.Turn.XorPeerAddress = xorPeerAddress;
            msg.Turn.Data           = new StunAttribute(StunAttributeType.Data, data);
            msg.Stun.Username       = new UTF8Attribute(StunAttributeType.Username, allocation.Username);
            msg.Stun.Realm          = new UTF8Attribute(StunAttributeType.Realm, allocation.Realm);
            msg.Stun.Nonce          = new UTF8Attribute(StunAttributeType.Nonce, allocation.Nonce);

            msg.AddMessageIntegrity(allocation.Password, true);

            this.StunClient.BeginSendMessage(msg, null);
        }
Example #4
0
        public void ParseIp4Test()
        {
            byte[] bytes = new byte[]
            {
                0x00, 0x01, 0x00, 0x08,
                0xff, 0x01, 0x34, 0xa1,
                0xe1, 0xba, 0xa5, 0x43,
            };

            int startIndex          = 0;
            XorMappedAddress target = new XorMappedAddress(TurnMessageRfc.Rfc3489);

            target.Parse(bytes, ref startIndex, GetTransactionId());
            Assert.AreEqual(12, startIndex);
            Assert.AreEqual(AttributeType.XorMappedAddressStun, target.AttributeType);
            Assert.AreEqual(0x15b3, target.Port);
            Assert.AreEqual(@"192.168.1.1", target.IpAddress.ToString());
        }
Example #5
0
        public void XorMappedAddressConstructorTest()
        {
            XorMappedAddress target = new XorMappedAddress(TurnMessageRfc.Rfc3489);

            Assert.AreEqual(AttributeType.XorMappedAddressStun, target.AttributeType);
        }
Example #6
0
        internal static IEnumerable <Attribute> ParseAttributes(
            IEnumerable <byte> bytes,
            byte[] transactionId = null
            )
        {
            while (bytes.Any())
            {
                var    type    = (Attribute.AttributeType)bytes.Take(2).ToUShort();
                ushort length  = bytes.Skip(2).Take(2).ToUShort();
                byte[] payload = bytes.Skip(4).Take(length).ToArray();

                switch (type)
                {
                case Attribute.AttributeType.ErrorCode:
                    yield return(ErrorCode.Parse(payload));

                    break;

                case Attribute.AttributeType.Realm:
                    yield return(Realm.Parse(payload));

                    break;

                case Attribute.AttributeType.Nonce:
                    yield return(Stun.Attributes.Nonce.Parse(payload));

                    break;

                case Attribute.AttributeType.Software:
                    yield return(Software.Parse(payload));

                    break;

                case Attribute.AttributeType.Fingerprint:
                    yield return(Fingerprint.Parse(payload));

                    break;

                case Attribute.AttributeType.XorMappedAddress:
                    yield return(XorMappedAddress.Parse(
                                     payload, transactionId));

                    break;

                case Attribute.AttributeType.XorRelayedAddress:
                    yield return(XorRelayedAddress.Parse(
                                     payload, transactionId));

                    break;

                case Attribute.AttributeType.ConnectionId:
                    yield return(new ConnectionId(payload));

                    break;

                case Attribute.AttributeType.Lifetime:
                    yield return(new Lifetime((int)payload.ToUInt()));

                    break;
                }

                // Detect padding
                var padBytes = (ushort)((4 + length) % 4);
                if (padBytes > 0)
                {
                    length += padBytes;
                }

                bytes = bytes.Skip(4 + length);
            }
        }