Ejemplo n.º 1
0
        /// <summary>
        /// TODO: Documentation CreatePermission
        /// </summary>
        /// <param name="xorPeerAddress"></param>
        /// <param name="allocation"></param>
        public void CreatePermission(XorMappedAddress xorPeerAddress, TurnAllocation allocation)
        {
            StunMessage msg = new StunMessage(StunMethodType.CreatePermission, StunMethodClass.Request, StunUtilities.NewTransactionId);

            msg.Turn.XorPeerAddress = xorPeerAddress;
            msg.Stun.Username       = new UTF8Attribute(StunAttributeType.Username, allocation.Username);
            msg.Stun.Realm          = new UTF8Attribute(StunAttributeType.Realm, allocation.Realm);
            msg.Stun.Nonce          = new UTF8Attribute(StunAttributeType.Nonce, allocation.Nonce);

            msg.AddMessageIntegrity(allocation.Password, true);

            this.StunClient.BeginSendMessage(msg, allocation);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// TODO: Documentation RefreshAllocation
        /// </summary>
        /// <param name="allocation"></param>
        /// <param name="lifeTime"></param>
        public void RefreshAllocation(TurnAllocation allocation, UInt32 lifeTime)
        {
            StunMessage msg = new StunMessage(StunMethodType.Refresh, StunMethodClass.Request, StunUtilities.NewTransactionId);

            msg.Stun.Username = new UTF8Attribute(StunAttributeType.Username, allocation.Username);
            msg.Stun.Realm    = new UTF8Attribute(StunAttributeType.Realm, allocation.Realm);
            msg.Stun.Nonce    = new UTF8Attribute(StunAttributeType.Nonce, allocation.Nonce);
            msg.Turn.LifeTime = new StunAttribute(StunAttributeType.LifeTime, BitConverter.GetBytes(StunUtilities.ReverseBytes(lifeTime)));

            msg.AddMessageIntegrity(allocation.Password, true);

            this.StunClient.BeginSendMessage(msg, null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// TODO: Documentation SendIndication
        /// </summary>
        /// <param name="xorPeerAddress"></param>
        /// <param name="data"></param>
        /// <param name="allocation"></param>
        public void SendIndication(XorMappedAddress xorPeerAddress, byte[] data, TurnAllocation allocation)
        {
            StunMessage msg = new StunMessage(StunMethodType.Send, StunMethodClass.Indication, StunUtilities.NewTransactionId);

            msg.Turn.XorPeerAddress = xorPeerAddress;
            msg.Turn.Data           = new StunAttribute(StunAttributeType.Data, data);
            msg.Stun.Username       = new UTF8Attribute(StunAttributeType.Username, allocation.Username);
            msg.Stun.Realm          = new UTF8Attribute(StunAttributeType.Realm, allocation.Realm);
            msg.Stun.Nonce          = new UTF8Attribute(StunAttributeType.Nonce, allocation.Nonce);

            msg.AddMessageIntegrity(allocation.Password, true);

            this.StunClient.BeginSendMessage(msg, null);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// TODO: Documentation SendIndication
        /// </summary>
        /// <param name="xorPeerAddress"></param>
        /// <param name="data"></param>
        /// <param name="allocation"></param>
        public void SendIndication(XorMappedAddress xorPeerAddress, byte[] data, TurnAllocation allocation)
        {
            StunMessage msg = new StunMessage(StunMethodType.Send, StunMethodClass.Indication, StunUtilities.NewTransactionId);

            msg.Turn.XorPeerAddress = xorPeerAddress;
            msg.Turn.Data = new StunAttribute(StunAttributeType.Data, data);
            msg.Stun.Username = new UTF8Attribute(StunAttributeType.Username, allocation.Username);
            msg.Stun.Realm = new UTF8Attribute(StunAttributeType.Realm, allocation.Realm);
            msg.Stun.Nonce = new UTF8Attribute(StunAttributeType.Nonce, allocation.Nonce);

            msg.AddMessageIntegrity(allocation.Password, true);

            this.StunClient.BeginSendMessage(msg, null);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// TODO: Documentation BindChannel
        /// </summary>
        /// <param name="channelNumber"></param>
        /// <param name="xorPeerAddress"></param>
        /// <param name="allocation"></param>
        public void BindChannel(byte[] channelNumber, XorMappedAddress xorPeerAddress, TurnAllocation allocation)
        {
            StunMessage msg = new StunMessage(StunMethodType.ChannelBind, StunMethodClass.Request, StunUtilities.NewTransactionId);

            msg.Turn.ChannelNumber = new StunAttribute(StunAttributeType.ChannelNumber, channelNumber);
            msg.Turn.XorPeerAddress = xorPeerAddress;
            msg.Stun.Username = new UTF8Attribute(StunAttributeType.Username, allocation.Username);
            msg.Stun.Realm = new UTF8Attribute(StunAttributeType.Realm, allocation.Realm);
            msg.Stun.Nonce = new UTF8Attribute(StunAttributeType.Nonce, allocation.Nonce);

            msg.AddMessageIntegrity(allocation.Password, true);

            this.StunClient.BeginSendMessage(msg, allocation);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// TODO: Documentation RefreshAllocation
        /// </summary>
        /// <param name="allocation"></param>
        /// <param name="lifeTime"></param>
        public void RefreshAllocation(TurnAllocation allocation, UInt32 lifeTime)
        {
            StunMessage msg = new StunMessage(StunMethodType.Refresh, StunMethodClass.Request, StunUtilities.NewTransactionId);

            msg.Stun.Username = new UTF8Attribute(StunAttributeType.Username, allocation.Username);
            msg.Stun.Realm = new UTF8Attribute(StunAttributeType.Realm, allocation.Realm);
            msg.Stun.Nonce = new UTF8Attribute(StunAttributeType.Nonce, allocation.Nonce);
            msg.Turn.LifeTime = new StunAttribute(StunAttributeType.LifeTime, BitConverter.GetBytes(StunUtilities.ReverseBytes(lifeTime)));

            msg.AddMessageIntegrity(allocation.Password, true);

            this.StunClient.BeginSendMessage(msg, null);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// TODO: Documentation StunClient_OnReceivedSuccessResponse
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="receivedMsg"></param>
        /// <param name="sentMsg"></param>
        /// <param name="transactionObject"></param>
        private void StunClient_OnReceivedSuccessResponse(object sender, StunMessage receivedMsg, StunMessage sentMsg, object transactionObject)
        {
            switch (receivedMsg.MethodType)
            {
                case StunMethodType.Allocate:
                    TurnAllocation allocation = new TurnAllocation()
                    {
                        Username = sentMsg.Stun.Username.ValueString,
                        Password = transactionObject as String,
                        Realm = sentMsg.Stun.Realm.ValueString,
                        Nonce = sentMsg.Stun.Nonce.ValueString,
                        RelayedMappedAddress = receivedMsg.Turn.XorRelayedAddress,
                        MappedAddress = receivedMsg.Stun.XorMappedAddress,
                        StartTime = DateTime.Now,
                        LifeTime = StunUtilities.ReverseBytes(BitConverter.ToUInt32(receivedMsg.Turn.LifeTime.Value, 0))
                    };

                    if (this.Allocations.ContainsKey(receivedMsg.Turn.XorRelayedAddress))
                        this.Allocations[receivedMsg.Turn.XorRelayedAddress] = allocation;
                    else
                    {
                        this.Allocations.Add(receivedMsg.Turn.XorRelayedAddress, allocation);

                        if (this.OnAllocateSucceed != null)
                            this.OnAllocateSucceed(this, allocation, sentMsg, receivedMsg);
                    }
                    break;

                case StunMethodType.CreatePermission:
                    TurnPermission permission = new TurnPermission()
                    {
                        PeerAddress = sentMsg.Turn.XorPeerAddress,
                        StartTime = DateTime.Now,
                        LifeTime = 300
                    };
                    TurnAllocation permAllocation = transactionObject as TurnAllocation;

                    if (permAllocation.Permissions.ContainsKey(sentMsg.Turn.XorPeerAddress))
                        permAllocation.Permissions[sentMsg.Turn.XorPeerAddress] = permission;
                    else
                    {
                        permAllocation.Permissions.Add(sentMsg.Turn.XorPeerAddress, permission);

                        if (this.OnCreatePermissionSucceed != null)
                            this.OnCreatePermissionSucceed(this, permAllocation, permission, sentMsg, receivedMsg);
                    }

                    break;

                case StunMethodType.ChannelBind:
                    TurnChannel channel = new TurnChannel()
                    {
                        Channel = sentMsg.Turn.ChannelNumber,
                        PeerAddress = sentMsg.Turn.XorPeerAddress,
                        StartTime = DateTime.Now,
                        LifeTime = 600
                    };
                    TurnAllocation channelAllocation = transactionObject as TurnAllocation;

                    if (channelAllocation.Channels.ContainsKey(sentMsg.Turn.ChannelNumber))
                        channelAllocation.Channels[sentMsg.Turn.ChannelNumber] = channel;
                    else
                    {
                        channelAllocation.Channels.Add(sentMsg.Turn.ChannelNumber, channel);

                        if (this.OnChannelBindSucceed != null)
                            this.OnChannelBindSucceed(this, transactionObject as TurnAllocation, channel, sentMsg, receivedMsg);
                    }
                    break;

                case StunMethodType.ConnectionBind:
                    if (this.OnConnectionBindSucceed != null)
                        this.OnConnectionBindSucceed(this, transactionObject as Socket, receivedMsg);
                    break;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// TODO: Documentation turnManager_OnAllocateSucceed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="allocation"></param>
        /// <param name="sentMsg"></param>
        /// <param name="receivedMsg"></param>
        private void turnManager_OnAllocateSucceed(object sender, TurnAllocation allocation, StunMessage sentMsg, StunMessage receivedMsg)
        {
            if (this.StartingSessionRecipient != null)
            {
                this.turnSessions[this.StartingSessionSid].TurnAllocation = allocation;

                XmlDocument doc = new XmlDocument();

                // Jingle Transport
                JingleIce jingleIce = new JingleIce(doc)
                {
                    Pwd = JingleUtilities.GenerateIcePwd,
                    Ufrag = JingleUtilities.GenerateIceUfrag
                };

                if (this.OnIceCandidatesGathering != null)
                    this.OnIceCandidatesGathering(this, jingleIce, (sender as TurnManager).HostEP, this.turnSessions[this.StartingSessionSid].UseTurnOnly, allocation);

                this.localCandidates.Add(this.StartingSessionSid, jingleIce.GetCandidates());

                JingleIQ jingleIq = null;

                // Jingle Description
                Element jingleDescription = null;
                String contentName = null;

                if (this.OnDescriptionGathering != null)
                    this.OnDescriptionGathering(this, doc, this.StartingSessionSid, ref jingleDescription, ref contentName);

                jingleIq = this.JingleManager.SessionRequest(this.StartingSessionRecipient,
                                                             this.StartingSessionAction,
                                                             this.StartingSessionSid, contentName,
                                                             jingleDescription, jingleIce);

                //this.jingleManager.FindSession(this.StartingSessionSid).Local = jingleIq.Instruction;

                this.Stream.Write(jingleIq);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// TODO: Documentation StunClient_OnReceivedSuccessResponse
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="receivedMsg"></param>
        /// <param name="sentMsg"></param>
        /// <param name="transactionObject"></param>
        private void StunClient_OnReceivedSuccessResponse(object sender, StunMessage receivedMsg, StunMessage sentMsg, object transactionObject)
        {
            switch (receivedMsg.MethodType)
            {
            case StunMethodType.Allocate:
                TurnAllocation allocation = new TurnAllocation()
                {
                    Username             = sentMsg.Stun.Username.ValueString,
                    Password             = transactionObject as String,
                    Realm                = sentMsg.Stun.Realm.ValueString,
                    Nonce                = sentMsg.Stun.Nonce.ValueString,
                    RelayedMappedAddress = receivedMsg.Turn.XorRelayedAddress,
                    MappedAddress        = receivedMsg.Stun.XorMappedAddress,
                    StartTime            = DateTime.Now,
                    LifeTime             = StunUtilities.ReverseBytes(BitConverter.ToUInt32(receivedMsg.Turn.LifeTime.Value, 0))
                };

                if (this.Allocations.ContainsKey(receivedMsg.Turn.XorRelayedAddress))
                {
                    this.Allocations[receivedMsg.Turn.XorRelayedAddress] = allocation;
                }
                else
                {
                    this.Allocations.Add(receivedMsg.Turn.XorRelayedAddress, allocation);

                    if (this.OnAllocateSucceed != null)
                    {
                        this.OnAllocateSucceed(this, allocation, sentMsg, receivedMsg);
                    }
                }
                break;

            case StunMethodType.CreatePermission:
                TurnPermission permission = new TurnPermission()
                {
                    PeerAddress = sentMsg.Turn.XorPeerAddress,
                    StartTime   = DateTime.Now,
                    LifeTime    = 300
                };
                TurnAllocation permAllocation = transactionObject as TurnAllocation;

                if (permAllocation.Permissions.ContainsKey(sentMsg.Turn.XorPeerAddress))
                {
                    permAllocation.Permissions[sentMsg.Turn.XorPeerAddress] = permission;
                }
                else
                {
                    permAllocation.Permissions.Add(sentMsg.Turn.XorPeerAddress, permission);

                    if (this.OnCreatePermissionSucceed != null)
                    {
                        this.OnCreatePermissionSucceed(this, permAllocation, permission, sentMsg, receivedMsg);
                    }
                }

                break;

            case StunMethodType.ChannelBind:
                TurnChannel channel = new TurnChannel()
                {
                    Channel     = sentMsg.Turn.ChannelNumber,
                    PeerAddress = sentMsg.Turn.XorPeerAddress,
                    StartTime   = DateTime.Now,
                    LifeTime    = 600
                };
                TurnAllocation channelAllocation = transactionObject as TurnAllocation;

                if (channelAllocation.Channels.ContainsKey(sentMsg.Turn.ChannelNumber))
                {
                    channelAllocation.Channels[sentMsg.Turn.ChannelNumber] = channel;
                }
                else
                {
                    channelAllocation.Channels.Add(sentMsg.Turn.ChannelNumber, channel);

                    if (this.OnChannelBindSucceed != null)
                    {
                        this.OnChannelBindSucceed(this, transactionObject as TurnAllocation, channel, sentMsg, receivedMsg);
                    }
                }
                break;

            case StunMethodType.ConnectionBind:
                if (this.OnConnectionBindSucceed != null)
                {
                    this.OnConnectionBindSucceed(this, transactionObject as Socket, receivedMsg);
                }
                break;
            }
        }