RegionProxyPlugin.cs 20 KB

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