-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathPcapHeader.cs
180 lines (164 loc) · 6.23 KB
/
PcapHeader.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
175
176
177
178
179
180
/*
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]>
*/
using System;
using System.Runtime.InteropServices;
using static SharpPcap.LibPcap.PcapUnmanagedStructures;
namespace SharpPcap.LibPcap
{
/// <summary>
/// A wrapper for libpcap's pcap_pkthdr structure
/// </summary>
public class PcapHeader : ICaptureHeader
{
private static readonly bool isMacOSX =
#if NET6_0_OR_GREATER
OperatingSystem.IsMacOS()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
#endif
;
private static readonly bool is32BitTs = IntPtr.Size == 4 ||
#if NET6_0_OR_GREATER
OperatingSystem.IsWindows()
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
;
internal static readonly int MemorySize = GetTimevalSize() + sizeof(uint) + sizeof(uint);
private static readonly int CaptureLengthOffset = GetTimevalSize();
private static readonly int PacketLengthOffset = GetTimevalSize() + sizeof(uint);
private static int GetTimevalSize()
{
if (is32BitTs)
{
return Marshal.SizeOf<timeval_windows>();
}
if (isMacOSX)
{
return Marshal.SizeOf<timeval_macosx>();
}
return Marshal.SizeOf<timeval_unix>();
}
/// <summary>
/// A wrapper class for libpcap's pcap_pkthdr structure
/// </summary>
private unsafe PcapHeader(IntPtr pcap_pkthdr, TimestampResolution resolution)
{
ulong tv_sec;
ulong tv_usec;
if (is32BitTs)
{
var ts = *(timeval_windows*)pcap_pkthdr;
tv_sec = (ulong)ts.tv_sec;
tv_usec = (ulong)ts.tv_usec;
}
else if (isMacOSX)
{
var ts = *(timeval_macosx*)pcap_pkthdr;
tv_sec = (ulong)ts.tv_sec;
tv_usec = (ulong)ts.tv_usec;
}
else
{
var ts = *(timeval_unix*)pcap_pkthdr;
tv_sec = (ulong)ts.tv_sec;
tv_usec = (ulong)ts.tv_usec;
}
Timeval = new PosixTimeval(tv_sec, tv_usec, resolution);
CaptureLength = (uint)Marshal.ReadInt32(pcap_pkthdr + CaptureLengthOffset);
PacketLength = (uint)Marshal.ReadInt32(pcap_pkthdr + PacketLengthOffset);
}
/// <summary>
/// Get a PcapHeader structure from a pcap_pkthdr pointer.
/// </summary>
public unsafe static PcapHeader FromPointer(IntPtr pcap_pkthdr, TimestampResolution resolution)
{
return new PcapHeader(pcap_pkthdr, resolution);
}
/// <summary>
/// Constructs a new PcapHeader
/// </summary>
/// <param name="seconds">The seconds value of the packet's timestamp</param>
/// <param name="microseconds">The microseconds value of the packet's timestamp</param>
/// <param name="packetLength">The actual length of the packet</param>
/// <param name="captureLength">The length of the capture</param>
public PcapHeader(uint seconds, uint microseconds, uint packetLength, uint captureLength)
{
this.Timeval = new PosixTimeval(seconds, microseconds);
this.PacketLength = packetLength;
this.CaptureLength = captureLength;
}
/// <summary>
/// The length of the packet on the line
/// </summary>
public uint PacketLength { get; set; }
/// <summary>
/// The the bytes actually captured. If the capture length
/// is small CaptureLength might be less than PacketLength
/// </summary>
public uint CaptureLength { get; set; }
public PosixTimeval Timeval { get; set; }
/// <summary>
/// Marshal this structure into the platform dependent version and return
/// and IntPtr to that memory
///
/// NOTE: IntPtr MUST BE FREED via Marshal.FreeHGlobal()
/// </summary>
/// <returns>
/// A <see cref="IntPtr"/>
/// </returns>
public IntPtr MarshalToIntPtr(TimestampResolution resolution)
{
var hdrPtr = Marshal.AllocHGlobal(MemorySize);
var tv_sec = Timeval.Seconds;
var unit = resolution == TimestampResolution.Nanosecond ? 1e9M : 1e6M;
var tv_usec = (ulong)((Timeval.Value % 1) * unit);
if (is32BitTs)
{
// setup the structure to marshal
var timeval = new timeval_windows
{
tv_sec = (int)tv_sec,
tv_usec = (int)tv_usec
};
Marshal.StructureToPtr(timeval, hdrPtr, true);
}
else if (isMacOSX)
{
var timeval = new timeval_macosx
{
tv_sec = (IntPtr)tv_sec,
tv_usec = (int)tv_usec
};
Marshal.StructureToPtr(timeval, hdrPtr, true);
}
else
{
var timeval = new timeval_unix
{
tv_sec = (IntPtr)tv_sec,
tv_usec = (IntPtr)tv_usec
};
Marshal.StructureToPtr(timeval, hdrPtr, true);
}
Marshal.WriteInt32(hdrPtr + CaptureLengthOffset, (int)CaptureLength);
Marshal.WriteInt32(hdrPtr + PacketLengthOffset, (int)PacketLength);
return hdrPtr;
}
}
}