1
0

AgentCircuitManager.cs 8.2 KB

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