1
0

AgentCircuitManager.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. RemoveCircuit(agentData.AgentID); // no duplications
  86. m_agentCircuits[agentData.circuitcode] = agentData;
  87. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  88. }
  89. public virtual void AddNewCircuit(uint circuitCode, AgentCircuitData agentData)
  90. {
  91. RemoveCircuit(agentData.AgentID); // no duplications
  92. m_agentCircuits[circuitCode] = agentData;
  93. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  94. }
  95. public virtual void RemoveCircuit(uint circuitCode)
  96. {
  97. if (m_agentCircuits.TryRemove(circuitCode, out AgentCircuitData ac))
  98. {
  99. m_agentCircuitsByUUID.TryRemove(ac.AgentID, out AgentCircuitData dummy);
  100. }
  101. }
  102. public virtual void RemoveCircuit(UUID agentID)
  103. {
  104. if (m_agentCircuitsByUUID.TryRemove(agentID, out AgentCircuitData ac))
  105. {
  106. m_agentCircuits.TryRemove(ac.circuitcode, out AgentCircuitData dummy);
  107. }
  108. }
  109. public virtual void RemoveCircuit(AgentCircuitData ac)
  110. {
  111. m_agentCircuitsByUUID.TryRemove(ac.AgentID, out AgentCircuitData dummy);
  112. m_agentCircuits.TryRemove(ac.circuitcode, out AgentCircuitData dummyb);
  113. if (dummy.circuitcode != ac.circuitcode) //??
  114. m_agentCircuits.TryRemove(dummy.circuitcode, out AgentCircuitData dummyc);
  115. }
  116. public AgentCircuitData GetAgentCircuitData(uint circuitCode)
  117. {
  118. if(m_agentCircuits.TryGetValue(circuitCode, out AgentCircuitData agentCircuit))
  119. return agentCircuit;
  120. return null;
  121. }
  122. public AgentCircuitData GetAgentCircuitData(UUID agentID)
  123. {
  124. if(m_agentCircuitsByUUID.TryGetValue(agentID, out AgentCircuitData agentCircuit))
  125. return agentCircuit;
  126. return null;
  127. }
  128. /// <summary>
  129. /// Get all current agent circuits indexed by agent UUID.
  130. /// </summary>
  131. /// <returns></returns>
  132. public Dictionary<UUID, AgentCircuitData> GetAgentCircuits()
  133. {
  134. lock (m_agentCircuits)
  135. return new Dictionary<UUID, AgentCircuitData>(m_agentCircuitsByUUID);
  136. }
  137. public void UpdateAgentData(AgentCircuitData agentData)
  138. {
  139. if (m_agentCircuits.TryGetValue(agentData.circuitcode, out AgentCircuitData ac))
  140. {
  141. ac.firstname = agentData.firstname;
  142. ac.lastname = agentData.lastname;
  143. ac.startpos = agentData.startpos;
  144. ac.startfar = agentData.startfar;
  145. // Updated for when we don't know them before calling Scene.NewUserConnection
  146. ac.SecureSessionID = agentData.SecureSessionID;
  147. ac.SessionID = agentData.SessionID;
  148. }
  149. }
  150. /// <summary>
  151. /// Sometimes the circuitcode may not be known before setting up the connection
  152. /// </summary>
  153. /// <param name="circuitcode"></param>
  154. /// <param name="newcircuitcode"></param>
  155. public bool TryChangeCircuitCode(uint circuitcode, uint newcircuitcode)
  156. {
  157. if(m_agentCircuits.ContainsKey(newcircuitcode))
  158. return false;
  159. if (m_agentCircuits.TryRemove(circuitcode, out AgentCircuitData agentData))
  160. {
  161. agentData.circuitcode = newcircuitcode;
  162. m_agentCircuits[newcircuitcode] = agentData;
  163. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  164. return true;
  165. }
  166. return false;
  167. }
  168. public void UpdateAgentChildStatus(uint circuitcode, bool childstatus)
  169. {
  170. if (m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData ac))
  171. ac.child = childstatus;
  172. }
  173. public bool GetAgentChildStatus(uint circuitcode)
  174. {
  175. if (m_agentCircuits.TryGetValue(circuitcode, out AgentCircuitData ac))
  176. return ac.child;
  177. return false;
  178. }
  179. }
  180. }