Skip to content

Commit

Permalink
Return null on Windows if address could not be found
Browse files Browse the repository at this point in the history
This is in line with the Linux implementation and the docs, see also #7.
This is a breaking change as what has thrown an exception before now returns null.
  • Loading branch information
georg-jung committed Jul 13, 2021
1 parent 6c69171 commit e486ad8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/ArpLookup/WindowsLookupService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
Expand Down Expand Up @@ -26,7 +26,7 @@ internal static class WindowsLookupService
/// <param name="ip">The <see cref="IPAddress"/> to pass to the Win32 API.</param>
/// <exception cref="Win32Exception">If IpHlpApi.SendARP returns non-zero.</exception>
/// <returns>A <see cref="PhysicalAddress"/> instance that represents the address found by IpHlpApi.SendARP.</returns>
public static PhysicalAddress Lookup(IPAddress ip)
public static PhysicalAddress? Lookup(IPAddress ip)
{
_ = ip ?? throw new ArgumentNullException(nameof(ip));
if (!IsSupported)
Expand All @@ -46,12 +46,18 @@ public static PhysicalAddress Lookup(IPAddress ip)
{
return new PhysicalAddress(addr);
}
else if (res == NativeMethods.ERROR_BAD_NET_NAME)
{
return null;
}

throw new Win32Exception(res);
}

private static class NativeMethods
{
public const int ERROR_BAD_NET_NAME = 67;

// based on code generated by https://github.com/microsoft/CsWin32, modified

/// <summary>The SendARP function sends an Address Resolution Protocol (ARP) request to obtain the physical address that corresponds to the specified destination IPv4 address.</summary>
Expand Down
4 changes: 2 additions & 2 deletions version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.3-alpha",
"version": "2.0-alpha",
"publicReleaseRefSpec": [
"^refs/heads/release/v\\d+(?:\\.\\d+)?$"
],
Expand All @@ -12,4 +12,4 @@
"release": {
"branchName": "release/v{version}"
}
}
}

0 comments on commit e486ad8

Please sign in to comment.