44#include " RawPacket.h"
55#include < fstream>
66
7- // forward declaration for structs and typedefs defined in pcap.h
8- struct pcap_dumper ;
9- typedef struct pcap_dumper pcap_dumper_t ;
10-
117// / @file
128
139// / @namespace pcpp
@@ -36,29 +32,22 @@ namespace pcpp
3632
3733 // / @class IFileDevice
3834 // / 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
35+ class IFileDevice : public IFilterableDevice , public IPcapStatisticsProvider
4036 {
4137 protected:
42- bool m_DeviceOpened = false ;
4338 std::string m_FileName;
39+ BpfFilterWrapper m_BpfWrapper;
4440
4541 explicit IFileDevice (const std::string& fileName);
46- virtual ~IFileDevice ();
42+
43+ bool doUpdateFilter (std::string const * filterAsString) override ;
4744
4845 public:
4946 // / @return The name of the file
5047 std::string getFileName () const ;
5148
5249 // override methods
5350
54- // / Close the file
55- void close () override ;
56-
57- bool isOpened () const override
58- {
59- return m_DeviceOpened;
60- }
61-
6251 // / @brief Get the statistics for this device.
6352 // /
6453 // / The PcapStats structure will hold the following:
@@ -105,7 +94,7 @@ namespace pcpp
10594
10695 public:
10796 // / A destructor for this class
108- virtual ~IFileReaderDevice () = default ;
97+ ~IFileReaderDevice () override = default ;
10998
11099 // / @return The file size in bytes
111100 uint64_t getFileSize () const ;
@@ -137,7 +126,7 @@ namespace pcpp
137126
138127 public:
139128 // / A destructor for this class
140- virtual ~IFileWriterDevice () = default ;
129+ ~IFileWriterDevice () override = default ;
141130
142131 virtual bool writePacket (RawPacket const & packet) = 0;
143132
@@ -194,6 +183,12 @@ namespace pcpp
194183
195184 // overridden methods
196185
186+ // / @return True if the file is opened, false otherwise
187+ bool isOpened () const override
188+ {
189+ return m_PcapFile.is_open ();
190+ }
191+
197192 // / Read the next packet from the file. Before using this method please verify the file is opened using open()
198193 // / @param[out] rawPacket A reference for an empty RawPacket where the packet will be written
199194 // / @return True if a packet was read successfully. False will be returned if the file isn't opened (also, an
@@ -208,16 +203,12 @@ namespace pcpp
208203 // / Close the pacp file
209204 void close () override ;
210205
211- protected:
212- bool doUpdateFilter (std::string const * filterAsString) override ;
213-
214206 private:
215207 FileTimestampPrecision m_Precision = FileTimestampPrecision::Unknown;
216208 LinkLayerType m_PcapLinkLayerType = LINKTYPE_ETHERNET;
217209 std::ifstream m_PcapFile;
218210 bool m_NeedsSwap = false ;
219211 uint32_t m_SnapshotLength = 0 ;
220- BpfFilterWrapper m_BpfWrapper;
221212
222213 bool readNextPacket (timespec& packetTimestamp, uint8_t * packetData, uint32_t packetDataLen,
223214 uint32_t & capturedLength, uint32_t & frameLength);
@@ -300,21 +291,23 @@ namespace pcpp
300291 // / for return values
301292 bool open (bool appendMode) override ;
302293
294+ // / @return True if the file is opened, false otherwise
295+ bool isOpened () const override
296+ {
297+ return m_PcapFile.is_open ();
298+ }
299+
303300 // / Flush and close the pacp file
304301 void close () override ;
305302
306303 // / Flush packets to disk.
307304 void flush ();
308305
309- protected:
310- bool doUpdateFilter (std::string const * filterAsString) override ;
311-
312306 private:
313307 LinkLayerType m_PcapLinkLayerType = LINKTYPE_ETHERNET;
314308 bool m_NeedsSwap = false ;
315309 FileTimestampPrecision m_Precision = FileTimestampPrecision::Unknown;
316310 std::fstream m_PcapFile;
317- BpfFilterWrapper m_BpfWrapper;
318311
319312 struct CheckHeaderResult
320313 {
@@ -358,11 +351,6 @@ namespace pcpp
358351 {
359352 private:
360353 internal::LightPcapNgHandle* m_LightPcapNg;
361- BpfFilterWrapper m_BpfWrapper;
362-
363- // private copy c'tor
364- PcapNgFileReaderDevice (const PcapNgFileReaderDevice& other);
365- PcapNgFileReaderDevice& operator =(const PcapNgFileReaderDevice& other);
366354
367355 public:
368356 // / A constructor for this class that gets the pcap-ng full path file name to open. Notice that after calling
@@ -371,11 +359,14 @@ namespace pcpp
371359 PcapNgFileReaderDevice (const std::string& fileName);
372360
373361 // / A destructor for this class
374- virtual ~PcapNgFileReaderDevice ()
362+ ~PcapNgFileReaderDevice () override
375363 {
376- close ();
364+ PcapNgFileReaderDevice:: close ();
377365 }
378366
367+ PcapNgFileReaderDevice (const PcapNgFileReaderDevice& other) = delete ;
368+ PcapNgFileReaderDevice& operator =(const PcapNgFileReaderDevice& other) = delete ;
369+
379370 // / The pcap-ng format allows storing metadata at the header of the file. Part of this metadata is a string
380371 // / specifying the operating system that was used for capturing the packets. This method reads this string from
381372 // / the metadata (if exists) and returns it
@@ -422,11 +413,14 @@ namespace pcpp
422413 // / for some reason (for example: file path does not exist)
423414 bool open () override ;
424415
416+ // / @return True if the file is opened, false otherwise
417+ bool isOpened () const override
418+ {
419+ return m_LightPcapNg != nullptr ;
420+ }
421+
425422 // / Close the pacp-ng file
426423 void close () override ;
427-
428- protected:
429- bool doUpdateFilter (std::string const * filter) override ;
430424 };
431425
432426 // / @class PcapNgFileWriterDevice
@@ -439,11 +433,6 @@ namespace pcpp
439433 private:
440434 internal::LightPcapNgHandle* m_LightPcapNg;
441435 int m_CompressionLevel;
442- BpfFilterWrapper m_BpfWrapper;
443-
444- // private copy c'tor
445- PcapNgFileWriterDevice (const PcapFileWriterDevice& other);
446- PcapNgFileWriterDevice& operator =(const PcapNgFileWriterDevice& other);
447436
448437 public:
449438 // / A constructor for this class that gets the pcap-ng full path file name to open for writing or create. Notice
@@ -455,11 +444,14 @@ namespace pcpp
455444 PcapNgFileWriterDevice (const std::string& fileName, int compressionLevel = 0 );
456445
457446 // / A destructor for this class
458- virtual ~PcapNgFileWriterDevice ()
447+ ~PcapNgFileWriterDevice () override
459448 {
460449 PcapNgFileWriterDevice::close ();
461450 }
462451
452+ PcapNgFileWriterDevice (const PcapFileWriterDevice& other) = delete ;
453+ PcapNgFileWriterDevice& operator =(const PcapNgFileWriterDevice& other) = delete ;
454+
463455 // / The pcap-ng format allows adding a user-defined comment for each stored packet. This method writes a
464456 // / RawPacket to the file and adds a comment to it. Before using this method please verify the file is opened
465457 // / using open(). This method won't change the written packet or the input comment
@@ -519,15 +511,18 @@ namespace pcpp
519511 bool open (const std::string& os, const std::string& hardware, const std::string& captureApp,
520512 const std::string& fileComment);
521513
514+ // / @return True if the file is opened, false otherwise
515+ bool isOpened () const override
516+ {
517+ return m_LightPcapNg != nullptr ;
518+ }
519+
522520 // / Flush packets to the pcap-ng file
523521 void flush ();
524522
525523 // / Flush and close the pcap-ng file
526524 void close () override ;
527525
528- protected:
529- bool doUpdateFilter (std::string const * filterAsString) override ;
530-
531526 private:
532527 // / @struct PcapNgMetadata
533528 // / @brief A struct for holding the metadata of a pcap-ng file. The metadata includes the operating system,
@@ -578,9 +573,8 @@ namespace pcpp
578573 LinkLayerType m_PcapLinkLayerType;
579574 std::ifstream m_snoopFile;
580575
581- // private copy c'tor
582- SnoopFileReaderDevice (const PcapFileReaderDevice& other);
583- SnoopFileReaderDevice& operator =(const PcapFileReaderDevice& other);
576+ bool readNextPacket (timespec& packetTimestamp, uint8_t * packetData, uint32_t packetDataLen,
577+ uint32_t & capturedLength);
584578
585579 public:
586580 // / A constructor for this class that gets the snoop full path file name to open. Notice that after calling this
@@ -591,7 +585,10 @@ namespace pcpp
591585 {}
592586
593587 // / A destructor for this class
594- virtual ~SnoopFileReaderDevice ();
588+ ~SnoopFileReaderDevice () override ;
589+
590+ SnoopFileReaderDevice (const PcapFileReaderDevice& other) = delete ;
591+ SnoopFileReaderDevice& operator =(const PcapFileReaderDevice& other) = delete ;
595592
596593 // / @return The link layer type of this file
597594 LinkLayerType getLinkLayerType () const
@@ -605,14 +602,20 @@ namespace pcpp
605602 // / @param[out] rawPacket A reference for an empty RawPacket where the packet will be written
606603 // / @return True if a packet was read successfully. False will be returned if the file isn't opened (also, an
607604 // / error log will be printed) or if reached end-of-file
608- bool getNextPacket (RawPacket& rawPacket);
605+ bool getNextPacket (RawPacket& rawPacket) override ;
609606
610607 // / Open the file name which path was specified in the constructor in a read-only mode
611608 // / @return True if file was opened successfully or if file is already opened. False if opening the file failed
612609 // / for some reason (for example: file path does not exist)
613- bool open ();
610+ bool open () override ;
611+
612+ // / @return True if the file is opened, false otherwise
613+ bool isOpened () const override
614+ {
615+ return m_snoopFile.is_open ();
616+ }
614617
615618 // / Close the snoop file
616- void close ();
619+ void close () override ;
617620 };
618621} // namespace pcpp
0 commit comments