Example #1
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;
            }
        }
Example #2
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 + ".");
            }
        }