AgentCircuitData.cs 15 KB

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