Skip to content

fix: NGO UnityTransport not detecting invalid endpoint [MTT-3937] #2496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ Additional documentation and release notes are available at [Multiplayer Documen

## [Unreleased]

### Added

### Fixed

- Fixed issue where invalid endpoint addresses were not being detected and returning false from NGO UnityTransport. (#2496)
- Fixed some errors that could occur if a connection is lost and the loss is detected when attempting to write to the socket. (#2495)

## Changed

## [1.4.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,13 @@ private bool ClientBindAndConnect()
serverEndpoint = ConnectionData.ServerEndPoint;
}

// Verify the endpoint is valid before proceeding
if (serverEndpoint.Family == NetworkFamily.Invalid)
{
Debug.LogError($"Target server network address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!");
return false;
}

InitDriver();

var bindEndpoint = serverEndpoint.Family == NetworkFamily.Ipv6 ? NetworkEndpoint.AnyIpv6 : NetworkEndpoint.AnyIpv4;
Expand All @@ -554,6 +561,13 @@ private bool ClientBindAndConnect()

private bool ServerBindAndListen(NetworkEndpoint endPoint)
{
// Verify the endpoint is valid before proceeding
if (endPoint.Family == NetworkFamily.Invalid)
{
Debug.LogError($"Network listen address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!");
return false;
}

InitDriver();

int result = m_Driver.Bind(endPoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
using System.Text.RegularExpressions;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace Unity.Netcode.RuntimeTests
{
public class NetcodeLogAssert
public class NetcodeLogAssert : IDisposable
{
private struct LogData
{
Expand All @@ -20,8 +21,11 @@ private struct LogData

private List<LogData> AllLogs { get; }

public NetcodeLogAssert()
private bool m_ResetIgnoreFailingMessagesOnTearDown;
public NetcodeLogAssert(bool ignorFailingMessages = false, bool resetOnTearDown = true)
{
LogAssert.ignoreFailingMessages = ignorFailingMessages;
m_ResetIgnoreFailingMessagesOnTearDown = resetOnTearDown;
AllLogs = new List<LogData>();
Activate();
}
Expand Down Expand Up @@ -51,6 +55,16 @@ public void AddLog(string message, string stacktrace, LogType type)
}
}

[UnityTearDown]
public void OnTearDown()
{
// Defaults to true and will reset LogAssert.ignoreFailingMessages during tear down
if (m_ResetIgnoreFailingMessagesOnTearDown)
{
LogAssert.ignoreFailingMessages = false;
}
}

public void Dispose()
{
Dispose(true);
Expand All @@ -59,6 +73,8 @@ public void Dispose()

private void Dispose(bool disposing)
{
// Always reset when disposing
LogAssert.ignoreFailingMessages = false;
if (m_Disposed)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ public void UnityTransport_RestartSucceedsAfterFailure()
transport.Initialize();

transport.SetConnectionData("127.0.0.", 4242, "127.0.0.");

Assert.False(transport.StartServer());

LogAssert.Expect(LogType.Error, "Invalid network endpoint: 127.0.0.:4242.");
LogAssert.Expect(LogType.Error, $"Network listen address (127.0.0.) is Invalid!");
#if UTP_TRANSPORT_2_0_ABOVE
LogAssert.Expect(LogType.Error, "Socket creation failed (error Unity.Baselib.LowLevel.Binding+Baselib_ErrorState: Invalid argument (0x01000003) <argument name stripped>");
#endif
LogAssert.Expect(LogType.Error, "Server failed to bind. This is usually caused by another process being bound to the same port.");

transport.SetConnectionData("127.0.0.1", 4242, "127.0.0.1");
Assert.True(transport.StartServer());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ public IEnumerator Cleanup()
yield return null;
}

// Check that invalid endpoint addresses are detected and return false if detected
[Test]
public void DetectInvalidEndpoint()
{
using var netcodeLogAssert = new NetcodeLogAssert(true);
InitializeTransport(out m_Server, out m_ServerEvents);
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
m_Server.ConnectionData.Address = "Fubar";
m_Server.ConnectionData.ServerListenAddress = "Fubar";
m_Clients[0].ConnectionData.Address = "MoreFubar";
Assert.False(m_Server.StartServer(), "Server failed to detect invalid endpoint!");
Assert.False(m_Clients[0].StartClient(), "Client failed to detect invalid endpoint!");
netcodeLogAssert.LogWasReceived(LogType.Error, $"Network listen address ({m_Server.ConnectionData.Address}) is Invalid!");
netcodeLogAssert.LogWasReceived(LogType.Error, $"Target server network address ({m_Clients[0].ConnectionData.Address}) is Invalid!");
}

// Check connection with a single client.
[UnityTest]
public IEnumerator ConnectSingleClient()
Expand Down