Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions SharpPcap/LibPcap/LibPcapLiveDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions SharpPcap/LibPcap/LibPcapSafeNativeMethods.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <summary>
/// Open a generic source in order to capture / send traffic.
/// </summary>
[DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)]
internal extern static PcapHandle /* pcap_t* */ pcap_open(
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PcapStringMarshaler))] string dev,
Expand Down
8 changes: 7 additions & 1 deletion SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down
21 changes: 18 additions & 3 deletions SharpPcap/LibPcap/LibPcapSafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions SharpPcap/LibPcap/NativeLibraryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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");

Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions SharpPcap/SharpPcap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SPDX-License-Identifier: MIT
-->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0;net8.0</TargetFrameworks>
<Version>6.3.0</Version>
<Description>A packet capture framework for .NET</Description>
<Authors>Tamir Gal, Chris Morgan and others</Authors>
Expand All @@ -33,8 +33,6 @@ SPDX-License-Identifier: MIT
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All"/>
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
Expand Down
4 changes: 4 additions & 0 deletions SharpPcap/Tunneling/WinTap/WinTapDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ internal static void SetMediaStatus(SafeFileHandle handle, bool connected)
int value = connected ? 1 : 0;
Span<byte> inBuffer = stackalloc byte[4];
Span<byte> 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);
}

Expand Down