Example #1
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 #2
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);
            }
        }