Skip to content

Commit

Permalink
Trimmable Support (#522)
Browse files Browse the repository at this point in the history
* Trimmable Support
* Change macOS image
  • Loading branch information
kayoub5 authored Jul 30, 2024
1 parent f660b42 commit dead0f7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
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 @@ public static IEnumerable<RawCapture> GetSequence(ICaptureDevice dev, bool maskE
break;
packet = e.GetPacket();
}
catch (PcapException pe)
catch (PcapException)
{
if (!maskExceptions)
throw pe;
throw;
}

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

0 comments on commit dead0f7

Please sign in to comment.