RegionProxyPlugin.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 OpenSim 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.Collections;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Net.Sockets;
  32. using System.Reflection;
  33. using log4net;
  34. using Mono.Addins;
  35. using Nwc.XmlRpc;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Servers;
  38. [assembly : Addin("RegionProxy", "0.1")]
  39. [assembly : AddinDependency("OpenSim", "0.5")]
  40. namespace OpenSim.ApplicationPlugins.RegionProxy
  41. {
  42. /* This module has an interface to OpenSim clients that is constant, and is responsible for relaying
  43. * messages to and from clients to the region objects. Since the region objects can be duplicated and
  44. * moved dynamically, the proxy provides methods for changing and adding regions. If more than one region
  45. * is associated with a client port, then the message will be broadcasted to all those regions.
  46. *
  47. * The client interface port may be blocked. While being blocked, all messages from the clients will be
  48. * stored in the proxy. Once the interface port is unblocked again, all stored messages will be resent
  49. * to the regions. This functionality is used when moving or cloning an region to make sure that no messages
  50. * are sent to the region while it is being reconfigured.
  51. *
  52. * The proxy opens a XmlRpc interface with these public methods:
  53. * - AddPort
  54. * - AddRegion
  55. * - ChangeRegion
  56. * - BlockClientMessages
  57. * - UnblockClientMessages
  58. */
  59. [Extension("/OpenSim/Startup")]
  60. public class RegionProxyPlugin : IApplicationPlugin
  61. {
  62. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  63. private BaseHttpServer command_server;
  64. private ProxyServer proxy;
  65. #region IApplicationPlugin Members
  66. // TODO: required by IPlugin, but likely not at all right
  67. string m_name = "RegionProxyPlugin";
  68. string m_version = "0.0";
  69. public string Version { get { return m_version; } }
  70. public string Name { get { return m_name; } }
  71. public void Initialise()
  72. {
  73. m_log.Info("[PROXY]: " + Name + " cannot be default-initialized!");
  74. throw new PluginNotInitialisedException (Name);
  75. }
  76. public void Initialise(OpenSimBase openSim)
  77. {
  78. m_log.Info("Starting proxy");
  79. string proxyURL = openSim.ConfigSource.Source.Configs["Network"].GetString("proxy_url", "");
  80. if (proxyURL.Length == 0) return;
  81. uint port = (uint) Int32.Parse(proxyURL.Split(new char[] {':'})[2]);
  82. command_server = new BaseHttpServer(port);
  83. command_server.Start();
  84. command_server.AddXmlRPCHandler("AddPort", AddPort);
  85. command_server.AddXmlRPCHandler("AddRegion", AddRegion);
  86. command_server.AddXmlRPCHandler("DeleteRegion", DeleteRegion);
  87. command_server.AddXmlRPCHandler("ChangeRegion", ChangeRegion);
  88. command_server.AddXmlRPCHandler("BlockClientMessages", BlockClientMessages);
  89. command_server.AddXmlRPCHandler("UnblockClientMessages", UnblockClientMessages);
  90. command_server.AddXmlRPCHandler("Stop", Stop);
  91. proxy = new ProxyServer(m_log);
  92. }
  93. public void Dispose()
  94. {
  95. }
  96. #endregion
  97. private XmlRpcResponse Stop(XmlRpcRequest request)
  98. {
  99. try
  100. {
  101. proxy.Stop();
  102. }
  103. catch (Exception e)
  104. {
  105. m_log.Error("[PROXY]" + e.Message);
  106. m_log.Error("[PROXY]" + e.StackTrace);
  107. }
  108. return new XmlRpcResponse();
  109. }
  110. private XmlRpcResponse AddPort(XmlRpcRequest request)
  111. {
  112. try
  113. {
  114. int clientPort = (int) request.Params[0];
  115. int regionPort = (int) request.Params[1];
  116. string regionUrl = (string) request.Params[2];
  117. proxy.AddPort(clientPort, regionPort, regionUrl);
  118. }
  119. catch (Exception e)
  120. {
  121. m_log.Error("[PROXY]" + e.Message);
  122. m_log.Error("[PROXY]" + e.StackTrace);
  123. }
  124. return new XmlRpcResponse();
  125. }
  126. private XmlRpcResponse AddRegion(XmlRpcRequest request)
  127. {
  128. try
  129. {
  130. int currentRegionPort = (int) request.Params[0];
  131. string currentRegionUrl = (string) request.Params[1];
  132. int newRegionPort = (int) request.Params[2];
  133. string newRegionUrl = (string) request.Params[3];
  134. proxy.AddRegion(currentRegionPort, currentRegionUrl, newRegionPort, newRegionUrl);
  135. }
  136. catch (Exception e)
  137. {
  138. m_log.Error("[PROXY]" + e.Message);
  139. m_log.Error("[PROXY]" + e.StackTrace);
  140. }
  141. return new XmlRpcResponse();
  142. }
  143. private XmlRpcResponse ChangeRegion(XmlRpcRequest request)
  144. {
  145. try
  146. {
  147. int currentRegionPort = (int) request.Params[0];
  148. string currentRegionUrl = (string) request.Params[1];
  149. int newRegionPort = (int) request.Params[2];
  150. string newRegionUrl = (string) request.Params[3];
  151. proxy.ChangeRegion(currentRegionPort, currentRegionUrl, newRegionPort, newRegionUrl);
  152. }
  153. catch (Exception e)
  154. {
  155. m_log.Error("[PROXY]" + e.Message);
  156. m_log.Error("[PROXY]" + e.StackTrace);
  157. }
  158. return new XmlRpcResponse();
  159. }
  160. private XmlRpcResponse DeleteRegion(XmlRpcRequest request)
  161. {
  162. try
  163. {
  164. int currentRegionPort = (int) request.Params[0];
  165. string currentRegionUrl = (string) request.Params[1];
  166. proxy.DeleteRegion(currentRegionPort, currentRegionUrl);
  167. }
  168. catch (Exception e)
  169. {
  170. m_log.Error("[PROXY]" + e.Message);
  171. m_log.Error("[PROXY]" + e.StackTrace);
  172. }
  173. return new XmlRpcResponse();
  174. }
  175. private XmlRpcResponse BlockClientMessages(XmlRpcRequest request)
  176. {
  177. try
  178. {
  179. string regionUrl = (string) request.Params[0];
  180. int regionPort = (int) request.Params[1];
  181. proxy.BlockClientMessages(regionUrl, regionPort);
  182. }
  183. catch (Exception e)
  184. {
  185. m_log.Error("[PROXY]" + e.Message);
  186. m_log.Error("[PROXY]" + e.StackTrace);
  187. }
  188. return new XmlRpcResponse();
  189. }
  190. private XmlRpcResponse UnblockClientMessages(XmlRpcRequest request)
  191. {
  192. try
  193. {
  194. string regionUrl = (string) request.Params[0];
  195. int regionPort = (int) request.Params[1];
  196. proxy.UnblockClientMessages(regionUrl, regionPort);
  197. }
  198. catch (Exception e)
  199. {
  200. m_log.Error("[PROXY]" + e.Message);
  201. m_log.Error("[PROXY]" + e.StackTrace);
  202. }
  203. return new XmlRpcResponse();
  204. }
  205. }
  206. public class ProxyServer
  207. {
  208. protected readonly ILog m_log;
  209. protected ProxyMap proxy_map = new ProxyMap();
  210. protected AsyncCallback receivedData;
  211. protected bool running;
  212. public ProxyServer(ILog log)
  213. {
  214. m_log = log;
  215. running = false;
  216. receivedData = new AsyncCallback(OnReceivedData);
  217. }
  218. public void BlockClientMessages(string regionUrl, int regionPort)
  219. {
  220. EndPoint client = proxy_map.GetClient(new IPEndPoint(IPAddress.Parse(regionUrl), regionPort));
  221. ProxyMap.RegionData rd = proxy_map.GetRegionData(client);
  222. rd.isBlocked = true;
  223. }
  224. public void UnblockClientMessages(string regionUrl, int regionPort)
  225. {
  226. EndPoint client = proxy_map.GetClient(new IPEndPoint(IPAddress.Parse(regionUrl), regionPort));
  227. ProxyMap.RegionData rd = proxy_map.GetRegionData(client);
  228. rd.isBlocked = false;
  229. while (rd.storedMessages.Count > 0)
  230. {
  231. StoredMessage msg = (StoredMessage) rd.storedMessages.Dequeue();
  232. //m_log.Verbose("[PROXY]"+"Resending blocked message from {0}", msg.senderEP);
  233. SendMessage(msg.buffer, msg.length, msg.senderEP, msg.sd);
  234. }
  235. }
  236. public void AddRegion(int oldRegionPort, string oldRegionUrl, int newRegionPort, string newRegionUrl)
  237. {
  238. //m_log.Verbose("[PROXY]"+"AddRegion {0} {1}", oldRegionPort, newRegionPort);
  239. EndPoint client = proxy_map.GetClient(new IPEndPoint(IPAddress.Parse(oldRegionUrl), oldRegionPort));
  240. ProxyMap.RegionData data = proxy_map.GetRegionData(client);
  241. data.regions.Add(new IPEndPoint(IPAddress.Parse(newRegionUrl), newRegionPort));
  242. }
  243. public void ChangeRegion(int oldRegionPort, string oldRegionUrl, int newRegionPort, string newRegionUrl)
  244. {
  245. //m_log.Verbose("[PROXY]"+"ChangeRegion {0} {1}", oldRegionPort, newRegionPort);
  246. EndPoint client = proxy_map.GetClient(new IPEndPoint(IPAddress.Parse(oldRegionUrl), oldRegionPort));
  247. ProxyMap.RegionData data = proxy_map.GetRegionData(client);
  248. data.regions.Clear();
  249. data.regions.Add(new IPEndPoint(IPAddress.Parse(newRegionUrl), newRegionPort));
  250. }
  251. public void DeleteRegion(int oldRegionPort, string oldRegionUrl)
  252. {
  253. m_log.InfoFormat("[PROXY]" + "DeleteRegion {0} {1}", oldRegionPort, oldRegionUrl);
  254. EndPoint regionEP = new IPEndPoint(IPAddress.Parse(oldRegionUrl), oldRegionPort);
  255. EndPoint client = proxy_map.GetClient(regionEP);
  256. ProxyMap.RegionData data = proxy_map.GetRegionData(client);
  257. data.regions.Remove(regionEP);
  258. }
  259. public void AddPort(int clientPort, int regionPort, string regionUrl)
  260. {
  261. running = true;
  262. //m_log.Verbose("[PROXY]"+"AddPort {0} {1}", clientPort, regionPort);
  263. IPEndPoint clientEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), clientPort);
  264. proxy_map.Add(clientEP, new IPEndPoint(IPAddress.Parse(regionUrl), regionPort));
  265. ServerData sd = new ServerData();
  266. sd.clientEP = new IPEndPoint(clientEP.Address, clientEP.Port);
  267. OpenPort(sd);
  268. }
  269. protected void OpenPort(ServerData sd)
  270. {
  271. // sd.clientEP must be set before calling this function
  272. ClosePort(sd);
  273. try
  274. {
  275. m_log.InfoFormat("[PROXY] Opening special UDP socket on {0}", sd.clientEP);
  276. sd.serverIP = new IPEndPoint(IPAddress.Parse("0.0.0.0"), ((IPEndPoint) sd.clientEP).Port);
  277. sd.server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  278. sd.server.Bind(sd.serverIP);
  279. sd.senderEP = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  280. //receivedData = new AsyncCallback(OnReceivedData);
  281. sd.server.BeginReceiveFrom(sd.recvBuffer, 0, sd.recvBuffer.Length, SocketFlags.None, ref sd.senderEP, receivedData, sd);
  282. }
  283. catch (Exception e)
  284. {
  285. m_log.ErrorFormat("[PROXY] Failed to (re)open socket {0}", sd.clientEP);
  286. m_log.Error("[PROXY]" + e.Message);
  287. m_log.Error("[PROXY]" + e.StackTrace);
  288. }
  289. }
  290. protected static void ClosePort(ServerData sd)
  291. {
  292. // Close the port if it exists and is open
  293. if (sd.server == null) return;
  294. try
  295. {
  296. sd.server.Shutdown(SocketShutdown.Both);
  297. sd.server.Close();
  298. }
  299. catch (Exception)
  300. {
  301. }
  302. }
  303. public void Stop()
  304. {
  305. running = false;
  306. m_log.InfoFormat("[PROXY] Stopping the proxy server");
  307. }
  308. protected virtual void OnReceivedData(IAsyncResult result)
  309. {
  310. if (!running) return;
  311. ServerData sd = (ServerData) result.AsyncState;
  312. sd.senderEP = new IPEndPoint(IPAddress.Parse("0.0.0.0"), 0);
  313. try
  314. {
  315. int numBytes = sd.server.EndReceiveFrom(result, ref sd.senderEP);
  316. if (numBytes > 0)
  317. {
  318. SendMessage(sd.recvBuffer, numBytes, sd.senderEP, sd);
  319. }
  320. }
  321. catch (Exception e)
  322. {
  323. // OpenPort(sd); // reopen the port just in case
  324. m_log.ErrorFormat("[PROXY] EndReceiveFrom failed in {0}", sd.clientEP);
  325. m_log.Error("[PROXY]" + e.Message);
  326. m_log.Error("[PROXY]" + e.StackTrace);
  327. }
  328. WaitForNextMessage(sd);
  329. }
  330. protected void WaitForNextMessage(ServerData sd)
  331. {
  332. bool error = true;
  333. while (error)
  334. {
  335. error = false;
  336. try
  337. {
  338. sd.server.BeginReceiveFrom(sd.recvBuffer, 0, sd.recvBuffer.Length, SocketFlags.None, ref sd.senderEP, receivedData, sd);
  339. }
  340. catch (Exception e)
  341. {
  342. error = true;
  343. m_log.ErrorFormat("[PROXY] BeginReceiveFrom failed, retrying... {0}", sd.clientEP);
  344. m_log.Error("[PROXY]" + e.Message);
  345. m_log.Error("[PROXY]" + e.StackTrace);
  346. OpenPort(sd);
  347. }
  348. }
  349. }
  350. protected void SendMessage(byte[] buffer, int length, EndPoint senderEP, ServerData sd)
  351. {
  352. int numBytes = length;
  353. //m_log.ErrorFormat("[PROXY] Got message from {0} in thread {1}, size {2}", senderEP, sd.clientEP, numBytes);
  354. EndPoint client = proxy_map.GetClient(senderEP);
  355. if (client != null)
  356. {
  357. try
  358. {
  359. client = PacketPool.DecodeProxyMessage(buffer, ref numBytes);
  360. try
  361. {
  362. // This message comes from a region object, forward it to the its client
  363. sd.server.SendTo(buffer, numBytes, SocketFlags.None, client);
  364. //m_log.InfoFormat("[PROXY] Sending region message from {0} to {1}, size {2}", senderEP, client, numBytes);
  365. }
  366. catch (Exception e)
  367. {
  368. OpenPort(sd); // reopen the port just in case
  369. m_log.ErrorFormat("[PROXY] Failed sending region message from {0} to {1}", senderEP, client);
  370. m_log.Error("[PROXY]" + e.Message);
  371. m_log.Error("[PROXY]" + e.StackTrace);
  372. return;
  373. }
  374. }
  375. catch (Exception e)
  376. {
  377. OpenPort(sd); // reopen the port just in case
  378. m_log.ErrorFormat("[PROXY] Failed decoding region message from {0}", senderEP);
  379. m_log.Error("[PROXY]" + e.Message);
  380. m_log.Error("[PROXY]" + e.StackTrace);
  381. return;
  382. }
  383. }
  384. else
  385. {
  386. // This message comes from a client object, forward it to the the region(s)
  387. PacketPool.EncodeProxyMessage(buffer, ref numBytes, senderEP);
  388. ProxyMap.RegionData rd = proxy_map.GetRegionData(sd.clientEP);
  389. foreach (EndPoint region in rd.regions)
  390. {
  391. if (rd.isBlocked)
  392. {
  393. rd.storedMessages.Enqueue(new StoredMessage(buffer, length, numBytes, senderEP, sd));
  394. }
  395. else
  396. {
  397. try
  398. {
  399. sd.server.SendTo(buffer, numBytes, SocketFlags.None, region);
  400. //m_log.InfoFormat("[PROXY] Sending client message from {0} to {1}", senderEP, region);
  401. }
  402. catch (Exception e)
  403. {
  404. OpenPort(sd); // reopen the port just in case
  405. m_log.ErrorFormat("[PROXY] Failed sending client message from {0} to {1}", senderEP, region);
  406. m_log.Error("[PROXY]" + e.Message);
  407. m_log.Error("[PROXY]" + e.StackTrace);
  408. return;
  409. }
  410. }
  411. }
  412. }
  413. }
  414. #region Nested type: ProxyMap
  415. protected class ProxyMap
  416. {
  417. private Dictionary<EndPoint, RegionData> map;
  418. public ProxyMap()
  419. {
  420. map = new Dictionary<EndPoint, RegionData>();
  421. }
  422. public void Add(EndPoint client, EndPoint region)
  423. {
  424. if (map.ContainsKey(client))
  425. {
  426. map[client].regions.Add(region);
  427. }
  428. else
  429. {
  430. RegionData regions = new RegionData();
  431. map.Add(client, regions);
  432. regions.regions.Add(region);
  433. }
  434. }
  435. public RegionData GetRegionData(EndPoint client)
  436. {
  437. return map[client];
  438. }
  439. public EndPoint GetClient(EndPoint region)
  440. {
  441. foreach (KeyValuePair<EndPoint, RegionData> pair in map)
  442. {
  443. if (pair.Value.regions.Contains(region))
  444. {
  445. return pair.Key;
  446. }
  447. }
  448. return null;
  449. }
  450. #region Nested type: RegionData
  451. public class RegionData
  452. {
  453. public bool isBlocked = false;
  454. public List<EndPoint> regions = new List<EndPoint>();
  455. public Queue storedMessages = new Queue();
  456. }
  457. #endregion
  458. }
  459. #endregion
  460. #region Nested type: ServerData
  461. protected class ServerData
  462. {
  463. public EndPoint clientEP;
  464. public byte[] recvBuffer = new byte[4096];
  465. public EndPoint senderEP;
  466. public Socket server;
  467. public IPEndPoint serverIP;
  468. public ServerData()
  469. {
  470. server = null;
  471. }
  472. }
  473. #endregion
  474. #region Nested type: StoredMessage
  475. protected class StoredMessage
  476. {
  477. public byte[] buffer;
  478. public int length;
  479. public ServerData sd;
  480. public EndPoint senderEP;
  481. public StoredMessage(byte[] buffer, int length, int maxLength, EndPoint senderEP, ServerData sd)
  482. {
  483. this.buffer = new byte[maxLength];
  484. this.length = length;
  485. for (int i = 0; i < length; i++) this.buffer[i] = buffer[i];
  486. this.senderEP = senderEP;
  487. this.sd = sd;
  488. }
  489. }
  490. #endregion
  491. }
  492. }