-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathBpfProgram.cs
158 lines (142 loc) · 5.7 KB
/
BpfProgram.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
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 2010 Chris Morgan <[email protected]>
* Copyright 2021 Ayoub Kaanich <[email protected]>
*/
using Microsoft.Win32.SafeHandles;
using PacketDotNet;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace SharpPcap.LibPcap
{
public class BpfProgram : SafeHandleZeroOrMinusOneIsInvalid
{
// pcap_compile() in 1.8.0 and later is newly thread-safe
// 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 =
#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();
public static BpfProgram TryCreate(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
{
var bpfProgram = new BpfProgram();
int result;
// Compile the expressions
if (ThreadSafeCompile)
{
result = LibPcapSafeNativeMethods.pcap_compile(pcapHandle,
bpfProgram,
filter,
optimize,
netmask);
}
else
{
lock (SyncCompile)
{
result = LibPcapSafeNativeMethods.pcap_compile(pcapHandle,
bpfProgram,
filter,
optimize,
netmask);
}
}
if (result < 0)
{
// Don't use Dispose since we don't want pcap_freecode to be called here
Marshal.FreeHGlobal(bpfProgram.handle);
bpfProgram.SetHandle(IntPtr.Zero);
bpfProgram = null;
}
return bpfProgram;
}
public static BpfProgram Create(PcapHandle pcapHandle, string filter, int optimize = 1, uint netmask = 0)
{
var bpfProgram = TryCreate(pcapHandle, filter, optimize, netmask);
if (bpfProgram == null)
{
throw new PcapException(PcapDevice.GetLastError(pcapHandle));
}
return bpfProgram;
}
public static BpfProgram TryCreate(LinkLayers linktype, string filter, int optimize = 1, uint netmask = 0)
{
using (var handle = LibPcapSafeNativeMethods.pcap_open_dead((int)linktype, Pcap.MAX_PACKET_SIZE))
{
return TryCreate(handle, filter, optimize, netmask);
}
}
public static BpfProgram Create(LinkLayers linktype, string filter, int optimize = 1, uint netmask = 0)
{
using (var handle = LibPcapSafeNativeMethods.pcap_open_dead((int)linktype, Pcap.MAX_PACKET_SIZE))
{
return Create(handle, filter, optimize, netmask);
}
}
private BpfProgram()
: base(true)
{
var bpfProgram = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(PcapUnmanagedStructures.bpf_program)));
SetHandle(bpfProgram);
}
/// <summary>
/// Runs the program and returns if a given filter applies to the packet
/// </summary>
/// <param name="bpfProgram">
/// A <see cref="IntPtr"/>
/// </param>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public bool Matches(IntPtr header, IntPtr data)
{
var result = LibPcapSafeNativeMethods.pcap_offline_filter(this, header, data);
return result != 0;
}
/// <summary>
/// Runs the program and returns if a given filter applies to the packet
/// </summary>
/// <param name="bpfProgram">
/// A <see cref="IntPtr"/>
/// </param>
public bool Matches(ReadOnlySpan<byte> data)
{
var header = new PcapHeader(0, 0, (uint)data.Length, (uint)data.Length);
var hdrPtr = header.MarshalToIntPtr(TimestampResolution.Microsecond);
int result;
unsafe
{
fixed (byte* p_packet = data)
{
result = LibPcapSafeNativeMethods.pcap_offline_filter(this, hdrPtr, new IntPtr(p_packet));
}
}
Marshal.FreeHGlobal(hdrPtr);
return result != 0;
}
protected override bool ReleaseHandle()
{
LibPcapSafeNativeMethods.pcap_freecode(handle);
//Alocate an unmanaged buffer
Marshal.FreeHGlobal(handle);
return true;
}
}
}