From 7310359f0bed359e0cf0471254e4f176ebd40591 Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Sat, 11 Nov 2023 14:08:38 -0500 Subject: [PATCH] OD uses UDP, so bind test to account for that --- .../Components/InstanceManager.cs | 2 +- .../Session/SessionControllerFactory.cs | 7 ++++--- .../Extensions/SocketExtensions.cs | 13 +++++++++---- .../Utils/PortAllocator.cs | 3 ++- .../Live/Instance/WatchdogTest.cs | 17 +++++++++++++++-- .../Live/TestLiveServer.cs | 1 - 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/InstanceManager.cs b/src/Tgstation.Server.Host/Components/InstanceManager.cs index 18dbd43fdb..0959e9ca01 100644 --- a/src/Tgstation.Server.Host/Components/InstanceManager.cs +++ b/src/Tgstation.Server.Host/Components/InstanceManager.cs @@ -648,7 +648,7 @@ namespace Tgstation.Server.Host.Components // This runs before the real socket is opened, ensures we don't perform reattaches unless we're fairly certain the bind won't fail // If it does fail, DD will be killed. - SocketExtensions.BindTest(platformIdentifier, serverPortProvider.HttpApiPort, true); + SocketExtensions.BindTest(platformIdentifier, serverPortProvider.HttpApiPort, true, false); } /// diff --git a/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs b/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs index 6c7b054367..3670e146b9 100644 --- a/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs +++ b/src/Tgstation.Server.Host/Components/Session/SessionControllerFactory.cs @@ -130,9 +130,10 @@ namespace Tgstation.Server.Host.Components.Session /// Check if a given can be bound to. /// /// The port number to test. + /// The we're bind testing for. /// The for the operation. /// A representing the running operation. - async ValueTask PortBindTest(ushort port, CancellationToken cancellationToken) + async ValueTask PortBindTest(ushort port, EngineType engineType, CancellationToken cancellationToken) { logger.LogTrace("Bind test: {port}", port); try @@ -142,7 +143,7 @@ namespace Tgstation.Server.Host.Components.Session for (var i = 0; i < MaxAttempts; ++i) try { - SocketExtensions.BindTest(platformIdentifier, port, false); + SocketExtensions.BindTest(platformIdentifier, port, false, engineType == EngineType.OpenDream); if (i > 0) logger.LogDebug("Clearing the socket took {iterations} attempts :/", i + 1); @@ -271,7 +272,7 @@ namespace Tgstation.Server.Host.Components.Session if (engineType == EngineType.Byond) await CheckPagerIsNotRunning(); - await PortBindTest(launchParameters.Port.Value, cancellationToken); + await PortBindTest(launchParameters.Port.Value, engineType, cancellationToken); string outputFilePath = null; var preserveLogFile = true; diff --git a/src/Tgstation.Server.Host/Extensions/SocketExtensions.cs b/src/Tgstation.Server.Host/Extensions/SocketExtensions.cs index 3c5d076e72..6c1a695025 100644 --- a/src/Tgstation.Server.Host/Extensions/SocketExtensions.cs +++ b/src/Tgstation.Server.Host/Extensions/SocketExtensions.cs @@ -17,7 +17,8 @@ namespace Tgstation.Server.Host.Extensions /// The to use. /// The port number to bind to. /// If IPV6 should be tested as well. - public static void BindTest(IPlatformIdentifier platformIdentifier, ushort port, bool includeIPv6) + /// If we're bind testing for UDP. If TCP will be checked. + public static void BindTest(IPlatformIdentifier platformIdentifier, ushort port, bool includeIPv6, bool udp) { ArgumentNullException.ThrowIfNull(platformIdentifier); ProcessExecutor.WithProcessLaunchExclusivity(() => @@ -26,12 +27,16 @@ namespace Tgstation.Server.Host.Extensions includeIPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork, - SocketType.Stream, - ProtocolType.Tcp); + udp + ? SocketType.Dgram + : SocketType.Stream, + udp + ? ProtocolType.Udp + : ProtocolType.Tcp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, true); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false); - if (platformIdentifier.IsWindows) + if (!udp && platformIdentifier.IsWindows) socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); if (includeIPv6) diff --git a/src/Tgstation.Server.Host/Utils/PortAllocator.cs b/src/Tgstation.Server.Host/Utils/PortAllocator.cs index cf53aabe11..310cfbfe3d 100644 --- a/src/Tgstation.Server.Host/Utils/PortAllocator.cs +++ b/src/Tgstation.Server.Host/Utils/PortAllocator.cs @@ -101,7 +101,8 @@ namespace Tgstation.Server.Host.Utils try { - SocketExtensions.BindTest(platformIdentifier, port, false); + SocketExtensions.BindTest(platformIdentifier, port, false, true); + SocketExtensions.BindTest(platformIdentifier, port, false, false); } catch (Exception ex) { diff --git a/tests/Tgstation.Server.Tests/Live/Instance/WatchdogTest.cs b/tests/Tgstation.Server.Tests/Live/Instance/WatchdogTest.cs index 473861045e..31b64c65ba 100644 --- a/tests/Tgstation.Server.Tests/Live/Instance/WatchdogTest.cs +++ b/tests/Tgstation.Server.Tests/Live/Instance/WatchdogTest.cs @@ -1,5 +1,6 @@ using Byond.TopicSender; +using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -19,6 +20,7 @@ using System.Net; using System.Net.Sockets; using System.Reflection; using System.Runtime.InteropServices; +using System.Runtime.Intrinsics.Arm; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -39,6 +41,8 @@ using Tgstation.Server.Host.Extensions; using Tgstation.Server.Host.IO; using Tgstation.Server.Host.System; +using static NuGet.Frameworks.FrameworkConstants; + namespace Tgstation.Server.Tests.Live.Instance { sealed class WatchdogTest : JobsRequiredTest @@ -544,10 +548,19 @@ namespace Tgstation.Server.Tests.Live.Instance JobResponse startJob; if (new PlatformIdentifier().IsWindows) // Can't get address reuse to trigger on linux for some reason - using (var blockSocket = new Socket(SocketType.Stream, ProtocolType.Tcp)) + using (var blockSocket = new Socket( + testVersion.Engine.Value == EngineType.OpenDream + ? SocketType.Dgram + : SocketType.Stream, + testVersion.Engine.Value == EngineType.OpenDream + ? ProtocolType.Udp + : ProtocolType.Tcp)) { blockSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, true); blockSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false); + if (testVersion.Engine.Value != EngineType.OpenDream) + blockSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); + blockSocket.Bind(new IPEndPoint(IPAddress.Any, ddPort)); // Don't use StartDD here @@ -742,7 +755,7 @@ namespace Tgstation.Server.Tests.Live.Instance { try { - SocketExtensions.BindTest(new PlatformIdentifier(), ddPort, false); + SocketExtensions.BindTest(new PlatformIdentifier(), ddPort, false, testVersion.Engine == EngineType.OpenDream); break; } catch diff --git a/tests/Tgstation.Server.Tests/Live/TestLiveServer.cs b/tests/Tgstation.Server.Tests/Live/TestLiveServer.cs index faf694db0e..681f4ea902 100644 --- a/tests/Tgstation.Server.Tests/Live/TestLiveServer.cs +++ b/tests/Tgstation.Server.Tests/Live/TestLiveServer.cs @@ -546,7 +546,6 @@ namespace Tgstation.Server.Tests.Live { // cleanup existing directories new LiveTestingServer(null, false).Dispose(); - const string PrivateKey = "adlfj73ywifhks7iwrgfegjs"; var controllerAddress = new Uri("http://localhost:15011");