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