1
0

AgentCircuitManager.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.Collections.Generic;
  28. using System.Runtime.InteropServices;
  29. using OpenMetaverse;
  30. namespace OpenSim.Framework
  31. {
  32. /// <summary>
  33. /// Manage client circuits
  34. /// </summary>
  35. public class AgentCircuitManager
  36. {
  37. /// <summary>
  38. /// Agent circuits indexed by circuit code.
  39. /// </summary>
  40. private readonly Dictionary<uint, AgentCircuitData> m_agentCircuits = new();
  41. /// <summary>
  42. /// Agent circuits indexed by agent UUID.
  43. /// </summary>
  44. private readonly Dictionary<UUID, AgentCircuitData> m_agentCircuitsByUUID = new();
  45. private readonly object m_lock = new();
  46. public virtual AuthenticateResponse AuthenticateSession(UUID sessionID, UUID agentID, uint circuitcode)
  47. {
  48. lock (m_lock)
  49. {
  50. if (m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData validcircuit) && validcircuit is not null)
  51. {
  52. if (sessionID.Equals(validcircuit.SessionID) && agentID.Equals(validcircuit.AgentID))
  53. {
  54. return new AuthenticateResponse()
  55. {
  56. Authorised = true,
  57. LoginInfo = new Login
  58. {
  59. Agent = agentID,
  60. Session = sessionID,
  61. SecureSession = validcircuit.SecureSessionID,
  62. First = validcircuit.firstname,
  63. Last = validcircuit.lastname,
  64. InventoryFolder = validcircuit.InventoryFolder,
  65. BaseFolder = validcircuit.BaseFolder,
  66. StartPos = validcircuit.startpos,
  67. StartFar = validcircuit.startfar
  68. }
  69. };
  70. }
  71. }
  72. }
  73. return new AuthenticateResponse();
  74. }
  75. /// <summary>
  76. /// Add information about a new circuit so that later on we can authenticate a new client session.
  77. /// </summary>
  78. /// <param name="circuitCode"></param>
  79. /// <param name="agentData"></param>
  80. public virtual void AddNewCircuit(AgentCircuitData agentData)
  81. {
  82. agentData.child = true;
  83. lock (m_lock)
  84. {
  85. ref AgentCircuitData acd = ref CollectionsMarshal.GetValueRefOrAddDefault(m_agentCircuits, agentData.circuitcode, out bool existed);
  86. if (existed && acd is not null && acd.AgentID.NotEqual(agentData.AgentID))
  87. m_agentCircuitsByUUID.Remove(acd.AgentID);
  88. acd = agentData;
  89. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  90. }
  91. }
  92. public virtual void AddNewCircuit(uint circuitCode, AgentCircuitData agentData)
  93. {
  94. agentData.circuitcode = circuitCode;
  95. lock (m_lock)
  96. {
  97. ref AgentCircuitData acd = ref CollectionsMarshal.GetValueRefOrAddDefault(m_agentCircuits, agentData.circuitcode, out bool existed);
  98. if (existed && acd is not null && acd.AgentID.NotEqual(agentData.AgentID))
  99. m_agentCircuitsByUUID.Remove(acd.AgentID);
  100. acd = agentData;
  101. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  102. }
  103. }
  104. public virtual void RemoveCircuit(uint circuitCode)
  105. {
  106. lock (m_lock)
  107. {
  108. if (m_agentCircuits.Remove(circuitCode, out AgentCircuitData ac))
  109. m_agentCircuitsByUUID.Remove(ac.AgentID);
  110. }
  111. }
  112. public virtual void RemoveCircuit(UUID agentID)
  113. {
  114. lock (m_lock)
  115. {
  116. if (m_agentCircuitsByUUID.Remove(agentID, out AgentCircuitData ac))
  117. m_agentCircuits.Remove(ac.circuitcode);
  118. }
  119. }
  120. public virtual void RemoveCircuit(AgentCircuitData ac)
  121. {
  122. lock (m_lock)
  123. {
  124. if (m_agentCircuitsByUUID.Remove(ac.AgentID, out AgentCircuitData byuuid))
  125. {
  126. if (byuuid.circuitcode != ac.circuitcode)
  127. m_agentCircuits.Remove(byuuid.circuitcode);
  128. }
  129. m_agentCircuits.Remove(ac.circuitcode);
  130. }
  131. }
  132. public AgentCircuitData GetAgentCircuitData(uint circuitCode)
  133. {
  134. lock (m_lock)
  135. {
  136. return m_agentCircuits.TryGetValue(circuitCode, out AgentCircuitData agentCircuit) ? agentCircuit : null;
  137. }
  138. }
  139. public AgentCircuitData GetAgentCircuitData(UUID agentID)
  140. {
  141. lock (m_lock)
  142. {
  143. return m_agentCircuitsByUUID.TryGetValue(agentID, out AgentCircuitData agentCircuit) ? agentCircuit : null;
  144. }
  145. }
  146. /// <summary>
  147. /// Get all current agent circuits indexed by agent UUID.
  148. /// </summary>
  149. /// <returns></returns>
  150. public Dictionary<UUID, AgentCircuitData> GetAgentCircuits()
  151. {
  152. lock (m_lock)
  153. return new Dictionary<UUID, AgentCircuitData>(m_agentCircuitsByUUID);
  154. }
  155. public void UpdateAgentData(AgentCircuitData agentData)
  156. {
  157. lock (m_lock)
  158. {
  159. if (m_agentCircuits.TryGetValue(agentData.circuitcode, out AgentCircuitData ac))
  160. {
  161. ac.firstname = agentData.firstname;
  162. ac.lastname = agentData.lastname;
  163. ac.startpos = agentData.startpos;
  164. ac.startfar = agentData.startfar;
  165. // Updated for when we don't know them before calling Scene.NewUserConnection
  166. ac.SecureSessionID = agentData.SecureSessionID;
  167. ac.SessionID = agentData.SessionID;
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// Sometimes the circuitcode may not be known before setting up the connection
  173. /// </summary>
  174. /// <param name="circuitcode"></param>
  175. /// <param name="newcircuitcode"></param>
  176. public bool TryChangeCircuitCode(uint circuitcode, uint newcircuitcode)
  177. {
  178. lock (m_lock)
  179. {
  180. if (m_agentCircuits.ContainsKey(newcircuitcode))
  181. return false;
  182. if (m_agentCircuits.Remove(circuitcode, out AgentCircuitData agentData))
  183. {
  184. agentData.circuitcode = newcircuitcode;
  185. m_agentCircuits[newcircuitcode] = agentData;
  186. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  187. return true;
  188. }
  189. return false;
  190. }
  191. }
  192. public void UpdateAgentChildStatus(uint circuitcode, bool childstatus)
  193. {
  194. lock (m_lock)
  195. {
  196. if (m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData ac))
  197. ac.child = childstatus;
  198. }
  199. }
  200. public bool GetAgentChildStatus(uint circuitcode)
  201. {
  202. lock (m_lock)
  203. {
  204. return m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData ac) && ac.child;
  205. }
  206. }
  207. }
  208. }