RegionProxyPlugin.cs 21 KB

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