/src/PcapPlusPlus/Pcap++/header/PcapFileDevice.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "PcapDevice.h" |
4 | | #include "RawPacket.h" |
5 | | #include <fstream> |
6 | | |
7 | | // forward declaration for structs and typedefs defined in pcap.h |
8 | | struct pcap_dumper; |
9 | | typedef struct pcap_dumper pcap_dumper_t; |
10 | | |
11 | | /// @file |
12 | | |
13 | | /// @namespace pcpp |
14 | | /// @brief The main namespace for the PcapPlusPlus lib |
15 | | namespace pcpp |
16 | | { |
17 | | namespace internal |
18 | | { |
19 | | /// @struct LightPcapNgHandle |
20 | | /// An opaque struct representing a handle for pcapng files. |
21 | | struct LightPcapNgHandle; |
22 | | } // namespace internal |
23 | | |
24 | | /// @enum FileTimestampPrecision |
25 | | /// An enumeration representing the precision of timestamps in a pcap file. |
26 | | /// The precision can be Unknown, Micro, or Nano. |
27 | | enum class FileTimestampPrecision : int8_t |
28 | | { |
29 | | /// Precision is unknown or not set/determined |
30 | | Unknown = -1, |
31 | | /// Precision is in microseconds. |
32 | | Microseconds = 0, |
33 | | /// Precision is in nanoseconds. |
34 | | Nanoseconds = 1 |
35 | | }; |
36 | | |
37 | | /// @class IFileDevice |
38 | | /// An abstract class (cannot be instantiated, has a private c'tor) which is the parent class for all file devices |
39 | | class IFileDevice : public IPcapDevice |
40 | | { |
41 | | protected: |
42 | | bool m_DeviceOpened = false; |
43 | | std::string m_FileName; |
44 | | |
45 | | explicit IFileDevice(const std::string& fileName); |
46 | | virtual ~IFileDevice(); |
47 | | |
48 | | public: |
49 | | /// @return The name of the file |
50 | | std::string getFileName() const; |
51 | | |
52 | | // override methods |
53 | | |
54 | | /// Close the file |
55 | | void close() override; |
56 | | |
57 | | bool isOpened() const override |
58 | 1.01k | { |
59 | 1.01k | return m_DeviceOpened; |
60 | 1.01k | } |
61 | | |
62 | | /// @brief Get the statistics for this device. |
63 | | /// |
64 | | /// The PcapStats structure will hold the following: |
65 | | /// - packetsRecv: Number of packets processed (read or written, depending on the device type) |
66 | | /// - packetsDrop: Number of packets dropped (not read or not written, depending on the device type) |
67 | | /// - packetsDropByInterface: Not supported for file devices, will always be 0 |
68 | | /// |
69 | | /// @param[out] stats The stats object to fill in. |
70 | | void getStatistics(PcapStats& stats) const override; |
71 | | |
72 | | protected: |
73 | | /// @brief Report that packets were processed (read or written, depending on the device type). |
74 | | /// @param numPackets The number of packets processed. Default is 1. |
75 | | void reportPacketProcessed(uint64_t numPackets = 1) |
76 | 213k | { |
77 | 213k | m_NumOfPacketsProcessed += numPackets; |
78 | 213k | } |
79 | | |
80 | | /// @brief Report that packets were dropped (not read or not written, depending on the device type). |
81 | | /// @param numPackets The number of packets dropped. Default is 1. |
82 | | void reportPacketDropped(uint64_t numPackets = 1) |
83 | 4.09k | { |
84 | 4.09k | m_NumOfPacketsDropped += numPackets; |
85 | 4.09k | } |
86 | | |
87 | | /// @brief Reset the internal statistic counters to zero. |
88 | | void resetStatisticCounters(); |
89 | | |
90 | | private: |
91 | | uint64_t m_NumOfPacketsProcessed = 0; |
92 | | uint64_t m_NumOfPacketsDropped = 0; |
93 | | }; |
94 | | |
95 | | /// @class IFileReaderDevice |
96 | | /// An abstract class (cannot be instantiated, has a private c'tor) which is the parent class for file reader |
97 | | /// devices |
98 | | class IFileReaderDevice : public IFileDevice |
99 | | { |
100 | | protected: |
101 | | /// A constructor for this class that gets the pcap full path file name to open. Notice that after calling this |
102 | | /// constructor the file isn't opened yet, so reading packets will fail. For opening the file call open() |
103 | | /// @param[in] fileName The full path of the file to read |
104 | | IFileReaderDevice(const std::string& fileName); |
105 | | |
106 | | public: |
107 | | /// A destructor for this class |
108 | | virtual ~IFileReaderDevice() = default; |
109 | | |
110 | | /// @return The file size in bytes |
111 | | uint64_t getFileSize() const; |
112 | | |
113 | | virtual bool getNextPacket(RawPacket& rawPacket) = 0; |
114 | | |
115 | | /// Read the next N packets into a raw packet vector |
116 | | /// @param[out] packetVec The raw packet vector to read packets into |
117 | | /// @param[in] numOfPacketsToRead Number of packets to read. If value <0 all remaining packets in the file will |
118 | | /// be read into the raw packet vector (this is the default value) |
119 | | /// @return The number of packets actually read |
120 | | int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1); |
121 | | |
122 | | /// A static method that creates an instance of the reader best fit to read the file. It decides by the file |
123 | | /// extension: for .pcapng files it returns an instance of PcapNgFileReaderDevice and for all other extensions |
124 | | /// it returns an instance of PcapFileReaderDevice |
125 | | /// @param[in] fileName The file name to open |
126 | | /// @return An instance of the reader to read the file. Notice you should free this instance when done using it |
127 | | static IFileReaderDevice* getReader(const std::string& fileName); |
128 | | }; |
129 | | |
130 | | /// @class IFileWriterDevice |
131 | | /// An abstract class (cannot be instantiated, has a private c'tor) which is the parent class for file writer |
132 | | /// devices |
133 | | class IFileWriterDevice : public IFileDevice |
134 | | { |
135 | | protected: |
136 | | IFileWriterDevice(const std::string& fileName); |
137 | | |
138 | | public: |
139 | | /// A destructor for this class |
140 | | virtual ~IFileWriterDevice() = default; |
141 | | |
142 | | virtual bool writePacket(RawPacket const& packet) = 0; |
143 | | |
144 | | virtual bool writePackets(const RawPacketVector& packets) = 0; |
145 | | |
146 | | using IFileDevice::open; |
147 | | virtual bool open(bool appendMode) = 0; |
148 | | }; |
149 | | |
150 | | /// @class PcapFileReaderDevice |
151 | | /// A class for opening a pcap file in read-only mode. This class enable to open the file and read all packets, |
152 | | /// packet-by-packet |
153 | | class PcapFileReaderDevice : public IFileReaderDevice |
154 | | { |
155 | | private: |
156 | | FileTimestampPrecision m_Precision; |
157 | | LinkLayerType m_PcapLinkLayerType; |
158 | | |
159 | | // private copy c'tor |
160 | | PcapFileReaderDevice(const PcapFileReaderDevice& other); |
161 | | PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other); |
162 | | |
163 | | public: |
164 | | /// A constructor for this class that gets the pcap full path file name to open. Notice that after calling this |
165 | | /// constructor the file isn't opened yet, so reading packets will fail. For opening the file call open() |
166 | | /// @param[in] fileName The full path of the file to read |
167 | | PcapFileReaderDevice(const std::string& fileName) |
168 | 4.62k | : IFileReaderDevice(fileName), m_Precision(FileTimestampPrecision::Unknown), |
169 | 4.62k | m_PcapLinkLayerType(LINKTYPE_ETHERNET) |
170 | 4.62k | {} |
171 | | |
172 | | /// A destructor for this class |
173 | | virtual ~PcapFileReaderDevice() = default; |
174 | | |
175 | | /// @return The link layer type of this file |
176 | | LinkLayerType getLinkLayerType() const |
177 | 0 | { |
178 | 0 | return m_PcapLinkLayerType; |
179 | 0 | } |
180 | | |
181 | | /// @return The precision of the timestamps in the file. If the platform supports nanosecond precision, this |
182 | | /// method will return nanoseconds even if the file has microseconds since libpcap scales timestamps before |
183 | | /// supply. Otherwise, it will return microseconds. |
184 | | FileTimestampPrecision getTimestampPrecision() const |
185 | 0 | { |
186 | 0 | return m_Precision; |
187 | 0 | } |
188 | | |
189 | | /// A static method that checks if nano-second precision is supported in the current platform and environment |
190 | | /// @return True if nano-second precision is supported, false otherwise |
191 | | static bool isNanoSecondPrecisionSupported(); |
192 | | |
193 | | // overridden methods |
194 | | |
195 | | /// Read the next packet from the file. Before using this method please verify the file is opened using open() |
196 | | /// @param[out] rawPacket A reference for an empty RawPacket where the packet will be written |
197 | | /// @return True if a packet was read successfully. False will be returned if the file isn't opened (also, an |
198 | | /// error log will be printed) or if reached end-of-file |
199 | | bool getNextPacket(RawPacket& rawPacket); |
200 | | |
201 | | /// Open the file name which path was specified in the constructor in a read-only mode |
202 | | /// @return True if file was opened successfully or if file is already opened. False if opening the file failed |
203 | | /// for some reason (for example: file path does not exist) |
204 | | bool open(); |
205 | | }; |
206 | | |
207 | | /// @class PcapFileWriterDevice |
208 | | /// A class for opening a pcap file for writing or create a new pcap file and write packets to it. This class adds |
209 | | /// a unique capability that isn't supported in WinPcap and in older libpcap versions which is to open a pcap file |
210 | | /// in append mode where packets are written at the end of the pcap file instead of running it over |
211 | | class PcapFileWriterDevice : public IFileWriterDevice |
212 | | { |
213 | | private: |
214 | | pcap_dumper_t* m_PcapDumpHandler; |
215 | | LinkLayerType m_PcapLinkLayerType; |
216 | | bool m_AppendMode; |
217 | | FileTimestampPrecision m_Precision; |
218 | | FILE* m_File; |
219 | | |
220 | | // private copy c'tor |
221 | | PcapFileWriterDevice(const PcapFileWriterDevice& other); |
222 | | PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other); |
223 | | |
224 | | void closeFile(); |
225 | | |
226 | | public: |
227 | | /// A constructor for this class that gets the pcap full path file name to open for writing or create. Notice |
228 | | /// that after calling this constructor the file isn't opened yet, so writing packets will fail. For opening the |
229 | | /// file call open() |
230 | | /// @param[in] fileName The full path of the file |
231 | | /// @param[in] linkLayerType The link layer type all packet in this file will be based on. The default is |
232 | | /// Ethernet |
233 | | /// @param[in] nanosecondsPrecision A boolean indicating whether to write timestamps in nano-precision. If set |
234 | | /// to false, timestamps will be written in micro-precision |
235 | | PcapFileWriterDevice(const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET, |
236 | | bool nanosecondsPrecision = false); |
237 | | |
238 | | /// A destructor for this class |
239 | | ~PcapFileWriterDevice() |
240 | 1.01k | { |
241 | 1.01k | PcapFileWriterDevice::close(); |
242 | 1.01k | } |
243 | | |
244 | | /// Write a RawPacket to the file. Before using this method please verify the file is opened using open(). This |
245 | | /// method won't change the written packet |
246 | | /// @param[in] packet A reference for an existing RawPcket to write to the file |
247 | | /// @return True if a packet was written successfully. False will be returned if the file isn't opened |
248 | | /// or if the packet link layer type is different than the one defined for the file |
249 | | /// (in all cases, an error will be printed to log) |
250 | | bool writePacket(RawPacket const& packet) override; |
251 | | |
252 | | /// Write multiple RawPacket to the file. Before using this method please verify the file is opened using |
253 | | /// open(). This method won't change the written packets or the RawPacketVector instance |
254 | | /// @param[in] packets A reference for an existing RawPcketVector, all of its packets will be written to the |
255 | | /// file |
256 | | /// @return True if all packets were written successfully to the file. False will be returned if the file isn't |
257 | | /// opened (also, an error log will be printed) or if at least one of the packets wasn't written successfully to |
258 | | /// the file |
259 | | bool writePackets(const RawPacketVector& packets) override; |
260 | | |
261 | | /// @return The precision of the timestamps in the file. |
262 | | FileTimestampPrecision getTimestampPrecision() const |
263 | 0 | { |
264 | 0 | return m_Precision; |
265 | 0 | } |
266 | | |
267 | | /// A static method that checks if nano-second precision is supported in the current platform and environment |
268 | | /// @return True if nano-second precision is supported, false otherwise |
269 | | static bool isNanoSecondPrecisionSupported(); |
270 | | |
271 | | // override methods |
272 | | |
273 | | /// Open the file in a write mode. If file doesn't exist, it will be created. If it does exist it will be |
274 | | /// overwritten, meaning all its current content will be deleted |
275 | | /// @return True if file was opened/created successfully or if file is already opened. False if opening the file |
276 | | /// failed for some reason (an error will be printed to log) |
277 | | bool open() override; |
278 | | |
279 | | /// Same as open(), but enables to open the file in append mode in which packets will be appended to the file |
280 | | /// instead of overwrite its current content. In append mode file must exist, otherwise opening will fail |
281 | | /// @param[in] appendMode A boolean indicating whether to open the file in append mode or not. If set to false |
282 | | /// this method will act exactly like open(). If set to true, file will be opened in append mode |
283 | | /// @return True of managed to open the file successfully. In case appendMode is set to true, false will be |
284 | | /// returned if file wasn't found or couldn't be read, if file type is not pcap, or if link type specified in |
285 | | /// c'tor is different from current file link type. In case appendMode is set to false, please refer to open() |
286 | | /// for return values |
287 | | bool open(bool appendMode) override; |
288 | | |
289 | | /// Flush and close the pacp file |
290 | | void close() override; |
291 | | |
292 | | /// Flush packets to disk. |
293 | | void flush(); |
294 | | |
295 | | private: |
296 | | bool openWrite(); |
297 | | bool openAppend(); |
298 | | }; |
299 | | |
300 | | /// @class PcapNgFileReaderDevice |
301 | | /// A class for opening a pcap-ng file in read-only mode. This class enable to open the file and read all packets, |
302 | | /// packet-by-packet |
303 | | class PcapNgFileReaderDevice : public IFileReaderDevice |
304 | | { |
305 | | private: |
306 | | internal::LightPcapNgHandle* m_LightPcapNg; |
307 | | BpfFilterWrapper m_BpfWrapper; |
308 | | |
309 | | // private copy c'tor |
310 | | PcapNgFileReaderDevice(const PcapNgFileReaderDevice& other); |
311 | | PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other); |
312 | | |
313 | | public: |
314 | | /// A constructor for this class that gets the pcap-ng full path file name to open. Notice that after calling |
315 | | /// this constructor the file isn't opened yet, so reading packets will fail. For opening the file call open() |
316 | | /// @param[in] fileName The full path of the file to read |
317 | | PcapNgFileReaderDevice(const std::string& fileName); |
318 | | |
319 | | /// A destructor for this class |
320 | | virtual ~PcapNgFileReaderDevice() |
321 | 8.54k | { |
322 | 8.54k | close(); |
323 | 8.54k | } |
324 | | |
325 | | /// The pcap-ng format allows storing metadata at the header of the file. Part of this metadata is a string |
326 | | /// specifying the operating system that was used for capturing the packets. This method reads this string from |
327 | | /// the metadata (if exists) and returns it |
328 | | /// @return The operating system string if exists, or an empty string otherwise |
329 | | std::string getOS() const; |
330 | | |
331 | | /// The pcap-ng format allows storing metadata at the header of the file. Part of this metadata is a string |
332 | | /// specifying the hardware that was used for capturing the packets. This method reads this string from the |
333 | | /// metadata (if exists) and returns it |
334 | | /// @return The hardware string if exists, or an empty string otherwise |
335 | | std::string getHardware() const; |
336 | | |
337 | | /// The pcap-ng format allows storing metadata at the header of the file. Part of this metadata is a string |
338 | | /// specifying the capture application that was used for capturing the packets. This method reads this string |
339 | | /// from the metadata (if exists) and returns it |
340 | | /// @return The capture application string if exists, or an empty string otherwise |
341 | | std::string getCaptureApplication() const; |
342 | | |
343 | | /// The pcap-ng format allows storing metadata at the header of the file. Part of this metadata is a string |
344 | | /// containing a user-defined comment (can be any string). This method reads this string from the metadata (if |
345 | | /// exists) and returns it |
346 | | /// @return The comment written inside the file if exists, or an empty string otherwise |
347 | | std::string getCaptureFileComment() const; |
348 | | |
349 | | /// The pcap-ng format allows storing a user-defined comment for every packet (besides the comment per-file). |
350 | | /// This method reads the next packet and the comment attached to it (if such comment exists), and returns them |
351 | | /// both |
352 | | /// @param[out] rawPacket A reference for an empty RawPacket where the packet will be written |
353 | | /// @param[out] packetComment The comment attached to the packet or an empty string if no comment exists |
354 | | /// @return True if a packet was read successfully. False will be returned if the file isn't opened (also, an |
355 | | /// error log will be printed) or if reached end-of-file |
356 | | bool getNextPacket(RawPacket& rawPacket, std::string& packetComment); |
357 | | |
358 | | // overridden methods |
359 | | |
360 | | /// Read the next packet from the file. Before using this method please verify the file is opened using open() |
361 | | /// @param[out] rawPacket A reference for an empty RawPacket where the packet will be written |
362 | | /// @return True if a packet was read successfully. False will be returned if the file isn't opened (also, an |
363 | | /// error log will be printed) or if reached end-of-file |
364 | | bool getNextPacket(RawPacket& rawPacket); |
365 | | |
366 | | /// Open the file name which path was specified in the constructor in a read-only mode |
367 | | /// @return True if file was opened successfully or if file is already opened. False if opening the file failed |
368 | | /// for some reason (for example: file path does not exist) |
369 | | bool open(); |
370 | | |
371 | | /// Set a filter for PcapNG reader device. Only packets that match the filter will be received |
372 | | /// @param[in] filterAsString The filter to be set in Berkeley Packet Filter (BPF) syntax |
373 | | /// (http://biot.com/capstats/bpf.html) |
374 | | /// @return True if filter set successfully, false otherwise |
375 | | bool setFilter(std::string filterAsString); |
376 | | |
377 | | /// Close the pacp-ng file |
378 | | void close(); |
379 | | }; |
380 | | |
381 | | /// @class PcapNgFileWriterDevice |
382 | | /// A class for opening a pcap-ng file for writing or creating a new pcap-ng file and write packets to it. This |
383 | | /// class adds unique capabilities such as writing metadata attributes into the file header, adding comments per |
384 | | /// packet and opening the file in append mode where packets are added to a file instead of overriding it. This |
385 | | /// capabilities are part of the pcap-ng standard but aren't supported in most tools and libraries |
386 | | class PcapNgFileWriterDevice : public IFileWriterDevice |
387 | | { |
388 | | private: |
389 | | internal::LightPcapNgHandle* m_LightPcapNg; |
390 | | int m_CompressionLevel; |
391 | | BpfFilterWrapper m_BpfWrapper; |
392 | | |
393 | | // private copy c'tor |
394 | | PcapNgFileWriterDevice(const PcapFileWriterDevice& other); |
395 | | PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other); |
396 | | |
397 | | public: |
398 | | /// A constructor for this class that gets the pcap-ng full path file name to open for writing or create. Notice |
399 | | /// that after calling this constructor the file isn't opened yet, so writing packets will fail. For opening the |
400 | | /// file call open() |
401 | | /// @param[in] fileName The full path of the file |
402 | | /// @param[in] compressionLevel The compression level to use when writing the file, use 0 to disable compression |
403 | | /// or 10 for max compression. Default is 0 |
404 | | PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0); |
405 | | |
406 | | /// A destructor for this class |
407 | | virtual ~PcapNgFileWriterDevice() |
408 | 3.83k | { |
409 | 3.83k | PcapNgFileWriterDevice::close(); |
410 | 3.83k | } |
411 | | |
412 | | /// The pcap-ng format allows adding a user-defined comment for each stored packet. This method writes a |
413 | | /// RawPacket to the file and adds a comment to it. Before using this method please verify the file is opened |
414 | | /// using open(). This method won't change the written packet or the input comment |
415 | | /// @param[in] packet A reference for an existing RawPcket to write to the file |
416 | | /// @param[in] comment The comment to be written for the packet. If this string is empty or null it will be |
417 | | /// ignored |
418 | | /// @return True if a packet was written successfully. False will be returned if the file isn't opened (an error |
419 | | /// will be printed to log) |
420 | | bool writePacket(RawPacket const& packet, const std::string& comment); |
421 | | |
422 | | // overridden methods |
423 | | |
424 | | /// Write a RawPacket to the file. Before using this method please verify the file is opened using open(). This |
425 | | /// method won't change the written packet |
426 | | /// @param[in] packet A reference for an existing RawPcket to write to the file |
427 | | /// @return True if a packet was written successfully. False will be returned if the file isn't opened (an error |
428 | | /// will be printed to log) |
429 | | bool writePacket(RawPacket const& packet) override; |
430 | | |
431 | | /// Write multiple RawPacket to the file. Before using this method please verify the file is opened using |
432 | | /// open(). This method won't change the written packets or the RawPacketVector instance |
433 | | /// @param[in] packets A reference for an existing RawPcketVector, all of its packets will be written to the |
434 | | /// file |
435 | | /// @return True if all packets were written successfully to the file. False will be returned if the file isn't |
436 | | /// opened (also, an error log will be printed) or if at least one of the packets wasn't written successfully to |
437 | | /// the file |
438 | | bool writePackets(const RawPacketVector& packets) override; |
439 | | |
440 | | /// Open the file in a write mode. If file doesn't exist, it will be created. If it does exist it will be |
441 | | /// overwritten, meaning all its current content will be deleted |
442 | | /// @return True if file was opened/created successfully or if file is already opened. False if opening the file |
443 | | /// failed for some reason (an error will be printed to log) |
444 | | bool open() override; |
445 | | |
446 | | /// Same as open(), but enables to open the file in append mode in which packets will be appended to the file |
447 | | /// instead of overwrite its current content. In append mode file must exist, otherwise opening will fail |
448 | | /// @param[in] appendMode A boolean indicating whether to open the file in append mode or not. If set to false |
449 | | /// this method will act exactly like open(). If set to true, file will be opened in append mode |
450 | | /// @return True of managed to open the file successfully. In case appendMode is set to true, false will be |
451 | | /// returned if file wasn't found or couldn't be read, if file type is not pcap-ng. In case appendMode is set to |
452 | | /// false, please refer to open() for return values |
453 | | bool open(bool appendMode) override; |
454 | | |
455 | | /// Open the file in a write mode. If file doesn't exist, it will be created. If it does exist it will be |
456 | | /// overwritten, meaning all its current content will be deleted. As opposed to open(), this method also allows |
457 | | /// writing several metadata attributes that will be stored in the header of the file |
458 | | /// @param[in] os A string describing the operating system that was used to capture the packets. If this string |
459 | | /// is empty or null it will be ignored |
460 | | /// @param[in] hardware A string describing the hardware that was used to capture the packets. If this string is |
461 | | /// empty or null it will be ignored |
462 | | /// @param[in] captureApp A string describing the application that was used to capture the packets. If this |
463 | | /// string is empty or null it will be ignored |
464 | | /// @param[in] fileComment A string containing a user-defined comment that will be part of the metadata of the |
465 | | /// file. If this string is empty or null it will be ignored |
466 | | /// @return True if file was opened/created successfully or if file is already opened. False if opening the file |
467 | | /// failed for some reason (an error will be printed to log) |
468 | | bool open(const std::string& os, const std::string& hardware, const std::string& captureApp, |
469 | | const std::string& fileComment); |
470 | | |
471 | | /// Flush packets to the pcap-ng file |
472 | | void flush(); |
473 | | |
474 | | /// Flush and close the pcap-ng file |
475 | | void close() override; |
476 | | |
477 | | /// Set a filter for PcapNG writer device. Only packets that match the filter will be persisted |
478 | | /// @param[in] filterAsString The filter to be set in Berkeley Packet Filter (BPF) syntax |
479 | | /// (http://biot.com/capstats/bpf.html) |
480 | | /// @return True if filter set successfully, false otherwise |
481 | | bool setFilter(std::string filterAsString) override; |
482 | | |
483 | | private: |
484 | | /// @struct PcapNgMetadata |
485 | | /// @brief A struct for holding the metadata of a pcap-ng file. The metadata includes the operating system, |
486 | | /// hardware, capture application and file comment. |
487 | | struct PcapNgMetadata |
488 | | { |
489 | | /// The operating system that was used for capturing the packets |
490 | | std::string os; |
491 | | /// The hardware that was used for capturing the packets |
492 | | std::string hardware; |
493 | | /// The capture application that was used for capturing the packets |
494 | | std::string captureApplication; |
495 | | /// The comment that was written inside the file |
496 | | std::string comment; |
497 | | }; |
498 | | |
499 | | bool openWrite(PcapNgMetadata const* metadata = nullptr); |
500 | | bool openAppend(); |
501 | | }; |
502 | | |
503 | | /// @class SnoopFileReaderDevice |
504 | | /// A class for opening a snoop file in read-only mode. This class enable to open the file and read all packets, |
505 | | /// packet-by-packet |
506 | | class SnoopFileReaderDevice : public IFileReaderDevice |
507 | | { |
508 | | private: |
509 | | #pragma pack(1) |
510 | | /// File format header. |
511 | | typedef struct |
512 | | { |
513 | | uint64_t identification_pattern; |
514 | | uint32_t version_number; |
515 | | uint32_t datalink_type; |
516 | | } snoop_file_header_t; |
517 | | |
518 | | /// Packet record header. |
519 | | typedef struct |
520 | | { |
521 | | uint32_t original_length; ///< original packet length |
522 | | uint32_t included_length; ///< saved packet length |
523 | | uint32_t packet_record_length; ///< total record length |
524 | | uint32_t ndrops_cumulative; ///< cumulative drops |
525 | | uint32_t time_sec; ///< timestamp |
526 | | uint32_t time_usec; ///< microsecond timestamp |
527 | | } snoop_packet_header_t; |
528 | | #pragma pack() |
529 | | |
530 | | LinkLayerType m_PcapLinkLayerType; |
531 | | std::ifstream m_snoopFile; |
532 | | |
533 | | // private copy c'tor |
534 | | SnoopFileReaderDevice(const PcapFileReaderDevice& other); |
535 | | SnoopFileReaderDevice& operator=(const PcapFileReaderDevice& other); |
536 | | |
537 | | public: |
538 | | /// A constructor for this class that gets the snoop full path file name to open. Notice that after calling this |
539 | | /// constructor the file isn't opened yet, so reading packets will fail. For opening the file call open() |
540 | | /// @param[in] fileName The full path of the file to read |
541 | | SnoopFileReaderDevice(const std::string& fileName) |
542 | 212 | : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET) |
543 | 212 | {} |
544 | | |
545 | | /// A destructor for this class |
546 | | virtual ~SnoopFileReaderDevice(); |
547 | | |
548 | | /// @return The link layer type of this file |
549 | | LinkLayerType getLinkLayerType() const |
550 | 0 | { |
551 | 0 | return m_PcapLinkLayerType; |
552 | 0 | } |
553 | | |
554 | | // overridden methods |
555 | | |
556 | | /// Read the next packet from the file. Before using this method please verify the file is opened using open() |
557 | | /// @param[out] rawPacket A reference for an empty RawPacket where the packet will be written |
558 | | /// @return True if a packet was read successfully. False will be returned if the file isn't opened (also, an |
559 | | /// error log will be printed) or if reached end-of-file |
560 | | bool getNextPacket(RawPacket& rawPacket); |
561 | | |
562 | | /// Open the file name which path was specified in the constructor in a read-only mode |
563 | | /// @return True if file was opened successfully or if file is already opened. False if opening the file failed |
564 | | /// for some reason (for example: file path does not exist) |
565 | | bool open(); |
566 | | |
567 | | /// Close the snoop file |
568 | | void close(); |
569 | | }; |
570 | | } // namespace pcpp |