LLPacketServer.cs 8.1 KB

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