-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathLibPcapSafeNativeMethods.Resolver.cs
115 lines (101 loc) · 3.28 KB
/
LibPcapSafeNativeMethods.Resolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
This file is part of SharpPcap.
SharpPcap is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SharpPcap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with SharpPcap. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright 2020-2021 Ayoub Kaanich <[email protected]>
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace SharpPcap.LibPcap
{
/// <summary>
/// Per http://msdn.microsoft.com/en-us/ms182161.aspx
/// </summary>
internal static partial class LibPcapSafeNativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetDllDirectory(string lpPathName);
static LibPcapSafeNativeMethods()
{
if (
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
SetDllDirectory(Path.Combine(Environment.SystemDirectory, "Npcap"));
}
else
{
RegisterResolver();
}
StringEncoding = ConfigureStringEncoding();
}
/// <summary>
/// The class NativeLibrary is only available since .NET Core 3.0
/// It's not available in .NET Framework,
/// but that does not affect us since we would use the Windows dll name wpcap
/// </summary>
private static void RegisterResolver()
{
NativeLibraryHelper.SetDllImportResolver(typeof(LibPcapSafeNativeMethods).Assembly, Resolver);
}
public static IntPtr Resolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
{
if (libraryName != PCAP_DLL)
{
// Use default resolver
return IntPtr.Zero;
}
var names = new List<string>();
if (
#if NET6_0_OR_GREATER
OperatingSystem.IsLinux()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
#endif
)
{
names.Add("libpcap.so");
names.Add("libpcap.so.0");
names.Add("libpcap.so.0.8");
names.Add("libpcap.so.1");
}
if (
#if NET6_0_OR_GREATER
OperatingSystem.IsMacOS()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
#endif
)
{
names.Add("libpcap.dylib");
}
foreach (var name in names)
{
if (NativeLibraryHelper.TryLoad(name, out var handle))
{
return handle;
}
}
return IntPtr.Zero;
}
}
}