From c7de3c545140c82558365b81174db1ad22f2b3cb Mon Sep 17 00:00:00 2001 From: jacobyoby Date: Tue, 8 Sep 2026 01:33:32 -0700 Subject: [PATCH] fix(wizard): bound reverse DNS discovery --- NetDiagram-PS/NetDiagram-PS.psm1 | 52 ++++++++++++++++++++------------ README.md | 3 ++ examples/New-NetworkDiagram.ps1 | 19 ++++++++++-- tests/NetDiagram.Tests.ps1 | 32 ++++++++++++++++++++ 4 files changed, 84 insertions(+), 22 deletions(-) diff --git a/NetDiagram-PS/NetDiagram-PS.psm1 b/NetDiagram-PS/NetDiagram-PS.psm1 index e84ea15..f8b8179 100644 --- a/NetDiagram-PS/NetDiagram-PS.psm1 +++ b/NetDiagram-PS/NetDiagram-PS.psm1 @@ -1366,6 +1366,38 @@ function Get-LocalARPTable { } } +function Wait-DnsLookupTask { + param( + [Parameter(Mandatory)][System.Threading.Tasks.Task]$LookupTask, + [Parameter(Mandatory)][string]$IPAddress, + [Parameter(Mandatory)][int]$TimeoutSeconds + ) + + try { + if ($LookupTask.Wait([TimeSpan]::FromSeconds($TimeoutSeconds))) { + $result = $LookupTask.Result + return [pscustomobject]@{ + IPAddress = $IPAddress + Hostname = $result.HostName + Aliases = $result.Aliases + Success = $true + } + } + + Write-Verbose "DNS lookup for $IPAddress timed out after $TimeoutSeconds second(s)" + } + catch { + Write-Verbose "DNS lookup for $IPAddress failed: $($_.Exception.Message)" + } + + return [pscustomobject]@{ + IPAddress = $IPAddress + Hostname = $null + Aliases = @() + Success = $false + } +} + function Resolve-IPHostname { <# .SYNOPSIS @@ -1406,25 +1438,7 @@ function Resolve-IPHostname { # pipeline. On timeout we return a failure object (the background task # is abandoned). $task = [System.Net.Dns]::GetHostEntryAsync($ip) - - if ($task.Wait([TimeSpan]::FromSeconds($TimeoutSeconds))) { - $result = $task.Result - [pscustomobject]@{ - IPAddress = $ip - Hostname = $result.HostName - Aliases = $result.Aliases - Success = $true - } - } - else { - Write-Verbose "DNS lookup for $ip timed out after $TimeoutSeconds second(s)" - [pscustomobject]@{ - IPAddress = $ip - Hostname = $null - Aliases = @() - Success = $false - } - } + Wait-DnsLookupTask -LookupTask $task -IPAddress $ip -TimeoutSeconds $TimeoutSeconds } catch { [pscustomobject]@{ diff --git a/README.md b/README.md index 989979a..c7402e1 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,9 @@ pwsh .\examples\New-NetworkDiagram.ps1 -ScanDepth Full -OutputPath .\office-netw # Choose an interface explicitly (useful with VPNs or multiple adapters) pwsh .\examples\New-NetworkDiagram.ps1 -InterfaceName en0 + +# Limit each reverse DNS lookup to one second +pwsh .\examples\New-NetworkDiagram.ps1 -DnsTimeoutSeconds 1 ``` ### Method 2: Manual Workflow (manual inventory) diff --git a/examples/New-NetworkDiagram.ps1 b/examples/New-NetworkDiagram.ps1 index 266f865..a04e725 100644 --- a/examples/New-NetworkDiagram.ps1 +++ b/examples/New-NetworkDiagram.ps1 @@ -16,6 +16,8 @@ .PARAMETER InterfaceName Interface name or numeric interface index to scan. By default, the interface used by the active IPv4 default route is selected. +.PARAMETER DnsTimeoutSeconds + Maximum time for each reverse DNS lookup during discovery (default: 2 seconds). .EXAMPLE .\New-NetworkDiagram.ps1 @@ -33,7 +35,10 @@ param( [string]$OutputPath = '.\my-network.drawio', - [string]$InterfaceName + [string]$InterfaceName, + + [ValidateRange(1, 30)] + [int]$DnsTimeoutSeconds = 2 ) function Get-WizardInventoryPath { @@ -521,8 +526,16 @@ $scanTargets | ForEach-Object -Parallel { if ($result) { $hostname = try { - $resolved = [System.Net.Dns]::GetHostEntry($testIP) - $resolved.HostName + if (-not (Get-Command Resolve-IPHostname -ErrorAction SilentlyContinue)) { + Import-Module $using:modulePath -Force + } + $resolved = Resolve-IPHostname -IPAddress $testIP -TimeoutSeconds $using:DnsTimeoutSeconds + if ($resolved.Success -and -not [string]::IsNullOrWhiteSpace($resolved.Hostname)) { + $resolved.Hostname + } + else { + "Device-$testIP" + } } catch { "Device-$testIP" } diff --git a/tests/NetDiagram.Tests.ps1 b/tests/NetDiagram.Tests.ps1 index aa790d1..9a07962 100644 --- a/tests/NetDiagram.Tests.ps1 +++ b/tests/NetDiagram.Tests.ps1 @@ -432,6 +432,38 @@ Describe 'Resolve-IPHostname' { $result.IPAddress | Should -Be '192.0.2.1' $result.Success | Should -Be $false } + + It 'Returns within the configured deadline for a stalled resolver task' { + InModuleScope 'NetDiagram-PS' { + $source = [System.Threading.Tasks.TaskCompletionSource[System.Net.IPHostEntry]]::new() + $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() + + $result = Wait-DnsLookupTask -LookupTask $source.Task ` + -IPAddress '192.0.2.10' -TimeoutSeconds 1 + $stopwatch.Stop() + + $result.IPAddress | Should -Be '192.0.2.10' + $result.Hostname | Should -BeNullOrEmpty + $result.Success | Should -Be $false + $stopwatch.Elapsed.TotalSeconds | Should -BeLessThan 2.5 + } + } + + It 'Returns a failure immediately for a faulted resolver task' { + InModuleScope 'NetDiagram-PS' { + $source = [System.Threading.Tasks.TaskCompletionSource[System.Net.IPHostEntry]]::new() + $source.SetException([InvalidOperationException]::new('fixture failure')) + $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() + + $result = Wait-DnsLookupTask -LookupTask $source.Task ` + -IPAddress '192.0.2.11' -TimeoutSeconds 5 + $stopwatch.Stop() + + $result.IPAddress | Should -Be '192.0.2.11' + $result.Success | Should -Be $false + $stopwatch.Elapsed.TotalSeconds | Should -BeLessThan 1 + } + } } Describe 'Invoke-SnmpWalk' {