GridManager.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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.Reflection;
  31. using System.Xml;
  32. using libsecondlife;
  33. using log4net;
  34. using Nwc.XmlRpc;
  35. using OpenSim.Data;
  36. using OpenSim.Data.MySQL;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers;
  39. namespace OpenSim.Grid.GridServer
  40. {
  41. public class GridManager
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private Dictionary<string, IGridData> _plugins = new Dictionary<string, IGridData>();
  45. private Dictionary<string, ILogData> _logplugins = new Dictionary<string, ILogData>();
  46. // This is here so that the grid server can hand out MessageServer settings to regions on registration
  47. private List<MessageServerInfo> _MessageServers = new List<MessageServerInfo>();
  48. public GridConfig Config;
  49. /// <summary>
  50. /// Adds a new grid server plugin - grid servers will be requested in the order they were loaded.
  51. /// </summary>
  52. /// <param name="FileName">The filename to the grid server plugin DLL</param>
  53. public void AddPlugin(string FileName)
  54. {
  55. m_log.Info("[DATA]: Attempting to load " + FileName);
  56. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  57. m_log.Info("[DATA]: Found " + pluginAssembly.GetTypes().Length + " interfaces.");
  58. foreach (Type pluginType in pluginAssembly.GetTypes())
  59. {
  60. if (!pluginType.IsAbstract)
  61. {
  62. // Regions go here
  63. Type typeInterface = pluginType.GetInterface("IGridData", true);
  64. if (typeInterface != null)
  65. {
  66. IGridData plug =
  67. (IGridData)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  68. plug.Initialise();
  69. _plugins.Add(plug.getName(), plug);
  70. m_log.Info("[DATA]: Added IGridData Interface");
  71. }
  72. // Logs go here
  73. typeInterface = pluginType.GetInterface("ILogData", true);
  74. if (typeInterface != null)
  75. {
  76. ILogData plug =
  77. (ILogData)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  78. plug.Initialise();
  79. _logplugins.Add(plug.getName(), plug);
  80. m_log.Info("[DATA]: Added ILogData Interface");
  81. }
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// Logs a piece of information to the database
  87. /// </summary>
  88. /// <param name="target">What you were operating on (in grid server, this will likely be the region UUIDs)</param>
  89. /// <param name="method">Which method is being called?</param>
  90. /// <param name="args">What arguments are being passed?</param>
  91. /// <param name="priority">How high priority is this? 1 = Max, 6 = Verbose</param>
  92. /// <param name="message">The message to log</param>
  93. private void logToDB(string target, string method, string args, int priority, string message)
  94. {
  95. foreach (KeyValuePair<string, ILogData> kvp in _logplugins)
  96. {
  97. try
  98. {
  99. kvp.Value.saveLog("Gridserver", target, method, args, priority, message);
  100. }
  101. catch (Exception)
  102. {
  103. m_log.Warn("[storage]: Unable to write log via " + kvp.Key);
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Returns a region by argument
  109. /// </summary>
  110. /// <param name="uuid">A UUID key of the region to return</param>
  111. /// <returns>A SimProfileData for the region</returns>
  112. public RegionProfileData GetRegion(LLUUID uuid)
  113. {
  114. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  115. {
  116. try
  117. {
  118. return kvp.Value.GetProfileByLLUUID(uuid);
  119. }
  120. catch (Exception e)
  121. {
  122. m_log.Warn("[storage]: GetRegion - " + e.Message);
  123. }
  124. }
  125. return null;
  126. }
  127. /// <summary>
  128. /// Returns a region by argument
  129. /// </summary>
  130. /// <param name="uuid">A regionHandle of the region to return</param>
  131. /// <returns>A SimProfileData for the region</returns>
  132. public RegionProfileData GetRegion(ulong handle)
  133. {
  134. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  135. {
  136. try
  137. {
  138. return kvp.Value.GetProfileByHandle(handle);
  139. }
  140. catch
  141. {
  142. m_log.Warn("[storage]: Unable to find region " + handle.ToString() + " via " + kvp.Key);
  143. }
  144. }
  145. return null;
  146. }
  147. /// <summary>
  148. /// Returns a region by argument
  149. /// </summary>
  150. /// <param name="regionName">A partial regionName of the region to return</param>
  151. /// <returns>A SimProfileData for the region</returns>
  152. public RegionProfileData GetRegion(string regionName)
  153. {
  154. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  155. {
  156. try
  157. {
  158. return kvp.Value.GetProfileByString(regionName);
  159. }
  160. catch
  161. {
  162. m_log.Warn("[storage]: Unable to find region " + regionName + " via " + kvp.Key);
  163. }
  164. }
  165. return null;
  166. }
  167. public Dictionary<ulong, RegionProfileData> GetRegions(uint xmin, uint ymin, uint xmax, uint ymax)
  168. {
  169. Dictionary<ulong, RegionProfileData> regions = new Dictionary<ulong, RegionProfileData>();
  170. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  171. {
  172. try
  173. {
  174. RegionProfileData[] neighbours = kvp.Value.GetProfilesInRange(xmin, ymin, xmax, ymax);
  175. foreach (RegionProfileData neighbour in neighbours)
  176. {
  177. regions[neighbour.regionHandle] = neighbour;
  178. }
  179. }
  180. catch
  181. {
  182. m_log.Warn("[storage]: Unable to query regionblock via " + kvp.Key);
  183. }
  184. }
  185. return regions;
  186. }
  187. /// <summary>
  188. /// Returns a XML String containing a list of the neighbouring regions
  189. /// </summary>
  190. /// <param name="reqhandle">The regionhandle for the center sim</param>
  191. /// <returns>An XML string containing neighbour entities</returns>
  192. public string GetXMLNeighbours(ulong reqhandle)
  193. {
  194. string response = String.Empty;
  195. RegionProfileData central_region = GetRegion(reqhandle);
  196. RegionProfileData neighbour;
  197. for (int x = -1; x < 2; x++)
  198. {
  199. for (int y = -1; y < 2; y++)
  200. {
  201. if (
  202. GetRegion(
  203. Util.UIntsToLong((uint)((central_region.regionLocX + x) * Constants.RegionSize),
  204. (uint)(central_region.regionLocY + y) * Constants.RegionSize)) != null)
  205. {
  206. neighbour =
  207. GetRegion(
  208. Util.UIntsToLong((uint)((central_region.regionLocX + x) * Constants.RegionSize),
  209. (uint)(central_region.regionLocY + y) * Constants.RegionSize));
  210. response += "<neighbour>";
  211. response += "<sim_ip>" + neighbour.serverIP + "</sim_ip>";
  212. response += "<sim_port>" + neighbour.serverPort.ToString() + "</sim_port>";
  213. response += "<locx>" + neighbour.regionLocX.ToString() + "</locx>";
  214. response += "<locy>" + neighbour.regionLocY.ToString() + "</locy>";
  215. response += "<regionhandle>" + neighbour.regionHandle.ToString() + "</regionhandle>";
  216. response += "</neighbour>";
  217. }
  218. }
  219. }
  220. return response;
  221. }
  222. /// <summary>
  223. /// Checks that it's valid to replace the existing region data with new data
  224. ///
  225. /// Currently, this means ensure that the keys passed in by the new region
  226. /// match those in the original region. (XXX Is this correct? Shouldn't we simply check
  227. /// against the keys in the current configuration?)
  228. /// </summary>
  229. /// <param name="sim"></param>
  230. /// <returns></returns>
  231. protected virtual bool ValidateOverwrite(RegionProfileData sim, RegionProfileData existingSim)
  232. {
  233. return (existingSim.regionRecvKey == sim.regionRecvKey &&
  234. existingSim.regionSendKey == sim.regionSendKey);
  235. }
  236. /// <summary>
  237. /// Checks that the new region data is valid.
  238. ///
  239. /// Currently, this means checking that the keys passed in by the new region
  240. /// match those in the grid server's configuration.
  241. /// </summary>
  242. /// <param name="sim"></param>
  243. /// <returns></returns>
  244. protected virtual bool ValidateNewRegion(RegionProfileData sim)
  245. {
  246. return (sim.regionRecvKey == Config.SimSendKey &&
  247. sim.regionSendKey == Config.SimRecvKey);
  248. }
  249. private static XmlRpcResponse ErrorResponse(string error)
  250. {
  251. XmlRpcResponse errorResponse = new XmlRpcResponse();
  252. Hashtable errorResponseData = new Hashtable();
  253. errorResponse.Value = errorResponseData;
  254. errorResponseData["error"] = error;
  255. return errorResponse;
  256. }
  257. /// <summary>
  258. /// Performed when a region connects to the grid server initially.
  259. /// </summary>
  260. /// <param name="request">The XMLRPC Request</param>
  261. /// <returns>Startup parameters</returns>
  262. public XmlRpcResponse XmlRpcSimulatorLoginMethod(XmlRpcRequest request)
  263. {
  264. RegionProfileData sim;
  265. RegionProfileData existingSim;
  266. Hashtable requestData = (Hashtable)request.Params[0];
  267. LLUUID uuid;
  268. if (!requestData.ContainsKey("UUID") || !LLUUID.TryParse((string)requestData["UUID"], out uuid))
  269. {
  270. m_log.Info("[GRID]: Region connected without a UUID, ignoring.");
  271. return ErrorResponse("No UUID passed to grid server - unable to connect you");
  272. }
  273. try
  274. {
  275. sim = RegionFromRequest(requestData);
  276. }
  277. catch (FormatException e)
  278. {
  279. m_log.Info("[GRID]: Invalid login parameters, ignoring.");
  280. return ErrorResponse("Wrong format in login parameters. Please verify parameters." + e.ToString() );
  281. }
  282. existingSim = GetRegion(sim.regionHandle);
  283. if (existingSim == null || existingSim.UUID == sim.UUID || sim.UUID != sim.originUUID)
  284. {
  285. bool validated;
  286. if (existingSim == null)
  287. {
  288. validated = ValidateNewRegion(sim);
  289. }
  290. else
  291. {
  292. validated = ValidateOverwrite(sim, existingSim);
  293. }
  294. if (validated)
  295. {
  296. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  297. {
  298. try
  299. {
  300. DataResponse insertResponse;
  301. if( existingSim == null )
  302. {
  303. insertResponse = kvp.Value.AddProfile(sim);
  304. }
  305. else
  306. {
  307. insertResponse = kvp.Value.UpdateProfile(sim);
  308. }
  309. switch (insertResponse)
  310. {
  311. case DataResponse.RESPONSE_OK:
  312. m_log.Info("[grid]: New sim " + (existingSim == null ? "creation" : "connection") + " successful: " + sim.regionName);
  313. break;
  314. case DataResponse.RESPONSE_ERROR:
  315. m_log.Warn("[storage]: New sim creation failed (Error): " + sim.regionName);
  316. break;
  317. case DataResponse.RESPONSE_INVALIDCREDENTIALS:
  318. m_log.Warn("[storage]: " +
  319. "New sim creation failed (Invalid Credentials): " + sim.regionName);
  320. break;
  321. case DataResponse.RESPONSE_AUTHREQUIRED:
  322. m_log.Warn("[storage]: " +
  323. "New sim creation failed (Authentication Required): " +
  324. sim.regionName);
  325. break;
  326. }
  327. }
  328. catch (Exception e)
  329. {
  330. m_log.Warn("[storage]: " +
  331. "Unable to add region " + sim.UUID.ToString() + " via " + kvp.Key);
  332. m_log.Warn("[storage]: " + e.ToString());
  333. }
  334. }
  335. XmlRpcResponse response = CreateLoginResponse(sim);
  336. return response;
  337. }
  338. else
  339. {
  340. if (existingSim == null)
  341. {
  342. m_log.Warn("[grid]: Authentication failed when trying to add new region " + sim.regionName +
  343. " at location " + sim.regionLocX +
  344. " " + sim.regionLocY + " with TheSim.regionRecvKey " + sim.regionRecvKey + "(" + Config.SimSendKey + ") and TheSim.regionRecvKey " + sim.regionSendKey + "(" + Config.SimRecvKey + ") ");
  345. }
  346. else
  347. {
  348. m_log.Warn("[grid]: Authentication failed when trying to add new region " + sim.regionName +
  349. " at location " + sim.regionLocX +
  350. " " + sim.regionLocY + " currently occupied by " + existingSim.regionName);
  351. }
  352. return ErrorResponse("The key required to connect to your region did not match. Please check your send and recieve keys.");
  353. }
  354. }
  355. else
  356. {
  357. m_log.Warn("[grid]: Failed to add new region " + sim.regionName + " at location " + sim.regionLocX + " " + sim.regionLocY + " currently occupied by " + existingSim.regionName);
  358. return ErrorResponse("Another region already exists at that location. Try another");
  359. }
  360. }
  361. /// <summary>
  362. /// Construct a successful response to a simulator's login attempt.
  363. /// </summary>
  364. /// <param name="sim"></param>
  365. /// <returns></returns>
  366. private XmlRpcResponse CreateLoginResponse(RegionProfileData sim)
  367. {
  368. XmlRpcResponse response = new XmlRpcResponse();
  369. Hashtable responseData = new Hashtable();
  370. response.Value = responseData;
  371. ArrayList SimNeighboursData = GetSimNeighboursData(sim);
  372. responseData["UUID"] = sim.UUID.ToString();
  373. responseData["region_locx"] = sim.regionLocX.ToString();
  374. responseData["region_locy"] = sim.regionLocY.ToString();
  375. responseData["regionname"] = sim.regionName;
  376. responseData["estate_id"] = "1";
  377. responseData["neighbours"] = SimNeighboursData;
  378. responseData["sim_ip"] = sim.serverIP;
  379. responseData["sim_port"] = sim.serverPort.ToString();
  380. responseData["asset_url"] = sim.regionAssetURI;
  381. responseData["asset_sendkey"] = sim.regionAssetSendKey;
  382. responseData["asset_recvkey"] = sim.regionAssetRecvKey;
  383. responseData["user_url"] = sim.regionUserURI;
  384. responseData["user_sendkey"] = sim.regionUserSendKey;
  385. responseData["user_recvkey"] = sim.regionUserRecvKey;
  386. responseData["authkey"] = sim.regionSecret;
  387. // New! If set, use as URL to local sim storage (ie http://remotehost/region.yap)
  388. responseData["data_uri"] = sim.regionDataURI;
  389. responseData["allow_forceful_banlines"] = Config.AllowForcefulBanlines;
  390. // Instead of sending a multitude of message servers to the registering sim
  391. // we should probably be sending a single one and parhaps it's backup
  392. // that has responsibility over routing it's messages.
  393. // The Sim won't be contacting us again about any of the message server stuff during it's time up.
  394. responseData["messageserver_count"] = _MessageServers.Count;
  395. for (int i = 0; i < _MessageServers.Count; i++)
  396. {
  397. responseData["messageserver_uri" + i] = _MessageServers[i].URI;
  398. responseData["messageserver_sendkey" + i] = _MessageServers[i].sendkey;
  399. responseData["messageserver_recvkey" + i] = _MessageServers[i].recvkey;
  400. }
  401. return response;
  402. }
  403. private ArrayList GetSimNeighboursData(RegionProfileData sim)
  404. {
  405. ArrayList SimNeighboursData = new ArrayList();
  406. RegionProfileData neighbour;
  407. Hashtable NeighbourBlock;
  408. bool fastMode = false; // Only compatible with MySQL right now
  409. if (fastMode)
  410. {
  411. Dictionary<ulong, RegionProfileData> neighbours =
  412. GetRegions(sim.regionLocX - 1, sim.regionLocY - 1, sim.regionLocX + 1,
  413. sim.regionLocY + 1);
  414. foreach (KeyValuePair<ulong, RegionProfileData> aSim in neighbours)
  415. {
  416. NeighbourBlock = new Hashtable();
  417. NeighbourBlock["sim_ip"] = Util.GetHostFromDNS(aSim.Value.serverIP.ToString()).ToString();
  418. NeighbourBlock["sim_port"] = aSim.Value.serverPort.ToString();
  419. NeighbourBlock["region_locx"] = aSim.Value.regionLocX.ToString();
  420. NeighbourBlock["region_locy"] = aSim.Value.regionLocY.ToString();
  421. NeighbourBlock["UUID"] = aSim.Value.UUID.ToString();
  422. NeighbourBlock["regionHandle"] = aSim.Value.regionHandle.ToString();
  423. if (aSim.Value.UUID != sim.UUID)
  424. {
  425. SimNeighboursData.Add(NeighbourBlock);
  426. }
  427. }
  428. }
  429. else
  430. {
  431. for (int x = -1; x < 2; x++)
  432. {
  433. for (int y = -1; y < 2; y++)
  434. {
  435. if (
  436. GetRegion(
  437. Helpers.UIntsToLong((uint)((sim.regionLocX + x) * Constants.RegionSize),
  438. (uint)(sim.regionLocY + y) * Constants.RegionSize)) != null)
  439. {
  440. neighbour =
  441. GetRegion(
  442. Helpers.UIntsToLong((uint)((sim.regionLocX + x) * Constants.RegionSize),
  443. (uint)(sim.regionLocY + y) * Constants.RegionSize));
  444. NeighbourBlock = new Hashtable();
  445. NeighbourBlock["sim_ip"] = Util.GetHostFromDNS(neighbour.serverIP).ToString();
  446. NeighbourBlock["sim_port"] = neighbour.serverPort.ToString();
  447. NeighbourBlock["region_locx"] = neighbour.regionLocX.ToString();
  448. NeighbourBlock["region_locy"] = neighbour.regionLocY.ToString();
  449. NeighbourBlock["UUID"] = neighbour.UUID.ToString();
  450. NeighbourBlock["regionHandle"] = neighbour.regionHandle.ToString();
  451. if (neighbour.UUID != sim.UUID) SimNeighboursData.Add(NeighbourBlock);
  452. }
  453. }
  454. }
  455. }
  456. return SimNeighboursData;
  457. }
  458. /// <summary>
  459. /// Loads the grid's own RegionProfileData object with data from the XMLRPC simulator_login request from a region
  460. /// </summary>
  461. /// <param name="requestData"></param>
  462. /// <returns></returns>
  463. private RegionProfileData RegionFromRequest(Hashtable requestData)
  464. {
  465. RegionProfileData sim;
  466. sim = new RegionProfileData();
  467. sim.UUID = new LLUUID((string)requestData["UUID"]);
  468. sim.originUUID = new LLUUID((string)requestData["originUUID"]);
  469. sim.regionRecvKey = String.Empty;
  470. sim.regionSendKey = String.Empty;
  471. if (requestData.ContainsKey("region_secret"))
  472. {
  473. string regionsecret = (string)requestData["region_secret"];
  474. if (regionsecret.Length > 0)
  475. sim.regionSecret = regionsecret;
  476. else
  477. sim.regionSecret = Config.SimRecvKey;
  478. }
  479. else
  480. {
  481. sim.regionSecret = Config.SimRecvKey;
  482. }
  483. sim.regionDataURI = String.Empty;
  484. sim.regionAssetURI = Config.DefaultAssetServer;
  485. sim.regionAssetRecvKey = Config.AssetRecvKey;
  486. sim.regionAssetSendKey = Config.AssetSendKey;
  487. sim.regionUserURI = Config.DefaultUserServer;
  488. sim.regionUserSendKey = Config.UserSendKey;
  489. sim.regionUserRecvKey = Config.UserRecvKey;
  490. sim.serverIP = (string)requestData["sim_ip"];
  491. sim.serverPort = Convert.ToUInt32((string)requestData["sim_port"]);
  492. sim.httpPort = Convert.ToUInt32((string)requestData["http_port"]);
  493. sim.remotingPort = Convert.ToUInt32((string)requestData["remoting_port"]);
  494. sim.regionLocX = Convert.ToUInt32((string)requestData["region_locx"]);
  495. sim.regionLocY = Convert.ToUInt32((string)requestData["region_locy"]);
  496. sim.regionLocZ = 0;
  497. LLUUID textureID;
  498. if (LLUUID.TryParse((string)requestData["map-image-id"], out textureID))
  499. {
  500. sim.regionMapTextureID = textureID;
  501. }
  502. // part of an initial brutish effort to provide accurate information (as per the xml region spec)
  503. // wrt the ownership of a given region
  504. // the (very bad) assumption is that this value is being read and handled inconsistently or
  505. // not at all. Current strategy is to put the code in place to support the validity of this information
  506. // and to roll forward debugging any issues from that point
  507. //
  508. // this particular section of the mod attempts to receive a value from the region's xml file by way of
  509. // OSG1GridServices for the region's owner
  510. sim.owner_uuid = (string)requestData["master_avatar_uuid"];
  511. try
  512. {
  513. sim.regionRecvKey = (string)requestData["recvkey"];
  514. sim.regionSendKey = (string)requestData["authkey"];
  515. }
  516. catch (KeyNotFoundException) { }
  517. sim.regionHandle = Helpers.UIntsToLong((sim.regionLocX * Constants.RegionSize), (sim.regionLocY * Constants.RegionSize));
  518. sim.serverURI = (string)requestData["server_uri"];
  519. sim.httpServerURI = "http://" + sim.serverIP + ":" + sim.httpPort + "/";
  520. sim.regionName = (string)requestData["sim_name"];
  521. return sim;
  522. }
  523. /// <summary>
  524. /// Returns an XML RPC response to a simulator profile request
  525. /// Performed after moving a region.
  526. /// </summary>
  527. /// <param name="request"></param>
  528. /// <returns></returns>
  529. /// <param name="request">The XMLRPC Request</param>
  530. /// <returns>Processing parameters</returns>
  531. public XmlRpcResponse XmlRpcDeleteRegionMethod(XmlRpcRequest request)
  532. {
  533. XmlRpcResponse response = new XmlRpcResponse();
  534. Hashtable responseData = new Hashtable();
  535. response.Value = responseData;
  536. //RegionProfileData TheSim = null;
  537. string uuid;
  538. Hashtable requestData = (Hashtable)request.Params[0];
  539. if (requestData.ContainsKey("UUID"))
  540. {
  541. //TheSim = GetRegion(new LLUUID((string) requestData["UUID"]));
  542. uuid = requestData["UUID"].ToString();
  543. Console.WriteLine("deleting region " + uuid);
  544. // logToDB((new LLUUID((string)requestData["UUID"])).ToString(),"XmlRpcDeleteRegionMethod","", 5,"Attempting delete with UUID.");
  545. }
  546. else
  547. {
  548. responseData["error"] = "No UUID or region_handle passed to grid server - unable to delete";
  549. return response;
  550. }
  551. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  552. {
  553. //OpenSim.Data.MySQL.MySQLGridData dbengine = new OpenSim.Data.MySQL.MySQLGridData();
  554. try
  555. {
  556. MySQLGridData mysqldata = (MySQLGridData)(kvp.Value);
  557. //DataResponse insertResponse = mysqldata.DeleteProfile(TheSim);
  558. DataResponse insertResponse = mysqldata.DeleteProfile(uuid);
  559. switch (insertResponse)
  560. {
  561. case DataResponse.RESPONSE_OK:
  562. //MainLog.Instance.Verbose("grid", "Deleting region successful: " + uuid);
  563. responseData["status"] = "Deleting region successful: " + uuid;
  564. break;
  565. case DataResponse.RESPONSE_ERROR:
  566. //MainLog.Instance.Warn("storage", "Deleting region failed (Error): " + uuid);
  567. responseData["status"] = "Deleting region failed (Error): " + uuid;
  568. break;
  569. case DataResponse.RESPONSE_INVALIDCREDENTIALS:
  570. //MainLog.Instance.Warn("storage", "Deleting region failed (Invalid Credentials): " + uuid);
  571. responseData["status"] = "Deleting region (Invalid Credentials): " + uuid;
  572. break;
  573. case DataResponse.RESPONSE_AUTHREQUIRED:
  574. //MainLog.Instance.Warn("storage", "Deleting region failed (Authentication Required): " + uuid);
  575. responseData["status"] = "Deleting region (Authentication Required): " + uuid;
  576. break;
  577. }
  578. }
  579. catch (Exception)
  580. {
  581. m_log.Error("storage Unable to delete region " + uuid + " via MySQL");
  582. //MainLog.Instance.Warn("storage", e.ToString());
  583. }
  584. }
  585. return response;
  586. }
  587. /// <summary>
  588. /// Returns an XML RPC response to a simulator profile request
  589. /// </summary>
  590. /// <param name="request"></param>
  591. /// <returns></returns>
  592. public XmlRpcResponse XmlRpcSimulatorDataRequestMethod(XmlRpcRequest request)
  593. {
  594. Hashtable requestData = (Hashtable)request.Params[0];
  595. Hashtable responseData = new Hashtable();
  596. RegionProfileData simData = null;
  597. if (requestData.ContainsKey("region_UUID"))
  598. {
  599. simData = GetRegion(new LLUUID((string)requestData["region_UUID"]));
  600. }
  601. else if (requestData.ContainsKey("region_handle"))
  602. {
  603. //CFK: The if/else below this makes this message redundant.
  604. //CFK: Console.WriteLine("requesting data for region " + (string) requestData["region_handle"]);
  605. simData = GetRegion(Convert.ToUInt64((string)requestData["region_handle"]));
  606. }
  607. else if (requestData.ContainsKey("region_name_search"))
  608. {
  609. simData = GetRegion((string)requestData["region_name_search"]);
  610. }
  611. if (simData == null)
  612. {
  613. //Sim does not exist
  614. Console.WriteLine("region not found");
  615. responseData["error"] = "Sim does not exist";
  616. }
  617. else
  618. {
  619. m_log.Info("[DATA]: found " + (string)simData.regionName + " regionHandle = " +
  620. (string)requestData["region_handle"]);
  621. responseData["sim_ip"] = Util.GetHostFromDNS(simData.serverIP).ToString();
  622. responseData["sim_port"] = simData.serverPort.ToString();
  623. responseData["server_uri"] = simData.serverURI;
  624. responseData["http_port"] = simData.httpPort.ToString();
  625. responseData["remoting_port"] = simData.remotingPort.ToString();
  626. responseData["region_locx"] = simData.regionLocX.ToString();
  627. responseData["region_locy"] = simData.regionLocY.ToString();
  628. responseData["region_UUID"] = simData.UUID.UUID.ToString();
  629. responseData["region_name"] = simData.regionName;
  630. responseData["regionHandle"] = simData.regionHandle.ToString();
  631. }
  632. XmlRpcResponse response = new XmlRpcResponse();
  633. response.Value = responseData;
  634. return response;
  635. }
  636. public XmlRpcResponse XmlRpcMapBlockMethod(XmlRpcRequest request)
  637. {
  638. int xmin = 980, ymin = 980, xmax = 1020, ymax = 1020;
  639. Hashtable requestData = (Hashtable)request.Params[0];
  640. if (requestData.ContainsKey("xmin"))
  641. {
  642. xmin = (Int32)requestData["xmin"];
  643. }
  644. if (requestData.ContainsKey("ymin"))
  645. {
  646. ymin = (Int32)requestData["ymin"];
  647. }
  648. if (requestData.ContainsKey("xmax"))
  649. {
  650. xmax = (Int32)requestData["xmax"];
  651. }
  652. if (requestData.ContainsKey("ymax"))
  653. {
  654. ymax = (Int32)requestData["ymax"];
  655. }
  656. //CFK: The second log is more meaningful and either standard or fast generally occurs.
  657. //CFK: m_log.Info("[MAP]: World map request for range (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  658. XmlRpcResponse response = new XmlRpcResponse();
  659. Hashtable responseData = new Hashtable();
  660. response.Value = responseData;
  661. IList simProfileList = new ArrayList();
  662. bool fastMode = (Config.DatabaseProvider == "OpenSim.Data.MySQL.dll");
  663. if (fastMode)
  664. {
  665. Dictionary<ulong, RegionProfileData> neighbours =
  666. GetRegions((uint)xmin, (uint)ymin, (uint)xmax, (uint)ymax);
  667. foreach (KeyValuePair<ulong, RegionProfileData> aSim in neighbours)
  668. {
  669. Hashtable simProfileBlock = new Hashtable();
  670. simProfileBlock["x"] = aSim.Value.regionLocX.ToString();
  671. simProfileBlock["y"] = aSim.Value.regionLocY.ToString();
  672. Console.WriteLine("send neighbour info for " + aSim.Value.regionLocX.ToString() + " , " +
  673. aSim.Value.regionLocY.ToString());
  674. simProfileBlock["name"] = aSim.Value.regionName;
  675. simProfileBlock["access"] = 21;
  676. simProfileBlock["region-flags"] = 512;
  677. simProfileBlock["water-height"] = 0;
  678. simProfileBlock["agents"] = 1;
  679. simProfileBlock["map-image-id"] = aSim.Value.regionMapTextureID.ToString();
  680. // For Sugilite compatibility
  681. simProfileBlock["regionhandle"] = aSim.Value.regionHandle.ToString();
  682. simProfileBlock["sim_ip"] = aSim.Value.serverIP.ToString();
  683. simProfileBlock["sim_port"] = aSim.Value.serverPort.ToString();
  684. simProfileBlock["sim_uri"] = aSim.Value.serverURI.ToString();
  685. simProfileBlock["uuid"] = aSim.Value.UUID.ToString();
  686. simProfileBlock["remoting_port"] = aSim.Value.remotingPort;
  687. simProfileList.Add(simProfileBlock);
  688. }
  689. m_log.Info("[MAP]: Fast map " + simProfileList.Count.ToString() +
  690. " regions @ (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  691. }
  692. else
  693. {
  694. RegionProfileData simProfile;
  695. for (int x = xmin; x < xmax + 1; x++)
  696. {
  697. for (int y = ymin; y < ymax + 1; y++)
  698. {
  699. ulong regHandle = Helpers.UIntsToLong((uint)(x * Constants.RegionSize), (uint)(y * Constants.RegionSize));
  700. simProfile = GetRegion(regHandle);
  701. if (simProfile != null)
  702. {
  703. Hashtable simProfileBlock = new Hashtable();
  704. simProfileBlock["x"] = x;
  705. simProfileBlock["y"] = y;
  706. simProfileBlock["name"] = simProfile.regionName;
  707. simProfileBlock["access"] = 0;
  708. simProfileBlock["region-flags"] = 0;
  709. simProfileBlock["water-height"] = 20;
  710. simProfileBlock["agents"] = 1;
  711. simProfileBlock["map-image-id"] = simProfile.regionMapTextureID.ToString();
  712. // For Sugilite compatibility
  713. simProfileBlock["regionhandle"] = simProfile.regionHandle.ToString();
  714. simProfileBlock["sim_ip"] = simProfile.serverIP.ToString();
  715. simProfileBlock["sim_port"] = simProfile.serverPort.ToString();
  716. simProfileBlock["sim_uri"] = simProfile.serverURI.ToString();
  717. simProfileBlock["uuid"] = simProfile.UUID.ToString();
  718. simProfileList.Add(simProfileBlock);
  719. }
  720. }
  721. }
  722. m_log.Info("[MAP]: Std map " + simProfileList.Count.ToString() +
  723. " regions @ (" + xmin + "," + ymin + ")..(" + xmax + "," + ymax + ")");
  724. }
  725. responseData["sim-profiles"] = simProfileList;
  726. return response;
  727. }
  728. /// <summary>
  729. /// Performs a REST Get Operation
  730. /// </summary>
  731. /// <param name="request"></param>
  732. /// <param name="path"></param>
  733. /// <param name="param"></param>
  734. /// <returns></returns>
  735. public string RestGetRegionMethod(string request, string path, string param)
  736. {
  737. return RestGetSimMethod(String.Empty, "/sims/", param);
  738. }
  739. /// <summary>
  740. /// Performs a REST Set Operation
  741. /// </summary>
  742. /// <param name="request"></param>
  743. /// <param name="path"></param>
  744. /// <param name="param"></param>
  745. /// <returns></returns>
  746. public string RestSetRegionMethod(string request, string path, string param)
  747. {
  748. return RestSetSimMethod(String.Empty, "/sims/", param);
  749. }
  750. /// <summary>
  751. /// Returns information about a sim via a REST Request
  752. /// </summary>
  753. /// <param name="request"></param>
  754. /// <param name="path"></param>
  755. /// <param name="param">A string representing the sim's UUID</param>
  756. /// <returns>Information about the sim in XML</returns>
  757. public string RestGetSimMethod(string request, string path, string param)
  758. {
  759. string respstring = String.Empty;
  760. RegionProfileData TheSim;
  761. LLUUID UUID;
  762. if (LLUUID.TryParse(param, out UUID))
  763. {
  764. TheSim = GetRegion(UUID);
  765. if (!(TheSim == null))
  766. {
  767. respstring = "<Root>";
  768. respstring += "<authkey>" + TheSim.regionSendKey + "</authkey>";
  769. respstring += "<sim>";
  770. respstring += "<uuid>" + TheSim.UUID.ToString() + "</uuid>";
  771. respstring += "<regionname>" + TheSim.regionName + "</regionname>";
  772. respstring += "<sim_ip>" + Util.GetHostFromDNS(TheSim.serverIP).ToString() + "</sim_ip>";
  773. respstring += "<sim_port>" + TheSim.serverPort.ToString() + "</sim_port>";
  774. respstring += "<region_locx>" + TheSim.regionLocX.ToString() + "</region_locx>";
  775. respstring += "<region_locy>" + TheSim.regionLocY.ToString() + "</region_locy>";
  776. respstring += "<estate_id>1</estate_id>";
  777. respstring += "</sim>";
  778. respstring += "</Root>";
  779. }
  780. }
  781. else
  782. {
  783. respstring = "<Root>";
  784. respstring += "<error>Param must be a UUID</error>";
  785. respstring += "</Root>";
  786. }
  787. return respstring;
  788. }
  789. /// <summary>
  790. /// Creates or updates a sim via a REST Method Request
  791. /// BROKEN with SQL Update
  792. /// </summary>
  793. /// <param name="request"></param>
  794. /// <param name="path"></param>
  795. /// <param name="param"></param>
  796. /// <returns>"OK" or an error</returns>
  797. public string RestSetSimMethod(string request, string path, string param)
  798. {
  799. Console.WriteLine("Processing region update via REST method");
  800. RegionProfileData theSim;
  801. theSim = GetRegion(new LLUUID(param));
  802. if (theSim == null)
  803. {
  804. theSim = new RegionProfileData();
  805. LLUUID UUID = new LLUUID(param);
  806. theSim.UUID = UUID;
  807. theSim.regionRecvKey = Config.SimRecvKey;
  808. }
  809. XmlDocument doc = new XmlDocument();
  810. doc.LoadXml(request);
  811. XmlNode rootnode = doc.FirstChild;
  812. XmlNode authkeynode = rootnode.ChildNodes[0];
  813. if (authkeynode.Name != "authkey")
  814. {
  815. return "ERROR! bad XML - expected authkey tag";
  816. }
  817. XmlNode simnode = rootnode.ChildNodes[1];
  818. if (simnode.Name != "sim")
  819. {
  820. return "ERROR! bad XML - expected sim tag";
  821. }
  822. //theSim.regionSendKey = Cfg;
  823. theSim.regionRecvKey = Config.SimRecvKey;
  824. theSim.regionSendKey = Config.SimSendKey;
  825. theSim.regionSecret = Config.SimRecvKey;
  826. theSim.regionDataURI = String.Empty;
  827. theSim.regionAssetURI = Config.DefaultAssetServer;
  828. theSim.regionAssetRecvKey = Config.AssetRecvKey;
  829. theSim.regionAssetSendKey = Config.AssetSendKey;
  830. theSim.regionUserURI = Config.DefaultUserServer;
  831. theSim.regionUserSendKey = Config.UserSendKey;
  832. theSim.regionUserRecvKey = Config.UserRecvKey;
  833. for (int i = 0; i < simnode.ChildNodes.Count; i++)
  834. {
  835. switch (simnode.ChildNodes[i].Name)
  836. {
  837. case "regionname":
  838. theSim.regionName = simnode.ChildNodes[i].InnerText;
  839. break;
  840. case "sim_ip":
  841. theSim.serverIP = simnode.ChildNodes[i].InnerText;
  842. break;
  843. case "sim_port":
  844. theSim.serverPort = Convert.ToUInt32(simnode.ChildNodes[i].InnerText);
  845. break;
  846. case "region_locx":
  847. theSim.regionLocX = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
  848. theSim.regionHandle = Helpers.UIntsToLong((theSim.regionLocX * Constants.RegionSize), (theSim.regionLocY * Constants.RegionSize));
  849. break;
  850. case "region_locy":
  851. theSim.regionLocY = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
  852. theSim.regionHandle = Helpers.UIntsToLong((theSim.regionLocX * Constants.RegionSize), (theSim.regionLocY * Constants.RegionSize));
  853. break;
  854. }
  855. }
  856. theSim.serverURI = "http://" + theSim.serverIP + ":" + theSim.serverPort + "/";
  857. bool requirePublic = false;
  858. bool requireValid = true;
  859. if (requirePublic &&
  860. (theSim.serverIP.StartsWith("172.16") || theSim.serverIP.StartsWith("192.168") ||
  861. theSim.serverIP.StartsWith("10.") || theSim.serverIP.StartsWith("0.") ||
  862. theSim.serverIP.StartsWith("255.")))
  863. {
  864. return "ERROR! Servers must register with public addresses.";
  865. }
  866. if (requireValid && (theSim.serverIP.StartsWith("0.") || theSim.serverIP.StartsWith("255.")))
  867. {
  868. return "ERROR! 0.*.*.* / 255.*.*.* Addresses are invalid, please check your server config and try again";
  869. }
  870. try
  871. {
  872. m_log.Info("[DATA]: " +
  873. "Updating / adding via " + _plugins.Count + " storage provider(s) registered.");
  874. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  875. {
  876. try
  877. {
  878. //Check reservations
  879. ReservationData reserveData =
  880. kvp.Value.GetReservationAtPoint(theSim.regionLocX, theSim.regionLocY);
  881. if ((reserveData != null && reserveData.gridRecvKey == theSim.regionRecvKey) ||
  882. (reserveData == null && authkeynode.InnerText != theSim.regionRecvKey))
  883. {
  884. kvp.Value.AddProfile(theSim);
  885. m_log.Info("[grid]: New sim added to grid (" + theSim.regionName + ")");
  886. logToDB(theSim.UUID.ToString(), "RestSetSimMethod", String.Empty, 5,
  887. "Region successfully updated and connected to grid.");
  888. }
  889. else
  890. {
  891. m_log.Warn("[grid]: " +
  892. "Unable to update region (RestSetSimMethod): Incorrect reservation auth key.");
  893. // Wanted: " + reserveData.gridRecvKey + ", Got: " + theSim.regionRecvKey + ".");
  894. return "Unable to update region (RestSetSimMethod): Incorrect auth key.";
  895. }
  896. }
  897. catch (Exception e)
  898. {
  899. m_log.Warn("[GRID]: GetRegionPlugin Handle " + kvp.Key + " unable to add new sim: " +
  900. e.ToString());
  901. }
  902. }
  903. return "OK";
  904. }
  905. catch (Exception e)
  906. {
  907. return "ERROR! Could not save to database! (" + e.ToString() + ")";
  908. }
  909. }
  910. public XmlRpcResponse XmlRPCRegisterMessageServer(XmlRpcRequest request)
  911. {
  912. XmlRpcResponse response = new XmlRpcResponse();
  913. Hashtable requestData = (Hashtable)request.Params[0];
  914. Hashtable responseData = new Hashtable();
  915. if (requestData.Contains("uri"))
  916. {
  917. string URI = (string)requestData["URI"];
  918. string sendkey = (string)requestData["sendkey"];
  919. string recvkey = (string)requestData["recvkey"];
  920. MessageServerInfo m = new MessageServerInfo();
  921. m.URI = URI;
  922. m.sendkey = sendkey;
  923. m.recvkey = recvkey;
  924. if (!_MessageServers.Contains(m))
  925. _MessageServers.Add(m);
  926. responseData["responsestring"] = "TRUE";
  927. response.Value = responseData;
  928. }
  929. return response;
  930. }
  931. public XmlRpcResponse XmlRPCDeRegisterMessageServer(XmlRpcRequest request)
  932. {
  933. XmlRpcResponse response = new XmlRpcResponse();
  934. Hashtable requestData = (Hashtable)request.Params[0];
  935. Hashtable responseData = new Hashtable();
  936. if (requestData.Contains("uri"))
  937. {
  938. string URI = (string)requestData["uri"];
  939. string sendkey = (string)requestData["sendkey"];
  940. string recvkey = (string)requestData["recvkey"];
  941. MessageServerInfo m = new MessageServerInfo();
  942. m.URI = URI;
  943. m.sendkey = sendkey;
  944. m.recvkey = recvkey;
  945. if (_MessageServers.Contains(m))
  946. _MessageServers.Remove(m);
  947. responseData["responsestring"] = "TRUE";
  948. response.Value = responseData;
  949. }
  950. return response;
  951. }
  952. }
  953. }