-
Notifications
You must be signed in to change notification settings - Fork 5.2k
fix ping with TTL on Linux #99875
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
fix ping with TTL on Linux #99875
Changes from all commits
a61163c
9eac8aa
385546e
b720047
f58107b
a4070ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Net.Sockets; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal static partial class Sys | ||
| { | ||
| [LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_ReceiveSocketError")] | ||
| internal static unsafe partial SocketError ReceiveSocketError(SafeHandle socket, MessageHeader* messageHeader); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,6 +102,12 @@ private static Socket GetRawSocket(SocketConfig socketConfig) | |
| { | ||
| // If it is not multicast, use Connect to scope responses only to the target address. | ||
| socket.Connect(socketConfig.EndPoint); | ||
| unsafe | ||
| { | ||
| int opt = 1; | ||
| // setsockopt(fd, IPPROTO_IP, IP_RECVERR, &value, sizeof(int)) | ||
| socket.SetRawSocketOption(0, 11, new ReadOnlySpan<byte>(&opt, sizeof(int))); | ||
| } | ||
| } | ||
| #pragma warning restore 618 | ||
|
|
||
|
|
@@ -232,11 +238,12 @@ private static bool TryGetPingReply( | |
| return true; | ||
| } | ||
|
|
||
| private static PingReply SendIcmpEchoRequestOverRawSocket(IPAddress address, byte[] buffer, int timeout, PingOptions? options) | ||
| private static unsafe PingReply SendIcmpEchoRequestOverRawSocket(IPAddress address, byte[] buffer, int timeout, PingOptions? options) | ||
| { | ||
| SocketConfig socketConfig = GetSocketConfig(address, buffer, timeout, options); | ||
| using (Socket socket = GetRawSocket(socketConfig)) | ||
| { | ||
| Span<byte> socketAddress = stackalloc byte[SocketAddress.GetMaximumAddressSize(address.AddressFamily)]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: move this just before its first usage, at line 288.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ping.RawSocket.cs(287,48): error CS0255: stackalloc may not be used in a catch or finally block |
||
| int ipHeaderLength = socketConfig.IsIpv4 ? MinIpHeaderLengthInBytes : 0; | ||
| try | ||
| { | ||
|
|
@@ -270,6 +277,29 @@ private static PingReply SendIcmpEchoRequestOverRawSocket(IPAddress address, byt | |
| { | ||
| return CreatePingReply(IPStatus.PacketTooBig); | ||
| } | ||
| catch (SocketException ex) when (ex.SocketErrorCode == SocketError.HostUnreachable) | ||
| { | ||
| // This happens on Linux where we explicitly subscribed to error messages | ||
| // We should be able to get more info by getting extended socket error from error queue. | ||
|
|
||
| Interop.Sys.MessageHeader header = default; | ||
|
|
||
| SocketError result; | ||
| fixed (byte* sockAddr = &MemoryMarshal.GetReference(socketAddress)) | ||
| { | ||
| header.SocketAddress = sockAddr; | ||
| header.SocketAddressLen = socketAddress.Length; | ||
| header.IOVectors = null; | ||
| header.IOVectorCount = 0; | ||
|
|
||
| result = Interop.Sys.ReceiveSocketError(socket.SafeHandle, &header); | ||
| } | ||
|
|
||
| if (result == SocketError.Success && header.SocketAddressLen > 0) | ||
| { | ||
| return CreatePingReply(IPStatus.TtlExpired, IPEndPointExtensions.GetIPAddress(socketAddress.Slice(0, header.SocketAddressLen))); | ||
| } | ||
| } | ||
|
|
||
| // We have exceeded our timeout duration, and no reply has been received. | ||
| return CreatePingReply(IPStatus.TimedOut); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.