1
0

AgentCircuitData.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 float startfar = -1.0f;
  153. public Dictionary<string, object> ServiceURLs;
  154. public AgentCircuitData()
  155. {
  156. }
  157. /// <summary>
  158. /// Pack AgentCircuitData into an OSDMap for transmission over LLSD XML or LLSD json
  159. /// </summary>
  160. /// <returns>map of the agent circuit data</returns>
  161. public OSDMap PackAgentCircuitData(EntityTransferContext ctx)
  162. {
  163. OSDMap args = new OSDMap();
  164. args["agent_id"] = OSD.FromUUID(AgentID);
  165. args["base_folder"] = OSD.FromUUID(BaseFolder);
  166. args["caps_path"] = OSD.FromString(CapsPath);
  167. if (ChildrenCapSeeds != null)
  168. {
  169. OSDArray childrenSeeds = new OSDArray(ChildrenCapSeeds.Count);
  170. foreach (KeyValuePair<ulong, string> kvp in ChildrenCapSeeds)
  171. {
  172. OSDMap pair = new OSDMap();
  173. pair["handle"] = OSD.FromString(kvp.Key.ToString());
  174. pair["seed"] = OSD.FromString(kvp.Value);
  175. childrenSeeds.Add(pair);
  176. }
  177. if (ChildrenCapSeeds.Count > 0)
  178. args["children_seeds"] = childrenSeeds;
  179. }
  180. args["child"] = OSD.FromBoolean(child);
  181. args["circuit_code"] = OSD.FromString(circuitcode.ToString());
  182. args["first_name"] = OSD.FromString(firstname);
  183. args["last_name"] = OSD.FromString(lastname);
  184. args["inventory_folder"] = OSD.FromUUID(InventoryFolder);
  185. args["secure_session_id"] = OSD.FromUUID(SecureSessionID);
  186. args["session_id"] = OSD.FromUUID(SessionID);
  187. args["service_session_id"] = OSD.FromString(ServiceSessionID);
  188. args["start_pos"] = OSD.FromString(startpos.ToString());
  189. args["client_ip"] = OSD.FromString(IPAddress);
  190. args["viewer"] = OSD.FromString(Viewer);
  191. args["channel"] = OSD.FromString(Channel);
  192. args["mac"] = OSD.FromString(Mac);
  193. args["id0"] = OSD.FromString(Id0);
  194. if(startfar > 0)
  195. args["far"] = OSD.FromReal(startfar);
  196. if (Appearance != null)
  197. {
  198. args["appearance_serial"] = OSD.FromInteger(Appearance.Serial);
  199. OSDMap appmap = Appearance.Pack(ctx);
  200. args["packed_appearance"] = appmap;
  201. }
  202. // Old, bad way. Keeping it fow now for backwards compatibility
  203. // OBSOLETE -- soon to be deleted
  204. if (ServiceURLs != null && ServiceURLs.Count > 0)
  205. {
  206. OSDArray urls = new OSDArray(ServiceURLs.Count * 2);
  207. foreach (KeyValuePair<string, object> kvp in ServiceURLs)
  208. {
  209. //System.Console.WriteLine("XXX " + kvp.Key + "=" + kvp.Value);
  210. urls.Add(OSD.FromString(kvp.Key));
  211. urls.Add(OSD.FromString((kvp.Value == null) ? string.Empty : kvp.Value.ToString()));
  212. }
  213. args["service_urls"] = urls;
  214. }
  215. // again, this time the right way
  216. if (ServiceURLs != null && ServiceURLs.Count > 0)
  217. {
  218. OSDMap urls = new OSDMap();
  219. foreach (KeyValuePair<string, object> kvp in ServiceURLs)
  220. {
  221. //System.Console.WriteLine("XXX " + kvp.Key + "=" + kvp.Value);
  222. urls[kvp.Key] = OSD.FromString((kvp.Value == null) ? string.Empty : kvp.Value.ToString());
  223. }
  224. args["serviceurls"] = urls;
  225. }
  226. return args;
  227. }
  228. /// <summary>
  229. /// Unpack agent circuit data map into an AgentCiruitData object
  230. /// </summary>
  231. /// <param name="args"></param>
  232. public void UnpackAgentCircuitData(OSDMap args)
  233. {
  234. OSD tmpOSD;
  235. if (args.TryGetValue("agent_id", out tmpOSD))
  236. AgentID = tmpOSD.AsUUID();
  237. if (args.TryGetValue("base_folder", out tmpOSD))
  238. BaseFolder =tmpOSD.AsUUID();
  239. if (args.TryGetValue("caps_path", out tmpOSD))
  240. CapsPath = tmpOSD.AsString();
  241. if ((args.TryGetValue("children_seeds", out tmpOSD) && tmpOSD is OSDArray))
  242. {
  243. OSDArray childrenSeeds = (OSDArray)tmpOSD;
  244. ChildrenCapSeeds = new Dictionary<ulong, string>();
  245. foreach (OSD o in childrenSeeds)
  246. {
  247. if (o.Type == OSDType.Map)
  248. {
  249. ulong handle = 0;
  250. string seed = "";
  251. OSDMap pair = (OSDMap)o;
  252. if (pair.TryGetValue("handle", out tmpOSD))
  253. {
  254. if (!UInt64.TryParse(tmpOSD.AsString(), out handle))
  255. continue;
  256. }
  257. if (!ChildrenCapSeeds.ContainsKey(handle))
  258. {
  259. if (pair.TryGetValue("seed", out tmpOSD))
  260. {
  261. seed = tmpOSD.AsString();
  262. ChildrenCapSeeds.Add(handle, seed);
  263. }
  264. }
  265. }
  266. }
  267. }
  268. else
  269. ChildrenCapSeeds = new Dictionary<ulong, string>();
  270. if (args.TryGetValue("child", out tmpOSD))
  271. child = tmpOSD.AsBoolean();
  272. if (args.TryGetValue("circuit_code", out tmpOSD))
  273. UInt32.TryParse(tmpOSD.AsString(), out circuitcode);
  274. if (args.TryGetValue("first_name", out tmpOSD))
  275. firstname = tmpOSD.AsString();
  276. if (args.TryGetValue("last_name", out tmpOSD))
  277. lastname = tmpOSD.AsString();
  278. if (args.TryGetValue("inventory_folder", out tmpOSD))
  279. InventoryFolder = tmpOSD.AsUUID();
  280. if (args.TryGetValue("secure_session_id", out tmpOSD))
  281. SecureSessionID = tmpOSD.AsUUID();
  282. if (args.TryGetValue("session_id", out tmpOSD))
  283. SessionID = tmpOSD.AsUUID();
  284. if (args.TryGetValue("service_session_id", out tmpOSD))
  285. ServiceSessionID = tmpOSD.AsString();
  286. if (args.TryGetValue("client_ip", out tmpOSD))
  287. IPAddress = tmpOSD.AsString();
  288. if (args.TryGetValue("viewer", out tmpOSD))
  289. Viewer = tmpOSD.AsString();
  290. if (args.TryGetValue("channel", out tmpOSD))
  291. Channel = tmpOSD.AsString();
  292. if (args.TryGetValue("mac", out tmpOSD))
  293. Mac = tmpOSD.AsString();
  294. if (args.TryGetValue("id0", out tmpOSD))
  295. Id0 = tmpOSD.AsString();
  296. if (args.TryGetValue("teleport_flags", out tmpOSD))
  297. teleportFlags = tmpOSD.AsUInteger();
  298. if (args.TryGetValue("start_pos", out tmpOSD))
  299. Vector3.TryParse(tmpOSD.AsString(), out startpos);
  300. if(args.TryGetValue("far", out tmpOSD))
  301. startfar = (float)tmpOSD.AsReal();
  302. //m_log.InfoFormat("[AGENTCIRCUITDATA]: agentid={0}, child={1}, startpos={2}", AgentID, child, startpos);
  303. try
  304. {
  305. // Unpack various appearance elements
  306. Appearance = new AvatarAppearance();
  307. // Eventually this code should be deprecated, use full appearance
  308. // packing in packed_appearance
  309. if (args.TryGetValue("appearance_serial", out tmpOSD))
  310. Appearance.Serial = tmpOSD.AsInteger();
  311. if (args.TryGetValue("packed_appearance", out tmpOSD) && (tmpOSD is OSDMap))
  312. {
  313. Appearance.Unpack((OSDMap)tmpOSD);
  314. // m_log.InfoFormat("[AGENTCIRCUITDATA] unpacked appearance");
  315. }
  316. else
  317. {
  318. m_log.Warn("[AGENTCIRCUITDATA]: failed to find a valid packed_appearance");
  319. }
  320. }
  321. catch (Exception e)
  322. {
  323. m_log.ErrorFormat("[AGENTCIRCUITDATA] failed to unpack appearance; {0}",e.Message);
  324. }
  325. ServiceURLs = new Dictionary<string, object>();
  326. // Try parse the new way, OSDMap
  327. if (args.TryGetValue("serviceurls", out tmpOSD) && (tmpOSD is OSDMap))
  328. {
  329. OSDMap urls = (OSDMap)tmpOSD;
  330. foreach (KeyValuePair<String, OSD> kvp in urls)
  331. {
  332. ServiceURLs[kvp.Key] = kvp.Value;
  333. //System.Console.WriteLine("XXX " + kvp.Key + "=" + ServiceURLs[kvp.Key]);
  334. }
  335. }
  336. // else try the old way, OSDArray
  337. // OBSOLETE -- soon to be deleted
  338. else if (args.TryGetValue("service_urls", out tmpOSD) && (tmpOSD is OSDArray))
  339. {
  340. OSDArray urls = (OSDArray)tmpOSD;
  341. OSD tmpOSDb;
  342. for (int i = 0; i < urls.Count - 1; i += 2)
  343. {
  344. tmpOSD = urls[i];
  345. tmpOSDb = urls[i + 1];
  346. ServiceURLs[tmpOSD.AsString()] = tmpOSDb.AsString();
  347. //System.Console.WriteLine("XXX " + urls[i * 2].AsString() + "=" + urls[(i * 2) + 1].AsString());
  348. }
  349. }
  350. }
  351. }
  352. }