Skip to content

Commit dead0f7

Browse files
authored
Trimmable Support (#522)
* Trimmable Support * Change macOS image
1 parent f660b42 commit dead0f7

File tree

6 files changed

+26
-9
lines changed

6 files changed

+26
-9
lines changed

Diff for: SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static LibPcapSafeNativeMethods()
4141
/// </summary>
4242
private static void RegisterResolver()
4343
{
44-
NativeLibraryHelper.SetDllImportResolver(typeof(LibPcapSafeNativeMethods).Assembly, Resolver);
44+
NativeLibrary.SetDllImportResolver(typeof(LibPcapSafeNativeMethods).Assembly, Resolver);
4545
}
4646

4747
public static IntPtr Resolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
@@ -69,7 +69,7 @@ public static IntPtr Resolver(string libraryName, Assembly assembly, DllImportSe
6969

7070
foreach (var name in names)
7171
{
72-
if (NativeLibraryHelper.TryLoad(name, out var handle))
72+
if (NativeLibrary.TryLoad(name, out var handle))
7373
{
7474
return handle;
7575
}

Diff for: SharpPcap/LibPcap/NativeLibraryHelper.cs renamed to SharpPcap/LibPcap/NativeLibrary.cs

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
using System.Reflection;
66
using System.Runtime.InteropServices;
77

8+
#if !NET8_0_OR_GREATER
89
namespace SharpPcap.LibPcap
910
{
10-
class NativeLibraryHelper
11+
/**
12+
* Helper class that uses reflection to access System.Runtime.InteropServices.NativeLibrary
13+
* This is needed in order to keep netstandard compatiblity
14+
*
15+
* We compile two variants of the DLL, one trimmable but requires .NET 8 thus have NativeLibrary available directly
16+
* and one that uses .NET standard, with no trimming support
17+
*
18+
*/
19+
class NativeLibrary
1120
{
1221

1322
public delegate IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath);
1423

1524
private static readonly Type NativeLibraryType;
16-
static NativeLibraryHelper()
25+
static NativeLibrary()
1726
{
1827
NativeLibraryType = typeof(DllImportSearchPath).Assembly
1928
.GetType("System.Runtime.InteropServices.NativeLibrary");
@@ -66,3 +75,4 @@ public static bool TryLoad(string libraryPath, out IntPtr handle)
6675
}
6776
}
6877
}
78+
#endif

Diff for: SharpPcap/LibPcap/PcapDevice.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,10 @@ public static IEnumerable<RawCapture> GetSequence(ICaptureDevice dev, bool maskE
525525
break;
526526
packet = e.GetPacket();
527527
}
528-
catch (PcapException pe)
528+
catch (PcapException)
529529
{
530530
if (!maskExceptions)
531-
throw pe;
531+
throw;
532532
}
533533

534534
if (packet == null)

Diff for: SharpPcap/SharpPcap.csproj

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SPDX-License-Identifier: MIT
77
-->
88
<Project Sdk="Microsoft.NET.Sdk">
99
<PropertyGroup>
10-
<TargetFramework>netstandard2.0</TargetFramework>
10+
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
1111
<Version>6.3.0</Version>
1212
<Description>A packet capture framework for .NET</Description>
1313
<Authors>Tamir Gal, Chris Morgan and others</Authors>
@@ -24,10 +24,12 @@ SPDX-License-Identifier: MIT
2424
It provides an API for capturing, injecting, analyzing and building packets using any .NET language such as C# and VB.NET.
2525
</Description>
2626
<PackageLicenseExpression>MIT</PackageLicenseExpression>
27-
<LangVersion>7.3</LangVersion>
2827
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
2928
<Deterministic>true</Deterministic>
3029
</PropertyGroup>
30+
<PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
31+
<IsTrimmable>true</IsTrimmable>
32+
</PropertyGroup>
3133
<ItemGroup>
3234
<PackageReference Include="PacketDotNet" Version="1.4.7" />
3335
<PackageReference Include="System.Memory" Version="4.5.5" />

Diff for: SharpPcap/Tunneling/WinTap/WinTapDriver.cs

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using Microsoft.Win32.SafeHandles;
66
using System;
7+
using System.Buffers.Binary;
78
using System.ComponentModel;
89
using System.IO;
910
using System.Net;
@@ -81,7 +82,11 @@ internal static void SetMediaStatus(SafeFileHandle handle, bool connected)
8182
int value = connected ? 1 : 0;
8283
Span<byte> inBuffer = stackalloc byte[4];
8384
Span<byte> outBuffer = stackalloc byte[4];
85+
#if NET8_0_OR_GREATER
86+
MemoryMarshal.Write(inBuffer, in value);
87+
#else
8488
MemoryMarshal.Write(inBuffer, ref value);
89+
#endif
8590
TapControl(handle, TapIoControl.SetMediaStatus, inBuffer, ref outBuffer);
8691
}
8792

Diff for: azure-pipelines.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929

3030
- job: macos
3131
pool:
32-
vmImage: macOS-11
32+
vmImage: macOS-12
3333
steps:
3434
- script: sudo -E bash scripts/install-dotnet.sh
3535
- script: sudo -E bash scripts/install-libpcap.sh

0 commit comments

Comments
 (0)