@@ -178,6 +178,103 @@ namespace pcpp
178
178
void getStatistics (PcapStats& stats) const ;
179
179
};
180
180
181
+ // / @class PcapFileWriterDevice
182
+ // / A class for opening a pcap file for writing or create a new pcap file and write packets to it. This class adds
183
+ // / a unique capability that isn't supported in WinPcap and in older libpcap versions which is to open a pcap file
184
+ // / in append mode where packets are written at the end of the pcap file instead of running it over
185
+ class PcapFileWriterDevice : public IFileWriterDevice
186
+ {
187
+ private:
188
+ pcap_dumper_t * m_PcapDumpHandler;
189
+ LinkLayerType m_PcapLinkLayerType;
190
+ bool m_AppendMode;
191
+ FileTimestampPrecision m_Precision;
192
+ FILE* m_File;
193
+
194
+ // private copy c'tor
195
+ PcapFileWriterDevice (const PcapFileWriterDevice& other);
196
+ PcapFileWriterDevice& operator =(const PcapFileWriterDevice& other);
197
+
198
+ void closeFile ();
199
+
200
+ public:
201
+ // / A constructor for this class that gets the pcap full path file name to open for writing or create. Notice
202
+ // / that after calling this constructor the file isn't opened yet, so writing packets will fail. For opening the
203
+ // / file call open()
204
+ // / @param[in] fileName The full path of the file
205
+ // / @param[in] linkLayerType The link layer type all packet in this file will be based on. The default is
206
+ // / Ethernet
207
+ // / @param[in] nanosecondsPrecision A boolean indicating whether to write timestamps in nano-precision. If set
208
+ // / to false, timestamps will be written in micro-precision
209
+ PcapFileWriterDevice (const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET,
210
+ bool nanosecondsPrecision = false );
211
+
212
+ // / A destructor for this class
213
+ ~PcapFileWriterDevice ()
214
+ {
215
+ PcapFileWriterDevice::close ();
216
+ }
217
+
218
+ // / Write a RawPacket to the file. Before using this method please verify the file is opened using open(). This
219
+ // / method won't change the written packet
220
+ // / @param[in] packet A reference for an existing RawPcket to write to the file
221
+ // / @return True if a packet was written successfully. False will be returned if the file isn't opened
222
+ // / or if the packet link layer type is different than the one defined for the file
223
+ // / (in all cases, an error will be printed to log)
224
+ bool writePacket (RawPacket const & packet) override ;
225
+
226
+ // / Write multiple RawPacket to the file. Before using this method please verify the file is opened using
227
+ // / open(). This method won't change the written packets or the RawPacketVector instance
228
+ // / @param[in] packets A reference for an existing RawPcketVector, all of its packets will be written to the
229
+ // / file
230
+ // / @return True if all packets were written successfully to the file. False will be returned if the file isn't
231
+ // / opened (also, an error log will be printed) or if at least one of the packets wasn't written successfully to
232
+ // / the file
233
+ bool writePackets (const RawPacketVector& packets) override ;
234
+
235
+ // / @return The precision of the timestamps in the file.
236
+ FileTimestampPrecision getTimestampPrecision () const
237
+ {
238
+ return m_Precision;
239
+ }
240
+
241
+ // / A static method that checks if nano-second precision is supported in the current platform and environment
242
+ // / @return True if nano-second precision is supported, false otherwise
243
+ static bool isNanoSecondPrecisionSupported ();
244
+
245
+ // override methods
246
+
247
+ // / Open the file in a write mode. If file doesn't exist, it will be created. If it does exist it will be
248
+ // / overwritten, meaning all its current content will be deleted
249
+ // / @return True if file was opened/created successfully or if file is already opened. False if opening the file
250
+ // / failed for some reason (an error will be printed to log)
251
+ bool open () override ;
252
+
253
+ // / Same as open(), but enables to open the file in append mode in which packets will be appended to the file
254
+ // / instead of overwrite its current content. In append mode file must exist, otherwise opening will fail
255
+ // / @param[in] appendMode A boolean indicating whether to open the file in append mode or not. If set to false
256
+ // / this method will act exactly like open(). If set to true, file will be opened in append mode
257
+ // / @return True of managed to open the file successfully. In case appendMode is set to true, false will be
258
+ // / returned if file wasn't found or couldn't be read, if file type is not pcap, or if link type specified in
259
+ // / c'tor is different from current file link type. In case appendMode is set to false, please refer to open()
260
+ // / for return values
261
+ bool open (bool appendMode) override ;
262
+
263
+ // / Flush and close the pacp file
264
+ void close () override ;
265
+
266
+ // / Flush packets to disk.
267
+ void flush ();
268
+
269
+ // / Get statistics of packets written so far.
270
+ // / @param[out] stats The stats struct where stats are returned
271
+ void getStatistics (PcapStats& stats) const override ;
272
+
273
+ private:
274
+ bool openWrite ();
275
+ bool openAppend ();
276
+ };
277
+
181
278
// / @class SnoopFileReaderDevice
182
279
// / A class for opening a snoop file in read-only mode. This class enable to open the file and read all packets,
183
280
// / packet-by-packet
@@ -336,103 +433,6 @@ namespace pcpp
336
433
void close ();
337
434
};
338
435
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
- {
373
- PcapFileWriterDevice::close ();
374
- }
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
- {
396
- return m_Precision;
397
- }
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
436
// / @class PcapNgFileWriterDevice
437
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
438
// / class adds unique capabilities such as writing metadata attributes into the file header, adding comments per
0 commit comments