From dead0f70932183abf3e0bbed9f3ebe7a5d23c06e Mon Sep 17 00:00:00 2001 From: Ayoub Kaanich Date: Tue, 30 Jul 2024 09:44:44 +0200 Subject: [PATCH] Trimmable Support (#522) * Trimmable Support * Change macOS image --- .../LibPcap/LibPcapSafeNativeMethods.Resolver.cs | 4 ++-- .../{NativeLibraryHelper.cs => NativeLibrary.cs} | 14 ++++++++++++-- SharpPcap/LibPcap/PcapDevice.cs | 4 ++-- SharpPcap/SharpPcap.csproj | 6 ++++-- SharpPcap/Tunneling/WinTap/WinTapDriver.cs | 5 +++++ azure-pipelines.yml | 2 +- 6 files changed, 26 insertions(+), 9 deletions(-) rename SharpPcap/LibPcap/{NativeLibraryHelper.cs => NativeLibrary.cs} (82%) diff --git a/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs b/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs index 9b56f85c..c23411f6 100644 --- a/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs +++ b/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs @@ -41,7 +41,7 @@ static LibPcapSafeNativeMethods() /// private static void RegisterResolver() { - NativeLibraryHelper.SetDllImportResolver(typeof(LibPcapSafeNativeMethods).Assembly, Resolver); + NativeLibrary.SetDllImportResolver(typeof(LibPcapSafeNativeMethods).Assembly, Resolver); } public static IntPtr Resolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath) @@ -69,7 +69,7 @@ public static IntPtr Resolver(string libraryName, Assembly assembly, DllImportSe foreach (var name in names) { - if (NativeLibraryHelper.TryLoad(name, out var handle)) + if (NativeLibrary.TryLoad(name, out var handle)) { return handle; } diff --git a/SharpPcap/LibPcap/NativeLibraryHelper.cs b/SharpPcap/LibPcap/NativeLibrary.cs similarity index 82% rename from SharpPcap/LibPcap/NativeLibraryHelper.cs rename to SharpPcap/LibPcap/NativeLibrary.cs index 4ca4ceba..a8692d8b 100644 --- a/SharpPcap/LibPcap/NativeLibraryHelper.cs +++ b/SharpPcap/LibPcap/NativeLibrary.cs @@ -5,15 +5,24 @@ using System.Reflection; using System.Runtime.InteropServices; +#if !NET8_0_OR_GREATER namespace SharpPcap.LibPcap { - class NativeLibraryHelper + /** + * Helper class that uses reflection to access System.Runtime.InteropServices.NativeLibrary + * This is needed in order to keep netstandard compatiblity + * + * We compile two variants of the DLL, one trimmable but requires .NET 8 thus have NativeLibrary available directly + * and one that uses .NET standard, with no trimming support + * + */ + class NativeLibrary { public delegate IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath); private static readonly Type NativeLibraryType; - static NativeLibraryHelper() + static NativeLibrary() { NativeLibraryType = typeof(DllImportSearchPath).Assembly .GetType("System.Runtime.InteropServices.NativeLibrary"); @@ -66,3 +75,4 @@ public static bool TryLoad(string libraryPath, out IntPtr handle) } } } +#endif \ No newline at end of file diff --git a/SharpPcap/LibPcap/PcapDevice.cs b/SharpPcap/LibPcap/PcapDevice.cs index ac1149da..0b2b20c7 100644 --- a/SharpPcap/LibPcap/PcapDevice.cs +++ b/SharpPcap/LibPcap/PcapDevice.cs @@ -525,10 +525,10 @@ public static IEnumerable GetSequence(ICaptureDevice dev, bool maskE break; packet = e.GetPacket(); } - catch (PcapException pe) + catch (PcapException) { if (!maskExceptions) - throw pe; + throw; } if (packet == null) diff --git a/SharpPcap/SharpPcap.csproj b/SharpPcap/SharpPcap.csproj index e090f946..5362aeca 100644 --- a/SharpPcap/SharpPcap.csproj +++ b/SharpPcap/SharpPcap.csproj @@ -7,7 +7,7 @@ SPDX-License-Identifier: MIT --> - netstandard2.0 + netstandard2.0;net8.0 6.3.0 A packet capture framework for .NET Tamir Gal, Chris Morgan and others @@ -24,10 +24,12 @@ SPDX-License-Identifier: MIT It provides an API for capturing, injecting, analyzing and building packets using any .NET language such as C# and VB.NET. MIT - 7.3 true true + + true + diff --git a/SharpPcap/Tunneling/WinTap/WinTapDriver.cs b/SharpPcap/Tunneling/WinTap/WinTapDriver.cs index dc9a5777..f49d3566 100644 --- a/SharpPcap/Tunneling/WinTap/WinTapDriver.cs +++ b/SharpPcap/Tunneling/WinTap/WinTapDriver.cs @@ -4,6 +4,7 @@ using Microsoft.Win32.SafeHandles; using System; +using System.Buffers.Binary; using System.ComponentModel; using System.IO; using System.Net; @@ -81,7 +82,11 @@ internal static void SetMediaStatus(SafeFileHandle handle, bool connected) int value = connected ? 1 : 0; Span inBuffer = stackalloc byte[4]; Span outBuffer = stackalloc byte[4]; +#if NET8_0_OR_GREATER + MemoryMarshal.Write(inBuffer, in value); +#else MemoryMarshal.Write(inBuffer, ref value); +#endif TapControl(handle, TapIoControl.SetMediaStatus, inBuffer, ref outBuffer); } diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 5359fb35..99bf405a 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -29,7 +29,7 @@ jobs: - job: macos pool: - vmImage: macOS-11 + vmImage: macOS-12 steps: - script: sudo -E bash scripts/install-dotnet.sh - script: sudo -E bash scripts/install-libpcap.sh