diff --git a/SharpPcap/LibPcap/LibPcapLiveDevice.cs b/SharpPcap/LibPcap/LibPcapLiveDevice.cs index 2827dcd9..12338e07 100644 --- a/SharpPcap/LibPcap/LibPcapLiveDevice.cs +++ b/SharpPcap/LibPcap/LibPcapLiveDevice.cs @@ -100,7 +100,13 @@ public override void Open(DeviceConfiguration configuration) var immediate_supported = Pcap.LibpcapVersion >= new Version(1, 5, 0); // Check if we can do immediate by setting mintocopy to 0 // See https://www.tcpdump.org/manpages/pcap_set_immediate_mode.3pcap.html - var mintocopy_supported = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + var mintocopy_supported = +#if NET6_0_OR_GREATER + OperatingSystem.IsWindows() +#else + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) +#endif + ; var errbuf = new StringBuilder(Pcap.PCAP_ERRBUF_SIZE); //will hold errors @@ -239,7 +245,13 @@ public override void Open(DeviceConfiguration configuration) } base.Open(configuration); // retrieve the file descriptor of the adapter for use with poll() - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + if ( +#if NET6_0_OR_GREATER + OperatingSystem.IsLinux() +#else + RuntimeInformation.IsOSPlatform(OSPlatform.Linux) +#endif + ) { FileDescriptor = LibPcapSafeNativeMethods.pcap_get_selectable_fd(Handle); } diff --git a/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Interop.cs b/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Interop.cs index bb77b61d..7eb69f0b 100644 --- a/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Interop.cs +++ b/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Interop.cs @@ -50,6 +50,9 @@ internal extern static int pcap_findalldevs_ex( [DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)] internal extern static void pcap_freealldevs(IntPtr /* pcap_if_t * */ alldevs); + /// + /// Open a generic source in order to capture / send traffic. + /// [DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)] internal extern static PcapHandle /* pcap_t* */ pcap_open( [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PcapStringMarshaler))] string dev, diff --git a/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs b/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs index 9b56f85c..d5a70b29 100644 --- a/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs +++ b/SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs @@ -23,7 +23,13 @@ internal static partial class LibPcapSafeNativeMethods static LibPcapSafeNativeMethods() { - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + if ( +#if NET6_0_OR_GREATER + OperatingSystem.IsWindows() +#else + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) +#endif + ) { SetDllDirectory(Path.Combine(Environment.SystemDirectory, "Npcap")); } diff --git a/SharpPcap/LibPcap/LibPcapSafeNativeMethods.cs b/SharpPcap/LibPcap/LibPcapSafeNativeMethods.cs index c31fd2eb..3c1d9a92 100644 --- a/SharpPcap/LibPcap/LibPcapSafeNativeMethods.cs +++ b/SharpPcap/LibPcap/LibPcapSafeNativeMethods.cs @@ -21,13 +21,23 @@ internal static partial class LibPcapSafeNativeMethods internal static PcapError pcap_setbuff(PcapHandle /* pcap_t */ adapter, int bufferSizeInBytes) { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + return +#if NET6_0_OR_GREATER + OperatingSystem.IsWindows() +#else + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) +# endif ? _pcap_setbuff(adapter, bufferSizeInBytes) : PcapError.PlatformNotSupported; } internal static PcapError pcap_setmintocopy(PcapHandle /* pcap_t */ adapter, int sizeInBytes) { - return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + return +#if NET6_0_OR_GREATER + OperatingSystem.IsWindows() +#else + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) +#endif ? _pcap_setmintocopy(adapter, sizeInBytes) : PcapError.PlatformNotSupported; } @@ -95,7 +105,12 @@ internal static int pcap_get_tstamp_precision(PcapHandle /* pcap_t* p */ adapter internal static PcapHandle pcap_open_handle_offline_with_tstamp_precision( SafeHandle handle, uint precision, StringBuilder errbuf) { - var pointer = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + var pointer = +#if NET6_0_OR_GREATER + OperatingSystem.IsWindows() +#else + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) +# endif ? _pcap_hopen_offline_with_tstamp_precision(handle, precision, errbuf) : _pcap_fopen_offline_with_tstamp_precision(handle, precision, errbuf); if (pointer == IntPtr.Zero) diff --git a/SharpPcap/LibPcap/NativeLibraryHelper.cs b/SharpPcap/LibPcap/NativeLibraryHelper.cs index 4ca4ceba..c52fafea 100644 --- a/SharpPcap/LibPcap/NativeLibraryHelper.cs +++ b/SharpPcap/LibPcap/NativeLibraryHelper.cs @@ -9,15 +9,10 @@ namespace SharpPcap.LibPcap { class NativeLibraryHelper { - public delegate IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath); - private static readonly Type NativeLibraryType; - static NativeLibraryHelper() - { - NativeLibraryType = typeof(DllImportSearchPath).Assembly + private static readonly Type NativeLibraryType = typeof(DllImportSearchPath).Assembly .GetType("System.Runtime.InteropServices.NativeLibrary"); - } public static void SetDllImportResolver(Assembly assembly, DllImportResolver resolver) { @@ -26,6 +21,9 @@ public static void SetDllImportResolver(Assembly assembly, DllImportResolver res return; } +#if NET6_0_OR_GREATER + NativeLibrary.SetDllImportResolver(assembly, (lib, asm, path) => resolver(lib, asm, path)); +#else var dllImportResolverType = typeof(DllImportSearchPath).Assembly .GetType("System.Runtime.InteropServices.DllImportResolver"); @@ -42,6 +40,7 @@ public static void SetDllImportResolver(Assembly assembly, DllImportResolver res assembly, Delegate.CreateDelegate(dllImportResolverType, resolver, "Invoke") }); +#endif } public static bool TryLoad(string libraryPath, out IntPtr handle) diff --git a/SharpPcap/SharpPcap.csproj b/SharpPcap/SharpPcap.csproj index 561b3b81..e8b680d5 100644 --- a/SharpPcap/SharpPcap.csproj +++ b/SharpPcap/SharpPcap.csproj @@ -7,7 +7,7 @@ SPDX-License-Identifier: MIT --> - netstandard2.0 + netstandard2.0;net6.0;net8.0 6.3.0 A packet capture framework for .NET Tamir Gal, Chris Morgan and others @@ -33,8 +33,6 @@ SPDX-License-Identifier: MIT - - diff --git a/SharpPcap/Tunneling/WinTap/WinTapDriver.cs b/SharpPcap/Tunneling/WinTap/WinTapDriver.cs index dc9a5777..e308ac81 100644 --- a/SharpPcap/Tunneling/WinTap/WinTapDriver.cs +++ b/SharpPcap/Tunneling/WinTap/WinTapDriver.cs @@ -81,7 +81,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); }