private byte[] ConnectionRequest(OSDMap args)
        {
            IGridRegistrationService module = IWC.Registry.RequestModuleInterface <IGridRegistrationService>();
            OSDMap result = new OSDMap();

            if (module != null)
            {
                //Add our URLs for them so that they can connect too
                string theirIdent = args["OurIdentifier"];
                ulong  handle;
                if (ulong.TryParse(theirIdent, out handle))
                {
                    //Fu**in hackers
                    //No making region handle sessionIDs!
                    result["Success"] = false;
                }
                else
                {
                    module.RemoveUrlsForClient(theirIdent);
                    IWC.IsGettingUrlsForIWCConnection = true;
                    result = module.GetUrlForRegisteringClient(theirIdent);
                    IWC.IsGettingUrlsForIWCConnection = false;
                    result["OurIdentifier"]           = Utilities.GetAddress();
                    MainConsole.Instance.Warn(theirIdent + " successfully connected to us");
                    IWC.AddNewConnectionFromRequest(theirIdent, args);
                    result["Success"] = true;
                }
            }

            string       json = OSDParser.SerializeJsonString(result);
            UTF8Encoding enc  = new UTF8Encoding();

            return(enc.GetBytes(json));
        }
        public OSDMap GetUrlsForUser(GridRegion region, UUID userID)
        {
            if ((((RegionFlags)region.Flags) & RegionFlags.Foreign) != RegionFlags.Foreign)
            {
                MainConsole.Instance.Debug("[IWC]: Not a foreign region");
                return(null);
            }
            string host = userID.ToString();
            IGridRegistrationService module = Registry.RequestModuleInterface <IGridRegistrationService>();

            if (module != null)
            {
                module.RemoveUrlsForClient(host);
                module.RemoveUrlsForClient(host + "|" + region.RegionHandle);
                IsGettingUrlsForIWCConnection = true;
                OSDMap map = module.GetUrlForRegisteringClient(host + "|" + region.RegionHandle);
                IsGettingUrlsForIWCConnection = false;

                string url = region.GenericMap["URL"];
                if (url == "")
                {
                    MainConsole.Instance.Warn("[IWC]: Foreign region with no URL");
                    return(null); //What the hell? Its a foreign region, it better have a URL!
                }
                //Remove the /Grid.... stuff
                url = url.Remove(url.Length - 5 - 36);
                OutgoingPublicComms.InformOfURLs(url + "/iwcconnection", map, userID, region.RegionHandle);

                return(map);
            }

            return(null);
        }
        /// <summary>
        /// Create secure Urls that only us and the sim that called us know of
        /// This Urls is used to add/remove agents and other information from the other sim
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public IWCCertificate BuildSecureUrlsForConnection(IWCCertificate c)
        {
            IGridRegistrationService gridRegistration = Registry.RequestModuleInterface <IGridRegistrationService>();

            if (gridRegistration != null)
            {
                IGridService gridService = Registry.RequestModuleInterface <IGridService>();
                if (gridService != null)
                {
                    GridRegion r = gridService.GetRegionByName(UUID.Zero, c.Connection.UserName + "_Link");
                    if (r == null)
                    {
                        uint  rX           = (uint)Util.RandomClass.Next(10000, 1000000);
                        uint  rY           = (uint)Util.RandomClass.Next(10000, 1000000);
                        ulong regionhandle = Utils.UIntsToLong(rX, rY);

                        r              = new GridRegion();
                        r.RegionID     = UUID.Random();
                        r.RegionHandle = regionhandle;
                        IHttpServer server = m_registry.RequestModuleInterface <ISimulationBase>().GetHttpServer(0);
                        r.ExternalHostName = server.HostName;
                        if (r.ExternalHostName.StartsWith("http://"))
                        {
                            r.ExternalHostName = r.ExternalHostName.Remove(0, 7);
                        }
                        else if (r.ExternalHostName.StartsWith("https://"))
                        {
                            r.ExternalHostName = r.ExternalHostName.Remove(0, 8);
                        }
                        r.InternalEndPoint = new IPEndPoint(IPAddress.Any, (int)server.Port);
                        r.Flags            = (int)Aurora.Framework.RegionFlags.Foreign;
                        r.RegionName       = c.Connection.UserName + "_Link";
                        r.RegionType       = "Link";

                        UUID SessionID;
                        gridService.RegisterRegion(r, UUID.Zero, out SessionID);
                    }
                    //Give the basic Urls that we have
                    c.Connection.SecureUrls = gridRegistration.GetUrlForRegisteringClient(c.Connection.UserName, r.RegionHandle);
                }
            }
            return(c);
        }
        /// <summary>
        ///   Query the given host (by connection) and verify that we can connect to it.
        /// </summary>
        /// <param name = "connector">The host to connect to</param>
        /// <returns>The connection that has been recieved from the host</returns>
        public bool AttemptConnection(string host)
        {
            IGridRegistrationService module = IWC.Registry.RequestModuleInterface <IGridRegistrationService>();

            if (module != null)
            {
                module.RemoveUrlsForClient(host);
                IWC.IsGettingUrlsForIWCConnection = true;
                OSDMap callThem = module.GetUrlForRegisteringClient(host);
                IWC.IsGettingUrlsForIWCConnection = false;
                callThem["OurIdentifier"]         = Utilities.GetAddress();

                callThem["Method"] = "ConnectionRequest";
                OSDMap result = WebUtils.PostToService(host, callThem, true, false, true);
                if (result["Success"])
                {
                    //Add their URLs back again
                    MainConsole.Instance.Warn("Successfully Connected to " + host);
                    IWC.AddNewConnectionFromRequest(result["OurIdentifier"], result);
                    return(true);
                }
            }
            return(false);
        }
        public OSDMap GetUrlsForUser(GridRegion region, UUID userID)
        {
            IGridRegistrationService r = Registry.RequestModuleInterface <IGridRegistrationService>();

            return(r.GetUrlForRegisteringClient(userID.ToString(), region.RegionHandle));
        }