-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathPcap.cs
127 lines (112 loc) · 3.91 KB
/
Pcap.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
/*
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 Chris Morgan <[email protected]>
* Copyright 2008-2009 Phillip Lemon <[email protected]>
*/
using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace SharpPcap
{
/// <summary>
/// Constants and static helper methods
/// </summary>
public class Pcap
{
/// <summary>Represents the infinite number for packet captures </summary>
internal const int InfinitePacketCount = -1;
/* interface is loopback */
internal const uint PCAP_IF_LOOPBACK = 0x00000001;
internal const int MAX_PACKET_SIZE = 65536;
internal const int PCAP_ERRBUF_SIZE = 256;
// Constants for address families
// These are set in a Pcap static initializer because the values
// differ between Windows and Linux
internal readonly static int AF_INET;
internal readonly static int AF_PACKET;
internal readonly static int AF_INET6;
// Constants for pcap loop exit status.
internal const int LOOP_USER_TERMINATED = -2;
internal const int LOOP_EXIT_WITH_ERROR = -1;
internal const int LOOP_COUNT_EXHAUSTED = 0;
/// <summary>
/// Returns the pcap version string retrieved via a call to pcap_lib_version()
/// </summary>
public static string Version
{
get
{
try
{
return LibPcap.LibPcapSafeNativeMethods.pcap_lib_version();
}
catch
{
return "Pcap version can't be identified. It is likely that pcap is not installed " +
"but you could be using a very old version.";
}
}
}
private static Version _libpcapVersion;
public static Version LibpcapVersion
{
get
{
_libpcapVersion = _libpcapVersion ?? GetLibpcapVersion(Version);
return _libpcapVersion;
}
}
public static Version SharpPcapVersion
{
get
{
return typeof(Pcap).Assembly.GetName().Version;
}
}
internal static Version GetLibpcapVersion(string version)
{
var regex = new Regex(@"libpcap version (\d+\.\d+(\.\d+)?)");
var match = regex.Match(version);
if (match.Success)
{
return new Version(match.Groups[1].Value);
}
return new Version();
}
static Pcap()
{
// happens to have the same value on Windows and Linux
AF_INET = 2;
// AF_PACKET = 17 on Linux, AF_NETBIOS = 17 on Windows
// FIXME: need to resolve the discrepency at some point
AF_PACKET = 17;
if (!
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
AF_INET6 = 10; // value for linux from socket.h
}
else
{
AF_INET6 = 23; // value for windows from winsock.h
}
}
}
}