Skip to content

Commit a67baa7

Browse files
test and updates
Adding FQDN validation and updating log errors provided to users. Adding back the invalid address tests to handle newer and older versions of UTP.
1 parent 8426358 commit a67baa7

File tree

5 files changed

+119
-5
lines changed

5 files changed

+119
-5
lines changed

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
using System;
99
using System.Collections.Generic;
10+
#if HOSTNAME_RESOLUTION_AVAILABLE && UTP_TRANSPORT_2_4_ABOVE
11+
using System.Text.RegularExpressions;
12+
#endif
1013
using Unity.Burst;
1114
using Unity.Collections;
1215
using Unity.Collections.LowLevel.Unsafe;
@@ -256,7 +259,14 @@ private static NetworkEndpoint ParseNetworkEndpoint(string ip, ushort port, bool
256259
if (!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv4) &&
257260
!NetworkEndpoint.TryParse(ip, port, out endpoint, NetworkFamily.Ipv6))
258261
{
262+
#if HOSTNAME_RESOLUTION_AVAILABLE && UTP_TRANSPORT_2_4_ABOVE
259263
return default;
264+
#else // If the user does not have the most recent version of UnityTransport installed
265+
if (!silent)
266+
{
267+
Debug.LogError($"Invalid network endpoint: {ip}:{port}.");
268+
}
269+
#endif
260270
}
261271

262272
return endpoint;
@@ -483,6 +493,15 @@ private NetworkPipeline SelectSendPipeline(NetworkDelivery delivery)
483493
return NetworkPipeline.Null;
484494
}
485495
}
496+
#if HOSTNAME_RESOLUTION_AVAILABLE && UTP_TRANSPORT_2_4_ABOVE
497+
private bool IsValidFqdn(string fqdn)
498+
{
499+
// Regular expression to validate FQDN
500+
string pattern = @"^(?=.{1,255}$)(?!-)[A-Za-z0-9-]{1,63}(?<!-)\.(?!-)(?:[A-Za-z0-9-]{1,63}\.?)+[A-Za-z]{2,6}$";
501+
var regex = new Regex(pattern);
502+
return regex.IsMatch(fqdn);
503+
}
504+
#endif
486505

487506
private bool ClientBindAndConnect()
488507
{
@@ -510,10 +529,21 @@ private bool ClientBindAndConnect()
510529
if (serverEndpoint.Family == NetworkFamily.Invalid)
511530
{
512531
#if HOSTNAME_RESOLUTION_AVAILABLE && UTP_TRANSPORT_2_4_ABOVE
513-
// If it's not valid, try to treat it like a URL.
514-
InitDriver();
515-
m_Driver.Connect(ConnectionData.Address, ConnectionData.Port);
516-
return true;
532+
533+
// If it's not valid, assure it meets FQDN standards
534+
if (IsValidFqdn(ConnectionData.Address))
535+
{
536+
// If so, then proceed with driver initialization and attempt to connect
537+
InitDriver();
538+
m_Driver.Connect(ConnectionData.Address, ConnectionData.Port);
539+
return true;
540+
}
541+
else
542+
{
543+
// If not then log an error and return false
544+
Debug.LogError($"Target server network address ({ConnectionData.Address}) is not a valid Fully Qualified Domain Name!");
545+
return false;
546+
}
517547
#else
518548
Debug.LogError($"Target server network address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!");
519549
return false;
@@ -550,8 +580,23 @@ private bool ServerBindAndListen(NetworkEndpoint endPoint)
550580
// Verify the endpoint is valid before proceeding
551581
if (endPoint.Family == NetworkFamily.Invalid)
552582
{
583+
#if HOSTNAME_RESOLUTION_AVAILABLE && UTP_TRANSPORT_2_4_ABOVE
584+
// If it's not valid, assure it meets FQDN standards
585+
if (!IsValidFqdn(ConnectionData.Address))
586+
{
587+
// If not then log an error and return false
588+
Debug.LogError($"Listen network address ({ConnectionData.Address}) is not a valid {NetworkFamily.Ipv4} or {NetworkFamily.Ipv6} address!");
589+
}
590+
else
591+
{
592+
Debug.LogError($"While ({ConnectionData.Address}) is a valid Fully Qualified Domain Name, you must use a valid {NetworkFamily.Ipv4} or {NetworkFamily.Ipv6} address when binding and listening for connections!");
593+
}
594+
return false;
595+
// Otherwise, attempt to bind to the FQDN's IP resolution
596+
#else
553597
Debug.LogError($"Network listen address ({ConnectionData.Address}) is {nameof(NetworkFamily.Invalid)}!");
554598
return false;
599+
#endif
555600
}
556601

557602
InitDriver();

com.unity.netcode.gameobjects/Tests/Editor/Transports/UnityTransportTests.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ public void UnityTransport_RestartSucceedsAfterFailure()
130130
transport.SetConnectionData("127.0.0.", 4242, "127.0.0.");
131131

132132
Assert.False(transport.StartServer());
133-
133+
#if !HOSTNAME_RESOLUTION_AVAILABLE && !UTP_TRANSPORT_2_4_ABOVE
134+
LogAssert.Expect(LogType.Error, "Invalid network endpoint: 127.0.0.:4242.");
135+
#endif
134136
LogAssert.Expect(LogType.Error, "Network listen address (127.0.0.) is Invalid!");
135137

136138
transport.SetConnectionData("127.0.0.1", 4242, "127.0.0.1");
@@ -152,6 +154,24 @@ public void UnityTransport_StartServerWithoutAddresses()
152154
transport.Shutdown();
153155
}
154156

157+
// Check that StartClient returns false with bad connection data.
158+
[Test]
159+
public void UnityTransport_StartClientFailsWithBadAddress()
160+
{
161+
UnityTransport transport = new GameObject().AddComponent<UnityTransport>();
162+
transport.Initialize();
163+
164+
transport.SetConnectionData("foobar", 4242);
165+
Assert.False(transport.StartClient());
166+
#if HOSTNAME_RESOLUTION_AVAILABLE && UTP_TRANSPORT_2_4_ABOVE
167+
LogAssert.Expect(LogType.Error, "Target server network address (foobar) is not a valid Fully Qualified Domain Name!");
168+
#else
169+
LogAssert.Expect(LogType.Error, "Invalid network endpoint: foobar:4242.");
170+
LogAssert.Expect(LogType.Error, "Target server network address (foobar) is Invalid!");
171+
#endif
172+
transport.Shutdown();
173+
}
174+
155175
[Test]
156176
public void UnityTransport_EmptySecurityStringsShouldThrow([Values("", null)] string cert, [Values("", null)] string secret)
157177
{

com.unity.netcode.gameobjects/Tests/Editor/com.unity.netcode.editortests.asmdef

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@
3333
"name": "Unity",
3434
"expression": "(0,2022.2.0a5)",
3535
"define": "UNITY_UNET_PRESENT"
36+
},
37+
{
38+
"name": "com.unity.transport",
39+
"expression": "2.4.0",
40+
"define": "UTP_TRANSPORT_2_4_ABOVE"
41+
},
42+
{
43+
"name": "Unity",
44+
"expression": "6000.1.0a1",
45+
"define": "HOSTNAME_RESOLUTION_AVAILABLE"
3646
}
3747
]
3848
}

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportConnectionTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ public IEnumerator Cleanup()
4646
yield return null;
4747
}
4848

49+
// Check that invalid endpoint addresses are detected and return false if detected
50+
[Test]
51+
public void DetectInvalidEndpoint()
52+
{
53+
using var netcodeLogAssert = new NetcodeLogAssert(true);
54+
InitializeTransport(out m_Server, out m_ServerEvents);
55+
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
56+
m_Server.ConnectionData.Address = "Fubar";
57+
m_Server.ConnectionData.ServerListenAddress = "Fubar";
58+
m_Clients[0].ConnectionData.Address = "MoreFubar";
59+
Assert.False(m_Server.StartServer(), "Server failed to detect invalid endpoint!");
60+
Assert.False(m_Clients[0].StartClient(), "Client failed to detect invalid endpoint!");
61+
#if HOSTNAME_RESOLUTION_AVAILABLE && UTP_TRANSPORT_2_4_ABOVE
62+
LogAssert.Expect(LogType.Error, $"Listen network address ({m_Server.ConnectionData.Address}) is not a valid {Networking.Transport.NetworkFamily.Ipv4} or {Networking.Transport.NetworkFamily.Ipv6} address!");
63+
LogAssert.Expect(LogType.Error, $"Target server network address ({m_Clients[0].ConnectionData.Address}) is not a valid Fully Qualified Domain Name!");
64+
65+
m_Server.ConnectionData.Address = "my.fubar.com";
66+
m_Server.ConnectionData.ServerListenAddress = "my.fubar.com";
67+
Assert.False(m_Server.StartServer(), "Server failed to detect invalid endpoint!");
68+
LogAssert.Expect(LogType.Error, $"While ({m_Server.ConnectionData.Address}) is a valid Fully Qualified Domain Name, you must use a " +
69+
$"valid {Networking.Transport.NetworkFamily.Ipv4} or {Networking.Transport.NetworkFamily.Ipv6} address when binding and listening for connections!");
70+
#else
71+
netcodeLogAssert.LogWasReceived(LogType.Error, $"Network listen address ({m_Server.ConnectionData.Address}) is Invalid!");
72+
netcodeLogAssert.LogWasReceived(LogType.Error, $"Target server network address ({m_Clients[0].ConnectionData.Address}) is Invalid!");
73+
#endif
74+
75+
UnityTransportTestComponent.CleanUp();
76+
}
77+
4978
// Check connection with a single client.
5079
[UnityTest]
5180
public IEnumerator ConnectSingleClient()

com.unity.netcode.gameobjects/Tests/Runtime/com.unity.netcode.runtimetests.asmdef

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@
4848
"name": "com.unity.modules.physics",
4949
"expression": "",
5050
"define": "COM_UNITY_MODULES_PHYSICS"
51+
},
52+
{
53+
"name": "com.unity.transport",
54+
"expression": "2.4.0",
55+
"define": "UTP_TRANSPORT_2_4_ABOVE"
56+
},
57+
{
58+
"name": "Unity",
59+
"expression": "6000.1.0a1",
60+
"define": "HOSTNAME_RESOLUTION_AVAILABLE"
5161
}
5262
],
5363
"noEngineReferences": false

0 commit comments

Comments
 (0)