GridXmlRpcModule.cs 41 KB

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