AgentCircuitManager.cs 8.8 KB

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