Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ protected override void StopProcessing()

private void ProcessConnectionByTCPPort(string targetNameOrAddress)
{
if (!InitProcessPing(targetNameOrAddress, out _, out IPAddress? targetAddress))
if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress))
{
return;
}
Expand Down Expand Up @@ -336,7 +336,7 @@ private void ProcessTraceroute(string targetNameOrAddress)
{
byte[] buffer = GetSendBuffer(BufferSize);

if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
{
return;
}
Expand All @@ -351,8 +351,6 @@ private void ProcessTraceroute(string targetNameOrAddress)
IPAddress hopAddress;
do
{
// Clear the stored router name for every hop
string routerName = string.Empty;
pingOptions.Ttl = currentHop;

#if !UNIX
Expand Down Expand Up @@ -383,25 +381,25 @@ private void ProcessTraceroute(string targetNameOrAddress)
#endif
var hopAddressString = discoveryReply.Address.ToString();

string routerName = hopAddressString;
try
{
if (!TryResolveNameOrAddress(hopAddressString, out routerName, out _))
{
routerName = hopAddressString;
}
}
catch
{
// Swallow hostname resolve exceptions and continue with traceroute
}

// In traceroutes we don't use 'Count' parameter.
// If we change 'DefaultTraceRoutePingCount' we should change 'ConsoleTraceRouteReply' resource string.
for (uint i = 1; i <= DefaultTraceRoutePingCount; i++)
{
try
{
#if !UNIX
if (ResolveDestination.IsPresent && routerName == string.Empty)
{
try
{
InitProcessPing(hopAddressString, out routerName, out _);
}
catch
{
// Swallow host resolve exceptions and just use the IP address.
}
}
#endif
reply = SendCancellablePing(hopAddress, timeout, buffer, pingOptions, timer);

if (!Quiet.IsPresent)
Expand Down Expand Up @@ -475,7 +473,7 @@ private void ProcessTraceroute(string targetNameOrAddress)
private void ProcessMTUSize(string targetNameOrAddress)
{
PingReply? reply, replyResult = null;
if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
{
return;
}
Expand Down Expand Up @@ -578,7 +576,7 @@ private void ProcessMTUSize(string targetNameOrAddress)

private void ProcessPing(string targetNameOrAddress)
{
if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
{
return;
}
Expand Down Expand Up @@ -643,7 +641,7 @@ private void ProcessPing(string targetNameOrAddress)

#endregion PingTest

private bool InitProcessPing(
private bool TryResolveNameOrAddress(
string targetNameOrAddress,
out string resolvedTargetName,
[NotNullWhen(true)]
Expand Down Expand Up @@ -1007,6 +1005,16 @@ internal TraceStatus(
Source = source;
Target = destination;
TargetAddress = destinationAddress;

if (_status.Address == IPAddress.Any
|| _status.Address == IPAddress.IPv6Any)
{
Hostname = null;
}
else
{
Hostname = _status.Destination;
}
}

private readonly PingStatus _status;
Expand All @@ -1020,13 +1028,7 @@ internal TraceStatus(
/// Gets the hostname of the current hop point.
/// </summary>
/// <value></value>
public string? Hostname
{
get => _status.Destination != IPAddress.Any.ToString()
&& _status.Destination != IPAddress.IPv6Any.ToString()
? _status.Destination
: null;
}
public string? Hostname { get; }

/// <summary>
/// Gets the sequence number of the ping in the sequence of pings to the hop point.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ Describe "Test-Connection" -tags "CI" {
It 'returns false without error if MaxHops is exceeded during -Traceroute -Quiet' {
Test-Connection 8.8.8.8 -Traceroute -MaxHops 2 -Quiet | Should -BeFalse
}

It 'has a non-null value for Destination for reachable hosts' {
$results = Test-Connection 127.0.0.1 -Traceroute

$results.Hostname | Should -Not -BeNullOrEmpty
}
}
}

Expand Down