TravelingAgentInfo UpdateTravelInfo(AgentCircuitData agentCircuit, GridRegion region)
        {
            TravelingAgentInfo travel = new TravelingAgentInfo();
            TravelingAgentInfo old    = null;

            lock (m_TravelingAgents)
            {
                if (m_TravelingAgents.ContainsKey(agentCircuit.SessionID))
                {
                    // Very important! Override whatever this agent comes with.
                    // UserAgentService always sets the IP for every new agent
                    // with the original IP address.
                    agentCircuit.IPAddress = m_TravelingAgents[agentCircuit.SessionID].ClientIPAddress;

                    old = m_TravelingAgents[agentCircuit.SessionID];
                }

                m_TravelingAgents[agentCircuit.SessionID] = travel;
            }
            travel.UserID           = agentCircuit.AgentID;
            travel.GridExternalName = region.ServerURI;
            travel.ServiceToken     = agentCircuit.ServiceSessionID;
            if (old != null)
            {
                travel.ClientIPAddress = old.ClientIPAddress;
            }

            return(old);
        }
        // We need to prevent foreign users with the same UUID as a local user
        public bool AgentIsComingHome(UUID sessionID, string thisGridExternalName)
        {
            if (!m_TravelingAgents.ContainsKey(sessionID))
            {
                return(false);
            }

            TravelingAgentInfo travel = m_TravelingAgents[sessionID];

            string a = travel.GridExternalName, b = thisGridExternalName;

            try
            {
                a = NetworkUtils.GetHostFromDNS(travel.GridExternalName).ToString().ToLower();
            }
            catch
            {
                a = travel.GridExternalName;
            }
            try
            {
                b = NetworkUtils.GetHostFromDNS(thisGridExternalName).ToString().ToLower();
            }
            catch
            {
                b = thisGridExternalName;
            }

            return(a == b);
        }
        TravelingAgentInfo UpdateTravelInfo(AgentCircuitData agentCircuit, GridRegion region)
        {
            TravelingAgentInfo travel = new TravelingAgentInfo ();
            TravelingAgentInfo old = null;
            lock (m_TravelingAgents)
            {
                if (m_TravelingAgents.ContainsKey (agentCircuit.SessionID))
                {
                    // Very important! Override whatever this agent comes with.
                    // UserAgentService always sets the IP for every new agent
                    // with the original IP address.
                    agentCircuit.IPAddress = m_TravelingAgents[agentCircuit.SessionID].ClientIPAddress;

                    old = m_TravelingAgents[agentCircuit.SessionID];
                }

                m_TravelingAgents[agentCircuit.SessionID] = travel;
            }
            travel.UserID = agentCircuit.AgentID;
            travel.GridExternalName = region.ServerURI;
            travel.ServiceToken = agentCircuit.ServiceSessionID;
            if (old != null)
                travel.ClientIPAddress = old.ClientIPAddress;

            return old;
        }
        public bool LoginAgentToGrid(AgentCircuitData agentCircuit, GridRegion gatekeeper, GridRegion finalDestination, IPEndPoint clientIP, out string reason)
        {
            MainConsole.Instance.DebugFormat("[USER AGENT SERVICE]: Request to login user {0} (@{1}) to grid {2}",
                                             agentCircuit.AgentID, ((clientIP == null) ? "stored IP" : clientIP.Address.ToString()), gatekeeper.ServerURI);
            // Take the IP address + port of the gatekeeper (reg) plus the info of finalDestination
            GridRegion region = new GridRegion();

            region.FromOSD(gatekeeper.ToOSD());
            region.ServerURI        = gatekeeper.ServerURI;
            region.ExternalHostName = finalDestination.ExternalHostName;
            region.InternalEndPoint = finalDestination.InternalEndPoint;
            region.RegionName       = finalDestination.RegionName;
            region.RegionID         = finalDestination.RegionID;
            region.RegionLocX       = finalDestination.RegionLocX;
            region.RegionLocY       = finalDestination.RegionLocY;

            // Generate a new service session
            agentCircuit.ServiceSessionID = region.ServerURI + ";" + UUID.Random();
            TravelingAgentInfo old = UpdateTravelInfo(agentCircuit, region);

            bool   success      = false;
            string myExternalIP = string.Empty;
            string gridName     = gatekeeper.ServerURI;

            MainConsole.Instance.DebugFormat("[USER AGENT SERVICE]: this grid: {0}, desired grid: {1}", m_GridName, gridName);

            if (m_GridName == gridName)
            {
                success = m_GatekeeperService.LoginAgent(agentCircuit, finalDestination, out reason);
            }
            else
            {
                success = m_GatekeeperConnector.CreateAgent(region, agentCircuit, (uint)TeleportFlags.ViaLogin, out myExternalIP, out reason);
                if (success)
                {
                    // Report them as nowhere with the LOGIN_STATUS_LOCKED so that they don't get logged out automatically after an hour of not responding via HG
                    m_PresenceService.SetLastPosition(agentCircuit.AgentID.ToString(), AgentInfoHelpers.LOGIN_STATUS_LOCKED, Vector3.Zero, Vector3.Zero);
                }
            }

            if (!success)
            {
                MainConsole.Instance.DebugFormat("[USER AGENT SERVICE]: Unable to login user {0} to grid {1}, reason: {2}",
                                                 agentCircuit.AgentID, region.ServerURI, reason);

                // restore the old travel info
                lock (m_TravelingAgents)
                {
                    if (old == null)
                    {
                        m_TravelingAgents.Remove(agentCircuit.SessionID);
                    }
                    else
                    {
                        m_TravelingAgents[agentCircuit.SessionID] = old;
                    }
                }

                return(false);
            }
            else
            {
                reason = "";
            }

            MainConsole.Instance.DebugFormat("[USER AGENT SERVICE]: Gatekeeper sees me as {0}", myExternalIP);
            // else set the IP addresses associated with this client
            if (clientIP != null)
            {
                m_TravelingAgents[agentCircuit.SessionID].ClientIPAddress = clientIP.Address.ToString();
            }
            m_TravelingAgents[agentCircuit.SessionID].MyIpAddress = myExternalIP;

            return(true);
        }