* Translate SocketError.MessageSize to IPStatus.PacketTooBig in Ping.SendPing[Async]
* Address feedback
* Remove extra whitespace
p.Start();
if (!p.WaitForExit(timeout) || p.ExitCode == 1 || p.ExitCode == 2)
{
- return CreateTimedOutPingReply();
+ return CreatePingReply(IPStatus.TimedOut);
}
try
catch (TimeoutException)
{
p.Kill();
- return CreateTimedOutPingReply();
+ return CreatePingReply(IPStatus.TimedOut);
}
if (p.ExitCode == 1 || p.ExitCode == 2)
{
// Throw timeout for known failure return codes from ping functions.
- return CreateTimedOutPingReply();
+ return CreatePingReply(IPStatus.TimedOut);
}
try
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.TimedOut)
{
}
+ catch (SocketException ex) when (ex.SocketErrorCode == SocketError.MessageSize)
+ {
+ return CreatePingReply(IPStatus.PacketTooBig);
+ }
// We have exceeded our timeout duration, and no reply has been received.
- return CreateTimedOutPingReply();
+ return CreatePingReply(IPStatus.TimedOut);
}
}
catch (SocketException ex) when (ex.SocketErrorCode == SocketError.TimedOut)
{
}
+ catch (SocketException ex) when (ex.SocketErrorCode == SocketError.MessageSize)
+ {
+ return CreatePingReply(IPStatus.PacketTooBig);
+ }
catch (OperationCanceledException)
{
}
}
// We have exceeded our timeout duration, and no reply has been received.
- return CreateTimedOutPingReply();
+ return CreatePingReply(IPStatus.TimedOut);
}
}
- private static PingReply CreateTimedOutPingReply()
+ private static PingReply CreatePingReply(IPStatus status)
{
// Documentation indicates that you should only pay attention to the IPStatus value when
// its value is not "Success", but the rest of these values match that of the Windows implementation.
- return new PingReply(new IPAddress(0), null, IPStatus.TimedOut, 0, Array.Empty<byte>());
+ return new PingReply(new IPAddress(0), null, status, 0, Array.Empty<byte>());
}
#if DEBUG
});
}
+ [Fact]
+ public async Task SendPingWithIPAddressAndBigSize()
+ {
+ IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
+
+ using (Ping p = new Ping())
+ {
+ // Assert.DoesNotThrow
+ PingReply pingReply = await p.SendPingAsync(localIpAddress, TestSettings.PingTimeout, new byte[10001]);
+
+ // Depending on platform the call may either succeed, report timeout or report too big packet. It
+ // should not throw wrapped SocketException though which is what this test guards.
+ //
+ // On Windows 10 the maximum ping size seems essentially limited to 65500 bytes and thus any buffer
+ // size on the loopback ping succeeds. On macOS anything bigger than 8184 will report packet too
+ // big error. On Linux/Unix the result differs for privileged and unprivileged processes and may
+ // change with different platform versions.
+ if (OperatingSystem.IsMacOS())
+ {
+ Assert.Equal(IPStatus.PacketTooBig, pingReply.Status);
+ }
+ }
+ }
+
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[ActiveIssue("https://github.com/dotnet/runtime/issues/52617", TestPlatforms.iOS | TestPlatforms.tvOS | TestPlatforms.MacCatalyst)]
public async Task SendPings_ReuseInstance_Hostname()