Skip to content

Instantly share code, notes, and snippets.

@sakno
Last active April 11, 2020 16:10
Show Gist options
  • Save sakno/d348b772f0842d0336a655df69e6bbf8 to your computer and use it in GitHub Desktop.
Save sakno/d348b772f0842d0336a655df69e6bbf8 to your computer and use it in GitHub Desktop.

Revisions

  1. sakno revised this gist Apr 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mtu_discovery.md
    Original file line number Diff line number Diff line change
    @@ -32,4 +32,4 @@ Note that discovered MTU is the amount of data that can be placed inside of sing

    You can compare result of this method with the output produced by `tracepath` on Linux (which includes size of IP header).

    Complete implementation you can find [here](https://github.com/sakno/dotNext/blob/develop/src/DotNext/Net/NetworkInformation/MtuDiscovery.cs)
    Completed implementation you can find [here](https://github.com/sakno/dotNext/blob/develop/src/DotNext/Net/NetworkInformation/MtuDiscovery.cs)
  2. sakno revised this gist Apr 11, 2020. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions mtu_discovery.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    The following code allows to discover MTU between your local machine and remote host in C#:
    ```csharp
    private static int? DiscoverMtu(IPAddress address, int timeout, int ttl = 240)
    static int? DiscoverMtu(IPAddress address, int timeout, int ttl = 240)
    {
    const int icmpEchoHeaderSize = 8;
    const int mtuMinSize = 60;
    @@ -30,4 +30,6 @@ The method returns **null** if MTU cannot be discovered. If you need asynchronou

    Note that discovered MTU is the amount of data that can be placed inside of single IP packet with `DF`(don't fragment) flag. This value doesn't include IP header size (which is at least 20 bytes for IPv4).

    You can compare result of this method with the output produced by `tracepath` on Linux (which includes size of IP header).
    You can compare result of this method with the output produced by `tracepath` on Linux (which includes size of IP header).

    Complete implementation you can find [here](https://github.com/sakno/dotNext/blob/develop/src/DotNext/Net/NetworkInformation/MtuDiscovery.cs)
  3. sakno revised this gist Apr 11, 2020. No changes.
  4. sakno revised this gist Apr 11, 2020. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions mtu_discovery.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    The following code allows to discover MTU between your local machine and remote host in C#:
    ```csharp
    static int DiscoverMtu(IPAddress address, int timeout, int ttl = 240)
    private static int? DiscoverMtu(IPAddress address, int timeout, int ttl = 240)
    {
    const int icmpEchoHeaderSize = 8;
    const int mtuMinSize = 60;
    const int mtuMaxSize = 65500;
    int mtuLowerBound = mtuMinSize, mtuUpperBound = mtuMaxSize, bestMtu = -1;
    int mtuLowerBound = mtuMinSize, mtuUpperBound = mtuMaxSize;
    var bestMtu = default(int?);
    using var ping = new Ping();
    var options = new PingOptions(ttl, true);
    for(int currentMtu; mtuLowerBound <= mtuUpperBound; )
    @@ -15,17 +16,17 @@ static int DiscoverMtu(IPAddress address, int timeout, int ttl = 240)
    var reply = ping.Send(address, timeout, buffer, options);
    if(reply.Status == IPStatus.Success)
    {
    bestMtu = currentMtu;
    bestMtu = currentMtu + icmpEchoHeaderSize;
    mtuLowerBound = currentMtu + 1;
    }
    else
    mtuUpperBound = currentMtu - 1;
    }
    return bestMtu + icmpEchoHeaderSize;
    return bestMtu;
    }
    ```

    If you need asynchronous version then just replace `ping.Send` with `ping.SendPingAsync` call.
    The method returns **null** if MTU cannot be discovered. If you need asynchronous version then just replace `ping.Send` with `ping.SendPingAsync` call and change return type to `Task<int?>`.

    Note that discovered MTU is the amount of data that can be placed inside of single IP packet with `DF`(don't fragment) flag. This value doesn't include IP header size (which is at least 20 bytes for IPv4).

  5. sakno revised this gist Apr 11, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mtu_discovery.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    The following code allows to discover MTU between your local machine and remote host:
    The following code allows to discover MTU between your local machine and remote host in C#:
    ```csharp
    static int DiscoverMtu(IPAddress address, int timeout, int ttl = 240)
    {
  6. sakno created this gist Apr 11, 2020.
    32 changes: 32 additions & 0 deletions mtu_discovery.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    The following code allows to discover MTU between your local machine and remote host:
    ```csharp
    static int DiscoverMtu(IPAddress address, int timeout, int ttl = 240)
    {
    const int icmpEchoHeaderSize = 8;
    const int mtuMinSize = 60;
    const int mtuMaxSize = 65500;
    int mtuLowerBound = mtuMinSize, mtuUpperBound = mtuMaxSize, bestMtu = -1;
    using var ping = new Ping();
    var options = new PingOptions(ttl, true);
    for(int currentMtu; mtuLowerBound <= mtuUpperBound; )
    {
    currentMtu = (mtuLowerBound + mtuUpperBound) / 2;
    var buffer = new byte[currentMtu];
    var reply = ping.Send(address, timeout, buffer, options);
    if(reply.Status == IPStatus.Success)
    {
    bestMtu = currentMtu;
    mtuLowerBound = currentMtu + 1;
    }
    else
    mtuUpperBound = currentMtu - 1;
    }
    return bestMtu + icmpEchoHeaderSize;
    }
    ```

    If you need asynchronous version then just replace `ping.Send` with `ping.SendPingAsync` call.

    Note that discovered MTU is the amount of data that can be placed inside of single IP packet with `DF`(don't fragment) flag. This value doesn't include IP header size (which is at least 20 bytes for IPv4).

    You can compare result of this method with the output produced by `tracepath` on Linux (which includes size of IP header).