From 7e58dc4db0838486a90fd8ee718b120ccd99f89b Mon Sep 17 00:00:00 2001 From: Christopher Date: Fri, 18 Mar 2022 13:02:10 +0100 Subject: [PATCH] Add checking udp port available and use other port than already in use. --- OpenSim/Framework/RegionInfo.cs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/OpenSim/Framework/RegionInfo.cs b/OpenSim/Framework/RegionInfo.cs index 4282fe06c0..79ca997ee5 100755 --- a/OpenSim/Framework/RegionInfo.cs +++ b/OpenSim/Framework/RegionInfo.cs @@ -36,6 +36,7 @@ using log4net; using Nini.Config; using OpenMetaverse; using OpenMetaverse.StructuredData; +using System.Net.NetworkInformation; //using OpenSim.Framework.Console; namespace OpenSim.Framework @@ -560,6 +561,15 @@ namespace OpenSim.Framework port = Convert.ToInt32(MainConsole.Instance.Prompt("Internal port", "9000")); config.Set("InternalPort", port); } + + if(!CheckAvailableServerPort(port)) + { + while(!CheckAvailableServerPort(port)) + { + port++; + } + } + m_internalEndPoint = new IPEndPoint(address, port); // ResolveAddress @@ -699,6 +709,29 @@ namespace OpenSim.Framework } } + private bool CheckAvailableServerPort(int port) + { + bool isAvailable = true; + + // Evaluate current system tcp connections. This is the same information provided + // by the netstat command line application, just in .Net strongly-typed object + // form. We will look through the list, and if our port we would like to use + // in our TcpClient is occupied, we will set isAvailable to false. + IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); + IPEndPoint[] udpConnInfoArray = ipGlobalProperties.GetActiveUdpListeners(); + + foreach (IPEndPoint endpoint in udpConnInfoArray) + { + if (endpoint.Port == port) + { + isAvailable = false; + break; + } + } + + return isAvailable; + } + // Make sure DefaultLanding is within region borders with a buffer zone 5 meters from borders private void DoDefaultLandingSanityChecks() { -- 2.30.0.windows.2