@@ -285,7 +285,7 @@ protected override void StopProcessing()
285
285
286
286
private void ProcessConnectionByTCPPort ( string targetNameOrAddress )
287
287
{
288
- if ( ! InitProcessPing ( targetNameOrAddress , out _ , out IPAddress ? targetAddress ) )
288
+ if ( ! TryResolveNameOrAddress ( targetNameOrAddress , out _ , out IPAddress ? targetAddress ) )
289
289
{
290
290
return ;
291
291
}
@@ -336,7 +336,7 @@ private void ProcessTraceroute(string targetNameOrAddress)
336
336
{
337
337
byte [ ] buffer = GetSendBuffer ( BufferSize ) ;
338
338
339
- if ( ! InitProcessPing ( targetNameOrAddress , out string resolvedTargetName , out IPAddress ? targetAddress ) )
339
+ if ( ! TryResolveNameOrAddress ( targetNameOrAddress , out string resolvedTargetName , out IPAddress ? targetAddress ) )
340
340
{
341
341
return ;
342
342
}
@@ -351,8 +351,6 @@ private void ProcessTraceroute(string targetNameOrAddress)
351
351
IPAddress hopAddress ;
352
352
do
353
353
{
354
- // Clear the stored router name for every hop
355
- string routerName = string . Empty ;
356
354
pingOptions . Ttl = currentHop ;
357
355
358
356
#if ! UNIX
@@ -383,25 +381,25 @@ private void ProcessTraceroute(string targetNameOrAddress)
383
381
#endif
384
382
var hopAddressString = discoveryReply . Address . ToString ( ) ;
385
383
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
+
386
397
// In traceroutes we don't use 'Count' parameter.
387
398
// If we change 'DefaultTraceRoutePingCount' we should change 'ConsoleTraceRouteReply' resource string.
388
399
for ( uint i = 1 ; i <= DefaultTraceRoutePingCount ; i ++ )
389
400
{
390
401
try
391
402
{
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
405
403
reply = SendCancellablePing ( hopAddress , timeout , buffer , pingOptions , timer ) ;
406
404
407
405
if ( ! Quiet . IsPresent )
@@ -475,7 +473,7 @@ private void ProcessTraceroute(string targetNameOrAddress)
475
473
private void ProcessMTUSize ( string targetNameOrAddress )
476
474
{
477
475
PingReply ? reply , replyResult = null ;
478
- if ( ! InitProcessPing ( targetNameOrAddress , out string resolvedTargetName , out IPAddress ? targetAddress ) )
476
+ if ( ! TryResolveNameOrAddress ( targetNameOrAddress , out string resolvedTargetName , out IPAddress ? targetAddress ) )
479
477
{
480
478
return ;
481
479
}
@@ -578,7 +576,7 @@ private void ProcessMTUSize(string targetNameOrAddress)
578
576
579
577
private void ProcessPing ( string targetNameOrAddress )
580
578
{
581
- if ( ! InitProcessPing ( targetNameOrAddress , out string resolvedTargetName , out IPAddress ? targetAddress ) )
579
+ if ( ! TryResolveNameOrAddress ( targetNameOrAddress , out string resolvedTargetName , out IPAddress ? targetAddress ) )
582
580
{
583
581
return ;
584
582
}
@@ -643,7 +641,7 @@ private void ProcessPing(string targetNameOrAddress)
643
641
644
642
#endregion PingTest
645
643
646
- private bool InitProcessPing (
644
+ private bool TryResolveNameOrAddress (
647
645
string targetNameOrAddress ,
648
646
out string resolvedTargetName ,
649
647
[ NotNullWhen ( true ) ]
@@ -1007,6 +1005,16 @@ internal TraceStatus(
1007
1005
Source = source ;
1008
1006
Target = destination ;
1009
1007
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
+ }
1010
1018
}
1011
1019
1012
1020
private readonly PingStatus _status ;
@@ -1020,13 +1028,7 @@ internal TraceStatus(
1020
1028
/// Gets the hostname of the current hop point.
1021
1029
/// </summary>
1022
1030
/// <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 ; }
1030
1032
1031
1033
/// <summary>
1032
1034
/// Gets the sequence number of the ping in the sequence of pings to the hop point.
0 commit comments