Make sure to test IPv6

This commit is contained in:
Jordan Brown
2020-07-14 10:54:30 -04:00
parent e01aedee03
commit 415c15dfd3
3 changed files with 13 additions and 4 deletions
@@ -481,7 +481,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(serverPortProvider.HttpApiPort);
SocketExtensions.BindTest(serverPortProvider.HttpApiPort, true);
}
/// <inheritdoc />
@@ -120,7 +120,7 @@ namespace Tgstation.Server.Host.Components.Session
{
try
{
SocketExtensions.BindTest(port);
SocketExtensions.BindTest(port, false);
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
{
@@ -12,12 +12,21 @@ namespace Tgstation.Server.Host.Extensions
/// Attempt to exclusively bind to a given <paramref name="port"/>.
/// </summary>
/// <param name="port">The port number to bind to.</param>
public static void BindTest(ushort port)
/// <param name="includeIPv6">If IPV6 should be tested as well.</param>
public static void BindTest(ushort port, bool includeIPv6)
{
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, true);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
socket.Bind(new IPEndPoint(IPAddress.Any, port));
if (includeIPv6)
socket.DualMode = true;
socket.Bind(
new IPEndPoint(
includeIPv6
? IPAddress.IPv6Any
: IPAddress.Any,
port));
}
}
}