Пример #1
0
        public void PerformRequest()
        {
            ResponseMessage = null;
            WaitHandle.Reset();
            EndPoint epStun = SocketServer.ConnectMgr.GetIPEndpoint(StunServer, StunPort);

            STUNMessage msgRequest = new STUNMessage();
            msgRequest.Method = StunMethod.Binding;
            msgRequest.Class = StunClass.Request;

            MappedAddressAttribute mattr = new MappedAddressAttribute();
            mattr.IPAddress = LocalEndpoint.Address;
            mattr.Port = (ushort) LocalEndpoint.Port;

            msgRequest.AddAttribute(mattr);

            SocketServer.UDPSocketClient client = new SocketServer.UDPSocketClient(LocalEndpoint);
            client.OnReceivePacket += new SocketServer.UDPSocketClient.DelegateReceivePacket(client_OnReceivePacket3);
            #if !WINDOWS_PHONE
            client.StartReceiving(true);
            #else
            client.StartReceiving();
            #endif

            byte [] bMessage = msgRequest.Bytes;
            client.SendUDP(bMessage, bMessage.Length, epStun);
        }
Пример #2
0
        /// <summary>
        /// Read the next attribute from the stream
        /// </summary>
        /// <param name="bBytes"></param>
        /// <param name="parentmessage"></param>
        /// <returns>The number of bytes read, or 0 if no more can be read</returns>
        public int ReadFromBytes(byte[] bBytes, int nAt, bool bDwordAlign, STUNMessage parentmessage)
        {
            if ((bBytes.Length - nAt) < 4)
                return 0;

            ushort nType = (ushort)((bBytes[0 + nAt] << 8) | bBytes[1 + nAt]);
            ushort nLength = (ushort)((bBytes[2 + nAt] << 8) | bBytes[3 + nAt]);

            if (bDwordAlign == true)
            {
                while ((nLength % 4) != 0)
                    nLength++;
            }

            if ((bBytes.Length-nAt) < (nLength - 4))
                return 0;

            byte[] bAttribute = new byte[nLength];
            Array.Copy(bBytes, 4+nAt, bAttribute, 0, bAttribute.Length);

            BuildAttribute(nType, bAttribute, bDwordAlign, parentmessage);

            return 4 + nLength;
        }
Пример #3
0
 public virtual void SetBytes(byte[] bBytes, STUNMessage parentmessage)
 {
     m_bBytes = bBytes;
 }
Пример #4
0
 public override void SetBytes(byte[] bBytes, STUNMessage parentmessage)
 {
     m_bBytes = bBytes;
     Software = System.Text.UTF8Encoding.UTF8.GetString(m_bBytes, 0, m_bBytes.Length);
 }
Пример #5
0
 public override void SetBytes(byte[] bBytes, STUNMessage parentmessage)
 {
     m_bBytes = bBytes;
     Priority = (int) SocketServer.TLS.ByteHelper.ReadUintBigEndian(bBytes, 0);
 }
Пример #6
0
 public override void SetBytes(byte[] bBytes, STUNMessage parentmessage)
 {
     m_bBytes = bBytes;
 }
Пример #7
0
        public void ComputeHMACShortTermCredentials(STUNMessage msgWithoutHMAC, int nLengthWithoutMessageIntegrity, string strPassword)
        {
            if (strPassword == null)
                strPassword = "";

            /// No MD5 on short term credentials
            byte[] bKey = System.Text.UTF8Encoding.UTF8.GetBytes(strPassword);

            byte[] bBytes = msgWithoutHMAC.Bytes;

            System.Security.Cryptography.HMACSHA1 sha1 = new System.Security.Cryptography.HMACSHA1(bKey);
            HMAC = sha1.ComputeHash(bBytes, 0, nLengthWithoutMessageIntegrity);
        }
Пример #8
0
        public override void SetBytes(byte[] bBytes, STUNMessage parentmessage)
        {
            m_bBytes = bBytes;
            if (m_bBytes.Length < 8) // somethin rong
                return;

            AddressFamily = (StunAddressFamily)m_bBytes[1];
            Port = (ushort)((m_bBytes[2] << 8) | (m_bBytes[3]));

            int nIPLength = 4;
            if (AddressFamily == RTP.StunAddressFamily.IPv6)
                nIPLength = 16;
            byte[] bIPAddress = new byte[nIPLength];

            Array.Copy(m_bBytes, 4, bIPAddress, 0, nIPLength);
            IPAddress = new IPAddress(bIPAddress);
        }
Пример #9
0
 public override void SetBytes(byte[] bBytes, STUNMessage parentmessage)
 {
     m_bBytes = bBytes;
     Class = (byte)(bBytes[2] & 0x07);
     Number = bBytes[3];
     ReasonPhrase = System.Text.UTF8Encoding.UTF8.GetString(m_bBytes, 1, m_bBytes.Length - 1);
 }
Пример #10
0
 public override byte[] GetBytes(STUNMessage parentmessage)
 {
     byte[] bPhrase = System.Text.UTF8Encoding.UTF8.GetBytes(ReasonPhrase);
     m_bBytes = new byte[bPhrase.Length + 4];
     m_bBytes[2] = (byte)(Class & 0x07);
     m_bBytes[2] = (byte)Number;
     Array.Copy(bPhrase, 0, m_bBytes, 1, bPhrase.Length);
     return m_bBytes;
 }
Пример #11
0
 void client_OnReceivePacket3(byte[] bData, int nLength, IPEndPoint epfrom, IPEndPoint epthis, DateTime dtReceived)
 {
     ResponseMessage = new STUNMessage();
     ResponseMessage.Bytes = bData;
     WaitHandle.Set();
 }
Пример #12
0
        public IPEndPoint RecvSTUN(DnsEndPoint serverEp,int timeout)
        {
            byte[] recv = RTPUDPClient.ReceiveByteArray(serverEp.Port,timeout);
            STUNMessage ResponseMessage = new STUNMessage();
            ResponseMessage.Bytes = recv;
            IPEndPoint retep = LocalEndpoint;
            if (ResponseMessage != null)
            {
                foreach (STUNAttributeContainer cont in ResponseMessage.Attributes)
                {
                    if (cont.ParsedAttribute.Type == StunAttributeType.MappedAddress)
                    {

                        MappedAddressAttribute attrib = cont.ParsedAttribute as MappedAddressAttribute;
                        retep = new IPEndPoint(attrib.IPAddress, attrib.Port);
                    }
                }
            }
            return retep;
        }
Пример #13
0
        void  RTPUDPClient_OnReceiveMessage(byte[] bData, int nLength, IPEndPoint epfrom, IPEndPoint epthis, DateTime dtReceived)
        {
            /// if we are an performing ICE, see if this is an ICE packet instead of an RTP one
            if (nLength >= 8)
            {
                //0x2112A442
                if ((bData[4] == 0x21) && (bData[5] == 0x12) && (bData[6] == 0xA4) && (bData[7] == 0x42))
                {
                    /// STUN message
                    STUNMessage smsg  = new STUN2Message();
                    byte[]      bStun = new byte[nLength];
                    Array.Copy(bData, 0, bStun, 0, nLength);
                    try
                    {
                        smsg.Bytes = bStun;
                    }
                    catch (Exception ex)
                    {
                        smsg       = new STUNMessage();
                        smsg.Bytes = bStun;
                    }

                    STUNRequestResponse foundreq = null;
                    lock (StunLock)
                    {
                        foreach (STUNRequestResponse queuedreq in StunRequestResponses)
                        {
                            if (queuedreq.IsThisYourResponseSetIfItIs(smsg) == true)
                            {
                                foundreq = queuedreq;
                                break;
                            }
                        }

                        if (foundreq != null)
                        {
                            StunRequestResponses.Remove(foundreq);
                            return;
                        }
                    }

                    if (OnUnhandleSTUNMessage != null)
                    {
                        OnUnhandleSTUNMessage(smsg, epfrom);
                    }
                    return;
                }
            }

            RTPPacket packet = RTPPacket.BuildPacket(bData, 0, nLength);

            if (packet != null) /// Seems we get some TURN channel data messages from google talk
            {
                if (ReceiveSSRC == 0)
                {
                    ReceiveSSRC = packet.SSRC;
                }
                if ((packet.PayloadType == this.Payload) && (packet.SSRC == this.ReceiveSSRC))
                {
                    IncomingRTPPacketBuffer.AddPacket(packet);
                }
            }
        }
Пример #14
0
 public int SendSTUNMessage(STUNMessage msg, EndPoint epStun)
 {
     byte[] bMessage = msg.Bytes;
     return(this.RTPUDPClient.SendUDP(bMessage, bMessage.Length, epStun));
 }
Пример #15
0
 public override byte[] GetBytes(STUNMessage parentmessage)
 {
     return new byte[] {};
 }
Пример #16
0
        public override byte[] GetBytes(STUNMessage parentmessage)
        {
            byte[] bIPaddress = IPAddress.GetAddressBytes();
            m_bBytes = new byte[4 + bIPaddress.Length];
            m_bBytes[0] = 0;
            m_bBytes[1] = (byte)AddressFamily;
            m_bBytes[2] = (byte)((Port & 0xFF00) >> 8);
            m_bBytes[3] = (byte)(Port & 0xFF);
            Array.Copy(bIPaddress, 0, m_bBytes, 4, 4);

            return m_bBytes;
        }
Пример #17
0
        public override byte[] GetBytes(STUNMessage parentmessage)
        {
            byte[] bMagicCookie = new byte[4];
            bMagicCookie[0] = (byte)((parentmessage.MagicCookie & 0xFF000000) >> 24);
            bMagicCookie[1] = (byte)((parentmessage.MagicCookie & 0x00FF0000) >> 16);
            bMagicCookie[2] = (byte)((parentmessage.MagicCookie & 0x0000FF00) >> 08);
            bMagicCookie[3] = (byte)((parentmessage.MagicCookie & 0x000000FF) >> 00);

            byte[] bIPAddress = IPAddress.GetAddressBytes();
            m_bBytes = new byte[4 + bIPAddress.Length];
            m_bBytes[0] = 0;
            m_bBytes[1] = (byte)AddressFamily;
            m_bBytes[2] = (byte)(((Port & 0xFF00) >> 8) ^ bMagicCookie[0]);
            m_bBytes[3] = (byte)((Port & 0xFF) ^ bMagicCookie[1]);

            byte[] bXOR = new byte[16];
            Array.Copy(bMagicCookie, 0, bXOR, 0, 4);
            Array.Copy(parentmessage.TransactionId, 0, bXOR, 4, parentmessage.TransactionId.Length);
            for (int i = 0; i < bIPAddress.Length; i++)
            {
                bIPAddress[i] ^= bXOR[i];
            }

            Array.Copy(bIPAddress, 0, m_bBytes, 4, bIPAddress.Length);

            return m_bBytes;
        }
Пример #18
0
        public void ComputeHMACLongTermCredentials(STUNMessage msgWithoutHMAC, int nLengthWithoutMessageIntegrity, string strUserName, string strRealm, string strPassword)
        {
            string strKey = string.Format("{0}:{1}:{2}", strUserName, strRealm, strPassword);
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] bKey = md5.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(strKey));

            byte[] bBytes = msgWithoutHMAC.Bytes;

            System.Security.Cryptography.HMACSHA1 sha1 = new System.Security.Cryptography.HMACSHA1(bKey);
            HMAC = sha1.ComputeHash(bBytes, 0, nLengthWithoutMessageIntegrity);
        }
Пример #19
0
        public override void SetBytes(byte[] bBytes, STUNMessage parentmessage)
        {
            m_bBytes = bBytes;
            if (m_bBytes.Length < 8) // somethin rong
                return;

            byte[] bMagicCookie = new byte[4];
            bMagicCookie[0] = (byte)((parentmessage.MagicCookie & 0xFF000000) >> 24);
            bMagicCookie[1] = (byte)((parentmessage.MagicCookie & 0x00FF0000) >> 16);
            bMagicCookie[2] = (byte)((parentmessage.MagicCookie & 0x0000FF00) >> 08);
            bMagicCookie[3] = (byte)((parentmessage.MagicCookie & 0x000000FF) >> 00);

            AddressFamily = (StunAddressFamily)m_bBytes[1];
            Port = (ushort)(((m_bBytes[2] << 8) ^ bMagicCookie[0]) | (m_bBytes[3] ^ bMagicCookie[1]));

            int nIPLength = 4;
            if (AddressFamily == RTP.StunAddressFamily.IPv6)
                nIPLength = 16;
            byte[] bIPAddress = new byte[nIPLength];

            Array.Copy(m_bBytes, 4, bIPAddress, 0, nIPLength);

            byte[] bXOR = new byte[16];
            Array.Copy(bMagicCookie, 0, bXOR, 0, 4);
            Array.Copy(parentmessage.TransactionId, 0, bXOR, 4, parentmessage.TransactionId.Length);
            for (int i = 0; i < bIPAddress.Length; i++)
            {
                bIPAddress[i] ^= bXOR[i];
            }

            IPAddress = new IPAddress(bIPAddress);
        }
Пример #20
0
 public override byte[] GetBytes(STUNMessage parentmessage)
 {
     return m_bBytes;
 }
Пример #21
0
        public void ComputeCRC(STUNMessage msg, int nLengthWithoutFingerprint)
        {
            byte [] bMessage = msg.Bytes;
            CRC = 0xffffffff;

            for(int n = 0; n < nLengthWithoutFingerprint; ++n)
                CRC = (CRC >> 8) ^ (crctable[(CRC & 0xff) ^ bMessage[n]]);

            CRC ^= 0xffffffff;

            // XOR the result with 0x5354554e
            CRC ^= 0x5354554e;
        }
Пример #22
0
 public override byte[] GetBytes(STUNMessage parentmessage)
 {
     m_bBytes = SocketServer.TLS.ByteHelper.GetBytesForInt32(Priority, SocketServer.TLS.Endianess.Big);
     return m_bBytes;
 }
Пример #23
0
 public override byte[] GetBytes(STUNMessage parentmessage)
 {
     return SocketServer.TLS.ByteHelper.GetBytesForUint64(RandomNumber, SocketServer.TLS.Endianess.Big);
 }
Пример #24
0
 public override byte[] GetBytes(STUNMessage parentmessage)
 {
     m_bBytes = System.Text.UTF8Encoding.UTF8.GetBytes(Software);
     return m_bBytes;
 }
Пример #25
0
 public override void SetBytes(byte[] bBytes, STUNMessage parentmessage)
 {
     m_bBytes = bBytes;
     RandomNumber = SocketServer.TLS.ByteHelper.ReadULongBigEndian(m_bBytes, 0);
 }
Пример #26
0
 public virtual byte[] GetBytes(STUNMessage parentmessage)
 {
     return m_bBytes;
 }
Пример #27
0
        public override byte[] GetBytes(STUNMessage parentmessage)
        {
            m_bBytes = new byte[4];
            if (ChangeIP == true)
                m_bBytes[3] |= 0x04;
            if (ChangePort == true)
                m_bBytes[3] |= 0x02;

            return m_bBytes;
        }
Пример #28
0
        public byte[] GetBytes(STUNMessage parentmessage, bool bDwordAlign)
        {
            byte[] bAttribute = m_objParsedAttribute.GetBytes(parentmessage);
            int nLength = bAttribute.Length + 4;
            if (bDwordAlign == true)
            {
                while ((nLength % 4) != 0)
                    nLength++;
            }

            m_bValue = new byte[nLength];
            Length = (ushort) bAttribute.Length;
            m_bValue[0] = (byte)(((int)m_objParsedAttribute.Type & 0xFF00) >> 8);
            m_bValue[1] = (byte)(((int)m_objParsedAttribute.Type & 0x00FF) >> 0);

            m_bValue[2] = (byte)(((int)Length & 0xFF00) >> 8);
            m_bValue[3] = (byte)(((int)Length & 0x00FF) >> 0);

            Array.Copy(bAttribute, 0, m_bValue, 4, bAttribute.Length);

            return m_bValue;
        }
Пример #29
0
 public override void SetBytes(byte[] bBytes, STUNMessage parentmessage)
 {
     ChangeIP = ((m_bBytes[3] & 0x04) > 0) ? true : false;
     ChangePort = ((m_bBytes[3] & 0x02) > 0) ? true : false;
 }
Пример #30
0
        void BuildAttribute(ushort nType, byte[] bAttribute, bool bDwordAlign, STUNMessage parentmessage)
        {
            if (nType == (ushort)StunAttributeType.MappedAddress)
            {
                m_objParsedAttribute = new MappedAddressAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.AlternateServer)
            {
                m_objParsedAttribute = new AlternateServerAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.ErrorCode)
            {
                m_objParsedAttribute = new ErrorCodeAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.MessageIntegrity)
            {
                m_objParsedAttribute = new MessageIntegrityAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.Fingerprint)
            {
                m_objParsedAttribute = new FingerPrintAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.Nonce)
            {
                m_objParsedAttribute = new NonceAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.Realm)
            {
                m_objParsedAttribute = new RealmAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.Software)
            {
                m_objParsedAttribute = new SoftwareAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.UserName)
            {
                m_objParsedAttribute = new UserNameAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.Password)
            {
                m_objParsedAttribute = new PasswordAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.XorMappedAddress)
            {
                m_objParsedAttribute = new XORMappedAddressAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.LegacySourceAddress)
            {
                m_objParsedAttribute = new LegacySourceAddressAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.LegacyResponseAddress)
            {
                m_objParsedAttribute = new LegacyResponseAddressAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.LegacyChangeAddress)
            {
                m_objParsedAttribute = new LegacyChangeAddressAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.LegacyChangedAddress)
            {
                m_objParsedAttribute = new LegacyChangedAddressAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.Priority)
            {
                m_objParsedAttribute = new PriorityAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.UseCandidate)
            {
                m_objParsedAttribute = new UseCandidateAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.IceControlled)
            {
                m_objParsedAttribute = new IceControlledAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
            else if (nType == (ushort)StunAttributeType.IceControlling)
            {
                m_objParsedAttribute = new IceControllingAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }

            else
            {
                m_objParsedAttribute = new STUNAttribute();
                m_objParsedAttribute.SetBytes(bAttribute, parentmessage);
            }
        }
Пример #31
0
 public STUNRequestResponse(STUNMessage requestmessage)
 {
     RequestMessage = requestmessage;
 }