DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.4.1" />

 MtuDiscovery

public class MtuDiscovery : Ping
Allows to discover maximum size of Message Transfer Unit over IP network.
using System; using System.Net; using System.Net.NetworkInformation; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; namespace DotNext.Net.NetworkInformation { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class MtuDiscovery : Ping { private const int IcmpEchoHeaderSize = 8; public int? Discover(IPAddress address, int timeout, MtuDiscoveryOptions options, CancellationToken token = default(CancellationToken)) { int num = options.MinMtuSize; int num2 = options.MaxMtuSize; int? result = null; while (num <= num2) { int num3 = (num + num2) / 2; byte[] buffer = new byte[num3]; switch (Send(address, timeout, buffer, options).Status) { default: return null; case IPStatus.Success: result = num3 + 8; num = num3 + 1; break; case IPStatus.TimedOut: if (Environment.OSVersion.Platform != PlatformID.Unix) goto default; goto case IPStatus.PacketTooBig; case IPStatus.PacketTooBig: num2 = num3 - 1; break; } token.ThrowIfCancellationRequested(); } return result; } [AsyncStateMachine(typeof(<DiscoverAsync>d__2))] public Task<int?> DiscoverAsync(IPAddress address, int timeout, MtuDiscoveryOptions options, CancellationToken token = default(CancellationToken)) { <DiscoverAsync>d__2 stateMachine = default(<DiscoverAsync>d__2); stateMachine.<>t__builder = AsyncTaskMethodBuilder<int?>.Create(); stateMachine.<>4__this = this; stateMachine.address = address; stateMachine.timeout = timeout; stateMachine.options = options; stateMachine.token = token; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } } }