HGHyperlink.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.Net;
  29. using System.Reflection;
  30. using log4net;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. namespace OpenSim.Region.Framework.Scenes.Hypergrid
  34. {
  35. public class HGHyperlink
  36. {
  37. private static readonly ILog m_log =
  38. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  39. private static Random random = new Random();
  40. public static RegionInfo TryLinkRegionToCoords(Scene m_scene, IClientAPI client, string mapName, uint xloc, uint yloc)
  41. {
  42. string host = "127.0.0.1";
  43. string portstr;
  44. string regionName = "";
  45. uint port = 9000;
  46. string[] parts = mapName.Split(new char[] { ':' });
  47. if (parts.Length >= 1)
  48. {
  49. host = parts[0];
  50. }
  51. if (parts.Length >= 2)
  52. {
  53. portstr = parts[1];
  54. if (!UInt32.TryParse(portstr, out port))
  55. regionName = parts[1];
  56. }
  57. // always take the last one
  58. if (parts.Length >= 3)
  59. {
  60. regionName = parts[2];
  61. }
  62. // Sanity check. Don't ever link to this sim.
  63. IPAddress ipaddr = null;
  64. try
  65. {
  66. ipaddr = Util.GetHostFromDNS(host);
  67. }
  68. catch { }
  69. if ((ipaddr != null) &&
  70. !((m_scene.RegionInfo.ExternalEndPoint.Address.Equals(ipaddr)) && (m_scene.RegionInfo.HttpPort == port)))
  71. {
  72. RegionInfo regInfo;
  73. bool success = TryCreateLink(m_scene, client, xloc, yloc, regionName, port, host, out regInfo);
  74. if (success)
  75. {
  76. regInfo.RegionName = mapName;
  77. return regInfo;
  78. }
  79. }
  80. return null;
  81. }
  82. public static RegionInfo TryLinkRegion(Scene m_scene, IClientAPI client, string mapName)
  83. {
  84. uint xloc = (uint)(random.Next(0, Int16.MaxValue));
  85. return TryLinkRegionToCoords(m_scene, client, mapName, xloc, 0);
  86. }
  87. public static bool TryCreateLink(Scene m_scene, IClientAPI client, uint xloc, uint yloc,
  88. string externalRegionName, uint externalPort, string externalHostName, out RegionInfo regInfo)
  89. {
  90. m_log.DebugFormat("[HGrid]: Link to {0}:{1}, in {2}-{3}", externalHostName, externalPort, xloc, yloc);
  91. regInfo = new RegionInfo();
  92. regInfo.RegionName = externalRegionName;
  93. regInfo.HttpPort = externalPort;
  94. regInfo.ExternalHostName = externalHostName;
  95. regInfo.RegionLocX = xloc;
  96. regInfo.RegionLocY = yloc;
  97. try
  98. {
  99. regInfo.InternalEndPoint = new IPEndPoint(IPAddress.Parse("0.0.0.0"), (int)0);
  100. }
  101. catch (Exception e)
  102. {
  103. m_log.Warn("[HGrid]: Wrong format for link-region: " + e.Message);
  104. return false;
  105. }
  106. regInfo.RemotingAddress = regInfo.ExternalEndPoint.Address.ToString();
  107. // Finally, link it
  108. try
  109. {
  110. m_scene.CommsManager.GridService.RegisterRegion(regInfo);
  111. }
  112. catch (Exception e)
  113. {
  114. m_log.Warn("[HGrid]: Unable to link region: " + e.Message);
  115. return false;
  116. }
  117. uint x, y;
  118. if (!Check4096(m_scene, regInfo, out x, out y))
  119. {
  120. m_scene.CommsManager.GridService.DeregisterRegion(regInfo);
  121. if (client != null)
  122. client.SendAlertMessage("Region is too far (" + x + ", " + y + ")");
  123. m_log.Info("[HGrid]: Unable to link, region is too far (" + x + ", " + y + ")");
  124. return false;
  125. }
  126. if (!CheckCoords(m_scene.RegionInfo.RegionLocX, m_scene.RegionInfo.RegionLocY, x, y))
  127. {
  128. m_scene.CommsManager.GridService.DeregisterRegion(regInfo);
  129. if (client != null)
  130. client.SendAlertMessage("Region has incompatible coordinates (" + x + ", " + y + ")");
  131. m_log.Info("[HGrid]: Unable to link, region has incompatible coordinates (" + x + ", " + y + ")");
  132. return false;
  133. }
  134. m_log.Debug("[HGrid]: link region succeeded");
  135. return true;
  136. }
  137. /// <summary>
  138. /// Cope with this viewer limitation.
  139. /// </summary>
  140. /// <param name="regInfo"></param>
  141. /// <returns></returns>
  142. public static bool Check4096(Scene m_scene, RegionInfo regInfo, out uint x, out uint y)
  143. {
  144. ulong realHandle;
  145. if (UInt64.TryParse(regInfo.regionSecret, out realHandle))
  146. {
  147. Utils.LongToUInts(realHandle, out x, out y);
  148. x = x / Constants.RegionSize;
  149. y = y / Constants.RegionSize;
  150. if ((Math.Abs((int)m_scene.RegionInfo.RegionLocX - (int)x) >= 4096) ||
  151. (Math.Abs((int)m_scene.RegionInfo.RegionLocY - (int)y) >= 4096))
  152. {
  153. return false;
  154. }
  155. return true;
  156. }
  157. else
  158. {
  159. m_scene.CommsManager.GridService.RegisterRegion(regInfo);
  160. m_log.Debug("[HGrid]: Gnomes. Region deregistered.");
  161. x = y = 0;
  162. return false;
  163. }
  164. }
  165. public static bool CheckCoords(uint thisx, uint thisy, uint x, uint y)
  166. {
  167. if ((thisx == x) && (thisy == y))
  168. return false;
  169. return true;
  170. }
  171. }
  172. }