1
0

HypergridServerConnector.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.Reflection;
  31. using System.Net;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Server.Base;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Server.Handlers.Base;
  38. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  39. using OpenMetaverse;
  40. using log4net;
  41. using Nwc.XmlRpc;
  42. namespace OpenSim.Server.Handlers.Grid
  43. {
  44. public class HypergridServiceInConnector : ServiceConnector
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(
  48. MethodBase.GetCurrentMethod().DeclaringType);
  49. private List<GridRegion> m_RegionsOnSim = new List<GridRegion>();
  50. private IHyperlinkService m_HyperlinkService;
  51. public HypergridServiceInConnector(IConfigSource config, IHttpServer server, IHyperlinkService hyperService) :
  52. base(config, server, String.Empty)
  53. {
  54. m_HyperlinkService = hyperService;
  55. server.AddXmlRPCHandler("link_region", LinkRegionRequest, false);
  56. server.AddXmlRPCHandler("expect_hg_user", ExpectHGUser, false);
  57. }
  58. public void AddRegion(GridRegion rinfo)
  59. {
  60. m_RegionsOnSim.Add(rinfo);
  61. }
  62. public void RemoveRegion(GridRegion rinfo)
  63. {
  64. if (m_RegionsOnSim.Contains(rinfo))
  65. m_RegionsOnSim.Remove(rinfo);
  66. }
  67. /// <summary>
  68. /// Someone wants to link to us
  69. /// </summary>
  70. /// <param name="request"></param>
  71. /// <returns></returns>
  72. public XmlRpcResponse LinkRegionRequest(XmlRpcRequest request, IPEndPoint remoteClient)
  73. {
  74. Hashtable requestData = (Hashtable)request.Params[0];
  75. //string host = (string)requestData["host"];
  76. //string portstr = (string)requestData["port"];
  77. string name = (string)requestData["region_name"];
  78. m_log.DebugFormat("[HGrid]: Hyperlink request");
  79. GridRegion regInfo = null;
  80. foreach (GridRegion r in m_RegionsOnSim)
  81. {
  82. if ((r.RegionName != null) && (name != null) && (r.RegionName.ToLower() == name.ToLower()))
  83. {
  84. regInfo = r;
  85. break;
  86. }
  87. }
  88. if (regInfo == null)
  89. regInfo = m_RegionsOnSim[0]; // Send out the first region
  90. Hashtable hash = new Hashtable();
  91. hash["uuid"] = regInfo.RegionID.ToString();
  92. m_log.Debug(">> Here " + regInfo.RegionID);
  93. hash["handle"] = regInfo.RegionHandle.ToString();
  94. hash["region_image"] = regInfo.TerrainImage.ToString();
  95. hash["region_name"] = regInfo.RegionName;
  96. hash["internal_port"] = regInfo.InternalEndPoint.Port.ToString();
  97. //m_log.Debug(">> Here: " + regInfo.InternalEndPoint.Port);
  98. XmlRpcResponse response = new XmlRpcResponse();
  99. response.Value = hash;
  100. return response;
  101. }
  102. /// <summary>
  103. /// Received from other HGrid nodes when a user wants to teleport here. This call allows
  104. /// the region to prepare for direct communication from the client. Sends back an empty
  105. /// xmlrpc response on completion.
  106. /// This is somewhat similar to OGS1's ExpectUser, but with the additional task of
  107. /// registering the user in the local user cache.
  108. /// </summary>
  109. /// <param name="request"></param>
  110. /// <returns></returns>
  111. public XmlRpcResponse ExpectHGUser(XmlRpcRequest request, IPEndPoint remoteClient)
  112. {
  113. Hashtable requestData = (Hashtable)request.Params[0];
  114. ForeignUserProfileData userData = new ForeignUserProfileData();
  115. userData.FirstName = (string)requestData["firstname"];
  116. userData.SurName = (string)requestData["lastname"];
  117. userData.ID = new UUID((string)requestData["agent_id"]);
  118. UUID sessionID = new UUID((string)requestData["session_id"]);
  119. userData.HomeLocation = new Vector3((float)Convert.ToDecimal((string)requestData["startpos_x"]),
  120. (float)Convert.ToDecimal((string)requestData["startpos_y"]),
  121. (float)Convert.ToDecimal((string)requestData["startpos_z"]));
  122. userData.UserServerURI = (string)requestData["userserver_id"];
  123. userData.UserAssetURI = (string)requestData["assetserver_id"];
  124. userData.UserInventoryURI = (string)requestData["inventoryserver_id"];
  125. m_log.DebugFormat("[HGrid]: Prepare for connection from {0} {1} (@{2}) UUID={3}",
  126. userData.FirstName, userData.SurName, userData.UserServerURI, userData.ID);
  127. ulong userRegionHandle = 0;
  128. int userhomeinternalport = 0;
  129. if (requestData.ContainsKey("region_uuid"))
  130. {
  131. UUID uuid = UUID.Zero;
  132. UUID.TryParse((string)requestData["region_uuid"], out uuid);
  133. userData.HomeRegionID = uuid;
  134. userRegionHandle = Convert.ToUInt64((string)requestData["regionhandle"]);
  135. userData.UserHomeAddress = (string)requestData["home_address"];
  136. userData.UserHomePort = (string)requestData["home_port"];
  137. userhomeinternalport = Convert.ToInt32((string)requestData["internal_port"]);
  138. m_log.Debug("[HGrid]: home_address: " + userData.UserHomeAddress +
  139. "; home_port: " + userData.UserHomePort);
  140. }
  141. else
  142. m_log.WarnFormat("[HGrid]: User has no home region information");
  143. XmlRpcResponse resp = new XmlRpcResponse();
  144. // Let's check if someone is trying to get in with a stolen local identity.
  145. // The need for this test is a consequence of not having truly global names :-/
  146. bool comingHome = false;
  147. if (m_HyperlinkService.CheckUserAtEntry(userData.ID, sessionID, out comingHome) == false)
  148. {
  149. m_log.WarnFormat("[HGrid]: Access denied to foreign user.");
  150. Hashtable respdata = new Hashtable();
  151. respdata["success"] = "FALSE";
  152. respdata["reason"] = "Foreign user has the same ID as a local user, or logins disabled.";
  153. resp.Value = respdata;
  154. return resp;
  155. }
  156. // Finally, everything looks ok
  157. //m_log.Debug("XXX---- EVERYTHING OK ---XXX");
  158. if (!comingHome)
  159. {
  160. // We don't do this if the user is coming to the home grid
  161. GridRegion home = new GridRegion();
  162. home.RegionID = userData.HomeRegionID;
  163. home.ExternalHostName = userData.UserHomeAddress;
  164. home.HttpPort = Convert.ToUInt32(userData.UserHomePort);
  165. uint x = 0, y = 0;
  166. Utils.LongToUInts(userRegionHandle, out x, out y);
  167. home.RegionLocX = (int)x;
  168. home.RegionLocY = (int)y;
  169. home.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)userhomeinternalport);
  170. m_HyperlinkService.AcceptUser(userData, home);
  171. }
  172. // else the user is coming to a non-home region of the home grid
  173. // We simply drop this user information altogether
  174. Hashtable respdata2 = new Hashtable();
  175. respdata2["success"] = "TRUE";
  176. resp.Value = respdata2;
  177. return resp;
  178. }
  179. }
  180. }