Skip to content

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

@adityapatwardhan

Description

@adityapatwardhan

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.NetdocumentationDocumentation bug or enhancement, does not impact product or test code

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions