From 77b006f64d756a13358af6dd5ba718293d72f919 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 23:31:34 +0300 Subject: [PATCH 1/3] Updated copy/move constructors to cpp11 standard. --- Pcap++/header/PcapRemoteDevice.h | 9 +++++---- Pcap++/header/PcapRemoteDeviceList.h | 8 +++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Pcap++/header/PcapRemoteDevice.h b/Pcap++/header/PcapRemoteDevice.h index 860a32788..8a97b9530 100644 --- a/Pcap++/header/PcapRemoteDevice.h +++ b/Pcap++/header/PcapRemoteDevice.h @@ -86,10 +86,6 @@ namespace pcpp // c'tor is private, as only PcapRemoteDeviceList should create instances of it, and it'll create only one for every remote interface PcapRemoteDevice(pcap_if_t* iface, PcapRemoteAuthentication* remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort); - // private copy c'tor - PcapRemoteDevice( const PcapRemoteDevice& other ); - // private assignment operator - PcapRemoteDevice& operator=(const PcapRemoteDevice& other); static void* remoteDeviceCaptureThreadMain(void *ptr); @@ -97,6 +93,11 @@ namespace pcpp ThreadStart getCaptureThreadStart(); public: + PcapRemoteDevice(const PcapRemoteDevice&) = delete; + PcapRemoteDevice(PcapRemoteDevice&&) noexcept = delete; + PcapRemoteDevice& operator=(const PcapRemoteDevice&) = delete; + PcapRemoteDevice& operator=(PcapRemoteDevice&&) noexcept = delete; + virtual ~PcapRemoteDevice() {} /** diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 904866c40..c20d5278e 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -34,9 +34,6 @@ namespace pcpp // private c'tor. User should create the list via static methods PcapRemoteDeviceList::getRemoteDeviceList() PcapRemoteDeviceList() : m_RemoteMachinePort(0), m_RemoteAuthentication(NULL) {} - // private copy c'tor - PcapRemoteDeviceList(const PcapRemoteDeviceList& other); - PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other); void setRemoteMachineIpAddress(const IPAddress& ipAddress); void setRemoteMachinePort(uint16_t port); @@ -53,6 +50,11 @@ namespace pcpp */ typedef typename std::vector::const_iterator ConstRemoteDeviceListIterator; + PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; + PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; + PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList&) = delete; + PcapRemoteDeviceList& operator=(PcapRemoteDeviceList&&) noexcept = delete; + ~PcapRemoteDeviceList(); /** From 70bb739849073f9c31e6e7a9dca3c586dc1a4427 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 23:39:54 +0300 Subject: [PATCH 2/3] Changed shared remote authentication structure to be managed by a shared pointer. --- Pcap++/header/PcapRemoteDevice.h | 6 +++--- Pcap++/header/PcapRemoteDeviceList.h | 5 +++-- Pcap++/src/PcapRemoteDevice.cpp | 8 ++++---- Pcap++/src/PcapRemoteDeviceList.cpp | 13 +++---------- 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/Pcap++/header/PcapRemoteDevice.h b/Pcap++/header/PcapRemoteDevice.h index 8a97b9530..339405571 100644 --- a/Pcap++/header/PcapRemoteDevice.h +++ b/Pcap++/header/PcapRemoteDevice.h @@ -3,6 +3,7 @@ #if defined(_WIN32) #include +#include #include "PcapLiveDevice.h" @@ -81,11 +82,10 @@ namespace pcpp private: IPAddress m_RemoteMachineIpAddress; uint16_t m_RemoteMachinePort; - PcapRemoteAuthentication* m_RemoteAuthentication; + std::shared_ptr m_RemoteAuthentication; // c'tor is private, as only PcapRemoteDeviceList should create instances of it, and it'll create only one for every remote interface - PcapRemoteDevice(pcap_if_t* iface, PcapRemoteAuthentication* remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort); - + PcapRemoteDevice(pcap_if_t* iface, std::shared_ptr remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort); static void* remoteDeviceCaptureThreadMain(void *ptr); diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index c20d5278e..4d9e09a3d 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -2,6 +2,7 @@ #if defined(_WIN32) +#include #include "IpAddress.h" #include "PcapRemoteDevice.h" @@ -30,10 +31,10 @@ namespace pcpp std::vector m_RemoteDeviceList; IPAddress m_RemoteMachineIpAddress; uint16_t m_RemoteMachinePort; - PcapRemoteAuthentication* m_RemoteAuthentication; + std::shared_ptr m_RemoteAuthentication; // private c'tor. User should create the list via static methods PcapRemoteDeviceList::getRemoteDeviceList() - PcapRemoteDeviceList() : m_RemoteMachinePort(0), m_RemoteAuthentication(NULL) {} + PcapRemoteDeviceList() : m_RemoteMachinePort(0), m_RemoteAuthentication(nullptr) {} void setRemoteMachineIpAddress(const IPAddress& ipAddress); void setRemoteMachinePort(uint16_t port); diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index ac1ae9c46..b681891fb 100644 --- a/Pcap++/src/PcapRemoteDevice.cpp +++ b/Pcap++/src/PcapRemoteDevice.cpp @@ -19,14 +19,14 @@ pcap_rmtauth PcapRemoteAuthentication::getPcapRmAuth() const return result; } -PcapRemoteDevice::PcapRemoteDevice(pcap_if_t* iface, PcapRemoteAuthentication* remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort) +PcapRemoteDevice::PcapRemoteDevice(pcap_if_t* iface, std::shared_ptr remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort) : PcapLiveDevice(iface, false, false, false) + , m_RemoteMachineIpAddress(remoteMachineIP) + , m_RemoteMachinePort(remoteMachinePort) + , m_RemoteAuthentication(std::move(remoteAuthentication)) { PCPP_LOG_DEBUG("MTU calculation isn't supported for remote devices. Setting MTU to 1514"); m_DeviceMtu = 1514; - m_RemoteMachineIpAddress = remoteMachineIP; - m_RemoteMachinePort = remoteMachinePort; - m_RemoteAuthentication = remoteAuthentication; } diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index af107fee9..a8b789189 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -179,13 +179,11 @@ void PcapRemoteDeviceList::setRemoteMachinePort(uint16_t port) void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth) { - if (remoteAuth != NULL) - m_RemoteAuthentication = new PcapRemoteAuthentication(*remoteAuth); + if (remoteAuth != nullptr) + m_RemoteAuthentication = std::shared_ptr(new PcapRemoteAuthentication(*remoteAuth)); else { - if (m_RemoteAuthentication != NULL) - delete m_RemoteAuthentication; - m_RemoteAuthentication = NULL; + m_RemoteAuthentication = nullptr; } } @@ -197,11 +195,6 @@ PcapRemoteDeviceList::~PcapRemoteDeviceList() delete (*devIter); m_RemoteDeviceList.erase(devIter); } - - if (m_RemoteAuthentication != NULL) - { - delete m_RemoteAuthentication; - } } } // namespace pcpp From 501bda62d7df49e0cfea988edf8ad8cdcf2162bf Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 13 Jun 2024 13:42:07 +0300 Subject: [PATCH 3/3] Changed null to nullptr. --- Pcap++/src/PcapRemoteDevice.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index b681891fb..6b48ab17d 100644 --- a/Pcap++/src/PcapRemoteDevice.cpp +++ b/Pcap++/src/PcapRemoteDevice.cpp @@ -35,16 +35,16 @@ bool PcapRemoteDevice::open() char errbuf[PCAP_ERRBUF_SIZE]; int flags = PCAP_OPENFLAG_PROMISCUOUS | PCAP_OPENFLAG_NOCAPTURE_RPCAP; //PCAP_OPENFLAG_DATATX_UDP doesn't always work PCPP_LOG_DEBUG("Opening device '" << m_Name << "'"); - pcap_rmtauth* pRmAuth = NULL; + pcap_rmtauth* pRmAuth = nullptr; pcap_rmtauth rmAuth; - if (m_RemoteAuthentication != NULL) + if (m_RemoteAuthentication != nullptr) { rmAuth = m_RemoteAuthentication->getPcapRmAuth(); pRmAuth = &rmAuth; } m_PcapDescriptor = pcap_open(m_Name.c_str(), PCPP_MAX_PACKET_SIZE, flags, 250, pRmAuth, errbuf); - if (m_PcapDescriptor == NULL) + if (m_PcapDescriptor == nullptr) { PCPP_LOG_ERROR("Error opening device. Error was: " << errbuf); m_DeviceOpened = false; @@ -73,7 +73,7 @@ bool PcapRemoteDevice::open() void* PcapRemoteDevice::remoteDeviceCaptureThreadMain(void *ptr) { PcapRemoteDevice* pThis = (PcapRemoteDevice*)ptr; - if (pThis == NULL) + if (pThis == nullptr) { PCPP_LOG_ERROR("Capture thread: Unable to extract PcapLiveDevice instance"); return 0;