Ejemplo n.º 1
0
        public void DoubleTcpAllocationTest()
        {
            var msUsername = new MsUsername()
            {
                Value = new byte[52],
            };

            using (HMACSHA1 sha1 = new HMACSHA1(key1))
            {
                sha1.ComputeHash(msUsername.Value, 0, msUsername.TokenBlobLength);
                Array.Copy(sha1.Hash, 0, msUsername.Value, msUsername.TokenBlobLength, MsUsername.HashOfTokenBlobLength);
            }

            var allocate1 = new TurnMessage()
            {
                MessageType = MessageType.AllocateRequest,
                TransactionId = TransactionId.Generate(),

                MagicCookie = new MagicCookie(),
                MsVersion = new MsVersion()
                {
                    Value = 1,
                },
                MsUsername = msUsername,
            };

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(turnIp, tcpPort);

            var allocate1response = SendReceiveMessage(socket, allocate1);

            Assert.AreEqual(MessageType.AllocateErrorResponse, allocate1response.MessageType);
            Assert.AreEqual(401, allocate1response.ErrorCodeAttribute.ErrorCode);

            var allocate2 = new TurnMessage()
            {
                MessageType = MessageType.AllocateRequest,
                TransactionId = TransactionId.Generate(),

                MagicCookie = new MagicCookie(),
                MsVersion = new MsVersion()
                {
                    Value = 1,
                },
                MsUsername = msUsername,
                Nonce = allocate1response.Nonce,
                Realm = new Realm(TurnMessageRfc.MsTurn)
                {
                    Value = realm,
                },
                MessageIntegrity = new MessageIntegrity(),
            };

            var allocate2response = SendReceiveMessage(socket, allocate2);

            Assert.AreEqual(MessageType.AllocateResponse, allocate2response.MessageType);

            var allocate3response = SendReceiveMessage(socket, allocate2);

            Assert.AreEqual(MessageType.AllocateResponse, allocate3response.MessageType);
        }
Ejemplo n.º 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;
                    }
                }
            }
        }
Ejemplo n.º 3
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;
                }
            }
        }