Skip to content

Commit

Permalink
Fix: NullException in IPScanner Ping & NETBIOS for some addresses / c…
Browse files Browse the repository at this point in the history
…ases (#2964)

* Fix: NullException for IPScanner & NETBIOS if unkown error occours

* Chore: Improve ip scanner view

* Docs: #2964

* Docs: #2964
  • Loading branch information
BornToBeRoot authored Dec 22, 2024
1 parent 32f1f59 commit 7a86fc4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
41 changes: 26 additions & 15 deletions Source/NETworkManager.Models/Network/IPScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void ScanAsync(IEnumerable<(IPAddress ipAddress, string hostname)> hosts,
// Start netbios lookup async (if enabled)
var netbiosTask = options.NetBIOSEnabled
? NetBIOSResolver.ResolveAsync(host.ipAddress, options.NetBIOSTimeout, cancellationToken)
: Task.FromResult(new NetBIOSInfo());
: Task.FromResult(new NetBIOSInfo(host.ipAddress));

// Get ping result
pingTask.Wait(cancellationToken);
Expand Down Expand Up @@ -214,26 +214,34 @@ private Task<PingInfo> PingAsync(IPAddress ipAddress, CancellationToken cancella

for (var i = 0; i < options.ICMPAttempts; i++)
{
// Get timestamp
var timestamp = DateTime.Now;

try
{
// Get timestamp
var timestamp = DateTime.Now;

var pingReply = ping.Send(ipAddress, options.ICMPTimeout, options.ICMPBuffer);

// Success
if (pingReply is { Status: IPStatus.Success })
{
// IPv4
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
return new PingInfo(timestamp, pingReply.Address, pingReply.Buffer.Length,
pingReply.RoundtripTime,
pingReply.Options!.Ttl, pingReply.Status);

// IPv6
return new PingInfo(timestamp, pingReply.Address, pingReply.Buffer.Length,
pingReply.RoundtripTime,
pingReply.Status);
switch (ipAddress.AddressFamily)
{
case AddressFamily.InterNetwork:
return new PingInfo(
timestamp,
pingReply.Address,
pingReply.Buffer.Length,
pingReply.RoundtripTime,
pingReply.Options!.Ttl,
pingReply.Status);
case AddressFamily.InterNetworkV6:
return new PingInfo(
timestamp,
pingReply.Address,
pingReply.Buffer.Length,
pingReply.RoundtripTime,
pingReply.Status);
}
}

// Failed
Expand All @@ -242,14 +250,17 @@ private Task<PingInfo> PingAsync(IPAddress ipAddress, CancellationToken cancella
}
catch (PingException)
{
// Ping failed with unknown status
return new PingInfo(timestamp, ipAddress, IPStatus.Unknown);
}

// Don't scan again, if the user has canceled (when more than 1 attempt)
if (cancellationToken.IsCancellationRequested)
break;
}

return new PingInfo();
// Fall back to unknown status
return new PingInfo(DateTime.Now, ipAddress, IPStatus.Unknown);
}, cancellationToken);
}

Expand Down
7 changes: 4 additions & 3 deletions Source/NETworkManager.Models/Network/NetBIOSInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ public class NetBIOSInfo
/// <summary>
/// Constructor for an unreachable host.
/// </summary>
public NetBIOSInfo()
public NetBIOSInfo(IPAddress ipAddress)
{
IsReachable = false;
IPAddress = ipAddress;
}

/// <summary>
/// Constructor for a reachable host.
/// </summary>
/// <param name="ipAddress">IP address of the host.</param>
/// <param name="computerName">Computer name of the host.</param>
/// <param name="userName">User name of the host.</param>
/// <param name="userName">Username of the host.</param>
/// <param name="groupName">Group name or domain of the host.</param>
/// <param name="macAddress">MAC address of the host.</param>
/// <param name="vendor">Vendor of the host based on the MAC address.</param>
Expand Down Expand Up @@ -52,7 +53,7 @@ public NetBIOSInfo(IPAddress ipAddress, string computerName, string userName, st
public string ComputerName { get; set; }

/// <summary>
/// User name of the host.
/// Username of the host.
/// </summary>
public string UserName { get; set; }

Expand Down
8 changes: 4 additions & 4 deletions Source/NETworkManager.Models/Network/NetBIOSResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ public static async Task<NetBIOSInfo> ResolveAsync(IPAddress ipAddress, int time
var receiveTask = udpClient.ReceiveAsync();

if (!receiveTask.Wait(timeout, cancellationToken))
return new NetBIOSInfo();
return new NetBIOSInfo(ipAddress);

var response = receiveTask.Result;

if (response.Buffer.Length < ResponseBaseLen || response.Buffer[ResponseTypePos] != ResponseTypeNbstat)
return new NetBIOSInfo(); // response was too short
return new NetBIOSInfo(ipAddress); // response was too short

var count = response.Buffer[ResponseBaseLen - 1] & 0xFF;

if (response.Buffer.Length < ResponseBaseLen + ResponseBlockLen * count)
return new NetBIOSInfo(); // data was truncated or something is wrong
return new NetBIOSInfo(ipAddress); // data was truncated or something is wrong

var result = ExtractNames(response.Buffer, count);

Expand All @@ -96,7 +96,7 @@ public static async Task<NetBIOSInfo> ResolveAsync(IPAddress ipAddress, int time
}
catch (Exception)
{
return null;
return new NetBIOSInfo(ipAddress);
}
finally
{
Expand Down
24 changes: 12 additions & 12 deletions Source/NETworkManager/Views/IPScannerView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="100" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand All @@ -645,7 +645,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="100" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand All @@ -664,7 +664,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="100" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand All @@ -677,7 +677,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="100" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand Down Expand Up @@ -817,7 +817,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="150" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand All @@ -830,7 +830,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="150" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand All @@ -843,7 +843,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="150" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand All @@ -856,7 +856,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="150" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand All @@ -869,7 +869,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="150" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand Down Expand Up @@ -929,7 +929,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="150" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand Down Expand Up @@ -977,7 +977,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="150" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand All @@ -990,7 +990,7 @@
<ListViewItem>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition MinWidth="150" Width="Auto" />
<ColumnDefinition Width="10" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
Expand Down
8 changes: 7 additions & 1 deletion Website/docs/changelog/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Release date: **xx.xx.2024**
## What's new?

- **WiFi**

- 6 GHz networks are not supported. [#2912](https://github.com/BornToBeRoot/NETworkManager/pull/2912) [#2928](https://github.com/BornToBeRoot/NETworkManager/pull/2928)
- `WPA3 Personal (SAE)`, `WPA3 Enterprise` and `WPA3 Enterprise (192-bit)` are now supported. [#2912](https://github.com/BornToBeRoot/NETworkManager/pull/2912)
- `802.11be` (`EHT`) is now supported. [#2912](https://github.com/BornToBeRoot/NETworkManager/pull/2912)
Expand All @@ -39,11 +40,16 @@ Release date: **xx.xx.2024**
- Changed the Welcome dialog from `MahApps.Metro.Controls.Dialogs` to `MahApps.Metro.SimpleChildWindow`, so the main window can be dragged and resized on the first start. [#2914](https://github.com/BornToBeRoot/NETworkManager/pull/2914)

- **WiFi**

- Fixed a bug that caused the scan process to crash when a 6 GHz network was found. [#2912](https://github.com/BornToBeRoot/NETworkManager/pull/2912)

- **IP Scanner**

- Fixed two `NullReferenceException` in ICMP & NETBIOS for some IP addresses. [#2964](https://github.com/BornToBeRoot/NETworkManager/pull/2964)

## Dependencies, Refactoring & Documentation

Migrated code for some loading indicators from the library [LoadingIndicators.WPF] (https://github.com/zeluisping/LoadingIndicators.WPF) to the NETworkManager repo, as the original repo looks unmaintained and has problems with MahApps.Metro version 2 and later. [#2963](https://github.com/BornToBeRoot/NETworkManager/pull/2963)
- Migrated code for some loading indicators from the library [LoadingIndicators.WPF] (https://github.com/zeluisping/LoadingIndicators.WPF) to the NETworkManager repo, as the original repo looks unmaintained and has problems with MahApps.Metro version 2 and later. [#2963](https://github.com/BornToBeRoot/NETworkManager/pull/2963)
- Code cleanup & refactoring [#2940](https://github.com/BornToBeRoot/NETworkManager/pull/2940)
- Language files updated via [#transifex](https://github.com/BornToBeRoot/NETworkManager/pulls?q=author%3Aapp%2Ftransifex-integration)
- Dependencies updated via [#dependabot](https://github.com/BornToBeRoot/NETworkManager/pulls?q=author%3Aapp%2Fdependabot)

0 comments on commit 7a86fc4

Please sign in to comment.