Skip to content

Commit c6c2881

Browse files
committed
Fix: NullException for IPScanner & NETBIOS if unkown error occours
1 parent 32f1f59 commit c6c2881

File tree

3 files changed

+34
-22
lines changed

3 files changed

+34
-22
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
{

0 commit comments

Comments
 (0)