Exemple #1
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));
        }
Exemple #2
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));
        }
        public void CheckPriorityAttributeLengthUnitTest()
        {
            STUNMessage stunRequest = new STUNMessage(STUNMessageTypesEnum.BindingRequest);

            stunRequest.AddUsernameAttribute("dummy:dummy");
            stunRequest.Attributes.Add(new STUNAttribute(STUNAttributeTypesEnum.Priority, BitConverter.GetBytes(1234U)));
            stunRequest.Attributes.Add(new STUNAttribute(STUNAttributeTypesEnum.UseCandidate, null));
            byte[] stunReqBytes = stunRequest.ToByteBufferStringKey("dummy", true);

            var stunReq = STUNMessage.ParseSTUNMessage(stunReqBytes, stunReqBytes.Length);

            Assert.Equal(4, stunReq.Attributes.Single(x => x.AttributeType == STUNAttributeTypesEnum.Priority).Value.Length);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }
        public void IntegrityCheckUnitTest()
        {
            logger.LogDebug("--> " + System.Reflection.MethodBase.GetCurrentMethod().Name);
            logger.BeginScope(System.Reflection.MethodBase.GetCurrentMethod().Name);

            string icePassword = "******";

            STUNMessage stunRequest = new STUNMessage(STUNMessageTypesEnum.BindingRequest);

            stunRequest.Header.TransactionId = Encoding.ASCII.GetBytes(Crypto.GetRandomString(STUNHeader.TRANSACTION_ID_LENGTH));
            stunRequest.AddUsernameAttribute("xxxx:yyyy");
            stunRequest.Attributes.Add(new STUNAttribute(STUNAttributeTypesEnum.Priority, BitConverter.GetBytes(1)));

            var buffer = stunRequest.ToByteBufferStringKey(icePassword, true);

            //logger.LogDebug($"HMAC: {buffer.Skip(buffer.Length - ).Take(20).ToArray().HexStr()}.");
            //logger.LogDebug($"Fingerprint: {buffer.Skip(buffer.Length -4).ToArray().HexStr()}.");

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

            Assert.True(rndTripReq.isFingerprintValid);
            Assert.True(rndTripReq.CheckIntegrity(System.Text.Encoding.UTF8.GetBytes(icePassword)));
        }
Exemple #7
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 + ".");
            }
        }