Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trimmable Support #522

Merged
merged 3 commits into from
Jul 30, 2024
Merged
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
4 changes: 2 additions & 2 deletions SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static LibPcapSafeNativeMethods()
/// </summary>
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)
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -66,3 +75,4 @@ public static bool TryLoad(string libraryPath, out IntPtr handle)
}
}
}
#endif
4 changes: 2 additions & 2 deletions SharpPcap/LibPcap/PcapDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@
break;
packet = e.GetPacket();
}
catch (PcapException pe)
catch (PcapException)

Check warning on line 528 in SharpPcap/LibPcap/PcapDevice.cs

View check run for this annotation

Codecov / codecov/patch

SharpPcap/LibPcap/PcapDevice.cs#L528

Added line #L528 was not covered by tests
{
if (!maskExceptions)
throw pe;
throw;

Check warning on line 531 in SharpPcap/LibPcap/PcapDevice.cs

View check run for this annotation

Codecov / codecov/patch

SharpPcap/LibPcap/PcapDevice.cs#L531

Added line #L531 was not covered by tests
}

if (packet == null)
Expand Down
6 changes: 4 additions & 2 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;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 @@ -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.
</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LangVersion>7.3</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PacketDotNet" Version="1.4.7" />
<PackageReference Include="System.Memory" Version="4.5.5" />
Expand Down
5 changes: 5 additions & 0 deletions SharpPcap/Tunneling/WinTap/WinTapDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.Win32.SafeHandles;
using System;
using System.Buffers.Binary;
using System.ComponentModel;
using System.IO;
using System.Net;
Expand Down Expand Up @@ -81,7 +82,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
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading