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