Пример #1
0
        /// <summary>
        /// Handler for receiving packets from a Peer on a dummy TURN relay socket. The packets arrive from a peer
        /// and need to be forwarded to the client in a STUN Data Indication message.
        /// </summary>
        /// <param name="receiver">The receiver the packet was received on.</param>
        /// <param name="localPort">The port number the packet was received on.</param>
        /// <param name="remoteEndPoint">The end point of the peer sending traffic to the TURN server.</param>
        /// <param name="packet">The byes received from the peer.</param>
        private void OnRelayPacketReceived(UdpReceiver receiver, int localPort, IPEndPoint remoteEndPoint, byte[] packet)
        {
            STUNMessage dataInd = new STUNMessage(STUNMessageTypesEnum.DataIndication);

            dataInd.Attributes.Add(new STUNAttribute(STUNAttributeTypesEnum.Data, packet));
            dataInd.AddXORPeerAddressAttribute(remoteEndPoint.Address, remoteEndPoint.Port);

            _clientSocket.SendTo(dataInd.ToByteBuffer(null, false), _clientEndPoint);
        }
Пример #2
0
        public void STUNWithUsernameToBytesUnitTest()
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

            STUNMessage initMessage = new STUNMessage(STUNMessageTypesEnum.BindingRequest);

            initMessage.AddUsernameAttribute("someusername");
            byte[] stunMessageBytes = initMessage.ToByteBuffer();

            Console.WriteLine(BitConverter.ToString(stunMessageBytes));
        }
Пример #3
0
        public void STUNWithUsernameToBytesUnitTest()
        {
            logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            STUNMessage initMessage = new STUNMessage(STUNMessageTypesEnum.BindingRequest);

            initMessage.AddUsernameAttribute("someusername");
            byte[] stunMessageBytes = initMessage.ToByteBuffer();

            logger.LogDebug(BitConverter.ToString(stunMessageBytes));
        }
Пример #4
0
        public void PutResponseToBufferTestMethod()
        {
            logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            STUNMessage stunResponse = new STUNMessage(STUNMessageTypesEnum.BindingSuccessResponse);
            stunResponse.Header.TransactionId = Guid.NewGuid().ToByteArray().Take(12).ToArray();
            //stunResponse.AddFingerPrintAttribute();
            stunResponse.AddXORMappedAddressAttribute(IPAddress.Parse("127.0.0.1"), 1234);

            byte[] buffer = stunResponse.ToByteBuffer(null, true);
        }
Пример #5
0
        private void Answered(SDP xmppSDP)
        {
            //Console.WriteLine("Yay call answered.");
            //Console.WriteLine(sdp.ToString());
            m_xmppServerEndPoint = SDP.GetSDPRTPEndPoint(xmppSDP.ToString());
            logger.Debug("Sending STUN binding request to " + m_xmppServerEndPoint + ".");
            STUNMessage initMessage = new STUNMessage(STUNMessageTypesEnum.BindingRequest);

            initMessage.AddUsernameAttribute(xmppSDP.IceUfrag + m_localSTUNUFrag);
            byte[] stunMessageBytes = initMessage.ToByteBuffer();
            m_xmppMediaSocket.Send(stunMessageBytes, stunMessageBytes.Length, m_xmppServerEndPoint);

            m_uas.Answer("application/sdp", GetSDPForSIPResponse().ToString(), null, SIPDialogueTransferModesEnum.NotAllowed);
        }
Пример #6
0
        /// <summary>
        /// Event handler for an answer on an outgoing Google Voice call.
        /// </summary>
        /// <param name="xmppSDP">The SDP packet received from the Google Voice gateway.</param>
        private void XMPPAnswered(SDP xmppSDP)
        {
            StatusMessage("Google Voice call answered.");

            IPEndPoint remoteSDPEndPoint = SDP.GetSDPRTPEndPoint(xmppSDP.ToString());

            _audioChannel.SetRemoteRTPEndPoint(remoteSDPEndPoint);

            // Google Voice require that a STUN exchange occurs on the RTP socket before the RTP packet can flow.
            // This code block sends a STUN binding request to the Google Voice gateway.
            STUNMessage initMessage = new STUNMessage(STUNMessageTypesEnum.BindingRequest);

            initMessage.AddUsernameAttribute(xmppSDP.IceUfrag + m_localSTUNUFrag);
            byte[] stunMessageBytes = initMessage.ToByteBuffer();
            //_audioChannel.SendRTPRaw(stunMessageBytes, stunMessageBytes.Length);
        }
Пример #7
0
        private void OnPacketReceived(UdpReceiver receiver, int localPort, IPEndPoint remoteEndPoint, byte[] packet)
        {
            STUNMessage stunMessage = STUNMessage.ParseSTUNMessage(packet, packet.Length);

            switch (stunMessage.Header.MessageType)
            {
            case STUNMessageTypesEnum.Allocate:

                logger.LogDebug($"MockTurnServer received Allocate request from {remoteEndPoint}.");

                if (_relaySocket == null)
                {
                    _clientEndPoint = remoteEndPoint;

                    // Create a new relay socket.
                    NetServices.CreateRtpSocket(false, _listenAddress, 0, out _relaySocket, out _);

                    _relayEndPoint = _relaySocket.LocalEndPoint as IPEndPoint;

                    logger.LogDebug($"MockTurnServer created relay socket on {_relayEndPoint}.");

                    _relayListener = new UdpReceiver(_relaySocket);
                    _relayListener.OnPacketReceived += OnRelayPacketReceived;
                    _relayListener.OnClosed         += (reason) => logger.LogDebug($"MockTurnServer relay on {_relayEndPoint} closed.");
                    _relayListener.BeginReceiveFrom();
                }

                STUNMessage allocateResponse = new STUNMessage(STUNMessageTypesEnum.AllocateSuccessResponse);
                allocateResponse.Header.TransactionId = stunMessage.Header.TransactionId;
                allocateResponse.AddXORMappedAddressAttribute(remoteEndPoint.Address, remoteEndPoint.Port);
                allocateResponse.AddXORAddressAttribute(STUNAttributeTypesEnum.XORRelayedAddress, _relayEndPoint.Address, _relayEndPoint.Port);

                _clientSocket.SendTo(allocateResponse.ToByteBuffer(null, false), remoteEndPoint);
                break;

            case STUNMessageTypesEnum.BindingRequest:

                logger.LogDebug($"MockTurnServer received Binding request from {remoteEndPoint}.");

                STUNMessage stunResponse = new STUNMessage(STUNMessageTypesEnum.BindingSuccessResponse);
                stunResponse.Header.TransactionId = stunMessage.Header.TransactionId;
                stunResponse.AddXORMappedAddressAttribute(remoteEndPoint.Address, remoteEndPoint.Port);
                _clientSocket.SendTo(stunResponse.ToByteBuffer(null, false), remoteEndPoint);
                break;

            case STUNMessageTypesEnum.CreatePermission:

                logger.LogDebug($"MockTurnServer received CreatePermission request from {remoteEndPoint}.");

                STUNMessage permResponse = new STUNMessage(STUNMessageTypesEnum.CreatePermissionSuccessResponse);
                permResponse.Header.TransactionId = stunMessage.Header.TransactionId;
                _clientSocket.SendTo(permResponse.ToByteBuffer(null, false), remoteEndPoint);
                break;

            case STUNMessageTypesEnum.SendIndication:

                logger.LogDebug($"MockTurnServer received SendIndication request from {remoteEndPoint}.");
                var buffer = stunMessage.Attributes.Single(x => x.AttributeType == STUNAttributeTypesEnum.Data).Value;
                var destEP = (stunMessage.Attributes.Single(x => x.AttributeType == STUNAttributeTypesEnum.XORPeerAddress) as STUNXORAddressAttribute).GetIPEndPoint();

                logger.LogDebug($"MockTurnServer relaying {buffer.Length} bytes to {destEP}.");

                _relaySocket.SendTo(buffer, destEP);

                break;

            default:
                logger.LogDebug($"MockTurnServer received unknown STUN message from {remoteEndPoint}.");
                break;
            }
        }
Пример #8
0
        private void ListenForXMPPServerMedia(UdpClient localSocket)
        {
            try
            {
                logger.Debug("Commencing listen for media from XMPP server on local socket " + m_localSIPEndPoint + ".");
                bool stunResponseSent = false;

                IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[]     buffer         = localSocket.Receive(ref remoteEndPoint);
                while (buffer != null && buffer.Length > 0 && !m_exit)
                {
                    //if (!stunResponseSent && buffer[0] >> 0xef == 0)
                    if (!stunResponseSent)
                    {
                        //logger.Debug(buffer.Length + " bytes read on media socket from " + remoteEndPoint.ToString() + ", byte[0]=" + buffer[0].ToString() + ".");

                        STUNMessage stunMessage = STUNMessage.ParseSTUNMessage(buffer, buffer.Length);

                        logger.Debug("STUN message received " + stunMessage.Header.MessageType + ".");

                        if (stunMessage.Header.MessageType == STUNMessageTypesEnum.BindingRequest)
                        {
                            logger.Debug("Sending STUN response to " + remoteEndPoint + ".");
                            stunResponseSent = true;
                            STUNMessage stunResponse = new STUNMessage();
                            stunResponse.Header.MessageType   = STUNMessageTypesEnum.BindingResponse;
                            stunResponse.Header.TransactionId = stunMessage.Header.TransactionId;
                            stunResponse.AddUsernameAttribute(Encoding.UTF8.GetString(stunMessage.Attributes[0].Value));
                            byte[] stunRespBytes = stunResponse.ToByteBuffer();
                            m_xmppMediaSocket.Send(stunRespBytes, stunRespBytes.Length, remoteEndPoint);
                        }
                        else
                        {
                            //if (stunMessage.Attributes.Count > 0)
                            //{
                            //    foreach (STUNAttribute stunAttribute in stunMessage.Attributes)
                            //    {
                            //        Console.WriteLine(" " + stunAttribute.AttributeType + "=" + stunAttribute.Value + ".");
                            //    }
                            //}
                        }
                    }

                    if (buffer.Length > 100)
                    {
                        m_sipMediaSocket.Send(buffer, buffer.Length, m_sipPhoneRTPEndPoint);
                    }

                    buffer = localSocket.Receive(ref remoteEndPoint);
                }
            }
            catch (SocketException)
            { }
            catch (Exception excp)
            {
                logger.Error("Exception ListenForXMPPServerMedia. " + excp.Message);
            }
            finally
            {
                logger.Debug("Shutting down listen for SIP phone media on local socket " + m_localSIPEndPoint + ".");
            }
        }