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