-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathStatisticsDevice.cs
175 lines (149 loc) · 5.08 KB
/
StatisticsDevice.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
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-2011 Chris Morgan <[email protected]>
*/
using System;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using PacketDotNet;
using SharpPcap.LibPcap;
namespace SharpPcap.Statistics
{
/// <summary>
/// Npcap device
/// </summary>
public class StatisticsDevice : IPcapDevice
{
private readonly LibPcapLiveDevice LiveDevice;
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
/// </summary>
/// <param name="pcapIf">A 'pcapIf' struct representing
/// the pcap device</param>
public StatisticsDevice(PcapInterface pcapIf)
{
LiveDevice = new LibPcapLiveDevice(pcapIf);
}
/// <summary>
/// Fires whenever a new pcap statistics is available for this Pcap Device.<br/>
/// For network captured packets this event is invoked only when working in "PcapMode.Statistics" mode.
/// </summary>
public event EventHandler<StatisticsEventArgs> OnPcapStatistics;
/// <summary>
/// Starts the capturing process via a background thread
/// OnPcapStatistics() will be called for each statistics update
/// </summary>
public void StartCapture()
{
PrepareCapture();
LiveDevice.StartCapture();
}
public void Capture()
{
PrepareCapture();
LiveDevice.Capture();
}
public void Capture(int packetCount)
{
PrepareCapture();
LiveDevice.Capture(packetCount);
}
private void PrepareCapture()
{
if (OnPcapStatistics == null)
{
throw new DeviceNotReadyException("No delegates assigned to OnPcapStatistics, no where for captured packets to go.");
}
ReceivedPackets = 0;
ReceivedBytes = 0;
// prevent handler from being added twice
LiveDevice.OnPacketArrival -= LiveDevice_OnPacketArrival;
LiveDevice.OnPacketArrival += LiveDevice_OnPacketArrival;
}
public void StopCapture()
{
LiveDevice.OnPacketArrival -= LiveDevice_OnPacketArrival;
LiveDevice.StopCapture();
}
long ReceivedPackets;
long ReceivedBytes;
private void LiveDevice_OnPacketArrival(object sender, PacketCapture e)
{
var packet = e.GetPacket();
if (IsWindows)
{
ReceivedPackets += BitConverter.ToInt64(packet.Data, 0);
ReceivedBytes += BitConverter.ToInt64(packet.Data, 8);
}
else
{
ReceivedPackets++;
ReceivedBytes += packet.PacketLength;
}
var args = new StatisticsEventArgs(
this,
packet.Timeval,
ReceivedPackets,
ReceivedBytes
);
OnPcapStatistics?.Invoke(this, args);
}
public string Name => LiveDevice.Name;
public string Description => LiveDevice.Description;
public string LastError => LiveDevice.LastError;
public string Filter
{
get => LiveDevice.Filter;
set => LiveDevice.Filter = value;
}
/// <summary>
/// Retrieves pcap statistics
/// </summary>
/// <returns>
/// A <see cref="PcapStatistics"/>
/// </returns>
public ICaptureStatistics Statistics => LiveDevice.Statistics;
public PhysicalAddress MacAddress => LiveDevice.MacAddress;
public LinkLayers LinkType => LiveDevice.LinkType;
public void Open(DeviceConfiguration configuration)
{
LiveDevice.Open(configuration);
if (IsWindows)
{
LibPcapSafeNativeMethods.pcap_setmode(LiveDevice.Handle, (int)CaptureMode.Statistics);
}
}
/// <summary>
/// Close the device
/// </summary>
public void Close()
{
OnPcapStatistics = null;
StopCapture();
LiveDevice.Close();
}
public void Dispose()
{
Close();
}
}
}