コード例 #1
0
ファイル: DataTest.cs プロジェクト: kztao/turnmessage
        public void ParseTest()
        {
            Data target = new Data()
            {
                Value = new byte[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 },
            };

            byte[] expected = new byte[]
            {
                0xee, 0xee, 0xee, 0xee,
                0x00, 0x13, 0x00, 0x09,
                0x09, 0x08, 0x07, 0x06,
                0x05, 0x04, 0x03, 0x02,
                0x01,
            };

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

            int startIndex = 4;
            target.GetBytes(actual, ref startIndex);

            Assert.AreEqual(17, startIndex);
            Helpers.AreArrayEqual(expected, actual);
        }
コード例 #2
0
ファイル: DataTest.cs プロジェクト: kztao/turnmessage
        public void GetBytesTest()
        {
            byte[] bytes = new byte[]
            {
                0x00, 0x13, 0x00, 0x09,
                0x01, 0x02, 0x03, 0x04,
                0x05, 0x06, 0x07, 0x08,
                0x09
            };

            int startIndex = 0;
            Data target = new Data();
            target.Parse(bytes, ref startIndex);

            Assert.AreEqual(13, startIndex);
            Assert.AreEqual(AttributeType.Data, target.AttributeType);
            Assert.AreEqual(9, target.ValueLength);

            byte[] expectedValue = new byte[9];
            Array.Copy(bytes, 4, expectedValue, 0, 9);

            Helpers.AreArrayEqual(expectedValue, 0, target.Value, 4);
        }
コード例 #3
0
ファイル: DataTest.cs プロジェクト: kztao/turnmessage
 public void DataConstructorTest()
 {
     Data target = new Data();
     Assert.AreEqual(AttributeType.Data, target.AttributeType);
 }
コード例 #4
0
ファイル: TurnMessage.cs プロジェクト: kztao/turnmessage
        private void ParseAttributes(byte[] bytes, int startIndex, int length, TurnMessageRfc rfc)
        {
            int currentIndex = startIndex + HeaderLength;
            int endIndex = startIndex + length;

            while (currentIndex < endIndex)
            {
                UInt16 attributeType1 = bytes.BigendianToUInt16(currentIndex);
                if (Enum.IsDefined(typeof(AttributeType), (Int32)attributeType1) == false)
                    throw new TurnMessageException(ErrorCode.UnknownAttribute);
                AttributeType attributeType = (AttributeType)attributeType1;

                if (rfc == TurnMessageRfc.Rfc3489)
                    if (IsRfc3489(attributeType))
                        throw new TurnMessageException(ErrorCode.UnknownAttribute);

                if (attributeType == AttributeType.Fingerprint)
                {
                    if (Fingerprint == null)
                    {
                        if (storedBytes == null)
                        {
                            storedBytes = new byte[length];
                            Array.Copy(bytes, startIndex, storedBytes, 0, length);
                        }
                        fingerprintStartOffset = currentIndex - startIndex;
                        Fingerprint = new Fingerprint();
                        Fingerprint.Parse(bytes, ref currentIndex);
                    }
                    else
                    {
                        Attribute.Skip(bytes, ref currentIndex);
                    }
                }
                else if (MessageIntegrity != null)
                {
                    Attribute.Skip(bytes, ref currentIndex);
                }
                else
                {
                    switch (attributeType)
                    {
                        case AttributeType.AlternateServer:
                            AlternateServer = new AlternateServer();
                            AlternateServer.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.Bandwidth:
                            Bandwidth = new Bandwidth();
                            Bandwidth.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.Data:
                            Data = new Data();
                            Data.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.DestinationAddress:
                            DestinationAddress = new DestinationAddress();
                            DestinationAddress.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.ErrorCode:
                            ErrorCodeAttribute = new ErrorCodeAttribute();
                            ErrorCodeAttribute.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.Fingerprint:
                            break;

                        case AttributeType.Lifetime:
                            Lifetime = new Lifetime();
                            Lifetime.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.MagicCookie:
                            MagicCookie = new MagicCookie();
                            MagicCookie.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.MappedAddress:
                            MappedAddress = new MappedAddress();
                            MappedAddress.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.MessageIntegrity:
                            messageIntegrityStartOffset = currentIndex - startIndex;
                            if (storedBytes == null)
                            {
                                if (rfc == TurnMessageRfc.MsTurn)
                                {
                                    storedBytes = new byte[GetPadded64(messageIntegrityStartOffset)];
                                    Array.Copy(bytes, startIndex, storedBytes, 0, messageIntegrityStartOffset);
                                }
                                else
                                {
                                    storedBytes = new byte[length];
                                    Array.Copy(bytes, startIndex, storedBytes, 0, length);
                                }
                            }
                            MessageIntegrity = new MessageIntegrity();
                            MessageIntegrity.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.MsVersion:
                            MsVersion = new MsVersion();
                            MsVersion.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.MsSequenceNumber:
                            MsSequenceNumber = new MsSequenceNumber();
                            MsSequenceNumber.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.RemoteAddress:
                            RemoteAddress = new RemoteAddress();
                            RemoteAddress.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.Software:
                            Software = new Software();
                            Software.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.UnknownAttributes:
                            UnknownAttributes = new UnknownAttributes();
                            // Not Implemented
                            UnknownAttributes.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.Username:
                            if (MsVersion != null)
                            {
                                MsUsername = new MsUsername();
                                MsUsername.Parse(bytes, ref currentIndex);
                            }
                            else
                            {
                                Username = new Username();
                                Username.Parse(bytes, ref currentIndex);
                            }
                            break;

                        // ietf-mmusic-ice
                        case AttributeType.Priority:
                        case AttributeType.UseCandidate:
                        case AttributeType.IceControlled:
                        case AttributeType.IceControlling:
                            Attribute.Skip(bytes, ref currentIndex);
                            break;

                        // rfc3489
                        case AttributeType.ChangedAddress:
                            ChangedAddress = new ChangedAddress();
                            ChangedAddress.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.ChangeRequest:
                            ChangeRequest = new ChangeRequest();
                            ChangeRequest.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.ResponseAddress:
                            ResponseAddress = new ResponseAddress();
                            ResponseAddress.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.SourceAddress:
                            SourceAddress = new SourceAddress();
                            SourceAddress.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.ReflectedFrom:
                            ReflectedFrom = new ReflectedFrom();
                            ReflectedFrom.Parse(bytes, ref currentIndex);
                            break;

                        case AttributeType.Password:
                            Password = new Password();
                            Password.Parse(bytes, ref currentIndex);
                            break;

                        default:
                            if (rfc == TurnMessageRfc.MsTurn)
                            {
                                switch (attributeType)
                                {
                                    case AttributeType.Nonce:
                                        Nonce = new Nonce(rfc);
                                        Nonce.Parse(bytes, ref currentIndex);
                                        break;

                                    case AttributeType.Realm:
                                        Realm = new Realm(rfc);
                                        Realm.Parse(bytes, ref currentIndex);
                                        break;

                                    case AttributeType.XorMappedAddress:
                                        XorMappedAddress = new XorMappedAddress(rfc);
                                        XorMappedAddress.Parse(bytes, ref currentIndex, TransactionId);
                                        break;

                                    default:
                                        throw new NotImplementedException();
                                }
                            }
                            else
                            {
                                switch (attributeType)
                                {
                                    case AttributeType.NonceStun:
                                        Nonce = new Nonce(rfc);
                                        Nonce.Parse(bytes, ref currentIndex);
                                        break;

                                    case AttributeType.RealmStun:
                                        Realm = new Realm(rfc);
                                        Realm.Parse(bytes, ref currentIndex);
                                        break;

                                    case AttributeType.XorMappedAddressStun:
                                        XorMappedAddress = new XorMappedAddress(rfc);
                                        XorMappedAddress.Parse(bytes, ref currentIndex, TransactionId);
                                        break;

                                    default:
                                        throw new NotImplementedException();
                                }
                            }
                            break;
                    }
                }

                if (rfc != TurnMessageRfc.MsTurn)
                {
                    if (currentIndex % 4 > 0)
                        currentIndex += 4 - currentIndex % 4;
                }
            }
        }