GridXmlRpcModule.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Net;
  32. using System.Reflection;
  33. using System.Xml;
  34. using log4net;
  35. using Nwc.XmlRpc;
  36. using OpenMetaverse;
  37. using OpenSim.Data;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Communications;
  40. using OpenSim.Framework.Servers;
  41. using OpenSim.Framework.Servers.HttpServer;
  42. using OpenSim.Grid.Framework;
  43. namespace OpenSim.Grid.GridServer.Modules
  44. {
  45. public class GridXmlRpcModule
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private IRegionProfileService m_gridDBService;
  49. private IGridServiceCore m_gridCore;
  50. protected GridConfig m_config;
  51. protected IMessagingServerDiscovery m_messagingServerMapper;
  52. /// <value>
  53. /// Used to notify old regions as to which OpenSim version to upgrade to
  54. /// </value>
  55. private string m_opensimVersion;
  56. protected BaseHttpServer m_httpServer;
  57. /// <summary>
  58. /// Constructor
  59. /// </summary>
  60. /// <param name="opensimVersion">
  61. /// Used to notify old regions as to which OpenSim version to upgrade to
  62. /// </param>
  63. public GridXmlRpcModule()
  64. {
  65. }
  66. public void Initialise(string opensimVersion, IRegionProfileService gridDBService, IGridServiceCore gridCore, GridConfig config)
  67. {
  68. m_opensimVersion = opensimVersion;
  69. m_gridDBService = gridDBService;
  70. m_gridCore = gridCore;
  71. m_config = config;
  72. RegisterHandlers();
  73. }
  74. public void PostInitialise()
  75. {
  76. IMessagingServerDiscovery messagingModule;
  77. if (m_gridCore.TryGet<IMessagingServerDiscovery>(out messagingModule))
  78. {
  79. m_messagingServerMapper = messagingModule;
  80. }
  81. }
  82. public void RegisterHandlers()
  83. {
  84. //have these in separate method as some servers restart the http server and reregister all the handlers.
  85. m_httpServer = m_gridCore.GetHttpServer();
  86. m_httpServer.AddXmlRPCHandler("simulator_login", XmlRpcSimulatorLoginMethod);
  87. m_httpServer.AddXmlRPCHandler("simulator_data_request", XmlRpcSimulatorDataRequestMethod);
  88. m_httpServer.AddXmlRPCHandler("simulator_after_region_moved", XmlRpcDeleteRegionMethod);
  89. m_httpServer.AddXmlRPCHandler("map_block", XmlRpcMapBlockMethod);
  90. m_httpServer.AddXmlRPCHandler("search_for_region_by_name", XmlRpcSearchForRegionMethod);
  91. }
  92. /// <summary>
  93. /// Returns a XML String containing a list of the neighbouring regions
  94. /// </summary>
  95. /// <param name="reqhandle">The regionhandle for the center sim</param>
  96. /// <returns>An XML string containing neighbour entities</returns>
  97. public string GetXMLNeighbours(ulong reqhandle)
  98. {
  99. string response = String.Empty;
  100. RegionProfileData central_region = m_gridDBService.GetRegion(reqhandle);
  101. RegionProfileData neighbour;
  102. for (int x = -1; x < 2; x++)
  103. {
  104. for (int y = -1; y < 2; y++)
  105. {
  106. if (
  107. m_gridDBService.GetRegion(
  108. Util.UIntsToLong((uint)((central_region.regionLocX + x) * Constants.RegionSize),
  109. (uint)(central_region.regionLocY + y) * Constants.RegionSize)) != null)
  110. {
  111. neighbour =
  112. m_gridDBService.GetRegion(
  113. Util.UIntsToLong((uint)((central_region.regionLocX + x) * Constants.RegionSize),
  114. (uint)(central_region.regionLocY + y) * Constants.RegionSize));
  115. response += "<neighbour>";
  116. response += "<sim_ip>" + neighbour.serverIP + "</sim_ip>";
  117. response += "<sim_port>" + neighbour.serverPort.ToString() + "</sim_port>";
  118. response += "<locx>" + neighbour.regionLocX.ToString() + "</locx>";
  119. response += "<locy>" + neighbour.regionLocY.ToString() + "</locy>";
  120. response += "<regionhandle>" + neighbour.regionHandle.ToString() + "</regionhandle>";
  121. response += "</neighbour>";
  122. }
  123. }
  124. }
  125. return response;
  126. }
  127. /// <summary>
  128. /// Checks that it's valid to replace the existing region data with new data
  129. ///
  130. /// Currently, this means ensure that the keys passed in by the new region
  131. /// match those in the original region. (XXX Is this correct? Shouldn't we simply check
  132. /// against the keys in the current configuration?)
  133. /// </summary>
  134. /// <param name="sim"></param>
  135. /// <returns></returns>
  136. protected virtual void ValidateOverwriteKeys(RegionProfileData sim, RegionProfileData existingSim)
  137. {
  138. if (!(existingSim.regionRecvKey == sim.regionRecvKey && existingSim.regionSendKey == sim.regionSendKey))
  139. {
  140. throw new LoginException(
  141. String.Format(
  142. "Authentication failed when trying to login existing region {0} at location {1} {2} currently occupied by {3}"
  143. + " with the region's send key {4} (expected {5}) and the region's receive key {6} (expected {7})",
  144. sim.regionName, sim.regionLocX, sim.regionLocY, existingSim.regionName,
  145. sim.regionSendKey, existingSim.regionSendKey, sim.regionRecvKey, existingSim.regionRecvKey),
  146. "The keys required to login your region did not match the grid server keys. Please check your grid send and receive keys.");
  147. }
  148. }
  149. /// <summary>
  150. /// Checks that the new region data is valid.
  151. ///
  152. /// Currently, this means checking that the keys passed in by the new region
  153. /// match those in the grid server's configuration.
  154. /// </summary>
  155. ///
  156. /// <param name="sim"></param>
  157. /// <exception cref="LoginException">Thrown if region login failed</exception>
  158. protected virtual void ValidateNewRegionKeys(RegionProfileData sim)
  159. {
  160. if (!(sim.regionRecvKey == m_config.SimSendKey && sim.regionSendKey == m_config.SimRecvKey))
  161. {
  162. throw new LoginException(
  163. String.Format(
  164. "Authentication failed when trying to login new region {0} at location {1} {2}"
  165. + " with the region's send key {3} (expected {4}) and the region's receive key {5} (expected {6})",
  166. sim.regionName, sim.regionLocX, sim.regionLocY,
  167. sim.regionSendKey, m_config.SimRecvKey, sim.regionRecvKey, m_config.SimSendKey),
  168. "The keys required to login your region did not match your existing region keys. Please check your grid send and receive keys.");
  169. }
  170. }
  171. /// <summary>
  172. /// Check that a region's http uri is externally contactable.
  173. /// </summary>
  174. /// <param name="sim"></param>
  175. /// <exception cref="LoginException">Thrown if the region is not contactable</exception>
  176. protected virtual void ValidateRegionContactable(RegionProfileData sim)
  177. {
  178. string regionStatusUrl = String.Format("{0}{1}", sim.httpServerURI, "simstatus/");
  179. string regionStatusResponse;
  180. RestClient rc = new RestClient(regionStatusUrl);
  181. rc.RequestMethod = "GET";
  182. m_log.DebugFormat("[LOGIN]: Contacting {0} for status of region {1}", regionStatusUrl, sim.regionName);
  183. try
  184. {
  185. Stream rs = rc.Request();
  186. StreamReader sr = new StreamReader(rs);
  187. regionStatusResponse = sr.ReadToEnd();
  188. sr.Close();
  189. }
  190. catch (Exception e)
  191. {
  192. throw new LoginException(
  193. String.Format("Region status request to {0} failed", regionStatusUrl),
  194. String.Format(
  195. "The grid service could not contact the http url {0} at your region. Please make sure this url is reachable by the grid service",
  196. regionStatusUrl),
  197. e);
  198. }
  199. if (!regionStatusResponse.Equals("OK"))
  200. {
  201. throw new LoginException(
  202. String.Format(
  203. "Region {0} at {1} returned status response {2} rather than {3}",
  204. sim.regionName, regionStatusUrl, regionStatusResponse, "OK"),
  205. String.Format(
  206. "When the grid service asked for the status of your region, it received the response {0} rather than {1}. Please check your status",
  207. regionStatusResponse, "OK"));
  208. }
  209. }
  210. /// <summary>
  211. /// Construct an XMLRPC error response
  212. /// </summary>
  213. /// <param name="error"></param>
  214. /// <returns></returns>
  215. public static XmlRpcResponse ErrorResponse(string error)
  216. {
  217. XmlRpcResponse errorResponse = new XmlRpcResponse();
  218. Hashtable errorResponseData = new Hashtable();
  219. errorResponse.Value = errorResponseData;
  220. errorResponseData["error"] = error;
  221. return errorResponse;
  222. }
  223. /// <summary>
  224. /// Performed when a region connects to the grid server initially.
  225. /// </summary>
  226. /// <param name="request">The XML RPC Request</param>
  227. /// <returns>Startup parameters</returns>
  228. public XmlRpcResponse XmlRpcSimulatorLoginMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  229. {
  230. RegionProfileData sim;
  231. RegionProfileData existingSim;
  232. Hashtable requestData = (Hashtable)request.Params[0];
  233. UUID uuid;
  234. if (!requestData.ContainsKey("UUID") || !UUID.TryParse((string)requestData["UUID"], out uuid))
  235. {
  236. m_log.Debug("[LOGIN PRELUDE]: Region connected without a UUID, sending back error response.");
  237. return ErrorResponse("No UUID passed to grid server - unable to connect you");
  238. }
  239. try
  240. {
  241. sim = RegionFromRequest(requestData);
  242. }
  243. catch (FormatException e)
  244. {
  245. m_log.Debug("[LOGIN PRELUDE]: Invalid login parameters, sending back error response.");
  246. return ErrorResponse("Wrong format in login parameters. Please verify parameters." + e.ToString());
  247. }
  248. m_log.InfoFormat("[LOGIN BEGIN]: Received login request from simulator: {0}", sim.regionName);
  249. if (!m_config.AllowRegionRegistration)
  250. {
  251. m_log.DebugFormat(
  252. "[LOGIN END]: Disabled region registration blocked login request from simulator: {0}",
  253. sim.regionName);
  254. return ErrorResponse("This grid is currently not accepting region registrations.");
  255. }
  256. int majorInterfaceVersion = 0;
  257. if (requestData.ContainsKey("major_interface_version"))
  258. int.TryParse((string)requestData["major_interface_version"], out majorInterfaceVersion);
  259. if (majorInterfaceVersion != VersionInfo.MajorInterfaceVersion)
  260. {
  261. return ErrorResponse(
  262. String.Format(
  263. "Your region service implements OGS1 interface version {0}"
  264. + " but this grid requires that the region implement OGS1 interface version {1} to connect."
  265. + " Try changing to OpenSimulator {2}",
  266. majorInterfaceVersion, VersionInfo.MajorInterfaceVersion, m_opensimVersion));
  267. }
  268. existingSim = m_gridDBService.GetRegion(sim.regionHandle);
  269. if (existingSim == null || existingSim.UUID == sim.UUID || sim.UUID != sim.originUUID)
  270. {
  271. try
  272. {
  273. if (existingSim == null)
  274. {
  275. ValidateNewRegionKeys(sim);
  276. }
  277. else
  278. {
  279. ValidateOverwriteKeys(sim, existingSim);
  280. }
  281. ValidateRegionContactable(sim);
  282. }
  283. catch (LoginException e)
  284. {
  285. string logMsg = e.Message;
  286. if (e.InnerException != null)
  287. logMsg += ", " + e.InnerException.Message;
  288. m_log.WarnFormat("[LOGIN END]: {0}", logMsg);
  289. return e.XmlRpcErrorResponse;
  290. }
  291. DataResponse insertResponse = m_gridDBService.AddUpdateRegion(sim, existingSim);
  292. switch (insertResponse)
  293. {
  294. case DataResponse.RESPONSE_OK:
  295. m_log.Info("[LOGIN END]: " + (existingSim == null ? "New" : "Existing") + " sim login successful: " + sim.regionName);
  296. break;
  297. case DataResponse.RESPONSE_ERROR:
  298. m_log.Warn("[LOGIN END]: Sim login failed (Error): " + sim.regionName);
  299. break;
  300. case DataResponse.RESPONSE_INVALIDCREDENTIALS:
  301. m_log.Warn("[LOGIN END]: " +
  302. "Sim login failed (Invalid Credentials): " + sim.regionName);
  303. break;
  304. case DataResponse.RESPONSE_AUTHREQUIRED:
  305. m_log.Warn("[LOGIN END]: " +
  306. "Sim login failed (Authentication Required): " +
  307. sim.regionName);
  308. break;
  309. }
  310. XmlRpcResponse response = CreateLoginResponse(sim);
  311. return response;
  312. }
  313. else
  314. {
  315. m_log.Warn("[LOGIN END]: Failed to login region " + sim.regionName + " at location " + sim.regionLocX + " " + sim.regionLocY + " currently occupied by " + existingSim.regionName);
  316. return ErrorResponse("Another region already exists at that location. Please try another.");
  317. }
  318. }
  319. /// <summary>
  320. /// Construct a successful response to a simulator's login attempt.
  321. /// </summary>
  322. /// <param name="sim"></param>
  323. /// <returns></returns>
  324. private XmlRpcResponse CreateLoginResponse(RegionProfileData sim)
  325. {
  326. XmlRpcResponse response = new XmlRpcResponse();
  327. Hashtable responseData = new Hashtable();
  328. response.Value = responseData;
  329. ArrayList SimNeighboursData = GetSimNeighboursData(sim);
  330. responseData["UUID"] = sim.UUID.ToString();
  331. responseData["region_locx"] = sim.regionLocX.ToString();
  332. responseData["region_locy"] = sim.regionLocY.ToString();
  333. responseData["regionname"] = sim.regionName;
  334. responseData["estate_id"] = "1";
  335. responseData["neighbours"] = SimNeighboursData;
  336. responseData["sim_ip"] = sim.serverIP;
  337. responseData["sim_port"] = sim.serverPort.ToString();
  338. responseData["asset_url"] = sim.regionAssetURI;
  339. responseData["asset_sendkey"] = sim.regionAssetSendKey;
  340. responseData["asset_recvkey"] = sim.regionAssetRecvKey;
  341. responseData["user_url"] = sim.regionUserURI;
  342. responseData["user_sendkey"] = sim.regionUserSendKey;
  343. responseData["user_recvkey"] = sim.regionUserRecvKey;
  344. responseData["authkey"] = sim.regionSecret;
  345. // New! If set, use as URL to local sim storage (ie http://remotehost/region.Yap)
  346. responseData["data_uri"] = sim.regionDataURI;
  347. responseData["allow_forceful_banlines"] = m_config.AllowForcefulBanlines;
  348. // Instead of sending a multitude of message servers to the registering sim
  349. // we should probably be sending a single one and parhaps it's backup
  350. // that has responsibility over routing it's messages.
  351. // The Sim won't be contacting us again about any of the message server stuff during it's time up.
  352. responseData["messageserver_count"] = 0;
  353. // IGridMessagingModule messagingModule;
  354. // if (m_gridCore.TryGet<IGridMessagingModule>(out messagingModule))
  355. //{
  356. if (m_messagingServerMapper != null)
  357. {
  358. List<MessageServerInfo> messageServers = m_messagingServerMapper.GetMessageServersList();
  359. responseData["messageserver_count"] = messageServers.Count;
  360. for (int i = 0; i < messageServers.Count; i++)
  361. {
  362. responseData["messageserver_uri" + i] = messageServers[i].URI;
  363. responseData["messageserver_sendkey" + i] = messageServers[i].sendkey;
  364. responseData["messageserver_recvkey" + i] = messageServers[i].recvkey;
  365. }
  366. }
  367. return response;
  368. }
  369. private ArrayList GetSimNeighboursData(RegionProfileData sim)
  370. {
  371. ArrayList SimNeighboursData = new ArrayList();
  372. RegionProfileData neighbour;
  373. Hashtable NeighbourBlock;
  374. //First use the fast method. (not implemented in SQLLite)
  375. List<RegionProfileData> neighbours = m_gridDBService.GetRegions(sim.regionLocX - 1, sim.regionLocY - 1, sim.regionLocX + 1, sim.regionLocY + 1);
  376. if (neighbours.Count > 0)
  377. {
  378. foreach (RegionProfileData aSim in neighbours)
  379. {
  380. NeighbourBlock = new Hashtable();
  381. NeighbourBlock["sim_ip"] = aSim.serverIP;
  382. NeighbourBlock["sim_port"] = aSim.serverPort.ToString();
  383. NeighbourBlock["region_locx"] = aSim.regionLocX.ToString();
  384. NeighbourBlock["region_locy"] = aSim.regionLocY.ToString();
  385. NeighbourBlock["UUID"] = aSim.ToString();
  386. NeighbourBlock["regionHandle"] = aSim.regionHandle.ToString();
  387. if (aSim.UUID != sim.UUID)
  388. {
  389. SimNeighboursData.Add(NeighbourBlock);
  390. }
  391. }
  392. }
  393. else
  394. {
  395. for (int x = -1; x < 2; x++)
  396. {
  397. for (int y = -1; y < 2; y++)
  398. {
  399. if (
  400. m_gridDBService.GetRegion(
  401. Utils.UIntsToLong((uint)((sim.regionLocX + x) * Constants.RegionSize),
  402. (uint)(sim.regionLocY + y) * Constants.RegionSize)) != null)
  403. {
  404. neighbour =
  405. m_gridDBService.GetRegion(
  406. Utils.UIntsToLong((uint)((sim.regionLocX + x) * Constants.RegionSize),
  407. (uint)(sim.regionLocY + y) * Constants.RegionSize));
  408. NeighbourBlock = new Hashtable();
  409. NeighbourBlock["sim_ip"] = neighbour.serverIP;
  410. NeighbourBlock["sim_port"] = neighbour.serverPort.ToString();
  411. NeighbourBlock["region_locx"] = neighbour.regionLocX.ToString();
  412. NeighbourBlock["region_locy"] = neighbour.regionLocY.ToString();
  413. NeighbourBlock["UUID"] = neighbour.UUID.ToString();
  414. NeighbourBlock["regionHandle"] = neighbour.regionHandle.ToString();
  415. if (neighbour.UUID != sim.UUID) SimNeighboursData.Add(NeighbourBlock);
  416. }
  417. }
  418. }
  419. }
  420. return SimNeighboursData;
  421. }
  422. /// <summary>
  423. /// Loads the grid's own RegionProfileData object with data from the XMLRPC simulator_login request from a region
  424. /// </summary>
  425. /// <param name="requestData"></param>
  426. /// <returns></returns>
  427. private RegionProfileData RegionFromRequest(Hashtable requestData)
  428. {
  429. RegionProfileData sim;
  430. sim = new RegionProfileData();
  431. sim.UUID = new UUID((string)requestData["UUID"]);
  432. sim.originUUID = new UUID((string)requestData["originUUID"]);
  433. sim.regionRecvKey = String.Empty;
  434. sim.regionSendKey = String.Empty;
  435. if (requestData.ContainsKey("region_secret"))
  436. {
  437. string regionsecret = (string)requestData["region_secret"];
  438. if (regionsecret.Length > 0)
  439. sim.regionSecret = regionsecret;
  440. else
  441. sim.regionSecret = m_config.SimRecvKey;
  442. }
  443. else
  444. {
  445. sim.regionSecret = m_config.SimRecvKey;
  446. }
  447. sim.regionDataURI = String.Empty;
  448. sim.regionAssetURI = m_config.DefaultAssetServer;
  449. sim.regionAssetRecvKey = m_config.AssetRecvKey;
  450. sim.regionAssetSendKey = m_config.AssetSendKey;
  451. sim.regionUserURI = m_config.DefaultUserServer;
  452. sim.regionUserSendKey = m_config.UserSendKey;
  453. sim.regionUserRecvKey = m_config.UserRecvKey;
  454. sim.serverIP = (string)requestData["sim_ip"];
  455. sim.serverPort = Convert.ToUInt32((string)requestData["sim_port"]);
  456. sim.httpPort = Convert.ToUInt32((string)requestData["http_port"]);
  457. sim.remotingPort = Convert.ToUInt32((string)requestData["remoting_port"]);
  458. sim.regionLocX = Convert.ToUInt32((string)requestData["region_locx"]);
  459. sim.regionLocY = Convert.ToUInt32((string)requestData["region_locy"]);
  460. sim.regionLocZ = 0;
  461. UUID textureID;
  462. if (UUID.TryParse((string)requestData["map-image-id"], out textureID))
  463. {
  464. sim.regionMapTextureID = textureID;
  465. }
  466. // part of an initial brutish effort to provide accurate information (as per the xml region spec)
  467. // wrt the ownership of a given region
  468. // the (very bad) assumption is that this value is being read and handled inconsistently or
  469. // not at all. Current strategy is to put the code in place to support the validity of this information
  470. // and to roll forward debugging any issues from that point
  471. //
  472. // this particular section of the mod attempts to receive a value from the region's xml file by way of
  473. // OSG1GridServices for the region's owner
  474. sim.owner_uuid = (UUID)(string)requestData["master_avatar_uuid"];
  475. try
  476. {
  477. sim.regionRecvKey = (string)requestData["recvkey"];
  478. sim.regionSendKey = (string)requestData["authkey"];
  479. }
  480. catch (KeyNotFoundException) { }
  481. sim.regionHandle = Utils.UIntsToLong((sim.regionLocX * Constants.RegionSize), (sim.regionLocY * Constants.RegionSize));
  482. sim.serverURI = (string)requestData["server_uri"];
  483. sim.httpServerURI = "http://" + sim.serverIP + ":" + sim.httpPort + "/";
  484. sim.regionName = (string)requestData["sim_name"];
  485. try
  486. {
  487. sim.maturity = Convert.ToUInt32((string)requestData["maturity"]);
  488. }
  489. catch (KeyNotFoundException)
  490. {
  491. //older region not providing this key - so default to Mature
  492. sim.maturity = 1;
  493. }
  494. return sim;
  495. }
  496. /// <summary>
  497. /// Returns an XML RPC response to a simulator profile request
  498. /// Performed after moving a region.
  499. /// </summary>
  500. /// <param name="request"></param>
  501. /// <returns></returns>
  502. /// <param name="request">The XMLRPC Request</param>
  503. /// <returns>Processing parameters</returns>
  504. public XmlRpcResponse XmlRpcDeleteRegionMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  505. {
  506. XmlRpcResponse response = new XmlRpcResponse();
  507. Hashtable responseData = new Hashtable();
  508. response.Value = responseData;
  509. //RegionProfileData TheSim = null;
  510. string uuid;
  511. Hashtable requestData = (Hashtable)request.Params[0];
  512. if (requestData.ContainsKey("UUID"))
  513. {
  514. //TheSim = GetRegion(new UUID((string) requestData["UUID"]));
  515. uuid = requestData["UUID"].ToString();
  516. m_log.InfoFormat("[LOGOUT]: Logging out region: {0}", uuid);
  517. // logToDB((new LLUUID((string)requestData["UUID"])).ToString(),"XmlRpcDeleteRegionMethod","", 5,"Attempting delete with UUID.");
  518. }
  519. else
  520. {
  521. responseData["error"] = "No UUID or region_handle passed to grid server - unable to delete";
  522. return response;
  523. }
  524. DataResponse insertResponse = m_gridDBService.DeleteRegion(uuid);
  525. string insertResp = "";
  526. switch (insertResponse)
  527. {
  528. case DataResponse.RESPONSE_OK:
  529. //MainLog.Instance.Verbose("grid", "Deleting region successful: " + uuid);
  530. insertResp = "Deleting region successful: " + uuid;
  531. break;
  532. case DataResponse.RESPONSE_ERROR:
  533. //MainLog.Instance.Warn("storage", "Deleting region failed (Error): " + uuid);
  534. insertResp = "Deleting region failed (Error): " + uuid;
  535. break;
  536. case DataResponse.RESPONSE_INVALIDCREDENTIALS:
  537. //MainLog.Instance.Warn("storage", "Deleting region failed (Invalid Credentials): " + uuid);
  538. insertResp = "Deleting region (Invalid Credentials): " + uuid;
  539. break;
  540. case DataResponse.RESPONSE_AUTHREQUIRED:
  541. //MainLog.Instance.Warn("storage", "Deleting region failed (Authentication Required): " + uuid);
  542. insertResp = "Deleting region (Authentication Required): " + uuid;
  543. break;
  544. }
  545. responseData["status"] = insertResp;
  546. return response;
  547. }
  548. /// <summary>
  549. /// Returns an XML RPC response to a simulator profile request
  550. /// </summary>
  551. /// <param name="request"></param>
  552. /// <returns></returns>
  553. public XmlRpcResponse XmlRpcSimulatorDataRequestMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  554. {
  555. Hashtable requestData = (Hashtable)request.Params[0];
  556. Hashtable responseData = new Hashtable();
  557. RegionProfileData simData = null;
  558. if (requestData.ContainsKey("region_UUID"))
  559. {
  560. UUID regionID = new UUID((string)requestData["region_UUID"]);
  561. simData = m_gridDBService.GetRegion(regionID);
  562. if (simData == null)
  563. {
  564. m_log.WarnFormat("[DATA] didn't find region for regionID {0} from {1}",
  565. regionID, request.Params.Count > 1 ? request.Params[1] : "unknwon source");
  566. }
  567. }
  568. else if (requestData.ContainsKey("region_handle"))
  569. {
  570. //CFK: The if/else below this makes this message redundant.
  571. //CFK: m_log.Info("requesting data for region " + (string) requestData["region_handle"]);
  572. ulong regionHandle = Convert.ToUInt64((string)requestData["region_handle"]);
  573. simData = m_gridDBService.GetRegion(regionHandle);
  574. if (simData == null)
  575. {
  576. m_log.WarnFormat("[DATA] didn't find region for regionHandle {0} from {1}",
  577. regionHandle, request.Params.Count > 1 ? request.Params[1] : "unknwon source");
  578. }
  579. }
  580. else if (requestData.ContainsKey("region_name_search"))
  581. {
  582. string regionName = (string)requestData["region_name_search"];
  583. simData = m_gridDBService.GetRegion(regionName);
  584. if (simData == null)
  585. {
  586. m_log.WarnFormat("[DATA] didn't find region for regionName {0} from {1}",
  587. regionName, request.Params.Count > 1 ? request.Params[1] : "unknwon source");
  588. }
  589. }
  590. else m_log.Warn("[DATA] regionlookup without regionID, regionHandle or regionHame");
  591. if (simData == null)
  592. {
  593. //Sim does not exist
  594. responseData["error"] = "Sim does not exist";
  595. }
  596. else
  597. {
  598. m_log.Info("[DATA]: found " + (string)simData.regionName + " regionHandle = " +
  599. (string)requestData["region_handle"]);
  600. responseData["sim_ip"] = simData.serverIP;
  601. responseData["sim_port"] = simData.serverPort.ToString();
  602. responseData["server_uri"] = simData.serverURI;
  603. responseData["http_port"] = simData.httpPort.ToString();
  604. responseData["remoting_port"] = simData.remotingPort.ToString();
  605. responseData["region_locx"] = simData.regionLocX.ToString();
  606. responseData["region_locy"] = simData.regionLocY.ToString();
  607. responseData["region_UUID"] = simData.UUID.Guid.ToString();
  608. responseData["region_name"] = simData.regionName;
  609. responseData["regionHandle"] = simData.regionHandle.ToString();
  610. }
  611. XmlRpcResponse response = new XmlRpcResponse();
  612. response.Value = responseData;
  613. return response;
  614. }
  615. public XmlRpcResponse XmlRpcMapBlockMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  616. {
  617. int xmin = 980, ymin = 980, xmax = 1020, ymax = 1020;
  618. Hashtable requestData = (Hashtable)request.Params[0];
  619. if (requestData.ContainsKey("xmin"))
  620. {
  621. xmin = (Int32)requestData["xmin"];
  622. }
  623. if (requestData.ContainsKey("ymin"))
  624. {
  625. ymin = (Int32)requestData["ymin"];
  626. }
  627. if (requestData.ContainsKey("xmax"))
  628. {
  629. xmax = (Int32)requestData["xmax"];
  630. }
  631. if (requestData.ContainsKey("ymax"))
  632. {
  633. ymax = (Int32)requestData["ymax"];
  634. }
  635. //CFK: The second log is more meaningful and either standard or fast generally occurs.
  636. //CFK: m_log.Info("[MAP]: World map request for range (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  637. XmlRpcResponse response = new XmlRpcResponse();
  638. Hashtable responseData = new Hashtable();
  639. response.Value = responseData;
  640. IList simProfileList = new ArrayList();
  641. bool fastMode = (m_config.DatabaseProvider == "OpenSim.Data.MySQL.dll" || m_config.DatabaseProvider == "OpenSim.Data.MSSQL.dll");
  642. if (fastMode)
  643. {
  644. List<RegionProfileData> neighbours = m_gridDBService.GetRegions((uint)xmin, (uint)ymin, (uint)xmax, (uint)ymax);
  645. foreach (RegionProfileData aSim in neighbours)
  646. {
  647. Hashtable simProfileBlock = new Hashtable();
  648. simProfileBlock["x"] = aSim.regionLocX.ToString();
  649. simProfileBlock["y"] = aSim.regionLocY.ToString();
  650. //m_log.DebugFormat("[MAP]: Sending neighbour info for {0},{1}", aSim.regionLocX, aSim.regionLocY);
  651. simProfileBlock["name"] = aSim.regionName;
  652. simProfileBlock["access"] = aSim.AccessLevel.ToString();
  653. simProfileBlock["region-flags"] = 512;
  654. simProfileBlock["water-height"] = 0;
  655. simProfileBlock["agents"] = 1;
  656. simProfileBlock["map-image-id"] = aSim.regionMapTextureID.ToString();
  657. // For Sugilite compatibility
  658. simProfileBlock["regionhandle"] = aSim.regionHandle.ToString();
  659. simProfileBlock["sim_ip"] = aSim.serverIP;
  660. simProfileBlock["sim_port"] = aSim.serverPort.ToString();
  661. simProfileBlock["sim_uri"] = aSim.serverURI.ToString();
  662. simProfileBlock["uuid"] = aSim.UUID.ToString();
  663. simProfileBlock["remoting_port"] = aSim.remotingPort.ToString();
  664. simProfileBlock["http_port"] = aSim.httpPort.ToString();
  665. simProfileList.Add(simProfileBlock);
  666. }
  667. m_log.Info("[MAP]: Fast map " + simProfileList.Count.ToString() +
  668. " regions @ (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  669. }
  670. else
  671. {
  672. RegionProfileData simProfile;
  673. for (int x = xmin; x < xmax + 1; x++)
  674. {
  675. for (int y = ymin; y < ymax + 1; y++)
  676. {
  677. ulong regHandle = Utils.UIntsToLong((uint)(x * Constants.RegionSize), (uint)(y * Constants.RegionSize));
  678. simProfile = m_gridDBService.GetRegion(regHandle);
  679. if (simProfile != null)
  680. {
  681. Hashtable simProfileBlock = new Hashtable();
  682. simProfileBlock["x"] = x;
  683. simProfileBlock["y"] = y;
  684. simProfileBlock["name"] = simProfile.regionName;
  685. simProfileBlock["access"] = simProfile.AccessLevel.ToString();
  686. simProfileBlock["region-flags"] = 0;
  687. simProfileBlock["water-height"] = 20;
  688. simProfileBlock["agents"] = 1;
  689. simProfileBlock["map-image-id"] = simProfile.regionMapTextureID.ToString();
  690. // For Sugilite compatibility
  691. simProfileBlock["regionhandle"] = simProfile.regionHandle.ToString();
  692. simProfileBlock["sim_ip"] = simProfile.serverIP.ToString();
  693. simProfileBlock["sim_port"] = simProfile.serverPort.ToString();
  694. simProfileBlock["sim_uri"] = simProfile.serverURI.ToString();
  695. simProfileBlock["uuid"] = simProfile.UUID.ToString();
  696. simProfileBlock["remoting_port"] = simProfile.remotingPort.ToString();
  697. simProfileBlock["http_port"] = simProfile.httpPort;
  698. simProfileList.Add(simProfileBlock);
  699. }
  700. }
  701. }
  702. m_log.Info("[MAP]: Std map " + simProfileList.Count.ToString() +
  703. " regions @ (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  704. }
  705. responseData["sim-profiles"] = simProfileList;
  706. return response;
  707. }
  708. /// <summary>
  709. /// Returns up to <code>maxNumber</code> profiles of regions that have a name starting with <code>name</code>
  710. /// </summary>
  711. /// <param name="request"></param>
  712. /// <returns></returns>
  713. public XmlRpcResponse XmlRpcSearchForRegionMethod(XmlRpcRequest request, IPEndPoint remoteClient)
  714. {
  715. Hashtable requestData = (Hashtable)request.Params[0];
  716. if (!requestData.ContainsKey("name") || !requestData.Contains("maxNumber"))
  717. {
  718. m_log.Warn("[DATA] Invalid region-search request; missing name or maxNumber");
  719. return new XmlRpcResponse(500, "Missing name or maxNumber in region search request");
  720. }
  721. Hashtable responseData = new Hashtable();
  722. string name = (string)requestData["name"];
  723. int maxNumber = Convert.ToInt32((string)requestData["maxNumber"]);
  724. if (maxNumber == 0 || name.Length < 3)
  725. {
  726. // either we didn't want any, or we were too unspecific
  727. responseData["numFound"] = 0;
  728. }
  729. else
  730. {
  731. List<RegionProfileData> sims = m_gridDBService.GetRegions(name, maxNumber);
  732. responseData["numFound"] = sims.Count;
  733. for (int i = 0; i < sims.Count; ++i)
  734. {
  735. RegionProfileData sim = sims[i];
  736. string prefix = "region" + i + ".";
  737. responseData[prefix + "region_name"] = sim.regionName;
  738. responseData[prefix + "region_UUID"] = sim.UUID.ToString();
  739. responseData[prefix + "region_locx"] = sim.regionLocX.ToString();
  740. responseData[prefix + "region_locy"] = sim.regionLocY.ToString();
  741. responseData[prefix + "sim_ip"] = sim.serverIP.ToString();
  742. responseData[prefix + "sim_port"] = sim.serverPort.ToString();
  743. responseData[prefix + "remoting_port"] = sim.remotingPort.ToString();
  744. responseData[prefix + "http_port"] = sim.httpPort.ToString();
  745. responseData[prefix + "map_UUID"] = sim.regionMapTextureID.ToString();
  746. }
  747. }
  748. XmlRpcResponse response = new XmlRpcResponse();
  749. response.Value = responseData;
  750. return response;
  751. }
  752. /// <summary>
  753. /// Construct an XMLRPC registration disabled response
  754. /// </summary>
  755. /// <param name="error"></param>
  756. /// <returns></returns>
  757. public static XmlRpcResponse XmlRPCRegionRegistrationDisabledResponse(string error)
  758. {
  759. XmlRpcResponse errorResponse = new XmlRpcResponse();
  760. Hashtable errorResponseData = new Hashtable();
  761. errorResponse.Value = errorResponseData;
  762. errorResponseData["restricted"] = error;
  763. return errorResponse;
  764. }
  765. }
  766. /// <summary>
  767. /// Exception generated when a simulator fails to login to the grid
  768. /// </summary>
  769. public class LoginException : Exception
  770. {
  771. /// <summary>
  772. /// Return an XmlRpcResponse version of the exception message suitable for sending to a client
  773. /// </summary>
  774. /// <param name="message"></param>
  775. /// <param name="xmlRpcMessage"></param>
  776. public XmlRpcResponse XmlRpcErrorResponse
  777. {
  778. get { return m_xmlRpcErrorResponse; }
  779. }
  780. private XmlRpcResponse m_xmlRpcErrorResponse;
  781. public LoginException(string message, string xmlRpcMessage)
  782. : base(message)
  783. {
  784. // FIXME: Might be neater to refactor and put the method inside here
  785. m_xmlRpcErrorResponse = GridXmlRpcModule.ErrorResponse(xmlRpcMessage);
  786. }
  787. public LoginException(string message, string xmlRpcMessage, Exception e)
  788. : base(message, e)
  789. {
  790. // FIXME: Might be neater to refactor and put the method inside here
  791. m_xmlRpcErrorResponse = GridXmlRpcModule.ErrorResponse(xmlRpcMessage);
  792. }
  793. }
  794. }