AgentCircuitData.cs 15 KB

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