Skip to content

Commit 32384c2

Browse files
authored
Remove protected field m_DeviceOpened from IDevice interface. (#1961)
* Remove protected field `m_DeviceOpened` from `IDevice` interface. The C++ core guidelines recommend against usages of protected fields, due to the maintainability issues in large class hierarchies. As `IDevice` is the base interface of every device, it is better to let concrete implementations handle their own state detection than to force a boolean to be used everywhere. A `PCapLiveDevice` for instance, can instead use the state of its `PcapHandle` for state detection. * Add `isOpened` to XDP device. * Change IDevice destructor to `= default`.
1 parent 098dd4b commit 32384c2

File tree

9 files changed

+52
-11
lines changed

9 files changed

+52
-11
lines changed

Pcap++/header/Device.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,11 @@ namespace pcpp
1919
class IDevice
2020
{
2121
protected:
22-
bool m_DeviceOpened;
23-
2422
// c'tor should not be public
25-
IDevice() : m_DeviceOpened(false)
26-
{}
23+
IDevice() = default;
2724

2825
public:
29-
virtual ~IDevice()
30-
{}
26+
virtual ~IDevice() = default;
3127

3228
/// Open the device
3329
/// @return True if device was opened successfully, false otherwise
@@ -37,10 +33,7 @@ namespace pcpp
3733
virtual void close() = 0;
3834

3935
/// @return True if the file is opened, false otherwise
40-
inline bool isOpened()
41-
{
42-
return m_DeviceOpened;
43-
}
36+
virtual bool isOpened() const = 0;
4437
};
4538

4639
/// @class IFilterableDevice

Pcap++/header/DpdkDevice.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,11 @@ namespace pcpp
731731
/// Close the DpdkDevice. When device is closed it's not possible work with it
732732
void close() override;
733733

734+
bool isOpened() const override
735+
{
736+
return m_DeviceOpened;
737+
}
738+
734739
private:
735740
struct DpdkCoreConfiguration
736741
{
@@ -769,6 +774,8 @@ namespace pcpp
769774
uint64_t convertRssHfToDpdkRssHf(uint64_t rssHF) const;
770775
uint64_t convertDpdkRssHfToRssHf(uint64_t dpdkRssHF) const;
771776

777+
bool m_DeviceOpened = false;
778+
772779
std::string m_DeviceName;
773780
DpdkPMDType m_PMDType;
774781
std::string m_PMDName;

Pcap++/header/KniDevice.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,14 @@ namespace pcpp
524524
/// Stops asynchronous packet capture if it is running.
525525
void close();
526526

527+
bool isOpened() const override
528+
{
529+
return m_DeviceOpened;
530+
}
531+
527532
private:
533+
bool m_DeviceOpened = false;
534+
528535
struct rte_kni* m_Device;
529536
struct rte_mempool* m_MBufMempool;
530537
struct KniDeviceInfo

Pcap++/header/PcapFileDevice.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace pcpp
3939
class IFileDevice : public IPcapDevice
4040
{
4141
protected:
42+
bool m_DeviceOpened = false;
4243
std::string m_FileName;
4344

4445
explicit IFileDevice(const std::string& fileName);
@@ -53,6 +54,11 @@ namespace pcpp
5354
/// Close the file
5455
void close() override;
5556

57+
bool isOpened() const override
58+
{
59+
return m_DeviceOpened;
60+
}
61+
5662
/// @brief Get the statistics for this device.
5763
///
5864
/// The PcapStats structure will hold the following:

Pcap++/header/PcapLiveDevice.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ namespace pcpp
120120
std::thread m_WorkerThread;
121121
};
122122

123+
bool m_DeviceOpened = false;
124+
123125
// This is a second descriptor for the same device. It is needed because of a bug
124126
// that occurs in libpcap on Linux (on Windows using WinPcap/Npcap it works well):
125127
// It's impossible to capture packets sent by the same descriptor
@@ -653,6 +655,11 @@ namespace pcpp
653655

654656
void close() override;
655657

658+
bool isOpened() const override
659+
{
660+
return m_DeviceOpened;
661+
}
662+
656663
/// Clones the current device class
657664
/// @return Pointer to the copied class
658665
virtual PcapLiveDevice* clone() const;

Pcap++/header/PfRingDevice.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ namespace pcpp
5959
bool m_Ready = false;
6060
};
6161

62+
bool m_DeviceOpened = false;
63+
6264
std::vector<pfring*> m_PfRingDescriptors;
6365
std::string m_DeviceName;
6466
int m_InterfaceIndex;
@@ -308,6 +310,11 @@ namespace pcpp
308310
/// Closes all RX channels currently opened in device
309311
void close();
310312

313+
bool isOpened() const override
314+
{
315+
return m_DeviceOpened;
316+
}
317+
311318
using IFilterableDevice::setFilter;
312319

313320
/// Sets a BPF filter to the device

Pcap++/header/RawSocketDevice.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ namespace pcpp
124124
/// Close the raw socket
125125
void close() override;
126126

127+
bool isOpened() const override
128+
{
129+
return m_DeviceOpened;
130+
}
131+
127132
private:
128133
enum SocketFamily
129134
{
@@ -132,6 +137,8 @@ namespace pcpp
132137
IPv6 = 2
133138
};
134139

140+
bool m_DeviceOpened = false;
141+
135142
SocketFamily m_SockFamily;
136143
void* m_Socket;
137144
IPAddress m_InterfaceIP;

Pcap++/header/XdpDevice.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ namespace pcpp
179179
/// Close the device. This method closes the AF_XDP socket and frees the UMEM that was allocated for it.
180180
void close() override;
181181

182+
bool isOpened() const override
183+
{
184+
return m_DeviceOpened;
185+
}
186+
182187
/// Start receiving packets. In order to use this method the device should be open. Note that this method is
183188
/// blocking and will return if:
184189
/// - stopReceivePackets() was called from within the user callback
@@ -285,6 +290,8 @@ namespace pcpp
285290
uint64_t txCompletedPackets;
286291
};
287292

293+
bool m_DeviceOpened = false;
294+
288295
std::string m_InterfaceName;
289296
XdpDeviceConfiguration* m_Config;
290297
bool m_ReceivingPackets;

Pcap++/src/PcapDevice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ namespace pcpp
136136
bool IPcapDevice::setFilter(std::string filterAsString)
137137
{
138138
PCPP_LOG_DEBUG("Filter to be set: '" << filterAsString << "'");
139-
if (!m_DeviceOpened)
139+
if (!isOpened())
140140
{
141141
PCPP_LOG_ERROR("Device not Opened!! cannot set filter");
142142
return false;

0 commit comments

Comments
 (0)