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