Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

avoid processing incorrect ICMP message on Unix #34084

Merged
merged 2 commits into from
Dec 19, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,27 @@ private SocketConfig GetSocketConfig(IPAddress address, byte[] buffer, int timeo

private Socket GetRawSocket(SocketConfig socketConfig)
{
IPEndPoint ep = socketConfig.EndPoint as IPEndPoint;

// Setting Socket.DontFragment and .Ttl is not supported on Unix, so socketConfig.Options is ignored.
AddressFamily addrFamily = ((IPEndPoint)socketConfig.EndPoint).Address.AddressFamily;
AddressFamily addrFamily = ep.Address.AddressFamily;
Socket socket = new Socket(addrFamily, SocketType.Raw, socketConfig.ProtocolType);
socket.ReceiveTimeout = socketConfig.Timeout;
socket.SendTimeout = socketConfig.Timeout;
if (socketConfig.Options != null && socketConfig.Options.Ttl > 0)
{
socket.Ttl = (short)socketConfig.Options.Ttl;
}

#pragma warning disable 618
// Disable warning about obsolete property. We could use GetAddressBytes but that allocates.
if (!ep.Address.IsIPv6Multicast && !(addrFamily == AddressFamily.InterNetwork && (ep.Address.Address & 0xf0) == 0xe0))
{
// If it is not multicast, use Connect to scope responses only tho target address.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: tho => to the

socket.Connect(socketConfig.EndPoint);
}
#pragma warning restore 618

return socket;
}

Expand Down Expand Up @@ -129,7 +141,7 @@ private bool TryGetPingReply(SocketConfig socketConfig, byte[] receiveBuffer, in
{
return false;
}

sw.Stop();
long roundTripTime = sw.ElapsedMilliseconds;
int dataOffset = ipHeaderLength + IcmpHeaderLengthInBytes;
Expand Down