コード例 #1
0
        public void GetIpV4BytesTest()
        {
            AddressAttribute target = new MappedAddress()
            {
                IpAddress = IPAddress.Loopback,
                Port = 0x1234,
            };

            byte[] expected = new byte[]
            {
                0xee, 0xee, 0xee, 0xee,
                0x00, 0x01, 0x00, 0x08,
                0x00, 0x01, 0x12, 0x34,
                0x7f, 0x00, 0x00, 0x01,
            };

            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);

            Assert.AreEqual(16, startIndex);
            Helpers.AreArrayEqual(expected, actual);
            Assert.AreEqual(AddressFamily.InterNetwork, target.IpEndPoint.AddressFamily);
        }
コード例 #2
0
        public void GetIpV6BytesTest()
        {
            AddressAttribute target = new MappedAddress()
            {
                IpAddress = IPAddress.Parse("0102:0304:0506:0708:090a:0b0c:0d0e:0f10"),
                Port = 0x1234,
            };

            byte[] expected = new byte[]
            {
                0xee, 0xee, 0xee, 0xee,
                0x00, 0x01, 0x00, 20,
                0x00, 0x02, 0x12, 0x34,
                0x01, 0x02, 0x03, 0x04,
                0x05, 0x06, 0x07, 0x08,
                0x09, 0x0a, 0x0b, 0x0c,
                0x0d, 0x0e, 0x0f, 0x10,
            };

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

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

            Assert.AreEqual(28, startIndex);
            Helpers.AreArrayEqual(expected, actual);

            Assert.AreEqual(AddressFamily.InterNetworkV6, target.IpEndPoint.AddressFamily);
        }
コード例 #3
0
ファイル: TurnMessage.cs プロジェクト: vf1/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;
                    }
                }
            }
        }
コード例 #4
0
        public void ParseIp6Test()
        {
            byte[] bytes = new byte[]
            {
                0x00, 0x01, 0x00, 20,
                0x00, 0x02, 0x12, 0x34,
                0x11, 0x22, 0x33, 0x44,
                0x55, 0x66, 0x77, 0x88,
                0x99, 0xaa, 0xbb, 0xcc,
                0xdd, 0xee, 0xff, 0xf2,
            };

            int startIndex = 0;
            AddressAttribute target = new MappedAddress();
            target.Parse(bytes, ref startIndex);
            Assert.AreEqual(24, startIndex);
            Assert.AreEqual(AttributeType.MappedAddress, target.AttributeType);
            Assert.AreEqual(0x1234, target.Port);
            Assert.AreEqual(@"1122:3344:5566:7788:99aa:bbcc:ddee:fff2", target.IpAddress.ToString());
        }
コード例 #5
0
        public void ParseIp4Test()
        {
            byte[] bytes = new byte[]
            {
                0x00, 0x01, 0x00, 0x08,
                0xff, 0x01, 0x12, 0x34,
                0x01, 0x02, 0x03, 0x04,
            };

            int startIndex = 0;
            AddressAttribute target = new MappedAddress();
            target.Parse(bytes, ref startIndex);
            Assert.AreEqual(12, startIndex);
            Assert.AreEqual(AttributeType.MappedAddress, target.AttributeType);
            Assert.AreEqual(0x1234, target.Port);
            Assert.AreEqual(@"1.2.3.4", target.IpAddress.ToString());
        }
コード例 #6
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;
                }
            }
        }