We need to trace network route to our servers to debug player’s network connectivity problems, similar to what tracert cmd win utility does. For that we use simple code featuring Ping (System.Net.NetworkInformation.Ping) with gradually increasing TTL values:
public static async Task TraceRoute()
{
string host = "8.8.8.8";
short maxHops = 30;
int timeout = 5000;
byte[] sendBuffer = Encoding.ASCII.GetBytes("abcdefghijklmnopqrstuvwabcdefghi");
using Ping ping = new();
for (int ttl = 1; ttl <= maxHops; ttl++)
{
PingOptions options = new PingOptions(ttl, true);
PingReply reply = await ping.SendPingAsync(host, timeout, sendBuffer, options);
Debug.LogError($"{reply?.Address}: {reply?.RoundtripTime / 2} ({reply?.Status})");
}
Debug.LogError("Finished tracing");
}
It works as intended outside Unity (regular C# project). But When we try using it in unity (2023 and 6, both editor and build) it either ignores the TTL setting completely and ping the final destination from the get go or gives an exception “Unexpected socket error during ping request: MessageSize”. If we remove the options parameter (containing TTL) from call - it works fine in Unity as well, so it definitely has nothing to do with sendBuffer size. We also can’t use Raw ICMP sockets to perform ping manually as they require administrator priveleges in Windows which is unacceptable for us. Neither we can use Unity Ping as it doesn’t have TTL option.
So the question is: is this behaviour an expected one (limitations of current Unity backends) or does it have to do with something else? And can we perform network route tracing in some other way from our game build?