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

Support statically linked NativeAOT distribution #448

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
7 changes: 6 additions & 1 deletion SharpPcap/LibPcap/BpfProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public class BpfProgram : SafeHandleZeroOrMinusOneIsInvalid
// Requires calls to pcap_compile to be non-concurrent to avoid crashes due to known lack of thread-safety
// See https://github.com/chmorgan/sharppcap/issues/311
// Problem of thread safety does not affect Windows
private static readonly bool ThreadSafeCompile = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
private static readonly bool ThreadSafeCompile =
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
|| Pcap.LibpcapVersion >= new Version(1, 8, 0);
private static readonly object SyncCompile = new object();

Expand Down
25 changes: 17 additions & 8 deletions SharpPcap/LibPcap/LibPcapLiveDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,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 @@ -170,8 +176,6 @@ public override void Open(DeviceConfiguration configuration)
}
else
{
// We got authentication, so this is an rpcap device
var auth = RemoteAuthentication.CreateAuth(credentials);
// Immediate and MaxResponsiveness are the same thing
if (immediateMode == true)
{
Expand All @@ -181,12 +185,11 @@ public override void Open(DeviceConfiguration configuration)
immediateMode = null;
try
{
Handle = LibPcapSafeNativeMethods.pcap_open(
Handle = LibPcapSafeNativeMethods.pcap_open_live(
Name, // name of the device
configuration.Snaplen, // portion of the packet to capture.
(short)mode, // flags
(short)configuration.ReadTimeout, // read timeout
ref auth, // authentication
(int)mode, // flags
configuration.ReadTimeout, // read timeout
errbuf); // error buffer
}
catch (TypeLoadException)
Expand Down Expand Up @@ -255,7 +258,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
8 changes: 7 additions & 1 deletion SharpPcap/LibPcap/LibPcapSafeNativeMethods.Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ internal static partial class LibPcapSafeNativeMethods

private static Encoding ConfigureStringEncoding()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
{
// libpcap always use UTF-8 when not on Windows
return Encoding.UTF8;
Expand Down
14 changes: 8 additions & 6 deletions SharpPcap/LibPcap/LibPcapSafeNativeMethods.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal static partial class LibPcapSafeNativeMethods
// This file is called $assembly_name.dll.config and is placed in the
// same directory as the assembly
// See http://www.mono-project.com/Interop_with_Native_Libraries#Library_Names
private const string PCAP_DLL = "wpcap";
private const string PCAP_DLL = "pcap";
kayoub5 marked this conversation as resolved.
Show resolved Hide resolved

[DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)]
internal extern static int pcap_init(
Expand All @@ -66,13 +66,15 @@ 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 (WinPcap only) traffic.
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
[DllImport(PCAP_DLL, CallingConvention = CallingConvention.Cdecl)]
internal extern static PcapHandle /* pcap_t* */ pcap_open(
internal extern static PcapHandle /* pcap_t* */ pcap_open_live(
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PcapStringMarshaler))] string dev,
int packetLen,
int flags,
int read_timeout,
ref pcap_rmtauth rmtauth,
kayoub5 marked this conversation as resolved.
Show resolved Hide resolved
int snaplen,
int promisc,
int to_ms,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(PcapStringMarshaler))] StringBuilder errbuf
);

Expand Down
24 changes: 21 additions & 3 deletions SharpPcap/LibPcap/LibPcapSafeNativeMethods.Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 Expand Up @@ -70,15 +76,27 @@ public static IntPtr Resolver(string libraryName, Assembly assembly, DllImportSe

var names = new List<string>();

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
if (
#if NET6_0_OR_GREATER
OperatingSystem.IsLinux()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
#endif
)
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
{
names.Add("libpcap.so");
names.Add("libpcap.so.0");
names.Add("libpcap.so.0.8");
names.Add("libpcap.so.1");
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
if (
#if NET6_0_OR_GREATER
OperatingSystem.IsMacOS()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
#endif
)
hez2010 marked this conversation as resolved.
Show resolved Hide resolved
{
names.Add("libpcap.dylib");
}
Expand Down
21 changes: 18 additions & 3 deletions SharpPcap/LibPcap/LibPcapSafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,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 @@ -111,7 +121,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
16 changes: 14 additions & 2 deletions SharpPcap/LibPcap/PcapHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,20 @@ namespace SharpPcap.LibPcap
/// </summary>
public class PcapHeader : ICaptureHeader
{
private static readonly bool isMacOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
private static readonly bool is32BitTs = IntPtr.Size == 4 || RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
private static readonly bool isMacOSX =
#if NET6_0_OR_GREATER
OperatingSystem.IsMacOS()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
#endif
;
private static readonly bool is32BitTs = IntPtr.Size == 4 ||
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
;

internal static readonly int MemorySize = GetTimevalSize() + sizeof(uint) + sizeof(uint);

Expand Down
8 changes: 7 additions & 1 deletion SharpPcap/LibPcap/PcapInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ internal PcapInterface(pcap_if pcapIf, NetworkInterface networkInterface, Remote
}
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
else if (
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
FriendlyName = WindowsNativeMethods.GetInterfaceAlias(Name);
}
Expand Down
8 changes: 7 additions & 1 deletion SharpPcap/Pcap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ static Pcap()
// FIXME: need to resolve the discrepency at some point
AF_PACKET = 17;

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (!
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
AF_INET6 = 10; // value for linux from socket.h
}
Expand Down
2 changes: 1 addition & 1 deletion SharpPcap/SharpPcap.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<Version>6.2.5</Version>
<Description>A packet capture framework for .NET</Description>
<Authors>Tamir Gal, Chris Morgan and others</Authors>
Expand Down
8 changes: 7 additions & 1 deletion SharpPcap/Statistics/StatisticsDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public class StatisticsDevice : IPcapDevice
{
private readonly LibPcapLiveDevice LiveDevice;

private static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
private static readonly bool IsWindows =
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
;

/// <summary>
/// Constructs a new PcapDevice based on a 'pcapIf' struct
Expand Down
8 changes: 7 additions & 1 deletion SharpPcap/Tunneling/TunnelDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public partial class TunnelDevice : BaseLiveDevice, ILiveDevice

private static ITunnelDriver GetDriver()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
return WinTapDriver.Instance;
}
Expand Down