Skip to content

Commit 7b33cfe

Browse files
vexx32daxian-dbw
authored andcommitted
Test-Connection: Fallback to hop IP Address on -Traceroute without -ResolveDestination (#11335)
1 parent 54e6199 commit 7b33cfe

File tree

2 files changed

+35
-27
lines changed

2 files changed

+35
-27
lines changed

src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ protected override void StopProcessing()
285285

286286
private void ProcessConnectionByTCPPort(string targetNameOrAddress)
287287
{
288-
if (!InitProcessPing(targetNameOrAddress, out _, out IPAddress? targetAddress))
288+
if (!TryResolveNameOrAddress(targetNameOrAddress, out _, out IPAddress? targetAddress))
289289
{
290290
return;
291291
}
@@ -336,7 +336,7 @@ private void ProcessTraceroute(string targetNameOrAddress)
336336
{
337337
byte[] buffer = GetSendBuffer(BufferSize);
338338

339-
if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
339+
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
340340
{
341341
return;
342342
}
@@ -351,8 +351,6 @@ private void ProcessTraceroute(string targetNameOrAddress)
351351
IPAddress hopAddress;
352352
do
353353
{
354-
// Clear the stored router name for every hop
355-
string routerName = string.Empty;
356354
pingOptions.Ttl = currentHop;
357355

358356
#if !UNIX
@@ -383,25 +381,25 @@ private void ProcessTraceroute(string targetNameOrAddress)
383381
#endif
384382
var hopAddressString = discoveryReply.Address.ToString();
385383

384+
string routerName = hopAddressString;
385+
try
386+
{
387+
if (!TryResolveNameOrAddress(hopAddressString, out routerName, out _))
388+
{
389+
routerName = hopAddressString;
390+
}
391+
}
392+
catch
393+
{
394+
// Swallow hostname resolve exceptions and continue with traceroute
395+
}
396+
386397
// In traceroutes we don't use 'Count' parameter.
387398
// If we change 'DefaultTraceRoutePingCount' we should change 'ConsoleTraceRouteReply' resource string.
388399
for (uint i = 1; i <= DefaultTraceRoutePingCount; i++)
389400
{
390401
try
391402
{
392-
#if !UNIX
393-
if (ResolveDestination.IsPresent && routerName == string.Empty)
394-
{
395-
try
396-
{
397-
InitProcessPing(hopAddressString, out routerName, out _);
398-
}
399-
catch
400-
{
401-
// Swallow host resolve exceptions and just use the IP address.
402-
}
403-
}
404-
#endif
405403
reply = SendCancellablePing(hopAddress, timeout, buffer, pingOptions, timer);
406404

407405
if (!Quiet.IsPresent)
@@ -475,7 +473,7 @@ private void ProcessTraceroute(string targetNameOrAddress)
475473
private void ProcessMTUSize(string targetNameOrAddress)
476474
{
477475
PingReply? reply, replyResult = null;
478-
if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
476+
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
479477
{
480478
return;
481479
}
@@ -578,7 +576,7 @@ private void ProcessMTUSize(string targetNameOrAddress)
578576

579577
private void ProcessPing(string targetNameOrAddress)
580578
{
581-
if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
579+
if (!TryResolveNameOrAddress(targetNameOrAddress, out string resolvedTargetName, out IPAddress? targetAddress))
582580
{
583581
return;
584582
}
@@ -643,7 +641,7 @@ private void ProcessPing(string targetNameOrAddress)
643641

644642
#endregion PingTest
645643

646-
private bool InitProcessPing(
644+
private bool TryResolveNameOrAddress(
647645
string targetNameOrAddress,
648646
out string resolvedTargetName,
649647
[NotNullWhen(true)]
@@ -1007,6 +1005,16 @@ internal TraceStatus(
10071005
Source = source;
10081006
Target = destination;
10091007
TargetAddress = destinationAddress;
1008+
1009+
if (_status.Address == IPAddress.Any
1010+
|| _status.Address == IPAddress.IPv6Any)
1011+
{
1012+
Hostname = null;
1013+
}
1014+
else
1015+
{
1016+
Hostname = _status.Destination;
1017+
}
10101018
}
10111019

10121020
private readonly PingStatus _status;
@@ -1020,13 +1028,7 @@ internal TraceStatus(
10201028
/// Gets the hostname of the current hop point.
10211029
/// </summary>
10221030
/// <value></value>
1023-
public string? Hostname
1024-
{
1025-
get => _status.Destination != IPAddress.Any.ToString()
1026-
&& _status.Destination != IPAddress.IPv6Any.ToString()
1027-
? _status.Destination
1028-
: null;
1029-
}
1031+
public string? Hostname { get; }
10301032

10311033
/// <summary>
10321034
/// Gets the sequence number of the ping in the sequence of pings to the hop point.

test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ Describe "Test-Connection" -tags "CI" {
275275
It 'returns false without error if MaxHops is exceeded during -Traceroute -Quiet' {
276276
Test-Connection 8.8.8.8 -Traceroute -MaxHops 2 -Quiet | Should -BeFalse
277277
}
278+
279+
It 'has a non-null value for Destination for reachable hosts' {
280+
$results = Test-Connection 127.0.0.1 -Traceroute
281+
282+
$results.Hostname | Should -Not -BeNullOrEmpty
283+
}
278284
}
279285
}
280286

0 commit comments

Comments
 (0)