1
0

AgentCircuitManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.Collections.Concurrent;
  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. /// <remarks>
  41. /// We lock this for operations both on this dictionary and on m_agentCircuitsByUUID
  42. /// </remarks>
  43. private ConcurrentDictionary<uint, AgentCircuitData> m_agentCircuits = new ConcurrentDictionary<uint, AgentCircuitData>();
  44. /// <summary>
  45. /// Agent circuits indexed by agent UUID.
  46. /// </summary>
  47. private ConcurrentDictionary<UUID, AgentCircuitData> m_agentCircuitsByUUID = new ConcurrentDictionary<UUID, AgentCircuitData>();
  48. public virtual AuthenticateResponse AuthenticateSession(UUID sessionID, UUID agentID, uint circuitcode)
  49. {
  50. AuthenticateResponse user = new AuthenticateResponse();
  51. if (!m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData validcircuit) || validcircuit == null)
  52. {
  53. //don't have this circuit code in our list
  54. user.Authorised = false;
  55. return user;
  56. }
  57. if ((sessionID == validcircuit.SessionID) && (agentID == validcircuit.AgentID))
  58. {
  59. user.Authorised = true;
  60. user.LoginInfo = new Login();
  61. user.LoginInfo.Agent = agentID;
  62. user.LoginInfo.Session = sessionID;
  63. user.LoginInfo.SecureSession = validcircuit.SecureSessionID;
  64. user.LoginInfo.First = validcircuit.firstname;
  65. user.LoginInfo.Last = validcircuit.lastname;
  66. user.LoginInfo.InventoryFolder = validcircuit.InventoryFolder;
  67. user.LoginInfo.BaseFolder = validcircuit.BaseFolder;
  68. user.LoginInfo.StartPos = validcircuit.startpos;
  69. user.LoginInfo.StartFar = (float)validcircuit.startfar;
  70. }
  71. else
  72. {
  73. // Invalid
  74. user.Authorised = false;
  75. }
  76. return user;
  77. }
  78. /// <summary>
  79. /// Add information about a new circuit so that later on we can authenticate a new client session.
  80. /// </summary>
  81. /// <param name="circuitCode"></param>
  82. /// <param name="agentData"></param>
  83. public virtual void AddNewCircuit(AgentCircuitData agentData)
  84. {
  85. agentData.child = true;
  86. RemoveCircuit(agentData.AgentID); // no duplications
  87. m_agentCircuits[agentData.circuitcode] = agentData;
  88. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  89. }
  90. public virtual void AddNewCircuit(uint circuitCode, AgentCircuitData agentData)
  91. {
  92. agentData.circuitcode = circuitCode;
  93. RemoveCircuit(agentData.AgentID); // no duplications
  94. m_agentCircuits[circuitCode] = agentData;
  95. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  96. }
  97. public virtual void RemoveCircuit(uint circuitCode)
  98. {
  99. if (m_agentCircuits.TryRemove(circuitCode, out AgentCircuitData ac))
  100. {
  101. m_agentCircuitsByUUID.TryRemove(ac.AgentID, out AgentCircuitData dummy);
  102. }
  103. }
  104. public virtual void RemoveCircuit(UUID agentID)
  105. {
  106. if (m_agentCircuitsByUUID.TryRemove(agentID, out AgentCircuitData ac))
  107. {
  108. m_agentCircuits.TryRemove(ac.circuitcode, out AgentCircuitData dummy);
  109. }
  110. }
  111. public virtual void RemoveCircuit(AgentCircuitData ac)
  112. {
  113. m_agentCircuitsByUUID.TryRemove(ac.AgentID, out AgentCircuitData dummy);
  114. m_agentCircuits.TryRemove(ac.circuitcode, out AgentCircuitData dummyb);
  115. if (dummy!= null && dummy.circuitcode != ac.circuitcode) //??
  116. m_agentCircuits.TryRemove(dummy.circuitcode, out AgentCircuitData dummyc);
  117. }
  118. public AgentCircuitData GetAgentCircuitData(uint circuitCode)
  119. {
  120. if(m_agentCircuits.TryGetValue(circuitCode, out AgentCircuitData agentCircuit))
  121. return agentCircuit;
  122. return null;
  123. }
  124. public AgentCircuitData GetAgentCircuitData(UUID agentID)
  125. {
  126. if(m_agentCircuitsByUUID.TryGetValue(agentID, out AgentCircuitData agentCircuit))
  127. return agentCircuit;
  128. return null;
  129. }
  130. /// <summary>
  131. /// Get all current agent circuits indexed by agent UUID.
  132. /// </summary>
  133. /// <returns></returns>
  134. public Dictionary<UUID, AgentCircuitData> GetAgentCircuits()
  135. {
  136. lock (m_agentCircuits)
  137. return new Dictionary<UUID, AgentCircuitData>(m_agentCircuitsByUUID);
  138. }
  139. public void UpdateAgentData(AgentCircuitData agentData)
  140. {
  141. if (m_agentCircuits.TryGetValue(agentData.circuitcode, out AgentCircuitData ac))
  142. {
  143. ac.firstname = agentData.firstname;
  144. ac.lastname = agentData.lastname;
  145. ac.startpos = agentData.startpos;
  146. ac.startfar = agentData.startfar;
  147. // Updated for when we don't know them before calling Scene.NewUserConnection
  148. ac.SecureSessionID = agentData.SecureSessionID;
  149. ac.SessionID = agentData.SessionID;
  150. }
  151. }
  152. /// <summary>
  153. /// Sometimes the circuitcode may not be known before setting up the connection
  154. /// </summary>
  155. /// <param name="circuitcode"></param>
  156. /// <param name="newcircuitcode"></param>
  157. public bool TryChangeCircuitCode(uint circuitcode, uint newcircuitcode)
  158. {
  159. if(m_agentCircuits.ContainsKey(newcircuitcode))
  160. return false;
  161. if (m_agentCircuits.TryRemove(circuitcode, out AgentCircuitData agentData))
  162. {
  163. agentData.circuitcode = newcircuitcode;
  164. m_agentCircuits[newcircuitcode] = agentData;
  165. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  166. return true;
  167. }
  168. return false;
  169. }
  170. public void UpdateAgentChildStatus(uint circuitcode, bool childstatus)
  171. {
  172. if (m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData ac))
  173. ac.child = childstatus;
  174. }
  175. public bool GetAgentChildStatus(uint circuitcode)
  176. {
  177. if (m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData ac))
  178. return ac.child;
  179. return false;
  180. }
  181. }
  182. }