RegionProxyPlugin.cs 20 KB

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