LLPacketServer.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.Net;
  28. using System.Net.Sockets;
  29. using System.Reflection;
  30. using log4net;
  31. using OpenMetaverse;
  32. using OpenMetaverse.Packets;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications.Cache;
  35. namespace OpenSim.Region.ClientStack.LindenUDP
  36. {
  37. public class LLPacketServer
  38. {
  39. // private static readonly log4net.ILog m_log
  40. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  41. protected readonly ILLClientStackNetworkHandler m_networkHandler;
  42. protected IScene m_scene;
  43. /// <summary>
  44. /// Tweakable user settings
  45. /// </summary>
  46. private ClientStackUserSettings m_userSettings;
  47. public LLPacketServer(ILLClientStackNetworkHandler networkHandler, ClientStackUserSettings userSettings)
  48. {
  49. m_userSettings = userSettings;
  50. m_networkHandler = networkHandler;
  51. m_networkHandler.RegisterPacketServer(this);
  52. }
  53. public IScene LocalScene
  54. {
  55. set { m_scene = value; }
  56. }
  57. /// <summary>
  58. /// Process an incoming packet.
  59. /// </summary>
  60. /// <param name="circuitCode"></param>
  61. /// <param name="packet"></param>
  62. public virtual void InPacket(uint circuitCode, Packet packet)
  63. {
  64. m_scene.ClientManager.InPacket(circuitCode, packet);
  65. }
  66. /// <summary>
  67. /// Create a new client circuit
  68. /// </summary>
  69. /// <param name="remoteEP"></param>
  70. /// <param name="scene"></param>
  71. /// <param name="assetCache"></param>
  72. /// <param name="packServer"></param>
  73. /// <param name="sessionInfo"></param>
  74. /// <param name="agentId"></param>
  75. /// <param name="sessionId"></param>
  76. /// <param name="circuitCode"></param>
  77. /// <param name="proxyEP"></param>
  78. /// <returns></returns>
  79. protected virtual IClientAPI CreateNewCircuit(
  80. EndPoint remoteEP, IScene scene, AssetCache assetCache,
  81. LLPacketServer packServer, AuthenticateResponse sessionInfo,
  82. UUID agentId, UUID sessionId, uint circuitCode, EndPoint proxyEP)
  83. {
  84. return
  85. new LLClientView(
  86. remoteEP, scene, assetCache, packServer, sessionInfo, agentId, sessionId, circuitCode, proxyEP,
  87. m_userSettings);
  88. }
  89. /// <summary>
  90. /// Check whether a given client is authorized to connect.
  91. /// </summary>
  92. /// <param name="useCircuit"></param>
  93. /// <param name="circuitManager"></param>
  94. /// <param name="sessionInfo"></param>
  95. /// <returns></returns>
  96. public virtual bool IsClientAuthorized(
  97. UseCircuitCodePacket useCircuit, AgentCircuitManager circuitManager, out AuthenticateResponse sessionInfo)
  98. {
  99. UUID agentId = useCircuit.CircuitCode.ID;
  100. UUID sessionId = useCircuit.CircuitCode.SessionID;
  101. uint circuitCode = useCircuit.CircuitCode.Code;
  102. sessionInfo = circuitManager.AuthenticateSession(sessionId, agentId, circuitCode);
  103. if (!sessionInfo.Authorised)
  104. return false;
  105. return true;
  106. }
  107. /// <summary>
  108. /// Add a new client circuit. We assume that is has already passed an authorization check
  109. /// </summary>
  110. /// <param name="epSender"></param>
  111. /// <param name="useCircuit"></param>
  112. /// <param name="assetCache"></param>
  113. /// <param name="sessionInfo"></param>
  114. /// <param name="proxyEP"></param>
  115. /// <returns>
  116. /// true if a new circuit was created, false if a circuit with the given circuit code already existed
  117. /// </returns>
  118. public virtual bool AddNewClient(
  119. EndPoint epSender, UseCircuitCodePacket useCircuit, AssetCache assetCache,
  120. AuthenticateResponse sessionInfo, EndPoint proxyEP)
  121. {
  122. IClientAPI newuser;
  123. uint circuitCode = useCircuit.CircuitCode.Code;
  124. if (m_scene.ClientManager.TryGetClient(circuitCode, out newuser))
  125. {
  126. // The circuit is already known to the scene. This not actually a problem since this will currently
  127. // occur if a client is crossing borders (hence upgrading its circuit). However, we shouldn't
  128. // really by trying to add a new client if this is the case.
  129. return false;
  130. }
  131. UUID agentId = useCircuit.CircuitCode.ID;
  132. UUID sessionId = useCircuit.CircuitCode.SessionID;
  133. newuser
  134. = CreateNewCircuit(
  135. epSender, m_scene, assetCache, this, sessionInfo, agentId, sessionId, circuitCode, proxyEP);
  136. m_scene.ClientManager.Add(circuitCode, newuser);
  137. newuser.OnViewerEffect += m_scene.ClientManager.ViewerEffectHandler;
  138. newuser.OnLogout += LogoutHandler;
  139. newuser.OnConnectionClosed += CloseClient;
  140. return true;
  141. }
  142. public void LogoutHandler(IClientAPI client)
  143. {
  144. client.SendLogoutPacket();
  145. CloseClient(client);
  146. }
  147. /// <summary>
  148. /// Send a packet to the given circuit
  149. /// </summary>
  150. /// <param name="buffer"></param>
  151. /// <param name="size"></param>
  152. /// <param name="flags"></param>
  153. /// <param name="circuitcode"></param>
  154. public virtual void SendPacketTo(byte[] buffer, int size, SocketFlags flags, uint circuitcode)
  155. {
  156. m_networkHandler.SendPacketTo(buffer, size, flags, circuitcode);
  157. }
  158. /// <summary>
  159. /// Close a client circuit only
  160. /// </summary>
  161. /// <param name="circuitcode"></param>
  162. public virtual void CloseCircuit(uint circuitcode)
  163. {
  164. m_networkHandler.RemoveClientCircuit(circuitcode);
  165. // XXX: Why is this commented out? Possibly because close mechanisms are so tangled right now
  166. //m_scene.ClientManager.CloseAllAgents(circuitcode);
  167. }
  168. /// <summary>
  169. /// Completely close down the given client.
  170. /// </summary>
  171. /// <param name="client"></param>
  172. public virtual void CloseClient(IClientAPI client)
  173. {
  174. //m_log.Info("PacketServer:CloseClient()");
  175. CloseCircuit(client.CircuitCode);
  176. m_scene.ClientManager.Remove(client.CircuitCode);
  177. client.Close(false);
  178. }
  179. }
  180. }