Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

add option to handle Ping TTL for raw socket and system utility#30324

Merged
wfurt merged 5 commits intodotnet:masterfrom
wfurt:fix_9350
Jun 26, 2018
Merged

add option to handle Ping TTL for raw socket and system utility#30324
wfurt merged 5 commits intodotnet:masterfrom
wfurt:fix_9350

Conversation

@wfurt
Copy link
Copy Markdown
Member

@wfurt wfurt commented Jun 12, 2018

related to #9350

This adds TTL option to Unix Ping implementation. It handles both - raw socket as well as system command line utility. I did some testing and -t seems pretty standard even on busybox on Alpine.

Don't fragment will need some more research and testing. It is less common (like busybox does not have it). Also PMTU discovery is default now on Linux so "don't fragment is set by default".

I tested the fix running tests as normal user and root. All existing tests are passing.
To really test this, we would need Outerloop test with some predictable and stable target.

@wfurt wfurt added bug Product bug (most likely) os-linux Linux OS (any supported distro) labels Jun 12, 2018
@wfurt wfurt self-assigned this Jun 12, 2018
@wfurt
Copy link
Copy Markdown
Member Author

wfurt commented Jun 12, 2018

@dotnet-bot test Outerloop Windows x64 Debug Build
@dotnet-bot test Outerloop Linux x64 Debug Build
@dotnet-bot test Outerloop NETFX x86 Debug Build
@dotnet-bot test Outerloop UWP CoreCLR x64 Debug Build
@dotnet-bot test Outerloop OSX x64 Debug Build

@davidsh davidsh added this to the 3.0 milestone Jun 12, 2018
}
else
{
// Linux uses -t ttl for both IPv4 & IPv6
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And macOS uses -t for ipv4?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. It is TTL for IPv4 and Hop Limit for IPv6.

}

string processArgs = UnixCommandLinePing.ConstructCommandLine(buffer.Length, address.ToString(), isIpv4);
string processArgs = UnixCommandLinePing.ConstructCommandLine(buffer.Length, address.ToString(), isIpv4, options?.Ttl ?? 0);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to test this?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test if the option is supported by ping?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't mean to comment on this line specifically... I meant test that this PR is doing what was intended, that the TTL is being respected now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. I did run wireshark when running on my Ubuntu VM. I'll re-run the test on OSX.
As far as unit test, we would need stable server with distance greater than 1.
Than we could craft outerloop test and verify results with different TTL.


private static readonly string s_discoveredPing4UtilityPath = GetPingUtilityPath(ipv4: true);
private static readonly string s_discoveredPing6UtilityPath = GetPingUtilityPath(ipv4: false);
private static readonly bool s_isOSX = Environment.OSVersion.Platform == PlatformID.MacOSX;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this and it worked? I thought we returned PlatformID.Unix for macOS.

@wfurt wfurt changed the title add option to handle TTL for raw socket and system utility add option to handle Ping TTL for raw socket and system utility Jun 12, 2018
@wfurt wfurt requested a review from a team June 12, 2018 18:46
@wfurt
Copy link
Copy Markdown
Member Author

wfurt commented Jun 12, 2018

cc: @janvorli for any other platform thoughts.

@wfurt
Copy link
Copy Markdown
Member Author

wfurt commented Jun 20, 2018

Merged with upstream changes and correct some issues.
The OS detection indeed did not work right but all test passed because of changes for #15018.
The other problem is that "localhost" normally only resolves to 127.0.0.1 on Unix.
/etc/host does not allow multiple addresses for the same name. (I check on Ubuntu, Centos and OSX)

So I refactored tests little bit so we try basic address tests for both IPv4 and IPv6 - in similar way how we test sockets.

As I mentioned originally, more work will be needed for "don't fragment" as busybox implementation on Alpine does not support it.

public void SendPingWithIPAddress_AddressAsString(AddressFamily addressFamily)
{
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
if (localIpAddress == null)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume addressFamily should be passed to GetLocalIPAddress here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it is. I don't know why github does not show it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why github does not show it

Because it's not there :-) I just pulled down your change, and it's:

            IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
             if (localIpAddress == null)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I found it. I was looking at wrong lines. sorry.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it so happened line 128 has almost exact (and right code). but the broken was on 146.
Updated and pushed change.

{
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
IPAddress localIpAddress = TestSettings.GetLocalIPAddress(addressFamily);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the other tests don't have this blank line

public void SendPingWithIPAddress_AddressAsString(AddressFamily addressFamily)
{
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
if (localIpAddress == null)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: extra space before if

{
IPAddress localIpAddress = TestSettings.GetLocalIPAddress();
IPAddress localIpAddress = TestSettings.GetLocalIPAddress(addressFamily);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove blank line

}

throw new InvalidOperationException("Unable to discover any addresses for the local host.");
return addressFamily == AddressFamily.InterNetwork ? IPAddress.Parse("127.0.0.1") : IPAddress.Parse("::1");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPAddress.Loopback and IPAddress.IPv6Loopback?

@wfurt wfurt merged commit 3035218 into dotnet:master Jun 26, 2018
@wfurt wfurt deleted the fix_9350 branch June 15, 2020 18:21
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…et/corefx#30324)

* add option to handle TTL for raw socket and system utility

* add missing blank line

* incorporate feedback and merge with upstream

* feedback from review


Commit migrated from dotnet/corefx@3035218
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Net bug Product bug (most likely) os-linux Linux OS (any supported distro)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants