From a9d3163e117c2a7ea4ecabc25c5922cc96efd7e5 Mon Sep 17 00:00:00 2001 From: Nick Rudzicz Date: Mon, 7 Aug 2023 10:02:50 -0400 Subject: [PATCH] Fix: SocketException with GetNetworkTime on cell During testing, we found that running GetNetworkTime on an iPhone XR (iOS 16.5.1c), on cellular data, caused a SocketException in Time.GetNetworkTime. After thorough investigation with Unity support, it was determined that ineligible (IPv6) addresses were being returned via Dns.GetHostEntryAsync; the present change addresses this. --- .../Assets/Apple.Core/Runtime/Time.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/Time.cs b/plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/Time.cs index 26e41584..c9f8af44 100644 --- a/plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/Time.cs +++ b/plug-ins/Apple.Core/Apple.Core_Unity/Assets/Apple.Core/Runtime/Time.cs @@ -26,16 +26,18 @@ public static class Time public static async Task GetNetworkTime() { var dnsLookupTask = Dns.GetHostEntryAsync(_ntpServer); - - if (await Task.WhenAny(dnsLookupTask, Task.Delay(DnsLookupTimeoutMS)) == dnsLookupTask) - { - var ipEndPoint = new IPEndPoint(dnsLookupTask.Result.AddressList[0], 123); - return await GetNetworkTime(ipEndPoint); - } - else - { + + if (await Task.WhenAny(dnsLookupTask, Task.Delay(DnsLookupTimeoutMS)) != dnsLookupTask) throw new Exception("Time.GetNetworkTime() DNS lookup has timed out."); + + var ipEndPoint = new IPEndPoint(0, 0); + foreach (var t in dnsLookupTask.Result.AddressList) + { + if (t.AddressFamily != AddressFamily.InterNetwork) continue; + ipEndPoint = new IPEndPoint(t, 123); + break; } + return await GetNetworkTime(ipEndPoint); } private static async Task GetNetworkTime(IPEndPoint endPoint)