Example #1
0
        StunMessage Query(ISocketClient client, StunMessage request, IPEndPoint remoteEndPoint)
        {
            Packet pk = null;

            try
            {
                if (client.Local.IsTcp)
                {
                    // Tcp协议不支持更换IP或者端口
                    if (request.ChangeIP || request.ChangePort)
                    {
                        return(null);
                    }

                    //if (!client.Connected) client.Connect(remoteEndPoint);
                    //client.Send(request.ToArray());
                }
                //else
                //    client.SendTo(request.ToArray(), remoteEndPoint);

                WriteLog("查询 {0} =>{1}", request, remoteEndPoint);

                client.Remote.EndPoint = remoteEndPoint;
                client.Send(request.ToArray());

                pk = client.Receive();
                if (pk == null || pk.Count == 0)
                {
                    return(null);
                }
            }
            catch { return(null); }

            var rs = StunMessage.Read(pk.GetStream());

            //if (rs != null && rs.Type != StunMessageType.BindingResponse) return null;
            if (rs == null)
            {
                return(null);
            }

            // 不是同一个会话不要
            if (!rs.TransactionID.SequenceEqual(request.TransactionID))
            {
                return(null);
            }

            // 不是期望的响应不要
            if (rs.Type != (StunMessageType)((UInt16)request.Type | 0x0100))
            {
                return(null);
            }
            return(rs);
        }
Example #2
0
        StunMessage Query(ISocketClient client, StunMessage request, IPEndPoint remoteEndPoint)
        {
            Byte[] buffer = null;
            try
            {
                if (client.Local.IsTcp)
                {
                    // Tcp协议不支持更换IP或者端口
                    if (request.ChangeIP || request.ChangePort)
                    {
                        return(null);
                    }

                    //if (!client.Connected) client.Connect(remoteEndPoint);
                    //client.Send(request.ToArray());
                }
                //else
                //    client.SendTo(request.ToArray(), remoteEndPoint);

                WriteLog("查询 {0} =>{1}", request, remoteEndPoint);

                client.Remote.EndPoint = remoteEndPoint;
                client.Send(request.ToArray());

                buffer = client.Receive();
                if (buffer == null || buffer.Length == 0)
                {
                    return(null);
                }
            }
            catch { return(null); }

            var rs = StunMessage.Read(new MemoryStream(buffer));

            //if (rs != null && rs.Type != StunMessageType.BindingResponse) return null;
            if (rs == null)
            {
                return(null);
            }

            // 不是同一个会话不要
            if (rs.TransactionID.CompareTo(request.TransactionID) != 0)
            {
                return(null);
            }
            // 不是期望的响应不要
            if (rs.Type != (StunMessageType)((UInt16)request.Type | 0x0100))
            {
                return(null);
            }
            return(rs);
        }
Example #3
0
        /// <summary>绑定</summary>
        /// <param name="request"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        protected void OnBind(StunMessage request, ISocketSession session)
        {
            var rs = new StunMessage
            {
                Type          = StunMessageType.BindingResponse,
                TransactionID = request.TransactionID.ReadBytes(),
                MappedAddress = session.Remote.EndPoint
            };

            //rs.SourceAddress = session.GetRelativeEndPoint(remote.Address);
            if (Public != null)
            {
                if (session.Local.IsTcp)
                {
                    rs.SourceAddress = Public[session.Port + 100000];
                }
                else
                {
                    rs.SourceAddress = Public[session.Port];
                }
            }

            // 找另一个
            ISocketSession session2    = null;
            var            anotherPort = 0;

            for (var i = 0; i < Servers.Count; i++)
            {
                var server = Servers[i];
                if (server.Local.Type == session.Local.Type && server.Local.Port != session.Local.Port)
                {
                    anotherPort = server.Port;
                    if (server.Local.IsTcp)
                    {
                        break;
                    }
                    else
                    {
                        session2 = (server as UdpServer).CreateSession(session.Remote.EndPoint);
                        if (session2 != null)
                        {
                            break;
                        }
                    }
                }
            }
            //rs.ChangedAddress = Partner ?? session2.GetRelativeEndPoint(remote.Address);
            if (Public != null)
            {
                if (session.Local.IsTcp)
                {
                    rs.ChangedAddress = Partner ?? Public[anotherPort + 100000];
                }
                else
                {
                    rs.ChangedAddress = Partner ?? Public[anotherPort];
                }
            }

            var name = Name;

            if (name == GetType().Name)
            {
                name = GetType().FullName;
            }
            rs.ServerName = String.Format("{0} v{1}", name, AssemblyX.Create(Assembly.GetExecutingAssembly()).CompileVersion);

            // 换成另一个
            if (request.ChangePort)
            {
                session = session2;
            }

            session.Send(rs.ToArray());
        }