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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ scanmeta.json
# Claude Code settings
.claude/

# Test files
test-*.ps1
# Test files (only in tests/ or repo root — not module source)
/test-*.ps1
tests/test-*.ps1

# OS files
.DS_Store
Expand Down
1 change: 1 addition & 0 deletions NetDiagram-PS/NetDiagram-PS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
# Level 2 & 3: SNMP Discovery
'Invoke-SnmpWalk'
'Get-SnmpNeighbors'
'Get-SnmpBridgeNeighbors'
'Get-CommonSNMPStrings'

# Export and Analysis
Expand Down
43 changes: 33 additions & 10 deletions NetDiagram-PS/Private/ConvertFrom-ArpText.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,44 @@ function ConvertFrom-ArpText {
if ($Format -eq 'MacOS') {
if ($line -notmatch '\s+at\s+') { continue }
if ($line -match '\s+at\s+\(incomplete\)') { continue }
if ($line -notmatch '\((\d+\.\d+\.\d+\.\d+)\)\s+at\s+([0-9a-fA-F:]{17})\s+on\s+(\S+)') {
throw "Unable to parse macOS ARP entry: $line"
# Try IPv4 first, then IPv6
if ($line -match '\((\d+\.\d+\.\d+\.\d+)\)\s+at\s+([0-9a-fA-F:]{17})\s+on\s+(\S+)') {
$entry = [pscustomobject]@{
IPAddress = $matches[1]; MACAddress = $matches[2].ToUpperInvariant()
State = 'Reachable'; InterfaceAlias = $matches[3]; InterfaceIndex = $null
AddressFamily = 'IPv4'
}
}
$entry = [pscustomobject]@{
IPAddress = $matches[1]; MACAddress = $matches[2].ToUpperInvariant()
State = 'Reachable'; InterfaceAlias = $matches[3]; InterfaceIndex = $null
elseif ($line -match '\(([0-9a-fA-F:]+)\)\s+at\s+([0-9a-fA-F:]{17})\s+on\s+(\S+)') {
$entry = [pscustomobject]@{
IPAddress = $matches[1]; MACAddress = $matches[2].ToUpperInvariant()
State = 'Reachable'; InterfaceAlias = $matches[3]; InterfaceIndex = $null
AddressFamily = 'IPv6'
}
}
else {
throw "Unable to parse macOS ARP entry: $line"
}
}
elseif ($Format -eq 'LinuxIp') {
if ($line -notmatch '\s+lladdr\s+') { continue }
if ($line -notmatch '^(\d+\.\d+\.\d+\.\d+)\s+dev\s+(\S+)\s+lladdr\s+([0-9a-fA-F:]{17})\s+(\S+)') {
throw "Unable to parse Linux ip-neigh entry: $line"
# Try IPv4 first, then IPv6
if ($line -match '^(\d+\.\d+\.\d+\.\d+)\s+dev\s+(\S+)\s+lladdr\s+([0-9a-fA-F:]{17})\s+(\S+)') {
$entry = [pscustomobject]@{
IPAddress = $matches[1]; MACAddress = $matches[3].ToUpperInvariant()
State = $matches[4]; InterfaceAlias = $matches[2]; InterfaceIndex = $null
AddressFamily = 'IPv4'
}
}
$entry = [pscustomobject]@{
IPAddress = $matches[1]; MACAddress = $matches[3].ToUpperInvariant()
State = $matches[4]; InterfaceAlias = $matches[2]; InterfaceIndex = $null
elseif ($line -match '^([0-9a-fA-F:]+)\s+dev\s+(\S+)\s+lladdr\s+([0-9a-fA-F:]{17})\s+(\S+)') {
$entry = [pscustomobject]@{
IPAddress = $matches[1]; MACAddress = $matches[3].ToUpperInvariant()
State = $matches[4]; InterfaceAlias = $matches[2]; InterfaceIndex = $null
AddressFamily = 'IPv6'
}
}
else {
throw "Unable to parse Linux ip-neigh entry: $line"
}
}
else {
Expand All @@ -36,6 +58,7 @@ function ConvertFrom-ArpText {
$entry = [pscustomobject]@{
IPAddress = $matches[1]; MACAddress = $matches[2].ToUpperInvariant()
State = 'Reachable'; InterfaceAlias = $null; InterfaceIndex = $null
AddressFamily = 'IPv4'
}
}

Expand Down
53 changes: 29 additions & 24 deletions NetDiagram-PS/Private/Test-IPInSubnet.ps1
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
function Test-IPInSubnet {
<#
.SYNOPSIS
Simple CIDR matching helper
CIDR matching helper for both IPv4 and IPv6
.DESCRIPTION
Returns true when Address falls within the network defined by CIDR.
Mixed address families (v4 address against v6 CIDR or vice versa)
return false without throwing.
#>
param([string]$IP, [string]$CIDR)

if ([string]::IsNullOrWhiteSpace($IP) -or [string]::IsNullOrWhiteSpace($CIDR)) {
return $false
}

if ($CIDR -notmatch '^(\d{1,3}(?:\.\d{1,3}){3})\/(\d{1,2})$') {
# Split CIDR into network address and prefix length. Accept both IPv4 and
# IPv6 CIDR notation (e.g. '10.0.0.0/8' or '2001:db8::/32').
$cidrParts = $CIDR -split '/'
if ($cidrParts.Count -ne 2) {
return $false
}

$networkAddressString = $matches[1]
$prefixLength = [int]$matches[2]

if ($prefixLength -lt 0 -or $prefixLength -gt 32) {
$networkAddressString = $cidrParts[0]
$prefixLength = 0
if (-not [int]::TryParse($cidrParts[1], [ref]$prefixLength)) {
return $false
}

Expand All @@ -28,31 +34,30 @@ function Test-IPInSubnet {
return $false
}

if ($ipAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork -or
$networkAddress.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork) {
# Mixed families never match — return false, never throw.
if ($ipAddress.AddressFamily -ne $networkAddress.AddressFamily) {
return $false
}

# Validate prefix length for the address family.
$maxPrefix = if ($ipAddress.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork) { 32 } else { 128 }
if ($prefixLength -lt 0 -or $prefixLength -gt $maxPrefix) {
return $false
}

# Compare bytes up to prefix length.
$ipBytes = $ipAddress.GetAddressBytes()
$networkBytes = $networkAddress.GetAddressBytes()

if ([System.BitConverter]::IsLittleEndian) {
[Array]::Reverse($ipBytes)
[Array]::Reverse($networkBytes)
}

$ipValue = [System.BitConverter]::ToUInt32($ipBytes, 0)
$networkValue = [System.BitConverter]::ToUInt32($networkBytes, 0)
$fullBytes = [math]::Floor($prefixLength / 8)
$remainBits = $prefixLength % 8

# Build the prefix mask. Compute in UInt64 to avoid the int overflow that
# '[uint32]0xFFFFFFFF -shl n' triggers (0xFFFFFFFF parses as int -1), then
# truncate back to 32 bits.
$mask = if ($prefixLength -eq 0) {
[uint32]0
for ($i = 0; $i -lt $fullBytes; $i++) {
if ($ipBytes[$i] -ne $networkBytes[$i]) { return $false }
}
else {
[uint32]((([uint64]4294967295) -shl (32 - $prefixLength)) -band [uint64]4294967295)
if ($remainBits -gt 0) {
$mask = 0xFF -shl (8 - $remainBits)
if (($ipBytes[$fullBytes] -band $mask) -ne ($networkBytes[$fullBytes] -band $mask)) { return $false }
}

return (($ipValue -band $mask) -eq ($networkValue -band $mask))
return $true
}
6 changes: 4 additions & 2 deletions NetDiagram-PS/Public/Get-LocalARPTable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ function Get-LocalARPTable {
$arpEntries = @()

if ($IsWindows) {
# Windows: Use Get-NetNeighbor
$neighbors = Get-NetNeighbor -AddressFamily IPv4 -ErrorAction Stop
# Windows: Use Get-NetNeighbor (both IPv4 and IPv6)
$neighbors = Get-NetNeighbor -ErrorAction Stop

if ($InterfaceAlias) {
$neighbors = $neighbors | Where-Object { $_.InterfaceAlias -eq $InterfaceAlias }
}

foreach ($entry in $neighbors) {
$family = if ($entry.AddressFamily -eq 'IPv4') { 'IPv4' } else { 'IPv6' }
$arpEntries += [pscustomobject]@{
IPAddress = $entry.IPAddress
MACAddress = $entry.LinkLayerAddress
State = $entry.State
InterfaceAlias = $entry.InterfaceAlias
InterfaceIndex = $entry.InterfaceIndex
AddressFamily = $family
}
}
}
Expand Down
Loading
Loading