diff --git a/Pcap++/header/PcapRemoteDevice.h b/Pcap++/header/PcapRemoteDevice.h index 860a32788..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,15 +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); - - // private copy c'tor - PcapRemoteDevice( const PcapRemoteDevice& other ); - // private assignment operator - PcapRemoteDevice& operator=(const PcapRemoteDevice& other); + PcapRemoteDevice(pcap_if_t* iface, std::shared_ptr remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort); 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..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,13 +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) {} - // private copy c'tor - PcapRemoteDeviceList(const PcapRemoteDeviceList& other); - PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other); + PcapRemoteDeviceList() : m_RemoteMachinePort(0), m_RemoteAuthentication(nullptr) {} void setRemoteMachineIpAddress(const IPAddress& ipAddress); void setRemoteMachinePort(uint16_t port); @@ -53,6 +51,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(); /** diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index ac1ae9c46..6b48ab17d 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; } @@ -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; diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index e6ff31205..e852e6c29 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -202,13 +202,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; } } @@ -220,11 +218,6 @@ PcapRemoteDeviceList::~PcapRemoteDeviceList() delete (*devIter); m_RemoteDeviceList.erase(devIter); } - - if (m_RemoteAuthentication != NULL) - { - delete m_RemoteAuthentication; - } } } // namespace pcpp