Smdn.Net.AddressResolution 1.1.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Smdn.Net.AddressResolution --version 1.1.0
NuGet\Install-Package Smdn.Net.AddressResolution -Version 1.1.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Smdn.Net.AddressResolution" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Smdn.Net.AddressResolution --version 1.1.0
#r "nuget: Smdn.Net.AddressResolution, 1.1.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Smdn.Net.AddressResolution as a Cake Addin
#addin nuget:?package=Smdn.Net.AddressResolution&version=1.1.0

// Install Smdn.Net.AddressResolution as a Cake Tool
#tool nuget:?package=Smdn.Net.AddressResolution&version=1.1.0

Smdn.Net.AddressResolution-1.1.0

A network address resolution library for .NET.

This library provides APIs for resolving between IP addresses and MAC addresses, mainly the MacAddressResolver class in the Smdn.Net.AddressResolution namespace.

This library also provides a functionality for referencing the system's address table such as the ARP table (Smdn.Net.AddressTables namespace), and a network scan functionality to refresh the address cache mainly using the installed commands (Smdn.Net.NetworkScanning namespace).

Usage

// SPDX-FileCopyrightText: 2023 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System;
using System.Net;
using Smdn.Net;
using Smdn.Net.AddressResolution;

// Create a MacAddressResolver that scopes to the network range of
// the automatically selected network interface.
using var resolver = new MacAddressResolver(IPNetworkProfile.Create());

// Resolve MAC address corresponding to the IP address.
var targetIPAddress = IPAddress.Parse("192.0.2.1");
var resolvedMacAddress = await resolver.ResolveIPAddressToMacAddressAsync(targetIPAddress);

// If address could not be resolved, the method returns null.
if (resolvedMacAddress == null)
  Console.WriteLine($"Not resolved: {targetIPAddress}");
else
  Console.WriteLine($"Resolved: {targetIPAddress} => {resolvedMacAddress.ToMacAddressString()}");

Contributing

This project welcomes contributions, feedbacks and suggestions. You can contribute to this project by submitting Issues or Pull Requests on the GitHub repository.

API List

List of APIs exposed by assembly Smdn.Net.AddressResolution-1.1.0 (net8.0)

// Smdn.Net.AddressResolution.dll (Smdn.Net.AddressResolution-1.1.0)
//   Name: Smdn.Net.AddressResolution
//   AssemblyVersion: 1.1.0.0
//   InformationalVersion: 1.1.0+fe88b9191dd60b0f5dc1e1881193ccd376b9795c
//   TargetFramework: .NETCoreApp,Version=v8.0
//   Configuration: Release
//   Referenced assemblies:
//     Microsoft.Extensions.DependencyInjection.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
//     Microsoft.Extensions.Logging.Abstractions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
//     System.Collections, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Collections.Concurrent, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.ComponentModel, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.ComponentModel.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Diagnostics.Process, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Linq, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Memory, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51
//     System.Net.NetworkInformation, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Net.Ping, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Net.Primitives, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Runtime.InteropServices, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     System.Threading, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
//     Vanara.PInvoke.IpHlpApi, Version=3.4.13.0, Culture=neutral, PublicKeyToken=c37e4080322237fa
//     Vanara.PInvoke.Shared, Version=3.4.13.0, Culture=neutral, PublicKeyToken=c37e4080322237fa
//     Vanara.PInvoke.Ws2_32, Version=3.4.13.0, Culture=neutral, PublicKeyToken=c37e4080322237fa
#nullable enable annotations

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Smdn.Net;
using Smdn.Net.AddressResolution;
using Smdn.Net.AddressTables;
using Smdn.Net.NetworkScanning;

namespace Smdn.Net {
  public abstract class IPNetworkProfile {
    public static IPNetworkProfile Create() {}
    public static IPNetworkProfile Create(Func<IEnumerable<IPAddress>?> addressRangeGenerator, NetworkInterface? networkInterface = null) {}
    public static IPNetworkProfile Create(IPAddress baseAddress, IPAddress subnetMask, NetworkInterface? networkInterface = null) {}
    public static IPNetworkProfile Create(IPAddress baseAddress, int prefixLength, NetworkInterface? networkInterface = null) {}
    public static IPNetworkProfile Create(IPNetwork network, NetworkInterface? networkInterface = null) {}
    public static IPNetworkProfile Create(NetworkInterface networkInterface) {}
    public static IPNetworkProfile Create(Predicate<NetworkInterface> predicateForNetworkInterface) {}
    public static IPNetworkProfile CreateFromNetworkInterface(Guid id) {}
    public static IPNetworkProfile CreateFromNetworkInterface(PhysicalAddress physicalAddress) {}
    public static IPNetworkProfile CreateFromNetworkInterface(string id) {}
    public static IPNetworkProfile CreateFromNetworkInterfaceName(string name) {}

    protected IPNetworkProfile(NetworkInterface? networkInterface) {}

    public NetworkInterface? NetworkInterface { get; }

    public abstract IEnumerable<IPAddress>? GetAddressRange();
  }

  public static class PhysicalAddressExtensions {
    public static string ToMacAddressString(this PhysicalAddress hardwareAddress, char delimiter = ':') {}
  }
}

namespace Smdn.Net.AddressResolution {
  public interface IAddressResolver<TAddress, TResolvedAddress> where TAddress : notnull where TResolvedAddress : notnull {
    void Invalidate(TAddress address);
    ValueTask<TResolvedAddress?> ResolveAsync(TAddress address, CancellationToken cancellationToken);
  }

  public class MacAddressResolver : MacAddressResolverBase {
    protected MacAddressResolver(IAddressTable addressTable, bool shouldDisposeAddressTable, INetworkScanner? networkScanner, bool shouldDisposeNetworkScanner, NetworkInterface? networkInterface, int maxParallelCountForRefreshInvalidatedAddresses, ILogger? logger) {}
    public MacAddressResolver() {}
    public MacAddressResolver(IAddressTable? addressTable, INetworkScanner? networkScanner, bool shouldDisposeAddressTable = false, bool shouldDisposeNetworkScanner = false, NetworkInterface? networkInterface = null, int maxParallelCountForRefreshInvalidatedAddresses = 3, IServiceProvider? serviceProvider = null) {}
    public MacAddressResolver(IPNetworkProfile? networkProfile, IServiceProvider? serviceProvider = null) {}

    public bool CanPerformNetworkScan { get; }
    public override bool HasInvalidated { get; }
    public TimeSpan NetworkScanInterval { get; set; }
    public TimeSpan NetworkScanMinInterval { get; set; }

    protected override void Dispose(bool disposing) {}
    public IAsyncEnumerable<AddressTableEntry> EnumerateAddressTableEntriesAsync(CancellationToken cancellationToken = default) {}
    public IAsyncEnumerable<AddressTableEntry> EnumerateAddressTableEntriesAsync(Predicate<AddressTableEntry> predicate, CancellationToken cancellationToken = default) {}
    protected override void InvalidateCore(IPAddress ipAddress) {}
    protected override void InvalidateCore(PhysicalAddress macAddress) {}
    protected override ValueTask RefreshAddressTableAsyncCore(CancellationToken cancellationToken = default) {}
    protected override ValueTask RefreshInvalidatedAddressesAsyncCore(CancellationToken cancellationToken = default) {}
    protected override async ValueTask<PhysicalAddress?> ResolveIPAddressToMacAddressAsyncCore(IPAddress ipAddress, CancellationToken cancellationToken) {}
    protected override async ValueTask<IPAddress?> ResolveMacAddressToIPAddressAsyncCore(PhysicalAddress macAddress, CancellationToken cancellationToken) {}
    protected virtual async ValueTask<AddressTableEntry> SelectAddressTableEntryAsync(Predicate<AddressTableEntry> predicate, CancellationToken cancellationToken) {}
  }

  public abstract class MacAddressResolverBase :
    IAddressResolver<IPAddress, PhysicalAddress>,
    IAddressResolver<PhysicalAddress, IPAddress>,
    IDisposable
  {
    protected static PhysicalAddress AllZeroMacAddress { get; }
    public static MacAddressResolverBase Null { get; }

    protected MacAddressResolverBase(ILogger? logger = null) {}

    public abstract bool HasInvalidated { get; }
    protected ILogger? Logger { get; }

    protected virtual void Dispose(bool disposing) {}
    public void Dispose() {}
    public void Invalidate(IPAddress ipAddress) {}
    public void Invalidate(PhysicalAddress macAddress) {}
    protected abstract void InvalidateCore(IPAddress ipAddress);
    protected abstract void InvalidateCore(PhysicalAddress macAddress);
    public ValueTask RefreshAddressTableAsync(CancellationToken cancellationToken = default) {}
    protected virtual ValueTask RefreshAddressTableAsyncCore(CancellationToken cancellationToken) {}
    public ValueTask RefreshInvalidatedAddressesAsync(CancellationToken cancellationToken = default) {}
    protected virtual ValueTask RefreshInvalidatedAddressesAsyncCore(CancellationToken cancellationToken) {}
    public ValueTask<PhysicalAddress?> ResolveIPAddressToMacAddressAsync(IPAddress ipAddress, CancellationToken cancellationToken = default) {}
    protected abstract ValueTask<PhysicalAddress?> ResolveIPAddressToMacAddressAsyncCore(IPAddress ipAddress, CancellationToken cancellationToken);
    public ValueTask<IPAddress?> ResolveMacAddressToIPAddressAsync(PhysicalAddress macAddress, CancellationToken cancellationToken = default) {}
    protected abstract ValueTask<IPAddress?> ResolveMacAddressToIPAddressAsyncCore(PhysicalAddress macAddress, CancellationToken cancellationToken);
    void IAddressResolver<IPAddress, PhysicalAddress>.Invalidate(IPAddress address) {}
    ValueTask<PhysicalAddress?> IAddressResolver<IPAddress, PhysicalAddress>.ResolveAsync(IPAddress address, CancellationToken cancellationToken) {}
    void IAddressResolver<PhysicalAddress, IPAddress>.Invalidate(PhysicalAddress address) {}
    ValueTask<IPAddress?> IAddressResolver<PhysicalAddress, IPAddress>.ResolveAsync(PhysicalAddress address, CancellationToken cancellationToken) {}
    protected void ThrowIfDisposed() {}
  }
}

namespace Smdn.Net.AddressTables {
  public interface IAddressTable : IDisposable {
    IAsyncEnumerable<AddressTableEntry> EnumerateEntriesAsync(CancellationToken cancellationToken);
  }

  public enum AddressTableEntryState : int {
    Delay = 4,
    Incomplete = 1,
    None = 0,
    Probe = 5,
    Reachable = 2,
    Stale = 3,
  }

  public abstract class AddressTable : IAddressTable {
    public static IAddressTable Null { get; }

    public static IAddressTable Create(IServiceProvider? serviceProvider = null) {}

    protected AddressTable(ILogger? logger = null) {}

    protected ILogger? Logger { get; }

    protected virtual void Dispose(bool disposing) {}
    public void Dispose() {}
    public IAsyncEnumerable<AddressTableEntry> EnumerateEntriesAsync(CancellationToken cancellationToken = default) {}
    protected abstract IAsyncEnumerable<AddressTableEntry> EnumerateEntriesAsyncCore(CancellationToken cancellationToken);
    protected void ThrowIfDisposed() {}
  }

  public sealed class IpHlpApiAddressTable : AddressTable {
    public static bool IsSupported { get; }

    public IpHlpApiAddressTable(IServiceProvider? serviceProvider = null) {}

    [AsyncIteratorStateMachine(typeof(IpHlpApiAddressTable.<EnumerateEntriesAsyncCore>d__4))]
    protected override IAsyncEnumerable<AddressTableEntry> EnumerateEntriesAsyncCore([EnumeratorCancellation] CancellationToken cancellationToken) {}
  }

  public sealed class ProcfsArpAddressTable : AddressTable {
    public static bool IsSupported { get; }

    public ProcfsArpAddressTable(IServiceProvider? serviceProvider = null) {}

    [AsyncIteratorStateMachine(typeof(ProcfsArpAddressTable.<EnumerateEntriesAsyncCore>d__5))]
    protected override IAsyncEnumerable<AddressTableEntry> EnumerateEntriesAsyncCore([EnumeratorCancellation] CancellationToken cancellationToken) {}
  }

  public readonly struct AddressTableEntry :
    IEquatable<AddressTableEntry>,
    IEquatable<IPAddress>,
    IEquatable<PhysicalAddress>
  {
    public static readonly AddressTableEntry Empty; // = "{IP=, MAC=(null), IsPermanent=False, State=None, Iface=}"

    public static IEqualityComparer<AddressTableEntry> DefaultEqualityComparer { get; }
    public static IEqualityComparer<AddressTableEntry> ExceptStateEqualityComparer { get; }

    public AddressTableEntry(IPAddress ipAddress, PhysicalAddress? physicalAddress, bool isPermanent, AddressTableEntryState state, string? interfaceId) {}

    public IPAddress? IPAddress { get; }
    public string? InterfaceId { get; }
    [MemberNotNullWhen(false, "IPAddress")]
    public bool IsEmpty { [MemberNotNullWhen(false, "IPAddress")] get; }
    public bool IsPermanent { get; }
    public PhysicalAddress? PhysicalAddress { get; }
    public AddressTableEntryState State { get; }

    public bool Equals(AddressTableEntry other) {}
    public bool Equals(IPAddress? other) {}
    public bool Equals(PhysicalAddress? other) {}
    public override bool Equals(object? obj) {}
    public override int GetHashCode() {}
    public override string ToString() {}
  }
}

namespace Smdn.Net.NetworkScanning {
  public interface INetworkScanner : IDisposable {
    ValueTask ScanAsync(CancellationToken cancellationToken);
    ValueTask ScanAsync(IEnumerable<IPAddress> addresses, CancellationToken cancellationToken);
  }

  public sealed class ArpScanCommandNetworkScanner : CommandNetworkScanner {
    public static bool IsSupported { get; }

    public ArpScanCommandNetworkScanner(IPNetworkProfile? networkProfile, IServiceProvider? serviceProvider = null) {}

    protected override bool GetCommandLineArguments(IEnumerable<IPAddress> addressesToScan, out string executable, out string arguments) {}
    protected override bool GetCommandLineArguments(out string executable, out string arguments) {}
  }

  public abstract class CommandNetworkScanner : INetworkScanner {
    public interface IProcessFactory {
      Process CreateProcess(ProcessStartInfo processStartInfo);
    }

    protected readonly struct Command {
      public Command(string name, string? executablePath) {}

      public bool IsAvailable { get; }
      public string Name { get; }

      public string GetExecutablePathOrThrow() {}
    }

    protected static IReadOnlyCollection<string> DefaultCommandPaths { get; }

    protected static CommandNetworkScanner.Command FindCommand(string command, IEnumerable<string> paths) {}

    protected CommandNetworkScanner(ILogger? logger, IServiceProvider? serviceProvider) {}

    protected virtual void Dispose(bool disposing) {}
    public void Dispose() {}
    protected abstract bool GetCommandLineArguments(IEnumerable<IPAddress> addressesToScan, out string executable, out string? arguments);
    protected abstract bool GetCommandLineArguments(out string executable, out string? arguments);
    public virtual ValueTask ScanAsync(CancellationToken cancellationToken = default) {}
    public virtual ValueTask ScanAsync(IEnumerable<IPAddress> addresses, CancellationToken cancellationToken = default) {}
    protected void ThrowIfDisposed() {}
  }

  public sealed class IpHlpApiNetworkScanner : NetworkScanner {
    public static bool IsSupported { get; }

    public IpHlpApiNetworkScanner(IPNetworkProfile networkProfile, IServiceProvider? serviceProvider = null) {}

    protected override async ValueTask ScanAsyncCore(IPAddress address, CancellationToken cancellationToken = default) {}
  }

  public abstract class NetworkScanner : INetworkScanner {
    public static INetworkScanner Null { get; }

    public static INetworkScanner Create(IPNetworkProfile? networkProfile, IServiceProvider? serviceProvider = null) {}

    protected NetworkScanner(IPNetworkProfile networkProfile, ILogger? logger = null) {}

    protected ILogger? Logger { get; }
    protected IPNetworkProfile NetworkProfile { get; }

    protected virtual void Dispose(bool disposing) {}
    public void Dispose() {}
    public virtual ValueTask ScanAsync(CancellationToken cancellationToken = default) {}
    public virtual ValueTask ScanAsync(IEnumerable<IPAddress> addresses, CancellationToken cancellationToken = default) {}
    protected virtual ValueTask ScanAsyncCore(IPAddress address, CancellationToken cancellationToken) {}
    protected void ThrowIfDisposed() {}
  }

  public sealed class NmapCommandNetworkScanner : CommandNetworkScanner {
    public static bool IsSupported { get; }

    public NmapCommandNetworkScanner(IPNetworkProfile networkProfile, IServiceProvider? serviceProvider = null) {}

    protected override bool GetCommandLineArguments(IEnumerable<IPAddress> addressesToScan, out string executable, out string arguments) {}
    protected override bool GetCommandLineArguments(out string executable, out string arguments) {}
  }

  public sealed class PingNetworkScanner : NetworkScanner {
    public static bool IsSupported { get; }

    public PingNetworkScanner(IPNetworkProfile networkProfile, IServiceProvider? serviceProvider = null) {}

    protected override void Dispose(bool disposing) {}
    protected override async ValueTask ScanAsyncCore(IPAddress address, CancellationToken cancellationToken = default) {}
  }
}
// API list generated by Smdn.Reflection.ReverseGenerating.ListApi.MSBuild.Tasks v1.3.2.0.
// Smdn.Reflection.ReverseGenerating.ListApi.Core v1.2.0.0 (https://github.com/smdn/Smdn.Reflection.ReverseGenerating)
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Smdn.Net.AddressResolution:

Package Downloads
Smdn.TPSmartHomeDevices.MacAddressEndPoint The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

An extension library to add a service to Smdn.TPSmartHomeDevices.Tapo and Smdn.TPSmartHomeDevices.Kasa that enables the resolution of devices' endpoints by MAC address.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 130 1/11/2024
1.0.2 201 5/15/2023
1.0.1 146 5/2/2023
1.0.0 628 4/19/2023
1.0.0-rc2 170 4/15/2023
1.0.0-rc1 132 4/13/2023
1.0.0-preview6 151 3/29/2023
1.0.0-preview5 293 3/23/2023
1.0.0-preview4 211 3/14/2023
1.0.0-preview3 132 3/10/2023
1.0.0-preview2 210 3/8/2023
1.0.0-preview1 227 3/5/2023