-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathLibPcapSafeNativeMethods.cs
139 lines (127 loc) · 5.02 KB
/
LibPcapSafeNativeMethods.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
/*
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 2005 Tamir Gal <[email protected]>
* Copyright 2008-2009 Phillip Lemon <[email protected]>
* Copyright 2008-2011 Chris Morgan <[email protected]>
*/
using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using static SharpPcap.LibPcap.PcapUnmanagedStructures;
namespace SharpPcap.LibPcap
{
/// <summary>
/// Per http://msdn.microsoft.com/en-us/ms182161.aspx
/// </summary>
[SuppressUnmanagedCodeSecurity]
internal static partial class LibPcapSafeNativeMethods
{
internal static PcapError pcap_setbuff(PcapHandle /* pcap_t */ adapter, int bufferSizeInBytes)
{
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
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
? _pcap_setmintocopy(adapter, sizeInBytes)
: PcapError.PlatformNotSupported;
}
/// <summary>
/// pcap_set_rfmon() sets whether monitor mode should be set on a capture handle when the handle is activated.
/// If rfmon is non-zero, monitor mode will be set, otherwise it will not be set.
/// </summary>
/// <param name="p">A <see cref="IntPtr"/></param>
/// <param name="rfmon">A <see cref="int"/></param>
/// <returns>Returns 0 on success or PCAP_ERROR_ACTIVATED if called on a capture handle that has been activated.</returns>
internal static PcapError pcap_set_rfmon(PcapHandle /* pcap_t* */ p, int rfmon)
{
try
{
return _pcap_set_rfmon(p, rfmon);
}
catch (EntryPointNotFoundException)
{
return PcapError.RfmonNotSupported;
}
}
#region Timestamp related functions
private static readonly Version Libpcap_1_5 = new Version(1, 5, 0);
/// <summary>
/// Available since libpcap 1.5
/// </summary>
/// <param name="adapter"></param>
/// <param name="precision"></param>
/// <returns></returns>
internal static PcapError pcap_set_tstamp_precision(PcapHandle /* pcap_t* p */ adapter, int precision)
{
if (Pcap.LibpcapVersion < Libpcap_1_5)
{
return PcapError.TimestampPrecisionNotSupported;
}
return _pcap_set_tstamp_precision(adapter, precision);
}
/// <summary>
/// Available since libpcap 1.5
/// </summary>
/// <param name="adapter"></param>
internal static int pcap_get_tstamp_precision(PcapHandle /* pcap_t* p */ adapter)
{
if (Pcap.LibpcapVersion < Libpcap_1_5)
{
return (int)TimestampResolution.Microsecond;
}
return _pcap_get_tstamp_precision(adapter);
}
#endregion
/// <summary>
/// Wraps a Pcap handle around an existing file handle.
/// </summary>
/// <param name="handle">Native file handle. On non-Windows systems, this must be a pointer
/// to a C runtime <c>FILE</c> object.</param>
/// <param name="precision">Desired timestamp precision (micro/nano).</param>
/// <param name="errbuf">Buffer that will receive an error description if an error occurs.</param>
/// <returns></returns>
internal static PcapHandle pcap_open_handle_offline_with_tstamp_precision(
SafeHandle handle, uint precision, StringBuilder errbuf)
{
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)
{
return PcapHandle.Invalid;
}
return new PcapFileHandle(pointer, handle);
}
}
}