Skip to content

Commit 7a86fc4

Browse files
authored
Fix: NullException in IPScanner Ping & NETBIOS for some addresses / cases (#2964)
* Fix: NullException for IPScanner & NETBIOS if unkown error occours * Chore: Improve ip scanner view * Docs: #2964 * Docs: #2964
1 parent 32f1f59 commit 7a86fc4

File tree

5 files changed

+53
-35
lines changed

5 files changed

+53
-35
lines changed

Source/NETworkManager.Models/Network/IPScanner.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void ScanAsync(IEnumerable<(IPAddress ipAddress, string hostname)> hosts,
9393
// Start netbios lookup async (if enabled)
9494
var netbiosTask = options.NetBIOSEnabled
9595
? NetBIOSResolver.ResolveAsync(host.ipAddress, options.NetBIOSTimeout, cancellationToken)
96-
: Task.FromResult(new NetBIOSInfo());
96+
: Task.FromResult(new NetBIOSInfo(host.ipAddress));
9797

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

215215
for (var i = 0; i < options.ICMPAttempts; i++)
216216
{
217+
// Get timestamp
218+
var timestamp = DateTime.Now;
219+
217220
try
218221
{
219-
// Get timestamp
220-
var timestamp = DateTime.Now;
221-
222222
var pingReply = ping.Send(ipAddress, options.ICMPTimeout, options.ICMPBuffer);
223223

224224
// Success
225225
if (pingReply is { Status: IPStatus.Success })
226226
{
227-
// IPv4
228-
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
229-
return new PingInfo(timestamp, pingReply.Address, pingReply.Buffer.Length,
230-
pingReply.RoundtripTime,
231-
pingReply.Options!.Ttl, pingReply.Status);
232-
233-
// IPv6
234-
return new PingInfo(timestamp, pingReply.Address, pingReply.Buffer.Length,
235-
pingReply.RoundtripTime,
236-
pingReply.Status);
227+
switch (ipAddress.AddressFamily)
228+
{
229+
case AddressFamily.InterNetwork:
230+
return new PingInfo(
231+
timestamp,
232+
pingReply.Address,
233+
pingReply.Buffer.Length,
234+
pingReply.RoundtripTime,
235+
pingReply.Options!.Ttl,
236+
pingReply.Status);
237+
case AddressFamily.InterNetworkV6:
238+
return new PingInfo(
239+
timestamp,
240+
pingReply.Address,
241+
pingReply.Buffer.Length,
242+
pingReply.RoundtripTime,
243+
pingReply.Status);
244+
}
237245
}
238246

239247
// Failed
@@ -242,14 +250,17 @@ private Task<PingInfo> PingAsync(IPAddress ipAddress, CancellationToken cancella
242250
}
243251
catch (PingException)
244252
{
253+
// Ping failed with unknown status
254+
return new PingInfo(timestamp, ipAddress, IPStatus.Unknown);
245255
}
246256

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

252-
return new PingInfo();
262+
// Fall back to unknown status
263+
return new PingInfo(DateTime.Now, ipAddress, IPStatus.Unknown);
253264
}, cancellationToken);
254265
}
255266

Source/NETworkManager.Models/Network/NetBIOSInfo.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@ public class NetBIOSInfo
1010
/// <summary>
1111
/// Constructor for an unreachable host.
1212
/// </summary>
13-
public NetBIOSInfo()
13+
public NetBIOSInfo(IPAddress ipAddress)
1414
{
1515
IsReachable = false;
16+
IPAddress = ipAddress;
1617
}
1718

1819
/// <summary>
1920
/// Constructor for a reachable host.
2021
/// </summary>
2122
/// <param name="ipAddress">IP address of the host.</param>
2223
/// <param name="computerName">Computer name of the host.</param>
23-
/// <param name="userName">User name of the host.</param>
24+
/// <param name="userName">Username of the host.</param>
2425
/// <param name="groupName">Group name or domain of the host.</param>
2526
/// <param name="macAddress">MAC address of the host.</param>
2627
/// <param name="vendor">Vendor of the host based on the MAC address.</param>
@@ -52,7 +53,7 @@ public NetBIOSInfo(IPAddress ipAddress, string computerName, string userName, st
5253
public string ComputerName { get; set; }
5354

5455
/// <summary>
55-
/// User name of the host.
56+
/// Username of the host.
5657
/// </summary>
5758
public string UserName { get; set; }
5859

Source/NETworkManager.Models/Network/NetBIOSResolver.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ public static async Task<NetBIOSInfo> ResolveAsync(IPAddress ipAddress, int time
5959
var receiveTask = udpClient.ReceiveAsync();
6060

6161
if (!receiveTask.Wait(timeout, cancellationToken))
62-
return new NetBIOSInfo();
62+
return new NetBIOSInfo(ipAddress);
6363

6464
var response = receiveTask.Result;
6565

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

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

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

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

@@ -96,7 +96,7 @@ public static async Task<NetBIOSInfo> ResolveAsync(IPAddress ipAddress, int time
9696
}
9797
catch (Exception)
9898
{
99-
return null;
99+
return new NetBIOSInfo(ipAddress);
100100
}
101101
finally
102102
{

Source/NETworkManager/Views/IPScannerView.xaml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@
632632
<ListViewItem>
633633
<Grid>
634634
<Grid.ColumnDefinitions>
635-
<ColumnDefinition Width="100" />
635+
<ColumnDefinition MinWidth="100" Width="Auto" />
636636
<ColumnDefinition Width="10" />
637637
<ColumnDefinition Width="*" />
638638
</Grid.ColumnDefinitions>
@@ -645,7 +645,7 @@
645645
<ListViewItem>
646646
<Grid>
647647
<Grid.ColumnDefinitions>
648-
<ColumnDefinition Width="100" />
648+
<ColumnDefinition MinWidth="100" Width="Auto" />
649649
<ColumnDefinition Width="10" />
650650
<ColumnDefinition Width="*" />
651651
</Grid.ColumnDefinitions>
@@ -664,7 +664,7 @@
664664
<ListViewItem>
665665
<Grid>
666666
<Grid.ColumnDefinitions>
667-
<ColumnDefinition Width="100" />
667+
<ColumnDefinition MinWidth="100" Width="Auto" />
668668
<ColumnDefinition Width="10" />
669669
<ColumnDefinition Width="*" />
670670
</Grid.ColumnDefinitions>
@@ -677,7 +677,7 @@
677677
<ListViewItem>
678678
<Grid>
679679
<Grid.ColumnDefinitions>
680-
<ColumnDefinition Width="100" />
680+
<ColumnDefinition MinWidth="100" Width="Auto" />
681681
<ColumnDefinition Width="10" />
682682
<ColumnDefinition Width="*" />
683683
</Grid.ColumnDefinitions>
@@ -817,7 +817,7 @@
817817
<ListViewItem>
818818
<Grid>
819819
<Grid.ColumnDefinitions>
820-
<ColumnDefinition Width="100" />
820+
<ColumnDefinition MinWidth="150" Width="Auto" />
821821
<ColumnDefinition Width="10" />
822822
<ColumnDefinition Width="*" />
823823
</Grid.ColumnDefinitions>
@@ -830,7 +830,7 @@
830830
<ListViewItem>
831831
<Grid>
832832
<Grid.ColumnDefinitions>
833-
<ColumnDefinition Width="100" />
833+
<ColumnDefinition MinWidth="150" Width="Auto" />
834834
<ColumnDefinition Width="10" />
835835
<ColumnDefinition Width="*" />
836836
</Grid.ColumnDefinitions>
@@ -843,7 +843,7 @@
843843
<ListViewItem>
844844
<Grid>
845845
<Grid.ColumnDefinitions>
846-
<ColumnDefinition Width="100" />
846+
<ColumnDefinition MinWidth="150" Width="Auto" />
847847
<ColumnDefinition Width="10" />
848848
<ColumnDefinition Width="*" />
849849
</Grid.ColumnDefinitions>
@@ -856,7 +856,7 @@
856856
<ListViewItem>
857857
<Grid>
858858
<Grid.ColumnDefinitions>
859-
<ColumnDefinition Width="100" />
859+
<ColumnDefinition MinWidth="150" Width="Auto" />
860860
<ColumnDefinition Width="10" />
861861
<ColumnDefinition Width="*" />
862862
</Grid.ColumnDefinitions>
@@ -869,7 +869,7 @@
869869
<ListViewItem>
870870
<Grid>
871871
<Grid.ColumnDefinitions>
872-
<ColumnDefinition Width="100" />
872+
<ColumnDefinition MinWidth="150" Width="Auto" />
873873
<ColumnDefinition Width="10" />
874874
<ColumnDefinition Width="*" />
875875
</Grid.ColumnDefinitions>
@@ -929,7 +929,7 @@
929929
<ListViewItem>
930930
<Grid>
931931
<Grid.ColumnDefinitions>
932-
<ColumnDefinition Width="100" />
932+
<ColumnDefinition MinWidth="150" Width="Auto" />
933933
<ColumnDefinition Width="10" />
934934
<ColumnDefinition Width="*" />
935935
</Grid.ColumnDefinitions>
@@ -977,7 +977,7 @@
977977
<ListViewItem>
978978
<Grid>
979979
<Grid.ColumnDefinitions>
980-
<ColumnDefinition Width="100" />
980+
<ColumnDefinition MinWidth="150" Width="Auto" />
981981
<ColumnDefinition Width="10" />
982982
<ColumnDefinition Width="*" />
983983
</Grid.ColumnDefinitions>
@@ -990,7 +990,7 @@
990990
<ListViewItem>
991991
<Grid>
992992
<Grid.ColumnDefinitions>
993-
<ColumnDefinition Width="100" />
993+
<ColumnDefinition MinWidth="150" Width="Auto" />
994994
<ColumnDefinition Width="10" />
995995
<ColumnDefinition Width="*" />
996996
</Grid.ColumnDefinitions>

Website/docs/changelog/next-release.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Release date: **xx.xx.2024**
2222
## What's new?
2323

2424
- **WiFi**
25+
2526
- 6 GHz networks are not supported. [#2912](https://github.com/BornToBeRoot/NETworkManager/pull/2912) [#2928](https://github.com/BornToBeRoot/NETworkManager/pull/2928)
2627
- `WPA3 Personal (SAE)`, `WPA3 Enterprise` and `WPA3 Enterprise (192-bit)` are now supported. [#2912](https://github.com/BornToBeRoot/NETworkManager/pull/2912)
2728
- `802.11be` (`EHT`) is now supported. [#2912](https://github.com/BornToBeRoot/NETworkManager/pull/2912)
@@ -39,11 +40,16 @@ Release date: **xx.xx.2024**
3940
- 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)
4041

4142
- **WiFi**
43+
4244
- 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)
4345

46+
- **IP Scanner**
47+
48+
- Fixed two `NullReferenceException` in ICMP & NETBIOS for some IP addresses. [#2964](https://github.com/BornToBeRoot/NETworkManager/pull/2964)
49+
4450
## Dependencies, Refactoring & Documentation
4551

46-
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)
52+
- 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)
4753
- Code cleanup & refactoring [#2940](https://github.com/BornToBeRoot/NETworkManager/pull/2940)
4854
- Language files updated via [#transifex](https://github.com/BornToBeRoot/NETworkManager/pulls?q=author%3Aapp%2Ftransifex-integration)
4955
- Dependencies updated via [#dependabot](https://github.com/BornToBeRoot/NETworkManager/pulls?q=author%3Aapp%2Fdependabot)

0 commit comments

Comments
 (0)