Last active
April 11, 2020 16:10
-
-
Save sakno/d348b772f0842d0336a655df69e6bbf8 to your computer and use it in GitHub Desktop.
Revisions
-
sakno revised this gist
Apr 11, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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). Completed implementation you can find [here](https://github.com/sakno/dotNext/blob/develop/src/DotNext/Net/NetworkInformation/MtuDiscovery.cs) -
sakno revised this gist
Apr 11, 2020 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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). Complete implementation you can find [here](https://github.com/sakno/dotNext/blob/develop/src/DotNext/Net/NetworkInformation/MtuDiscovery.cs) -
sakno revised this gist
Apr 11, 2020 . No changes.There are no files selected for viewing
-
sakno revised this gist
Apr 11, 2020 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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; 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 + icmpEchoHeaderSize; mtuLowerBound = currentMtu + 1; } else mtuUpperBound = currentMtu - 1; } return bestMtu; } ``` 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). -
sakno revised this gist
Apr 11, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 in C#: ```csharp static int DiscoverMtu(IPAddress address, int timeout, int ttl = 240) { -
sakno created this gist
Apr 11, 2020 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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).