Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Net.NetworkInformation.Ping needs sudo to ping an endpoint in .NET 7 preview 2 #66746

Closed
adityapatwardhan opened this issue Mar 17, 2022 · 5 comments
Labels
area-System.Net documentation Documentation bug or enhancement, does not impact product or test code
Milestone

Comments

@adityapatwardhan
Copy link

Description

PowerShell uses the System.Net.NetworkInformation.Ping API for Test-Connection cmdlet for pinging. Since .NET 7 Preview 2 we see that sudo is needed for using the cmdlet. It works fine on macOS.

This is a regression from .NET 6.

Output from test app:

PS /home/aditya/PingTest> /home/aditya/PingTest/bin/Debug/net6.0/linux-x64/publish/PingTest www.google.com
Hello
Address: 142.250.217.100
RoundTrip time: 12

PS /home/aditya/PingTest> /home/aditya/PingTest/bin/Debug/net7.0/linux-x64/publish/PingTest www.google.com
Hello
Unhandled exception. System.PlatformNotSupportedException: Unable to send custom ping payload. Run program under privileged user account or grant cap_net_raw capability using setcap(8).
at System.Net.NetworkInformation.Ping.GetPingProcess(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.SendWithPingUtility(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.SendPingCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at Program.

$(String[] args) in /home/aditya/PingTest/Program.cs:line 20

PS /home/aditya/PingTest> sudo /home/aditya/PingTest/bin/Debug/net7.0/linux-x64/publish/PingTest www.google.com
Hello
Address: 142.250.217.100
RoundTrip time: 13

Reproduction Steps

dotnet new console -b PingTest

Contents for Program.cs

// See https://aka.ms/new-console-template for more information
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

Console.WriteLine("Hello");

Ping pingSender = new Ping();
PingOptions options = new PingOptions();

// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
//options.DontFragment = true;

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(args[0], timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
    Console.WriteLine("Address: {0}", reply.Address.ToString());
    Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
}

dotnet run www.google.com

Expected behavior

Hello
Address: 204.79.197.200
RoundTrip time: 24
Time to live: 128
Don't fragment: False
Buffer size: 32

Actual behavior

Hello
Unhandled exception. System.PlatformNotSupportedException: Unable to send custom ping payload. Run program under privileged user account or grant cap_net_raw capability using setcap(8).
at System.Net.NetworkInformation.Ping.GetPingProcess(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.SendWithPingUtility(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.SendPingCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at Program.

$(String[] args) in /home/aditya/PingTest/Program.cs:line 20

Regression?

Yes, from .NET 6. Same project when targeted for .NET 6 works fine.

Known Workarounds

No response

Configuration

Ubuntu 20.04 with .NET 7 preview 2.

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net untriaged New issue has not been triaged by the area owner labels Mar 17, 2022
@ghost
Copy link

ghost commented Mar 17, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

PowerShell uses the System.Net.NetworkInformation.Ping API for Test-Connection cmdlet for pinging. Since .NET 7 Preview 2 we see that sudo is needed for using the cmdlet. It works fine on macOS.

This is a regression from .NET 6.

Output from test app:

PS /home/aditya/PingTest> /home/aditya/PingTest/bin/Debug/net6.0/linux-x64/publish/PingTest www.google.com
Hello
Address: 142.250.217.100
RoundTrip time: 12

PS /home/aditya/PingTest> /home/aditya/PingTest/bin/Debug/net7.0/linux-x64/publish/PingTest www.google.com
Hello
Unhandled exception. System.PlatformNotSupportedException: Unable to send custom ping payload. Run program under privileged user account or grant cap_net_raw capability using setcap(8).
at System.Net.NetworkInformation.Ping.GetPingProcess(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.SendWithPingUtility(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.SendPingCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at Program.

$(String[] args) in /home/aditya/PingTest/Program.cs:line 20

PS /home/aditya/PingTest> sudo /home/aditya/PingTest/bin/Debug/net7.0/linux-x64/publish/PingTest www.google.com
Hello
Address: 142.250.217.100
RoundTrip time: 13

Reproduction Steps

dotnet new console -b PingTest

Contents for Program.cs

// See https://aka.ms/new-console-template for more information
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

Console.WriteLine("Hello");

Ping pingSender = new Ping();
PingOptions options = new PingOptions();

// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
//options.DontFragment = true;

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(args[0], timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
    Console.WriteLine("Address: {0}", reply.Address.ToString());
    Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
}

dotnet run www.google.com

Expected behavior

Hello
Address: 204.79.197.200
RoundTrip time: 24
Time to live: 128
Don't fragment: False
Buffer size: 32

Actual behavior

Hello
Unhandled exception. System.PlatformNotSupportedException: Unable to send custom ping payload. Run program under privileged user account or grant cap_net_raw capability using setcap(8).
at System.Net.NetworkInformation.Ping.GetPingProcess(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.SendWithPingUtility(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.SendPingCore(IPAddress address, Byte[] buffer, Int32 timeout, PingOptions options)
at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at Program.

$(String[] args) in /home/aditya/PingTest/Program.cs:line 20

Regression?

Yes, from .NET 6. Same project when targeted for .NET 6 works fine.

Known Workarounds

No response

Configuration

Ubuntu 20.04 with .NET 7 preview 2.

Other information

No response

Author: adityapatwardhan
Assignees: -
Labels:

area-System.Net, untriaged

Milestone: -

@filipnavara
Copy link
Member

This is caused by #64625 and it was an intentional change. Sending custom payloads was never supported when running through the ping utility, it just silently ignored the data.

@rzikm
Copy link
Member

rzikm commented Mar 17, 2022

As filipnavara mentioned, this was an intentional change.

If the provided custom payload is not important for your use case, using overload which does not accept it (or using an empty array) should work fine.

I see we did not document this breaking change, in case we want to keep this behavior, we need to document it.

@karelz
Copy link
Member

karelz commented Mar 17, 2022

Triage: We need to document it as breaking change in 7.0.

@karelz karelz added this to the 7.0.0 milestone Mar 17, 2022
@karelz karelz added documentation Documentation bug or enhancement, does not impact product or test code and removed untriaged New issue has not been triaged by the area owner labels Mar 17, 2022
@rzikm
Copy link
Member

rzikm commented Mar 17, 2022

Task for changing docs created: dotnet/docs#28720

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net documentation Documentation bug or enhancement, does not impact product or test code
Projects
None yet
Development

No branches or pull requests

4 participants