AgentCircuitData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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;
  28. using System.Reflection;
  29. using System.Collections.Generic;
  30. using log4net;
  31. using OpenMetaverse;
  32. using OpenMetaverse.StructuredData;
  33. namespace OpenSim.Framework
  34. {
  35. /// <summary>
  36. /// Circuit data for an agent. Connection information shared between
  37. /// regions that accept UDP connections from a client
  38. /// </summary>
  39. public class AgentCircuitData
  40. {
  41. // DEBUG ON
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. // DEBUG OFF
  46. /// <summary>
  47. /// Avatar Unique Agent Identifier
  48. /// </summary>
  49. public UUID AgentID;
  50. /// <summary>
  51. /// Avatar's Appearance
  52. /// </summary>
  53. public AvatarAppearance Appearance;
  54. /// <summary>
  55. /// Agent's root inventory folder
  56. /// </summary>
  57. public UUID BaseFolder;
  58. /// <summary>
  59. /// Base Caps path for user
  60. /// </summary>
  61. public string CapsPath = String.Empty;
  62. /// <summary>
  63. /// Seed caps for neighbor regions that the user can see into
  64. /// </summary>
  65. public Dictionary<ulong, string> ChildrenCapSeeds;
  66. /// <summary>
  67. /// Root agent, or Child agent
  68. /// </summary>
  69. public bool child;
  70. /// <summary>
  71. /// Number given to the client when they log-in that they provide
  72. /// as credentials to the UDP server
  73. /// </summary>
  74. public uint circuitcode;
  75. /// <summary>
  76. /// How this agent got here
  77. /// </summary>
  78. public uint teleportFlags;
  79. /// <summary>
  80. /// Agent's account first name
  81. /// </summary>
  82. public string firstname;
  83. public UUID InventoryFolder;
  84. /// <summary>
  85. /// Agent's account last name
  86. /// </summary>
  87. public string lastname;
  88. /// <summary>
  89. /// Agent's full name.
  90. /// </summary>
  91. public string Name { get { return string.Format("{0} {1}", firstname, lastname); } }
  92. /// <summary>
  93. /// Random Unique GUID for this session. Client gets this at login and it's
  94. /// only supposed to be disclosed over secure channels
  95. /// </summary>
  96. public UUID SecureSessionID;
  97. /// <summary>
  98. /// Non secure Session ID
  99. /// </summary>
  100. public UUID SessionID;
  101. /// <summary>
  102. /// Hypergrid service token; generated by the user domain, consumed by the receiving grid.
  103. /// There is one such unique token for each grid visited.
  104. /// </summary>
  105. public string ServiceSessionID = string.Empty;
  106. /// <summary>
  107. /// The client's IP address, as captured by the login service
  108. /// </summary>
  109. public string IPAddress;
  110. /// <summary>
  111. /// Viewer's version string as reported by the viewer at login
  112. /// </summary>
  113. private string m_viewerInternal;
  114. /// <summary>
  115. /// Viewer's version string
  116. /// </summary>
  117. public string Viewer
  118. {
  119. set { m_viewerInternal = value; }
  120. // Try to return consistent viewer string taking into account
  121. // that viewers have chaagned how version is reported
  122. // See http://opensimulator.org/mantis/view.php?id=6851
  123. get
  124. {
  125. // Old style version string contains viewer name followed by a space followed by a version number
  126. if (m_viewerInternal == null || m_viewerInternal.Contains(" "))
  127. {
  128. return m_viewerInternal;
  129. }
  130. else // New style version contains no spaces, just version number
  131. {
  132. return Channel + " " + m_viewerInternal;
  133. }
  134. }
  135. }
  136. /// <summary>
  137. /// The channel strinf sent by the viewer at login
  138. /// </summary>
  139. public string Channel;
  140. /// <summary>
  141. /// The Mac address as reported by the viewer at login
  142. /// </summary>
  143. public string Mac;
  144. /// <summary>
  145. /// The id0 as reported by the viewer at login
  146. /// </summary>
  147. public string Id0;
  148. /// <summary>
  149. /// Position the Agent's Avatar starts in the region
  150. /// </summary>
  151. public Vector3 startpos;
  152. public Dictionary<string, object> ServiceURLs;
  153. public AgentCircuitData()
  154. {
  155. }
  156. /// <summary>
  157. /// Pack AgentCircuitData into an OSDMap for transmission over LLSD XML or LLSD json
  158. /// </summary>
  159. /// <returns>map of the agent circuit data</returns>
  160. public OSDMap PackAgentCircuitData()
  161. {
  162. OSDMap args = new OSDMap();
  163. args["agent_id"] = OSD.FromUUID(AgentID);
  164. args["base_folder"] = OSD.FromUUID(BaseFolder);
  165. args["caps_path"] = OSD.FromString(CapsPath);
  166. if (ChildrenCapSeeds != null)
  167. {
  168. OSDArray childrenSeeds = new OSDArray(ChildrenCapSeeds.Count);
  169. foreach (KeyValuePair<ulong, string> kvp in ChildrenCapSeeds)
  170. {
  171. OSDMap pair = new OSDMap();
  172. pair["handle"] = OSD.FromString(kvp.Key.ToString());
  173. pair["seed"] = OSD.FromString(kvp.Value);
  174. childrenSeeds.Add(pair);
  175. }
  176. if (ChildrenCapSeeds.Count > 0)
  177. args["children_seeds"] = childrenSeeds;
  178. }
  179. args["child"] = OSD.FromBoolean(child);
  180. args["circuit_code"] = OSD.FromString(circuitcode.ToString());
  181. args["first_name"] = OSD.FromString(firstname);
  182. args["last_name"] = OSD.FromString(lastname);
  183. args["inventory_folder"] = OSD.FromUUID(InventoryFolder);
  184. args["secure_session_id"] = OSD.FromUUID(SecureSessionID);
  185. args["session_id"] = OSD.FromUUID(SessionID);
  186. args["service_session_id"] = OSD.FromString(ServiceSessionID);
  187. args["start_pos"] = OSD.FromString(startpos.ToString());
  188. args["client_ip"] = OSD.FromString(IPAddress);
  189. args["viewer"] = OSD.FromString(Viewer);
  190. args["channel"] = OSD.FromString(Channel);
  191. args["mac"] = OSD.FromString(Mac);
  192. args["id0"] = OSD.FromString(Id0);
  193. if (Appearance != null)
  194. {
  195. args["appearance_serial"] = OSD.FromInteger(Appearance.Serial);
  196. OSDMap appmap = Appearance.Pack();
  197. args["packed_appearance"] = appmap;
  198. }
  199. // Old, bad way. Keeping it fow now for backwards compatibility
  200. // OBSOLETE -- soon to be deleted
  201. if (ServiceURLs != null && ServiceURLs.Count > 0)
  202. {
  203. OSDArray urls = new OSDArray(ServiceURLs.Count * 2);
  204. foreach (KeyValuePair<string, object> kvp in ServiceURLs)
  205. {
  206. //System.Console.WriteLine("XXX " + kvp.Key + "=" + kvp.Value);
  207. urls.Add(OSD.FromString(kvp.Key));
  208. urls.Add(OSD.FromString((kvp.Value == null) ? string.Empty : kvp.Value.ToString()));
  209. }
  210. args["service_urls"] = urls;
  211. }
  212. // again, this time the right way
  213. if (ServiceURLs != null && ServiceURLs.Count > 0)
  214. {
  215. OSDMap urls = new OSDMap();
  216. foreach (KeyValuePair<string, object> kvp in ServiceURLs)
  217. {
  218. //System.Console.WriteLine("XXX " + kvp.Key + "=" + kvp.Value);
  219. urls[kvp.Key] = OSD.FromString((kvp.Value == null) ? string.Empty : kvp.Value.ToString());
  220. }
  221. args["serviceurls"] = urls;
  222. }
  223. return args;
  224. }
  225. /// <summary>
  226. /// Unpack agent circuit data map into an AgentCiruitData object
  227. /// </summary>
  228. /// <param name="args"></param>
  229. public void UnpackAgentCircuitData(OSDMap args)
  230. {
  231. if (args["agent_id"] != null)
  232. AgentID = args["agent_id"].AsUUID();
  233. if (args["base_folder"] != null)
  234. BaseFolder = args["base_folder"].AsUUID();
  235. if (args["caps_path"] != null)
  236. CapsPath = args["caps_path"].AsString();
  237. if ((args["children_seeds"] != null) && (args["children_seeds"].Type == OSDType.Array))
  238. {
  239. OSDArray childrenSeeds = (OSDArray)(args["children_seeds"]);
  240. ChildrenCapSeeds = new Dictionary<ulong, string>();
  241. foreach (OSD o in childrenSeeds)
  242. {
  243. if (o.Type == OSDType.Map)
  244. {
  245. ulong handle = 0;
  246. string seed = "";
  247. OSDMap pair = (OSDMap)o;
  248. if (pair["handle"] != null)
  249. if (!UInt64.TryParse(pair["handle"].AsString(), out handle))
  250. continue;
  251. if (pair["seed"] != null)
  252. seed = pair["seed"].AsString();
  253. if (!ChildrenCapSeeds.ContainsKey(handle))
  254. ChildrenCapSeeds.Add(handle, seed);
  255. }
  256. }
  257. }
  258. else
  259. ChildrenCapSeeds = new Dictionary<ulong, string>();
  260. if (args["child"] != null)
  261. child = args["child"].AsBoolean();
  262. if (args["circuit_code"] != null)
  263. UInt32.TryParse(args["circuit_code"].AsString(), out circuitcode);
  264. if (args["first_name"] != null)
  265. firstname = args["first_name"].AsString();
  266. if (args["last_name"] != null)
  267. lastname = args["last_name"].AsString();
  268. if (args["inventory_folder"] != null)
  269. InventoryFolder = args["inventory_folder"].AsUUID();
  270. if (args["secure_session_id"] != null)
  271. SecureSessionID = args["secure_session_id"].AsUUID();
  272. if (args["session_id"] != null)
  273. SessionID = args["session_id"].AsUUID();
  274. if (args["service_session_id"] != null)
  275. ServiceSessionID = args["service_session_id"].AsString();
  276. if (args["client_ip"] != null)
  277. IPAddress = args["client_ip"].AsString();
  278. if (args["viewer"] != null)
  279. Viewer = args["viewer"].AsString();
  280. if (args["channel"] != null)
  281. Channel = args["channel"].AsString();
  282. if (args["mac"] != null)
  283. Mac = args["mac"].AsString();
  284. if (args["id0"] != null)
  285. Id0 = args["id0"].AsString();
  286. if (args["start_pos"] != null)
  287. Vector3.TryParse(args["start_pos"].AsString(), out startpos);
  288. //m_log.InfoFormat("[AGENTCIRCUITDATA]: agentid={0}, child={1}, startpos={2}", AgentID, child, startpos);
  289. try
  290. {
  291. // Unpack various appearance elements
  292. Appearance = new AvatarAppearance();
  293. // Eventually this code should be deprecated, use full appearance
  294. // packing in packed_appearance
  295. if (args["appearance_serial"] != null)
  296. Appearance.Serial = args["appearance_serial"].AsInteger();
  297. if (args.ContainsKey("packed_appearance") && (args["packed_appearance"].Type == OSDType.Map))
  298. {
  299. Appearance.Unpack((OSDMap)args["packed_appearance"]);
  300. // m_log.InfoFormat("[AGENTCIRCUITDATA] unpacked appearance");
  301. }
  302. else
  303. {
  304. m_log.Warn("[AGENTCIRCUITDATA]: failed to find a valid packed_appearance");
  305. }
  306. }
  307. catch (Exception e)
  308. {
  309. m_log.ErrorFormat("[AGENTCIRCUITDATA] failed to unpack appearance; {0}",e.Message);
  310. }
  311. ServiceURLs = new Dictionary<string, object>();
  312. // Try parse the new way, OSDMap
  313. if (args.ContainsKey("serviceurls") && args["serviceurls"] != null && (args["serviceurls"]).Type == OSDType.Map)
  314. {
  315. OSDMap urls = (OSDMap)(args["serviceurls"]);
  316. foreach (KeyValuePair<String, OSD> kvp in urls)
  317. {
  318. ServiceURLs[kvp.Key] = kvp.Value.AsString();
  319. //System.Console.WriteLine("XXX " + kvp.Key + "=" + ServiceURLs[kvp.Key]);
  320. }
  321. }
  322. // else try the old way, OSDArray
  323. // OBSOLETE -- soon to be deleted
  324. else if (args.ContainsKey("service_urls") && args["service_urls"] != null && (args["service_urls"]).Type == OSDType.Array)
  325. {
  326. OSDArray urls = (OSDArray)(args["service_urls"]);
  327. for (int i = 0; i < urls.Count / 2; i++)
  328. {
  329. ServiceURLs[urls[i * 2].AsString()] = urls[(i * 2) + 1].AsString();
  330. //System.Console.WriteLine("XXX " + urls[i * 2].AsString() + "=" + urls[(i * 2) + 1].AsString());
  331. }
  332. }
  333. }
  334. }
  335. }