AgentCircuitManager.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. user.LoginInfo.StartFar = (float)validcircuit.startfar;
  75. }
  76. else
  77. {
  78. // Invalid
  79. user.Authorised = false;
  80. }
  81. return user;
  82. }
  83. /// <summary>
  84. /// Add information about a new circuit so that later on we can authenticate a new client session.
  85. /// </summary>
  86. /// <param name="circuitCode"></param>
  87. /// <param name="agentData"></param>
  88. public virtual void AddNewCircuit(uint circuitCode, AgentCircuitData agentData)
  89. {
  90. lock (m_agentCircuits)
  91. {
  92. if (m_agentCircuits.ContainsKey(circuitCode))
  93. {
  94. m_agentCircuits[circuitCode] = agentData;
  95. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  96. }
  97. else
  98. {
  99. m_agentCircuits.Add(circuitCode, agentData);
  100. m_agentCircuitsByUUID[agentData.AgentID] = agentData;
  101. }
  102. }
  103. }
  104. public virtual void RemoveCircuit(uint circuitCode)
  105. {
  106. lock (m_agentCircuits)
  107. {
  108. if (m_agentCircuits.ContainsKey(circuitCode))
  109. {
  110. UUID agentID = m_agentCircuits[circuitCode].AgentID;
  111. m_agentCircuits.Remove(circuitCode);
  112. m_agentCircuitsByUUID.Remove(agentID);
  113. }
  114. }
  115. }
  116. public virtual void RemoveCircuit(UUID agentID)
  117. {
  118. lock (m_agentCircuits)
  119. {
  120. if (m_agentCircuitsByUUID.ContainsKey(agentID))
  121. {
  122. uint circuitCode = m_agentCircuitsByUUID[agentID].circuitcode;
  123. m_agentCircuits.Remove(circuitCode);
  124. m_agentCircuitsByUUID.Remove(agentID);
  125. }
  126. }
  127. }
  128. public AgentCircuitData GetAgentCircuitData(uint circuitCode)
  129. {
  130. AgentCircuitData agentCircuit = null;
  131. lock (m_agentCircuits)
  132. m_agentCircuits.TryGetValue(circuitCode, out agentCircuit);
  133. return agentCircuit;
  134. }
  135. public AgentCircuitData GetAgentCircuitData(UUID agentID)
  136. {
  137. AgentCircuitData agentCircuit = null;
  138. lock (m_agentCircuits)
  139. m_agentCircuitsByUUID.TryGetValue(agentID, out agentCircuit);
  140. return agentCircuit;
  141. }
  142. /// <summary>
  143. /// Get all current agent circuits indexed by agent UUID.
  144. /// </summary>
  145. /// <returns></returns>
  146. public Dictionary<UUID, AgentCircuitData> GetAgentCircuits()
  147. {
  148. lock (m_agentCircuits)
  149. return new Dictionary<UUID, AgentCircuitData>(m_agentCircuitsByUUID);
  150. }
  151. public void UpdateAgentData(AgentCircuitData agentData)
  152. {
  153. lock (m_agentCircuits)
  154. {
  155. if (m_agentCircuits.ContainsKey((uint) agentData.circuitcode))
  156. {
  157. m_agentCircuits[(uint) agentData.circuitcode].firstname = agentData.firstname;
  158. m_agentCircuits[(uint) agentData.circuitcode].lastname = agentData.lastname;
  159. m_agentCircuits[(uint)agentData.circuitcode].startpos = agentData.startpos;
  160. m_agentCircuits[(uint)agentData.circuitcode].startfar = agentData.startfar;
  161. // Updated for when we don't know them before calling Scene.NewUserConnection
  162. m_agentCircuits[(uint) agentData.circuitcode].SecureSessionID = agentData.SecureSessionID;
  163. m_agentCircuits[(uint) agentData.circuitcode].SessionID = agentData.SessionID;
  164. // m_log.Debug("update user start pos is " + agentData.startpos.X + " , " + agentData.startpos.Y + " , " + agentData.startpos.Z);
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// Sometimes the circuitcode may not be known before setting up the connection
  170. /// </summary>
  171. /// <param name="circuitcode"></param>
  172. /// <param name="newcircuitcode"></param>
  173. public bool TryChangeCiruitCode(uint circuitcode, uint newcircuitcode)
  174. {
  175. lock (m_agentCircuits)
  176. {
  177. if (m_agentCircuits.ContainsKey((uint)circuitcode) && !m_agentCircuits.ContainsKey((uint)newcircuitcode))
  178. {
  179. AgentCircuitData agentData = m_agentCircuits[(uint)circuitcode];
  180. agentData.circuitcode = newcircuitcode;
  181. m_agentCircuits.Remove((uint)circuitcode);
  182. m_agentCircuits.Add(newcircuitcode, agentData);
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. public void UpdateAgentChildStatus(uint circuitcode, bool childstatus)
  189. {
  190. lock (m_agentCircuits)
  191. if (m_agentCircuits.ContainsKey(circuitcode))
  192. m_agentCircuits[circuitcode].child = childstatus;
  193. }
  194. public bool GetAgentChildStatus(uint circuitcode)
  195. {
  196. lock (m_agentCircuits)
  197. if (m_agentCircuits.ContainsKey(circuitcode))
  198. return m_agentCircuits[circuitcode].child;
  199. return false;
  200. }
  201. }
  202. }