From 5d10a8981ee6cea427db7ec38acfb6af565c49a2 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 6 May 2024 15:10:26 +0300 Subject: [PATCH 01/81] Replaced the container of owning raw pointers of PcapDevices in PcapDeviceList with smart pointers. Added a secondary vector of non-owning pointers to the devices to keep the signature of some getter methods as they return references. --- Pcap++/header/PcapLiveDeviceList.h | 12 +++++--- Pcap++/src/PcapLiveDeviceList.cpp | 49 ++++++++++++++++++------------ 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 22d3b02c55..fb8280342c 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -3,6 +3,7 @@ #include "IpAddress.h" #include "PcapLiveDevice.h" #include +#include /// @file @@ -23,7 +24,9 @@ namespace pcpp class PcapLiveDeviceList { private: - std::vector m_LiveDeviceList; + std::vector> m_LiveDeviceList; + // Vector of raw device pointers to keep the signature of getPcapLiveDevicesList, as it returns a reference. + mutable std::vector m_LiveDeviceListView; std::vector m_DnsServers; @@ -36,6 +39,8 @@ namespace pcpp void init(); void setDnsServers(); + + void updateLiveDeviceListView() const; public: /** * The access method to the singleton @@ -50,7 +55,7 @@ namespace pcpp /** * @return A vector containing pointers to all live devices currently installed on the machine */ - const std::vector& getPcapLiveDevicesList() const { return m_LiveDeviceList; } + const std::vector& getPcapLiveDevicesList() const; /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 @@ -110,9 +115,6 @@ namespace pcpp * Reset the live device list and DNS server list, meaning clear and refetch them */ void reset(); - - // d'tor - ~PcapLiveDeviceList(); }; } // namespace pcpp diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 76a4417913..72c322a526 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -28,14 +28,6 @@ PcapLiveDeviceList::PcapLiveDeviceList() init(); } -PcapLiveDeviceList::~PcapLiveDeviceList() -{ - for(const auto &devIter : m_LiveDeviceList) - { - delete devIter; - } -} - void PcapLiveDeviceList::init() { pcap_if_t* interfaceList; @@ -52,12 +44,12 @@ void PcapLiveDeviceList::init() while (currInterface != nullptr) { #if defined(_WIN32) - PcapLiveDevice* dev = new WinPcapLiveDevice(currInterface, true, true, true); + std::unique_ptr dev = std::unique_ptr(new WinPcapLiveDevice(currInterface, true, true, true)); #else //__linux__, __APPLE__, __FreeBSD__ - PcapLiveDevice* dev = new PcapLiveDevice(currInterface, true, true, true); + std::unique_ptr dev = std::unique_ptr(new PcapLiveDevice(currInterface, true, true, true)); #endif currInterface = currInterface->next; - m_LiveDeviceList.insert(m_LiveDeviceList.end(), dev); + m_LiveDeviceList.insert(m_LiveDeviceList.end(), std::move(dev)); } setDnsServers(); @@ -252,6 +244,27 @@ void PcapLiveDeviceList::setDnsServers() #endif } +void PcapLiveDeviceList::updateLiveDeviceListView() const +{ + // Technically if a device is removed and a different device is added, it might cause issues, + // but as far as I can see the LiveDeviceList is only modified on construction and reset, and that is a whole list refresh + // which can easily be handled by clearing the view list too. + if (m_LiveDeviceList.size() != m_LiveDeviceListView.size()) + { + m_LiveDeviceListView.resize(m_LiveDeviceList.size()); + // Full update of all elements of the view vector to synchronize them with the main vector. + std::transform(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), m_LiveDeviceListView.begin(), + [](const std::unique_ptr& ptr) { return ptr.get(); } + ); + } +} + +const std::vector& PcapLiveDeviceList::getPcapLiveDevicesList() const +{ + updateLiveDeviceListView(); + return m_LiveDeviceListView; +} + PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAddr) const { if (ipAddr.getType() == IPAddress::IPv4AddressType) @@ -289,7 +302,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA if (currAddr->s_addr == ipAddr.toInt()) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter; + return devIter.get(); } } } @@ -325,7 +338,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 { PCPP_LOG_DEBUG("Found matched address!"); delete [] addrAsArr; - return devIter; + return devIter.get(); } delete [] addrAsArr; @@ -357,7 +370,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& n { PCPP_LOG_DEBUG("Searching all live devices..."); auto devIter = std::find_if(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), - [&name](const PcapLiveDevice *dev) { return dev->getName() == name; }); + [&name](const std::unique_ptr& dev) { return dev->getName() == name; }); if (devIter == m_LiveDeviceList.end()) { @@ -365,7 +378,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& n return nullptr; } - return *devIter; + return devIter->get(); } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const @@ -388,11 +401,7 @@ PcapLiveDeviceList* PcapLiveDeviceList::clone() void PcapLiveDeviceList::reset() { - for(auto devIter : m_LiveDeviceList) - { - delete devIter; - } - + m_LiveDeviceListView.clear(); m_LiveDeviceList.clear(); m_DnsServers.clear(); From 91463301f8747c807bfe8c183743b90256b57f95 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 6 May 2024 15:21:29 +0300 Subject: [PATCH 02/81] Replaced raw pcap_if_t pointers with smart pointers in LiveDeviceList initialization. --- Pcap++/src/PcapLiveDeviceList.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 72c322a526..03519e2404 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -28,19 +28,36 @@ PcapLiveDeviceList::PcapLiveDeviceList() init(); } +namespace internal +{ + /** + * @class FreeAllDevsDeleter + * A deleter that frees an interface list of pcap_if_t ptr by calling 'pcap_freealldevs' function on it. + */ + struct FreeAllDevsDeleter + { + void operator()(pcap_if_t *ptr) const { pcap_freealldevs(ptr); } + }; +} // namespace internal + void PcapLiveDeviceList::init() { - pcap_if_t* interfaceList; + std::unique_ptr interfaceList; + { + pcap_if_t* interfaceListRaw; char errbuf[PCAP_ERRBUF_SIZE]; - int err = pcap_findalldevs(&interfaceList, errbuf); + int err = pcap_findalldevs(&interfaceListRaw, errbuf); if (err < 0) { PCPP_LOG_ERROR("Error searching for devices: " << errbuf); } + // Assigns the raw pointer to the smart pointer with specialized deleter. + interfaceList = std::unique_ptr(interfaceListRaw); + } PCPP_LOG_DEBUG("Pcap lib version info: " << IPcapDevice::getPcapLibVersionInfo()); - pcap_if_t* currInterface = interfaceList; + pcap_if_t* currInterface = interfaceList.get(); while (currInterface != nullptr) { #if defined(_WIN32) @@ -54,8 +71,8 @@ void PcapLiveDeviceList::init() setDnsServers(); + // TODO: What to do with this log message? Free All devs is no longer needed here as that is handled by the deleter. PCPP_LOG_DEBUG("Freeing live device data"); - pcap_freealldevs(interfaceList); } void PcapLiveDeviceList::setDnsServers() From bbbcd71920712d473693fcec29e5b4db4bcf61a9 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 6 May 2024 15:30:43 +0300 Subject: [PATCH 03/81] Simplified device insertion call. --- Pcap++/src/PcapLiveDeviceList.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 03519e2404..37aef3b91e 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -45,12 +45,12 @@ void PcapLiveDeviceList::init() std::unique_ptr interfaceList; { pcap_if_t* interfaceListRaw; - char errbuf[PCAP_ERRBUF_SIZE]; + char errbuf[PCAP_ERRBUF_SIZE]; int err = pcap_findalldevs(&interfaceListRaw, errbuf); - if (err < 0) - { - PCPP_LOG_ERROR("Error searching for devices: " << errbuf); - } + if (err < 0) + { + PCPP_LOG_ERROR("Error searching for devices: " << errbuf); + } // Assigns the raw pointer to the smart pointer with specialized deleter. interfaceList = std::unique_ptr(interfaceListRaw); } @@ -66,7 +66,7 @@ void PcapLiveDeviceList::init() std::unique_ptr dev = std::unique_ptr(new PcapLiveDevice(currInterface, true, true, true)); #endif currInterface = currInterface->next; - m_LiveDeviceList.insert(m_LiveDeviceList.end(), std::move(dev)); + m_LiveDeviceList.push_back(std::move(dev)); } setDnsServers(); From 19c93f38b286e242d994d0cbb450bd17ba69a33e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 6 May 2024 15:32:38 +0300 Subject: [PATCH 04/81] Rafactored while-loop into a for-loop. --- Pcap++/src/PcapLiveDeviceList.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 37aef3b91e..c2a344046c 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -57,15 +57,13 @@ void PcapLiveDeviceList::init() PCPP_LOG_DEBUG("Pcap lib version info: " << IPcapDevice::getPcapLibVersionInfo()); - pcap_if_t* currInterface = interfaceList.get(); - while (currInterface != nullptr) + for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { #if defined(_WIN32) std::unique_ptr dev = std::unique_ptr(new WinPcapLiveDevice(currInterface, true, true, true)); #else //__linux__, __APPLE__, __FreeBSD__ std::unique_ptr dev = std::unique_ptr(new PcapLiveDevice(currInterface, true, true, true)); #endif - currInterface = currInterface->next; m_LiveDeviceList.push_back(std::move(dev)); } From 9359c2cd219c0a042aaeec77009673cac25e121e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 6 May 2024 15:46:33 +0300 Subject: [PATCH 05/81] Refactored to use a bytes view of IPv6 address when comparing the address, instead of copying the address to a new bytes array. --- Pcap++/src/PcapLiveDeviceList.cpp | 7 +------ Pcap++/src/PcapRemoteDeviceList.cpp | 6 +----- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index c2a344046c..cd5d92bdf9 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -347,16 +347,11 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 continue; } - uint8_t* addrAsArr; size_t addrLen; - ip6Addr.copyTo(&addrAsArr, addrLen); - if (memcmp(currAddr, addrAsArr, sizeof(struct in6_addr)) == 0) + if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { PCPP_LOG_DEBUG("Found matched address!"); - delete [] addrAsArr; return devIter.get(); } - - delete [] addrAsArr; } } diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 7cff5e73ba..6089f2dd02 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -153,15 +153,11 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& i continue; } - uint8_t* addrAsArr; size_t addrLen; - ip6Addr.copyTo(&addrAsArr, addrLen); - if (memcmp(currAddr, addrAsArr, sizeof(struct in6_addr)) == 0) + if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { PCPP_LOG_DEBUG("Found matched address!"); - delete [] addrAsArr; return (*devIter); } - delete [] addrAsArr; } } From 0ca74d9b651775e76d2c5bc617c48e695cc1e97c Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 20:57:37 +0300 Subject: [PATCH 06/81] Changed remote authentication structure in PcapRemoteDeviceList and PcapRemoteDevice to be a shared_ptr. --- Pcap++/header/PcapRemoteDevice.h | 5 +++-- Pcap++/header/PcapRemoteDeviceList.h | 6 ++++-- Pcap++/src/PcapRemoteDevice.cpp | 4 ++-- Pcap++/src/PcapRemoteDeviceList.cpp | 18 +++++------------- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/Pcap++/header/PcapRemoteDevice.h b/Pcap++/header/PcapRemoteDevice.h index 860a327888..ad5a0f5f70 100644 --- a/Pcap++/header/PcapRemoteDevice.h +++ b/Pcap++/header/PcapRemoteDevice.h @@ -3,6 +3,7 @@ #if defined(_WIN32) #include +#include #include "PcapLiveDevice.h" @@ -81,10 +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); // private copy c'tor PcapRemoteDevice( const PcapRemoteDevice& other ); diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 904866c40c..8cbcdfa9fd 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) {} // private copy c'tor PcapRemoteDeviceList(const PcapRemoteDeviceList& other); PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other); @@ -41,6 +42,7 @@ namespace pcpp void setRemoteMachineIpAddress(const IPAddress& ipAddress); void setRemoteMachinePort(uint16_t port); void setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth); + void setRemoteAuthentication(std::shared_ptr remoteAuth); public: /** diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index b08733f10c..5ecae6db18 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) { 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; + m_RemoteAuthentication = std::move(remoteAuthentication); } diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 6089f2dd02..d1659878d7 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -177,14 +177,11 @@ void PcapRemoteDeviceList::setRemoteMachinePort(uint16_t port) void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth) { - if (remoteAuth != NULL) - m_RemoteAuthentication = new PcapRemoteAuthentication(*remoteAuth); - else - { - if (m_RemoteAuthentication != NULL) - delete m_RemoteAuthentication; - m_RemoteAuthentication = NULL; - } + setRemoteAuthentication(remoteAuth != nullptr ? std::make_shared(*remoteAuth) : nullptr); +} +void PcapRemoteDeviceList::setRemoteAuthentication(std::shared_ptr remoteAuth) +{ + m_RemoteAuthentication = std::move(remoteAuth); } PcapRemoteDeviceList::~PcapRemoteDeviceList() @@ -195,11 +192,6 @@ PcapRemoteDeviceList::~PcapRemoteDeviceList() delete (*devIter); m_RemoteDeviceList.erase(devIter); } - - if (m_RemoteAuthentication != NULL) - { - delete m_RemoteAuthentication; - } } } // namespace pcpp From 276fdfb0e4f6eacbf6fe3ce6fef6be51c997cb6e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 21:06:05 +0300 Subject: [PATCH 07/81] Moved variable setup to initialization list. --- Pcap++/src/PcapRemoteDevice.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index 5ecae6db18..798895ad9c 100644 --- a/Pcap++/src/PcapRemoteDevice.cpp +++ b/Pcap++/src/PcapRemoteDevice.cpp @@ -21,12 +21,12 @@ pcap_rmtauth PcapRemoteAuthentication::getPcapRmAuth() const 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 = std::move(remoteAuthentication); } From 2c1bfefc176db28e707927f651ee44dbc1bd3d0d Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 21:08:28 +0300 Subject: [PATCH 08/81] C-cast -> Cpp cast. --- Pcap++/src/PcapRemoteDevice.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index 798895ad9c..0f417482b7 100644 --- a/Pcap++/src/PcapRemoteDevice.cpp +++ b/Pcap++/src/PcapRemoteDevice.cpp @@ -14,8 +14,9 @@ pcap_rmtauth PcapRemoteAuthentication::getPcapRmAuth() const { pcap_rmtauth result; result.type = RPCAP_RMTAUTH_PWD; - result.username = (char*)userName.c_str(); - result.password = (char*)password.c_str(); + // Const cast can cause access violation errors. Hope nobody modifies this... + result.username = const_cast(userName.c_str()); + result.password = const_cast(password.c_str()); return result; } From 074de0abaf4dc1049f4daaa29fcd640ee71d8730 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 21:13:24 +0300 Subject: [PATCH 09/81] Replaced NULL with nullptr. --- Pcap++/src/PcapRemoteDeviceList.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index d1659878d7..08672ade41 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -23,17 +23,17 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& char errbuf[PCAP_ERRBUF_SIZE]; std::ostringstream portAsString; portAsString << port; - if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), portAsString.str().c_str(), NULL, errbuf) != 0) + if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), portAsString.str().c_str(), nullptr, errbuf) != 0) { PCPP_LOG_ERROR("Error in creating the remote connection string. Error was: " << errbuf); - return NULL; + return nullptr; } PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString); - pcap_rmtauth* pRmAuth = NULL; + pcap_rmtauth* pRmAuth = nullptr; pcap_rmtauth rmAuth; - if (remoteAuth != NULL) + if (remoteAuth != nullptr) { PCPP_LOG_DEBUG("Authentication requested. Username: " << remoteAuth->userName << ", Password: " << remoteAuth->password); rmAuth = remoteAuth->getPcapRmAuth(); @@ -45,7 +45,7 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& if (pcap_findalldevs_ex(remoteCaptureString, pRmAuth, &interfaceList, errorBuf) < 0) { PCPP_LOG_ERROR("Error retrieving device on remote machine. Error string is: " << errorBuf); - return NULL; + return nullptr; } PcapRemoteDeviceList* resultList = new PcapRemoteDeviceList(); @@ -54,7 +54,7 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& resultList->setRemoteAuthentication(remoteAuth); pcap_if_t* currInterface = interfaceList; - while (currInterface != NULL) + while (currInterface != nullptr) { PcapRemoteDevice* pNewRemoteDevice = new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort()); @@ -127,7 +127,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i } } - return NULL; + return nullptr; } @@ -161,7 +161,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& i } } - return NULL; + return nullptr; } From 61fcfa818a46c2d8d21f4f9bacf8df5b893e2767 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 21:16:21 +0300 Subject: [PATCH 10/81] Changed shared_ptr params to const ref --- Pcap++/header/PcapRemoteDevice.h | 2 +- Pcap++/header/PcapRemoteDeviceList.h | 2 +- Pcap++/src/PcapRemoteDevice.cpp | 4 ++-- Pcap++/src/PcapRemoteDeviceList.cpp | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Pcap++/header/PcapRemoteDevice.h b/Pcap++/header/PcapRemoteDevice.h index ad5a0f5f70..76c8208523 100644 --- a/Pcap++/header/PcapRemoteDevice.h +++ b/Pcap++/header/PcapRemoteDevice.h @@ -85,7 +85,7 @@ namespace pcpp 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, std::shared_ptr remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort); + PcapRemoteDevice(pcap_if_t* iface, const std::shared_ptr& remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort); // private copy c'tor PcapRemoteDevice( const PcapRemoteDevice& other ); diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 8cbcdfa9fd..2b8ff090cc 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -42,7 +42,7 @@ namespace pcpp void setRemoteMachineIpAddress(const IPAddress& ipAddress); void setRemoteMachinePort(uint16_t port); void setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth); - void setRemoteAuthentication(std::shared_ptr remoteAuth); + void setRemoteAuthentication(const std::shared_ptr& remoteAuth); public: /** diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index 0f417482b7..b2f2f0c07f 100644 --- a/Pcap++/src/PcapRemoteDevice.cpp +++ b/Pcap++/src/PcapRemoteDevice.cpp @@ -20,11 +20,11 @@ pcap_rmtauth PcapRemoteAuthentication::getPcapRmAuth() const return result; } -PcapRemoteDevice::PcapRemoteDevice(pcap_if_t* iface, std::shared_ptr remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort) +PcapRemoteDevice::PcapRemoteDevice(pcap_if_t* iface, const 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)) + , m_RemoteAuthentication(remoteAuthentication) { PCPP_LOG_DEBUG("MTU calculation isn't supported for remote devices. Setting MTU to 1514"); m_DeviceMtu = 1514; diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 08672ade41..1b0b903fed 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -179,9 +179,9 @@ void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthenticatio { setRemoteAuthentication(remoteAuth != nullptr ? std::make_shared(*remoteAuth) : nullptr); } -void PcapRemoteDeviceList::setRemoteAuthentication(std::shared_ptr remoteAuth) +void PcapRemoteDeviceList::setRemoteAuthentication(const std::shared_ptr& remoteAuth) { - m_RemoteAuthentication = std::move(remoteAuth); + m_RemoteAuthentication = remoteAuth; } PcapRemoteDeviceList::~PcapRemoteDeviceList() From 0027113467d68717bc68df9dbc82e9f29f63f7c6 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 21:32:45 +0300 Subject: [PATCH 11/81] Added a smart pointer version of getRemoteDeviceList. --- Pcap++/header/PcapRemoteDeviceList.h | 2 ++ Pcap++/src/PcapRemoteDeviceList.cpp | 11 +++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 2b8ff090cc..b8fc43656e 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -86,6 +86,8 @@ namespace pcpp */ static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::unique_ptr remoteAuth); + /** * @return The IP address of the remote machine */ diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 1b0b903fed..4e14cf1dfb 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -17,6 +17,13 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& } PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth) +{ + // Uses the smart pointer version and releases management of the object to the caller. + std::unique_ptr uPtr = getRemoteDeviceList(ipAddress, port, std::unique_ptr(new PcapRemoteAuthentication(*remoteAuth))); + return uPtr.release(); +} + +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::unique_ptr remoteAuth) { PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); char remoteCaptureString[PCAP_BUF_SIZE]; @@ -48,10 +55,10 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& return nullptr; } - PcapRemoteDeviceList* resultList = new PcapRemoteDeviceList(); + std::unique_ptr resultList = std::unique_ptr(new PcapRemoteDeviceList()); resultList->setRemoteMachineIpAddress(ipAddress); resultList->setRemoteMachinePort(port); - resultList->setRemoteAuthentication(remoteAuth); + resultList->setRemoteAuthentication(std::move(remoteAuth)); pcap_if_t* currInterface = interfaceList; while (currInterface != nullptr) From bd1677fc2ceaf2e46ad116aac684ec0fab201c26 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 21:34:04 +0300 Subject: [PATCH 12/81] Changed while-loop to for-loop. --- Pcap++/src/PcapRemoteDeviceList.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 4e14cf1dfb..d2a4f9f3da 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -60,13 +60,11 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( resultList->setRemoteMachinePort(port); resultList->setRemoteAuthentication(std::move(remoteAuth)); - pcap_if_t* currInterface = interfaceList; - while (currInterface != nullptr) + for (pcap_if_t* currInterface = interfaceList; currInterface != nullptr; currInterface = currInterface->next) { PcapRemoteDevice* pNewRemoteDevice = new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort()); resultList->m_RemoteDeviceList.push_back(pNewRemoteDevice); - currInterface = currInterface->next; } pcap_freealldevs(interfaceList); From ca0602b442517d47894749f0533a613c22aed688 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 21:40:45 +0300 Subject: [PATCH 13/81] Fixed possible copy from nullptr. --- Pcap++/src/PcapRemoteDeviceList.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index d2a4f9f3da..55df3a099b 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -13,13 +13,14 @@ namespace pcpp PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port) { - return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, NULL); + return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, nullptr); } PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth) { // Uses the smart pointer version and releases management of the object to the caller. - std::unique_ptr uPtr = getRemoteDeviceList(ipAddress, port, std::unique_ptr(new PcapRemoteAuthentication(*remoteAuth))); + std::unique_ptr auth = remoteAuth != nullptr ? std::unique_ptr(new PcapRemoteAuthentication(*remoteAuth)) : nullptr; + std::unique_ptr uPtr = getRemoteDeviceList(ipAddress, port, std::move(auth)); return uPtr.release(); } From 9551dbed50e04a39d4cdc59f40f6ede60e892e6b Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 21:47:06 +0300 Subject: [PATCH 14/81] Added smart pointer factory overload for non-auth verison. --- Pcap++/header/PcapRemoteDeviceList.h | 8 ++++++++ Pcap++/src/PcapRemoteDeviceList.cpp | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index b8fc43656e..d173a176f6 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -55,6 +55,12 @@ namespace pcpp */ typedef typename std::vector::const_iterator ConstRemoteDeviceListIterator; + /* + * @class smart_ptr_tag + * Helper tag to disambiguate smart pointer factory. + */ + struct smart_ptr_tag {}; + ~PcapRemoteDeviceList(); /** @@ -72,6 +78,8 @@ namespace pcpp */ static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, smart_ptr_tag); + /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is suitable for connecting to * remote daemons which require authentication for accessing them diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 55df3a099b..40a64be8fc 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -16,6 +16,11 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, nullptr); } +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, PcapRemoteDeviceList::smart_ptr_tag) +{ + return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, std::unique_ptr()); +} + PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth) { // Uses the smart pointer version and releases management of the object to the caller. From 6cedf1bad7cc73cc592b9a8dc6b8087e232891fe Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 22:01:31 +0300 Subject: [PATCH 15/81] Changed setRemoteAuth to receive smart ptr by value as that enables more freedom on the caller, to skip a reference counter increase/decrease by moving. --- Pcap++/header/PcapRemoteDeviceList.h | 2 +- Pcap++/src/PcapRemoteDeviceList.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index d173a176f6..1b12b226d9 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -42,7 +42,7 @@ namespace pcpp void setRemoteMachineIpAddress(const IPAddress& ipAddress); void setRemoteMachinePort(uint16_t port); void setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth); - void setRemoteAuthentication(const std::shared_ptr& remoteAuth); + void setRemoteAuthentication(std::shared_ptr remoteAuth); public: /** diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 40a64be8fc..90589a7592 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -188,11 +188,11 @@ void PcapRemoteDeviceList::setRemoteMachinePort(uint16_t port) void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth) { - setRemoteAuthentication(remoteAuth != nullptr ? std::make_shared(*remoteAuth) : nullptr); + setRemoteAuthentication(remoteAuth != nullptr ? std::move(std::make_shared(*remoteAuth)) : nullptr); } -void PcapRemoteDeviceList::setRemoteAuthentication(const std::shared_ptr& remoteAuth) +void PcapRemoteDeviceList::setRemoteAuthentication(std::shared_ptr remoteAuth) { - m_RemoteAuthentication = remoteAuth; + m_RemoteAuthentication = std::move(remoteAuth); } PcapRemoteDeviceList::~PcapRemoteDeviceList() From 0a437be32b467b59caa71305535be81a5a3aa6ec Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 22:03:06 +0300 Subject: [PATCH 16/81] Changed remote device to receive shared ptr by value, to enable move optimizations if the caller does not require his own ptr anymore --- Pcap++/header/PcapRemoteDevice.h | 2 +- Pcap++/src/PcapRemoteDevice.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Pcap++/header/PcapRemoteDevice.h b/Pcap++/header/PcapRemoteDevice.h index 76c8208523..ad5a0f5f70 100644 --- a/Pcap++/header/PcapRemoteDevice.h +++ b/Pcap++/header/PcapRemoteDevice.h @@ -85,7 +85,7 @@ namespace pcpp 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, const std::shared_ptr& remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort); + PcapRemoteDevice(pcap_if_t* iface, std::shared_ptr remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort); // private copy c'tor PcapRemoteDevice( const PcapRemoteDevice& other ); diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index b2f2f0c07f..0f417482b7 100644 --- a/Pcap++/src/PcapRemoteDevice.cpp +++ b/Pcap++/src/PcapRemoteDevice.cpp @@ -20,11 +20,11 @@ pcap_rmtauth PcapRemoteAuthentication::getPcapRmAuth() const return result; } -PcapRemoteDevice::PcapRemoteDevice(pcap_if_t* iface, const std::shared_ptr& 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(remoteAuthentication) + , m_RemoteAuthentication(std::move(remoteAuthentication)) { PCPP_LOG_DEBUG("MTU calculation isn't supported for remote devices. Setting MTU to 1514"); m_DeviceMtu = 1514; From 5f683c40ffe3246b523a4edcf8d9c1a4b2fbe7e5 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 22:08:30 +0300 Subject: [PATCH 17/81] Added overload from authentication struct reference. --- Pcap++/header/PcapRemoteDeviceList.h | 1 + Pcap++/src/PcapRemoteDeviceList.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 1b12b226d9..6b18d90cb5 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -95,6 +95,7 @@ namespace pcpp static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::unique_ptr remoteAuth); + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, const PcapRemoteAuthentication& remoteAuth); /** * @return The IP address of the remote machine diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 90589a7592..75c85494a4 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -29,6 +29,11 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& return uPtr.release(); } +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, const PcapRemoteAuthentication& remoteAuth) +{ + return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, std::unique_ptr(new PcapRemoteAuthentication(remoteAuth))); +} + std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::unique_ptr remoteAuth) { PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); From 0a1619718f5e9fd27c3360bd02ce2ea262353d80 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 22:42:30 +0300 Subject: [PATCH 18/81] Cenrtalized deleters that do not need to be declared in the public headers in MemoryUtils header. --- Pcap++/CMakeLists.txt | 2 ++ Pcap++/header/MemoryUtils.h | 31 +++++++++++++++++++++++++++++++ Pcap++/src/MemoryUtils.cpp | 13 +++++++++++++ Pcap++/src/PcapFilter.cpp | 12 ++---------- Pcap++/src/PcapLiveDeviceList.cpp | 17 +++-------------- 5 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 Pcap++/header/MemoryUtils.h create mode 100644 Pcap++/src/MemoryUtils.cpp diff --git a/Pcap++/CMakeLists.txt b/Pcap++/CMakeLists.txt index a23efa7e17..3007f889eb 100644 --- a/Pcap++/CMakeLists.txt +++ b/Pcap++/CMakeLists.txt @@ -6,6 +6,7 @@ add_library( $<$:src/KniDeviceList.cpp> $<$:src/LinuxNicInformationSocket.cpp> $<$:src/MBufRawPacket.cpp> + src/MemoryUtils.cpp src/NetworkUtils.cpp src/PcapFileDevice.cpp src/PcapDevice.cpp @@ -24,6 +25,7 @@ add_library( set(public_headers header/Device.h + header/MemoryUtils.h header/NetworkUtils.h header/PcapDevice.h header/PcapFileDevice.h diff --git a/Pcap++/header/MemoryUtils.h b/Pcap++/header/MemoryUtils.h new file mode 100644 index 0000000000..db058b1a0f --- /dev/null +++ b/Pcap++/header/MemoryUtils.h @@ -0,0 +1,31 @@ +#pragma once + +// Forward declarations +struct pcap; +typedef pcap pcap_t; +struct pcap_if; +typedef pcap_if pcap_if_t; + +namespace pcpp +{ + namespace internal + { + /** + * @class PcapTDeleter + * A deleter that cleans up a pcap_t structure by calling pcap_close. + */ + struct PcapCloseDeleter + { + void operator()(pcap_t* ptr) const; + }; + + /** + * @class FreeAllDevsDeleter + * A deleter that frees an interface list of pcap_if_t ptr by calling 'pcap_freealldevs' function on it. + */ + struct PcapFreeAllDevsDeleter + { + void operator()(pcap_if_t* ptr) const; + }; + } +} diff --git a/Pcap++/src/MemoryUtils.cpp b/Pcap++/src/MemoryUtils.cpp new file mode 100644 index 0000000000..caddfeae05 --- /dev/null +++ b/Pcap++/src/MemoryUtils.cpp @@ -0,0 +1,13 @@ +#include "MemoryUtils.h" + +#include "pcap.h" + +namespace pcpp +{ + namespace internal + { + void PcapCloseDeleter::operator()(pcap_t* ptr) const { pcap_close(ptr); } + + void PcapFreeAllDevsDeleter::operator()(pcap_if_t* ptr) const { pcap_freealldevs(ptr); } + } +} \ No newline at end of file diff --git a/Pcap++/src/PcapFilter.cpp b/Pcap++/src/PcapFilter.cpp index 28e83f4a75..775ce5b317 100644 --- a/Pcap++/src/PcapFilter.cpp +++ b/Pcap++/src/PcapFilter.cpp @@ -11,6 +11,7 @@ #include "pcap.h" #include "RawPacket.h" #include "TimespecTimeval.h" +#include "MemoryUtils.h" namespace pcpp { @@ -35,15 +36,6 @@ namespace internal pcap_freecode(ptr); delete ptr; } - - /** - * @class PcapTDeleter - * A deleter that cleans up a pcap_t structure by calling pcap_close. - */ - struct PcapTDeleter - { - void operator()(pcap_t* ptr) const { pcap_close(ptr); } - }; } BpfFilterWrapper::BpfFilterWrapper() : m_LinkType(LinkLayerType::LINKTYPE_ETHERNET) {} @@ -66,7 +58,7 @@ bool BpfFilterWrapper::setFilter(const std::string& filter, LinkLayerType linkTy if (filter != m_FilterStr || linkType != m_LinkType) { - std::unique_ptr pcap = std::unique_ptr(pcap_open_dead(linkType, DEFAULT_SNAPLEN)); + std::unique_ptr pcap = std::unique_ptr(pcap_open_dead(linkType, DEFAULT_SNAPLEN)); if (pcap == nullptr) { return false; diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index cd5d92bdf9..5e9206f8d4 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -4,6 +4,7 @@ #include "PcapLiveDeviceList.h" #include "Logger.h" #include "SystemUtils.h" +#include "MemoryUtils.h" #include "pcap.h" #include #include @@ -28,21 +29,9 @@ PcapLiveDeviceList::PcapLiveDeviceList() init(); } -namespace internal -{ - /** - * @class FreeAllDevsDeleter - * A deleter that frees an interface list of pcap_if_t ptr by calling 'pcap_freealldevs' function on it. - */ - struct FreeAllDevsDeleter - { - void operator()(pcap_if_t *ptr) const { pcap_freealldevs(ptr); } - }; -} // namespace internal - void PcapLiveDeviceList::init() { - std::unique_ptr interfaceList; + std::unique_ptr interfaceList; { pcap_if_t* interfaceListRaw; char errbuf[PCAP_ERRBUF_SIZE]; @@ -52,7 +41,7 @@ void PcapLiveDeviceList::init() PCPP_LOG_ERROR("Error searching for devices: " << errbuf); } // Assigns the raw pointer to the smart pointer with specialized deleter. - interfaceList = std::unique_ptr(interfaceListRaw); + interfaceList = std::unique_ptr(interfaceListRaw); } PCPP_LOG_DEBUG("Pcap lib version info: " << IPcapDevice::getPcapLibVersionInfo()); From 14c951f80053b2d923ff1775c82efd39bca1b549 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 22:44:25 +0300 Subject: [PATCH 19/81] Simplified conversion to string. --- Pcap++/src/PcapRemoteDeviceList.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 75c85494a4..ecaff8a45c 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -39,9 +39,7 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); char remoteCaptureString[PCAP_BUF_SIZE]; char errbuf[PCAP_ERRBUF_SIZE]; - std::ostringstream portAsString; - portAsString << port; - if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), portAsString.str().c_str(), nullptr, errbuf) != 0) + if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), std::to_string(port).c_str(), nullptr, errbuf) != 0) { PCPP_LOG_ERROR("Error in creating the remote connection string. Error was: " << errbuf); return nullptr; From e8b008684085da73b52616010d8d98e2ff93c20d Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 22:47:37 +0300 Subject: [PATCH 20/81] Removed duplicated error buffer. --- Pcap++/src/PcapRemoteDeviceList.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index ecaff8a45c..cd59406227 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -38,10 +38,10 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( { PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); char remoteCaptureString[PCAP_BUF_SIZE]; - char errbuf[PCAP_ERRBUF_SIZE]; - if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), std::to_string(port).c_str(), nullptr, errbuf) != 0) + char errorBuf[PCAP_ERRBUF_SIZE]; + if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), std::to_string(port).c_str(), nullptr, errorBuf) != 0) { - PCPP_LOG_ERROR("Error in creating the remote connection string. Error was: " << errbuf); + PCPP_LOG_ERROR("Error in creating the remote connection string. Error was: " << errorBuf); return nullptr; } @@ -57,7 +57,6 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( } pcap_if_t* interfaceList; - char errorBuf[PCAP_ERRBUF_SIZE]; if (pcap_findalldevs_ex(remoteCaptureString, pRmAuth, &interfaceList, errorBuf) < 0) { PCPP_LOG_ERROR("Error retrieving device on remote machine. Error string is: " << errorBuf); From b5cd2a27cc73228a6f3356b918d142b39f484f9d Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 22:50:35 +0300 Subject: [PATCH 21/81] Changed interfaceList to be held by smart pointer. --- Pcap++/src/PcapRemoteDeviceList.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index cd59406227..c5b846efc7 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -5,6 +5,7 @@ #include "PcapRemoteDeviceList.h" #include "Logger.h" #include "IpUtils.h" +#include "MemoryUtils.h" #include "pcap.h" #include @@ -56,11 +57,15 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( pRmAuth = &rmAuth; } - pcap_if_t* interfaceList; - if (pcap_findalldevs_ex(remoteCaptureString, pRmAuth, &interfaceList, errorBuf) < 0) + std::unique_ptr interfaceList; { - PCPP_LOG_ERROR("Error retrieving device on remote machine. Error string is: " << errorBuf); - return nullptr; + pcap_if_t *interfaceListRaw; + if (pcap_findalldevs_ex(remoteCaptureString, pRmAuth, &interfaceListRaw, errorBuf) < 0) + { + PCPP_LOG_ERROR("Error retrieving device on remote machine. Error string is: " << errorBuf); + return nullptr; + } + interfaceList = std::unique_ptr(interfaceListRaw); } std::unique_ptr resultList = std::unique_ptr(new PcapRemoteDeviceList()); @@ -68,14 +73,13 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( resultList->setRemoteMachinePort(port); resultList->setRemoteAuthentication(std::move(remoteAuth)); - for (pcap_if_t* currInterface = interfaceList; currInterface != nullptr; currInterface = currInterface->next) + for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { PcapRemoteDevice* pNewRemoteDevice = new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort()); resultList->m_RemoteDeviceList.push_back(pNewRemoteDevice); } - pcap_freealldevs(interfaceList); return resultList; } From 412d5df06adafa6c67fa64c6aa8b5d80dcf5c5be Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 23:19:58 +0300 Subject: [PATCH 22/81] Liter fixes. --- Pcap++/src/MemoryUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/MemoryUtils.cpp b/Pcap++/src/MemoryUtils.cpp index caddfeae05..7716046497 100644 --- a/Pcap++/src/MemoryUtils.cpp +++ b/Pcap++/src/MemoryUtils.cpp @@ -10,4 +10,4 @@ namespace pcpp void PcapFreeAllDevsDeleter::operator()(pcap_if_t* ptr) const { pcap_freealldevs(ptr); } } -} \ No newline at end of file +} From be3a169c70c45c382b71bf12a9ac44fddd0c038c Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 23:21:08 +0300 Subject: [PATCH 23/81] Refactored RemoteDeviceList to use RAII constructor initialization. --- Pcap++/header/PcapRemoteDeviceList.h | 4 +++- Pcap++/src/PcapRemoteDeviceList.cpp | 23 +++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 6b18d90cb5..8d39283743 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -34,7 +34,7 @@ namespace pcpp std::shared_ptr m_RemoteAuthentication; // private c'tor. User should create the list via static methods PcapRemoteDeviceList::getRemoteDeviceList() - PcapRemoteDeviceList() : m_RemoteMachinePort(0) {} + PcapRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector deviceList); // private copy c'tor PcapRemoteDeviceList(const PcapRemoteDeviceList& other); PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other); @@ -44,6 +44,8 @@ namespace pcpp void setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth); void setRemoteAuthentication(std::shared_ptr remoteAuth); + // Implementation that uses a shared ptr is private to guarantee that the remote auth object is not shared externally. It is used by the other overloads. + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth); public: /** * Iterator object that can be used for iterating all PcapRemoteDevice in list diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index c5b846efc7..b5b17f7c69 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -12,6 +12,9 @@ namespace pcpp { +PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector deviceList) + : m_RemoteDeviceList(std::move(deviceList)), m_RemoteMachineIpAddress(ipAddress), m_RemoteMachinePort(port), m_RemoteAuthentication(std::move(remoteAuth)) {} + PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port) { return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, nullptr); @@ -36,6 +39,11 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( } std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::unique_ptr remoteAuth) +{ + return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, std::shared_ptr(std::move(remoteAuth))); +} + +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth) { PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); char remoteCaptureString[PCAP_BUF_SIZE]; @@ -68,19 +76,14 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( interfaceList = std::unique_ptr(interfaceListRaw); } - std::unique_ptr resultList = std::unique_ptr(new PcapRemoteDeviceList()); - resultList->setRemoteMachineIpAddress(ipAddress); - resultList->setRemoteMachinePort(port); - resultList->setRemoteAuthentication(std::move(remoteAuth)); - + std::vector remoteDeviceList; for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { - PcapRemoteDevice* pNewRemoteDevice = new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, - resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort()); - resultList->m_RemoteDeviceList.push_back(pNewRemoteDevice); + PcapRemoteDevice *pNewRemoteDevice = new PcapRemoteDevice(currInterface, remoteAuth, ipAddress, port); + remoteDeviceList.push_back(pNewRemoteDevice); } - return resultList; + return std::unique_ptr(new PcapRemoteDeviceList(ipAddress, port, std::move(remoteAuth), std::move(remoteDeviceList))); } PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& ipAddrAsString) const @@ -194,7 +197,7 @@ void PcapRemoteDeviceList::setRemoteMachinePort(uint16_t port) void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth) { - setRemoteAuthentication(remoteAuth != nullptr ? std::move(std::make_shared(*remoteAuth)) : nullptr); + setRemoteAuthentication(remoteAuth != nullptr ? std::move(std::make_shared(*remoteAuth)) : nullptr); } void PcapRemoteDeviceList::setRemoteAuthentication(std::shared_ptr remoteAuth) { From 820d4aa1d873b5fd6e4e2ce09e5e78fb876fb8f6 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 23:26:47 +0300 Subject: [PATCH 24/81] Lint --- Pcap++/src/PcapRemoteDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index b5b17f7c69..9dc2569184 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -67,7 +67,7 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( std::unique_ptr interfaceList; { - pcap_if_t *interfaceListRaw; + pcap_if_t* interfaceListRaw; if (pcap_findalldevs_ex(remoteCaptureString, pRmAuth, &interfaceListRaw, errorBuf) < 0) { PCPP_LOG_ERROR("Error retrieving device on remote machine. Error string is: " << errorBuf); From e0cdedaa0d59cc6a4cbb165af196ea91f460f3f6 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 23:29:01 +0300 Subject: [PATCH 25/81] Removed duplicate auth structure. --- Pcap++/src/PcapRemoteDeviceList.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 9dc2569184..dd8e599b5d 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -57,12 +57,10 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString); pcap_rmtauth* pRmAuth = nullptr; - pcap_rmtauth rmAuth; if (remoteAuth != nullptr) { PCPP_LOG_DEBUG("Authentication requested. Username: " << remoteAuth->userName << ", Password: " << remoteAuth->password); - rmAuth = remoteAuth->getPcapRmAuth(); - pRmAuth = &rmAuth; + pRmAuth = &remoteAuth->getPcapRmAuth(); } std::unique_ptr interfaceList; From 74d8be58987b4d110c9ce4c89c68741d2130cdce Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 7 May 2024 23:55:21 +0300 Subject: [PATCH 26/81] Changed PcapRemoteDevice raw pointers to smart pointers for memory management. Added a second helper 'view' vector to keep raw pointers of the devices for backward compat. Replaced iterator types with auto keyword. Removed destructor as the smart pointers handle cleanup. --- Pcap++/header/PcapRemoteDeviceList.h | 17 ++++++----- Pcap++/src/PcapRemoteDeviceList.cpp | 45 ++++++++++++++++------------ 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 8d39283743..85e9c87294 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -28,17 +28,20 @@ namespace pcpp class PcapRemoteDeviceList { private: - std::vector m_RemoteDeviceList; + std::vector> m_RemoteDeviceList; + // View vector to help keep backward compatability of iteration. + std::vector m_RemoteDeviceListView; IPAddress m_RemoteMachineIpAddress; uint16_t m_RemoteMachinePort; std::shared_ptr m_RemoteAuthentication; // private c'tor. User should create the list via static methods PcapRemoteDeviceList::getRemoteDeviceList() - PcapRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector deviceList); + PcapRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList); // private copy c'tor PcapRemoteDeviceList(const PcapRemoteDeviceList& other); PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other); + void updateDeviceListView(); void setRemoteMachineIpAddress(const IPAddress& ipAddress); void setRemoteMachinePort(uint16_t port); void setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth); @@ -63,8 +66,6 @@ namespace pcpp */ struct smart_ptr_tag {}; - ~PcapRemoteDeviceList(); - /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also * creates a list of PcapRemoteDevice instances stored in it, one for each remote network interface. Notice this method allocates @@ -140,22 +141,22 @@ namespace pcpp /** * @return An iterator object pointing to the first PcapRemoteDevice in list */ - RemoteDeviceListIterator begin() { return m_RemoteDeviceList.begin(); } + RemoteDeviceListIterator begin() { return m_RemoteDeviceListView.begin(); } /** * @return A const iterator object pointing to the first PcapRemoteDevice in list */ - ConstRemoteDeviceListIterator begin() const { return m_RemoteDeviceList.begin(); } + ConstRemoteDeviceListIterator begin() const { return m_RemoteDeviceListView.begin(); } /** * @return An iterator object pointing to the last PcapRemoteDevice in list */ - RemoteDeviceListIterator end() { return m_RemoteDeviceList.end(); } + RemoteDeviceListIterator end() { return m_RemoteDeviceListView.end(); } /** * @return A const iterator object pointing to the last PcapRemoteDevice in list */ - ConstRemoteDeviceListIterator end() const { return m_RemoteDeviceList.end(); } + ConstRemoteDeviceListIterator end() const { return m_RemoteDeviceListView.end(); } }; diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index dd8e599b5d..20e01fc002 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -12,8 +12,25 @@ namespace pcpp { -PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector deviceList) - : m_RemoteDeviceList(std::move(deviceList)), m_RemoteMachineIpAddress(ipAddress), m_RemoteMachinePort(port), m_RemoteAuthentication(std::move(remoteAuth)) {} +PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList) + : m_RemoteDeviceList(std::move(deviceList)), m_RemoteMachineIpAddress(ipAddress), m_RemoteMachinePort(port), m_RemoteAuthentication(std::move(remoteAuth)) +{ + updateDeviceListView(); +} + +void PcapRemoteDeviceList::updateDeviceListView() +{ + // Technically if a device is removed and a different device is added, it might cause issues, + // but as far as I can see the LiveDeviceList is only modified on construction and reset, and that is a whole list + // refresh which can easily be handled by clearing the view list too. + if (m_RemoteDeviceList.size() != m_RemoteDeviceListView.size()) + { + m_RemoteDeviceList.resize(m_RemoteDeviceListView.size()); + // Full update of all elements of the view vector to synchronize them with the main vector. + std::transform(m_RemoteDeviceList.begin(), m_RemoteDeviceList.end(), m_RemoteDeviceListView.begin(), + [](const std::unique_ptr& ptr) { return ptr.get(); }); + } +} PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port) { @@ -74,11 +91,11 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( interfaceList = std::unique_ptr(interfaceListRaw); } - std::vector remoteDeviceList; + std::vector> remoteDeviceList; for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { - PcapRemoteDevice *pNewRemoteDevice = new PcapRemoteDevice(currInterface, remoteAuth, ipAddress, port); - remoteDeviceList.push_back(pNewRemoteDevice); + std::unique_ptr pNewRemoteDevice = std::unique_ptr(new PcapRemoteDevice(currInterface, remoteAuth, ipAddress, port)); + remoteDeviceList.push_back(std::move(pNewRemoteDevice)); } return std::unique_ptr(new PcapRemoteDeviceList(ipAddress, port, std::move(remoteAuth), std::move(remoteDeviceList))); @@ -118,7 +135,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipA PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); - for(ConstRemoteDeviceListIterator devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); devIter++) + for(auto& devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); ++devIter) { PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); for(const auto &addrIter : (*devIter)->m_Addresses) @@ -140,7 +157,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i if (currAddr->s_addr == ip4Addr.toInt()) { PCPP_LOG_DEBUG("Found matched address!"); - return (*devIter); + return devIter->get(); } } } @@ -152,7 +169,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); - for(ConstRemoteDeviceListIterator devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); devIter++) + for(auto& devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); ++devIter) { PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); for(const auto &addrIter : (*devIter)->m_Addresses) @@ -174,7 +191,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& i if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { PCPP_LOG_DEBUG("Found matched address!"); - return (*devIter); + return devIter->get(); } } } @@ -202,16 +219,6 @@ void PcapRemoteDeviceList::setRemoteAuthentication(std::shared_ptr 0) - { - RemoteDeviceListIterator devIter = m_RemoteDeviceList.begin(); - delete (*devIter); - m_RemoteDeviceList.erase(devIter); - } -} - } // namespace pcpp #endif // _WIN32 From e755209394e927f47bf19a880759e7157e6f235e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 00:20:53 +0300 Subject: [PATCH 27/81] Added API for getting PcapRemoteDevice as a shared pointer instead of a non-owning raw pointer to allow the device to outlive the list if nessesary. --- Pcap++/header/PcapRemoteDeviceList.h | 13 +++++-- Pcap++/src/PcapRemoteDeviceList.cpp | 58 +++++++++++++++++++++------- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 85e9c87294..0c9f8599e8 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -28,7 +28,7 @@ namespace pcpp class PcapRemoteDeviceList { private: - std::vector> m_RemoteDeviceList; + std::vector> m_RemoteDeviceList; // View vector to help keep backward compatability of iteration. std::vector m_RemoteDeviceListView; IPAddress m_RemoteMachineIpAddress; @@ -36,7 +36,7 @@ namespace pcpp std::shared_ptr m_RemoteAuthentication; // private c'tor. User should create the list via static methods PcapRemoteDeviceList::getRemoteDeviceList() - PcapRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList); + PcapRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList); // private copy c'tor PcapRemoteDeviceList(const PcapRemoteDeviceList& other); PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other); @@ -64,7 +64,8 @@ namespace pcpp * @class smart_ptr_tag * Helper tag to disambiguate smart pointer factory. */ - struct smart_ptr_tag {}; + struct smart_ptr_api_tag {}; + const smart_ptr_api_tag smart_ptr_api{}; /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also @@ -81,7 +82,7 @@ namespace pcpp */ static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); - static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, smart_ptr_tag); + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, smart_ptr_api_tag); /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is suitable for connecting to @@ -116,6 +117,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; + std::shared_ptr getRemoteDeviceByIP(const IPv4Address &ip4Addr, smart_ptr_api_tag) const; /** * Search a PcapRemoteDevice in the list by its IPv6 address @@ -123,6 +125,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; + std::shared_ptr getRemoteDeviceByIP(const IPv6Address &ip4Addr, smart_ptr_api_tag) const; /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) @@ -130,6 +133,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; + std::shared_ptr getRemoteDeviceByIP(const IPAddress &ip4Addr, smart_ptr_api_tag) const; /** * Search a PcapRemoteDevice in the list by its IP address @@ -137,6 +141,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; + std::shared_ptr getRemoteDeviceByIP(const std::string &ip4Addr, smart_ptr_api_tag) const; /** * @return An iterator object pointing to the first PcapRemoteDevice in list diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 20e01fc002..3ab0f4de06 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -12,7 +12,7 @@ namespace pcpp { -PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList) +PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList) : m_RemoteDeviceList(std::move(deviceList)), m_RemoteMachineIpAddress(ipAddress), m_RemoteMachinePort(port), m_RemoteAuthentication(std::move(remoteAuth)) { updateDeviceListView(); @@ -28,7 +28,7 @@ void PcapRemoteDeviceList::updateDeviceListView() m_RemoteDeviceList.resize(m_RemoteDeviceListView.size()); // Full update of all elements of the view vector to synchronize them with the main vector. std::transform(m_RemoteDeviceList.begin(), m_RemoteDeviceList.end(), m_RemoteDeviceListView.begin(), - [](const std::unique_ptr& ptr) { return ptr.get(); }); + [](const std::shared_ptr& ptr) { return ptr.get(); }); } } @@ -37,7 +37,7 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, nullptr); } -std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, PcapRemoteDeviceList::smart_ptr_tag) +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, PcapRemoteDeviceList::smart_ptr_api_tag) { return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, std::unique_ptr()); } @@ -91,10 +91,11 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( interfaceList = std::unique_ptr(interfaceListRaw); } - std::vector> remoteDeviceList; + std::vector> remoteDeviceList; for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { - std::unique_ptr pNewRemoteDevice = std::unique_ptr(new PcapRemoteDevice(currInterface, remoteAuth, ipAddress, port)); + // PcapRemoteDevice ctor is private can't be accessed by std::make_shared. + std::shared_ptr pNewRemoteDevice = std::shared_ptr(new PcapRemoteDevice(currInterface, remoteAuth, ipAddress, port)); remoteDeviceList.push_back(std::move(pNewRemoteDevice)); } @@ -102,6 +103,14 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( } PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& ipAddrAsString) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getRemoteDeviceByIP(ipAddrAsString, smart_ptr_api).get(); +} + +std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& ipAddrAsString, smart_ptr_api_tag) const { IPAddress ipAddr; @@ -115,24 +124,38 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& i return nullptr; } - PcapRemoteDevice* result = getRemoteDeviceByIP(ipAddr); - return result; + return getRemoteDeviceByIP(ipAddr, smart_ptr_api); } PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipAddr) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getRemoteDeviceByIP(ipAddr, smart_ptr_api).get(); +} + +std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipAddr, smart_ptr_api_tag) const { if (ipAddr.getType() == IPAddress::IPv4AddressType) { - return getRemoteDeviceByIP(ipAddr.getIPv4()); + return getRemoteDeviceByIP(ipAddr.getIPv4(), smart_ptr_api); } else //IPAddress::IPv6AddressType { - return getRemoteDeviceByIP(ipAddr.getIPv6()); + return getRemoteDeviceByIP(ipAddr.getIPv6(), smart_ptr_api); } } - PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getRemoteDeviceByIP(ip4Addr, smart_ptr_api).get(); +} + +std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr, smart_ptr_api_tag) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); for(auto& devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); ++devIter) @@ -157,7 +180,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i if (currAddr->s_addr == ip4Addr.toInt()) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter->get(); + return *devIter; } } } @@ -166,7 +189,15 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i } -PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr) const +PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address &ip6Addr) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getRemoteDeviceByIP(ip6Addr, smart_ptr_api).get(); +} + +std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr, smart_ptr_api_tag) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); for(auto& devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); ++devIter) @@ -191,13 +222,12 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& i if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter->get(); + return *devIter; } } } return nullptr; - } void PcapRemoteDeviceList::setRemoteMachineIpAddress(const IPAddress& ipAddress) From a83d9b12848886b018a657377df8dec9c76cb4f8 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 00:22:35 +0300 Subject: [PATCH 28/81] Lint --- Pcap++/header/PcapRemoteDeviceList.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 0c9f8599e8..ca47ae2019 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -29,7 +29,7 @@ namespace pcpp { private: std::vector> m_RemoteDeviceList; - // View vector to help keep backward compatability of iteration. + // View vector to help keep backward compatibility of iteration. std::vector m_RemoteDeviceListView; IPAddress m_RemoteMachineIpAddress; uint16_t m_RemoteMachinePort; From 25cf25f6d7cd784ca478c0e2a4d7e1b960d2c81e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 00:25:11 +0300 Subject: [PATCH 29/81] Documentation fix. --- Pcap++/header/PcapRemoteDeviceList.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index ca47ae2019..b723b8eac4 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -62,7 +62,7 @@ namespace pcpp /* * @class smart_ptr_tag - * Helper tag to disambiguate smart pointer factory. + * Helper tag to disambiguate smart pointer api. */ struct smart_ptr_api_tag {}; const smart_ptr_api_tag smart_ptr_api{}; From 7b41ece1d0949d6d46e2355f8b83d3ae9590e482 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 00:36:29 +0300 Subject: [PATCH 30/81] Added API for getting PcapLiveDevice as a shared pointer instead of a non-owning raw pointer to mirror the PcapRemoteDeviceList API. --- Pcap++/header/PcapLiveDeviceList.h | 15 ++++++- Pcap++/src/PcapLiveDeviceList.cpp | 69 +++++++++++++++++++++++++----- 2 files changed, 72 insertions(+), 12 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index fb8280342c..583e2cf84b 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -24,7 +24,7 @@ namespace pcpp class PcapLiveDeviceList { private: - std::vector> m_LiveDeviceList; + std::vector> m_LiveDeviceList; // Vector of raw device pointers to keep the signature of getPcapLiveDevicesList, as it returns a reference. mutable std::vector m_LiveDeviceListView; @@ -42,6 +42,13 @@ namespace pcpp void updateLiveDeviceListView() const; public: + /* + * @class smart_ptr_tag + * Helper tag to disambiguate smart pointer api. + */ + struct smart_ptr_api_tag {}; + const smart_ptr_api_tag smart_ptr_api{}; + /** * The access method to the singleton * @return The singleton instance of this class @@ -63,6 +70,7 @@ namespace pcpp * @return A pointer to the live device if this IP address exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPAddress& ipAddr, smart_ptr_api_tag) const; /** * Get a pointer to the live device by its IPv4 address @@ -70,6 +78,7 @@ namespace pcpp * @return A pointer to the live device if this IPv4 address exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPv4Address& ipAddr, smart_ptr_api_tag) const; /** * Get a pointer to the live device by its IPv6 address @@ -77,6 +86,7 @@ namespace pcpp * @return A pointer to the live device if this IPv6 address exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, smart_ptr_api_tag) const; /** * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 @@ -84,6 +94,7 @@ namespace pcpp * @return A pointer to the live device if this IP address is valid and exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; + std::shared_ptr getPcapLiveDeviceByIp(const std::string& ipAddrAsString, smart_ptr_api_tag) const; /** * Get a pointer to the live device by its name @@ -91,6 +102,7 @@ namespace pcpp * @return A pointer to the live device if this name exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; + std::shared_ptr getPcapLiveDeviceByName(const std::string& name, smart_ptr_api_tag) const; /** * Get a pointer to the live device by its IP address or name @@ -98,6 +110,7 @@ namespace pcpp * @return A pointer to the live device if exists, NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; + std::shared_ptr getPcapLiveDeviceByIpOrName(const std::string& ipOrName, smart_ptr_api_tag) const; /** * @return A list of all DNS servers defined for this machine. If this list is empty it means no DNS servers were defined or they diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 5e9206f8d4..9300dfdda8 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -258,7 +258,7 @@ void PcapLiveDeviceList::updateLiveDeviceListView() const m_LiveDeviceListView.resize(m_LiveDeviceList.size()); // Full update of all elements of the view vector to synchronize them with the main vector. std::transform(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), m_LiveDeviceListView.begin(), - [](const std::unique_ptr& ptr) { return ptr.get(); } + [](const std::shared_ptr& ptr) { return ptr.get(); } ); } } @@ -270,18 +270,34 @@ const std::vector& PcapLiveDeviceList::getPcapLiveDevicesList() } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAddr) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getPcapLiveDeviceByIp(ipAddr, smart_ptr_api).get(); +} + +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAddr, smart_ptr_api_tag) const { if (ipAddr.getType() == IPAddress::IPv4AddressType) { - return getPcapLiveDeviceByIp(ipAddr.getIPv4()); + return getPcapLiveDeviceByIp(ipAddr.getIPv4(), smart_ptr_api); } else //IPAddress::IPv6AddressType { - return getPcapLiveDeviceByIp(ipAddr.getIPv6()); + return getPcapLiveDeviceByIp(ipAddr.getIPv6(), smart_ptr_api); } } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getPcapLiveDeviceByIp(ipAddr, smart_ptr_api).get(); +} + +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr, smart_ptr_api_tag) const { PCPP_LOG_DEBUG("Searching all live devices..."); for(const auto &devIter : m_LiveDeviceList) @@ -306,7 +322,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA if (currAddr->s_addr == ipAddr.toInt()) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter.get(); + return devIter; } } } @@ -315,6 +331,14 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getPcapLiveDeviceByIp(ip6Addr, smart_ptr_api).get(); +} + +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, smart_ptr_api_tag) const { PCPP_LOG_DEBUG("Searching all live devices..."); for(const auto &devIter : m_LiveDeviceList) @@ -339,7 +363,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter.get(); + return devIter; } } } @@ -348,6 +372,14 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getPcapLiveDeviceByIp(ipAddrAsString, smart_ptr_api).get(); +} + +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipAddrAsString, smart_ptr_api_tag) const { IPAddress ipAddr; try @@ -360,16 +392,23 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipA return nullptr; } - PcapLiveDevice* result = PcapLiveDeviceList::getPcapLiveDeviceByIp(ipAddr); - return result; + return PcapLiveDeviceList::getPcapLiveDeviceByIp(ipAddr, smart_ptr_api); } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getPcapLiveDeviceByIp(name, smart_ptr_api).get(); +} + +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name, smart_ptr_api_tag) const { PCPP_LOG_DEBUG("Searching all live devices..."); auto devIter = std::find_if(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), - [&name](const std::unique_ptr& dev) { return dev->getName() == name; }); + [&name](const std::shared_ptr& dev) { return dev->getName() == name; }); if (devIter == m_LiveDeviceList.end()) { @@ -377,19 +416,27 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& n return nullptr; } - return devIter->get(); + return *devIter; } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const +{ + // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. + // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the + // device alive. + return getPcapLiveDeviceByIp(ipOrName, smart_ptr_api).get(); +} + +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName, smart_ptr_api_tag) const { try { IPAddress interfaceIP = IPAddress(ipOrName); - return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP); + return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP, smart_ptr_api); } catch (std::exception&) { - return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(ipOrName); + return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(ipOrName, smart_ptr_api); } } From 49d77360c8cd448aeb6890c4fc6b1cb392bcfc32 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 00:40:09 +0300 Subject: [PATCH 31/81] Added smart pointer API of PcapLiveDeviceList::clone --- Pcap++/header/PcapLiveDeviceList.h | 5 +++++ Pcap++/src/PcapLiveDeviceList.cpp | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 583e2cf84b..52f5e618c2 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -123,6 +123,11 @@ namespace pcpp * @return A pointer to the cloned device list */ PcapLiveDeviceList* clone(); + /** + * Copies the current live device list + * @return A unique ptr managing the cloned device list + */ + std::unique_ptr clone(smart_ptr_api_tag) const; /** * Reset the live device list and DNS server list, meaning clear and refetch them diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 9300dfdda8..7167094e3e 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -440,9 +440,11 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIpOrName( } } -PcapLiveDeviceList* PcapLiveDeviceList::clone() +PcapLiveDeviceList* PcapLiveDeviceList::clone() { return clone(smart_ptr_api).release(); } + +std::unique_ptr PcapLiveDeviceList::clone(smart_ptr_api_tag) const { - return new PcapLiveDeviceList; + return std::unique_ptr(new PcapLiveDeviceList()); } void PcapLiveDeviceList::reset() From ff165e7d46ac9088d333f4b1ca657ad8c49a5bd2 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 00:41:28 +0300 Subject: [PATCH 32/81] Marked clone method as const. --- Pcap++/header/PcapLiveDeviceList.h | 2 +- Pcap++/src/PcapLiveDeviceList.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 52f5e618c2..3cc1729910 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -122,7 +122,7 @@ namespace pcpp * Copies the current live device list * @return A pointer to the cloned device list */ - PcapLiveDeviceList* clone(); + PcapLiveDeviceList* clone() const; /** * Copies the current live device list * @return A unique ptr managing the cloned device list diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 7167094e3e..a7f49e9004 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -440,7 +440,7 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIpOrName( } } -PcapLiveDeviceList* PcapLiveDeviceList::clone() { return clone(smart_ptr_api).release(); } +PcapLiveDeviceList* PcapLiveDeviceList::clone() const { return clone(smart_ptr_api).release(); } std::unique_ptr PcapLiveDeviceList::clone(smart_ptr_api_tag) const { From 0037b97a145fa3a3c8a6b2e5911916037e9fe2bf Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 00:43:13 +0300 Subject: [PATCH 33/81] Added smart ptr API for getLiveDevicesList. --- Pcap++/header/PcapLiveDeviceList.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 3cc1729910..52e1290041 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -63,6 +63,7 @@ namespace pcpp * @return A vector containing pointers to all live devices currently installed on the machine */ const std::vector& getPcapLiveDevicesList() const; + const std::vector> &getPcapLiveDevicesList(smart_ptr_api_tag) const { return m_LiveDeviceList; }; /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 From 8240730e8f991a36578090b7c517f34acb8a48ff Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 00:44:07 +0300 Subject: [PATCH 34/81] Removed unused private setters. --- Pcap++/header/PcapRemoteDeviceList.h | 4 ---- Pcap++/src/PcapRemoteDeviceList.cpp | 19 ------------------- 2 files changed, 23 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index b723b8eac4..5c58b6a20e 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -42,10 +42,6 @@ namespace pcpp PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other); void updateDeviceListView(); - void setRemoteMachineIpAddress(const IPAddress& ipAddress); - void setRemoteMachinePort(uint16_t port); - void setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth); - void setRemoteAuthentication(std::shared_ptr remoteAuth); // Implementation that uses a shared ptr is private to guarantee that the remote auth object is not shared externally. It is used by the other overloads. static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth); diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 3ab0f4de06..e17b851de1 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -230,25 +230,6 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons return nullptr; } -void PcapRemoteDeviceList::setRemoteMachineIpAddress(const IPAddress& ipAddress) -{ - m_RemoteMachineIpAddress = ipAddress; -} - -void PcapRemoteDeviceList::setRemoteMachinePort(uint16_t port) -{ - m_RemoteMachinePort = port; -} - -void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth) -{ - setRemoteAuthentication(remoteAuth != nullptr ? std::move(std::make_shared(*remoteAuth)) : nullptr); -} -void PcapRemoteDeviceList::setRemoteAuthentication(std::shared_ptr remoteAuth) -{ - m_RemoteAuthentication = std::move(remoteAuth); -} - } // namespace pcpp #endif // _WIN32 From f3ab6342423f74f49605be6041eb459bc517714f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 01:14:16 +0300 Subject: [PATCH 35/81] Fixed getByIpOrName calling pure IP implementation. --- Pcap++/src/PcapLiveDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index a7f49e9004..d1f6154c94 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -424,7 +424,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::strin // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getPcapLiveDeviceByIp(ipOrName, smart_ptr_api).get(); + return getPcapLiveDeviceByIpOrName(ipOrName, smart_ptr_api).get(); } std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName, smart_ptr_api_tag) const From 639b85d5e7ebb96b7f7f9f0570fbd9bd19fddca3 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 01:21:24 +0300 Subject: [PATCH 36/81] Fixed getByName calling pure IP implementation. --- Pcap++/src/PcapLiveDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index d1f6154c94..045dd5b55c 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -401,7 +401,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& n // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getPcapLiveDeviceByIp(name, smart_ptr_api).get(); + return getPcapLiveDeviceByName(name, smart_ptr_api).get(); } std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name, smart_ptr_api_tag) const From 786d59e2f581e8d89f871d83d4d26520a6c92c99 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 8 May 2024 01:31:33 +0300 Subject: [PATCH 37/81] Revert "Removed duplicate auth structure." This reverts commit e0cdedaa0d59cc6a4cbb165af196ea91f460f3f6. --- Pcap++/src/PcapRemoteDeviceList.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index e17b851de1..f9a49d4d4d 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -74,10 +74,12 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString); pcap_rmtauth* pRmAuth = nullptr; + pcap_rmtauth rmAuth; if (remoteAuth != nullptr) { PCPP_LOG_DEBUG("Authentication requested. Username: " << remoteAuth->userName << ", Password: " << remoteAuth->password); - pRmAuth = &remoteAuth->getPcapRmAuth(); + rmAuth = remoteAuth->getPcapRmAuth(); + pRmAuth = &rmAuth; } std::unique_ptr interfaceList; From 55a427bb651bef992165645073f9abc4a2544bc1 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 09:50:11 +0300 Subject: [PATCH 38/81] Renamed the smart API tag from snake_case to PascalCase. --- Pcap++/header/PcapLiveDeviceList.h | 24 +++++++++--------- Pcap++/header/PcapRemoteDeviceList.h | 18 ++++++------- Pcap++/src/PcapLiveDeviceList.cpp | 38 ++++++++++++++-------------- Pcap++/src/PcapRemoteDeviceList.cpp | 24 +++++++++--------- 4 files changed, 52 insertions(+), 52 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 52e1290041..75ed7db6a2 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -43,11 +43,11 @@ namespace pcpp void updateLiveDeviceListView() const; public: /* - * @class smart_ptr_tag - * Helper tag to disambiguate smart pointer api. + * @class SmartPtrApiTag + * Helper tag to disambiguate smart pointer API. */ - struct smart_ptr_api_tag {}; - const smart_ptr_api_tag smart_ptr_api{}; + struct SmartPtrApiTag {}; + const SmartPtrApiTag SmartPtrApi{}; /** * The access method to the singleton @@ -63,7 +63,7 @@ namespace pcpp * @return A vector containing pointers to all live devices currently installed on the machine */ const std::vector& getPcapLiveDevicesList() const; - const std::vector> &getPcapLiveDevicesList(smart_ptr_api_tag) const { return m_LiveDeviceList; }; + const std::vector> &getPcapLiveDevicesList(SmartPtrApiTag) const { return m_LiveDeviceList; }; /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 @@ -71,7 +71,7 @@ namespace pcpp * @return A pointer to the live device if this IP address exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; - std::shared_ptr getPcapLiveDeviceByIp(const IPAddress& ipAddr, smart_ptr_api_tag) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPAddress& ipAddr, SmartPtrApiTag) const; /** * Get a pointer to the live device by its IPv4 address @@ -79,7 +79,7 @@ namespace pcpp * @return A pointer to the live device if this IPv4 address exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; - std::shared_ptr getPcapLiveDeviceByIp(const IPv4Address& ipAddr, smart_ptr_api_tag) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPv4Address& ipAddr, SmartPtrApiTag) const; /** * Get a pointer to the live device by its IPv6 address @@ -87,7 +87,7 @@ namespace pcpp * @return A pointer to the live device if this IPv6 address exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; - std::shared_ptr getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, smart_ptr_api_tag) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, SmartPtrApiTag) const; /** * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 @@ -95,7 +95,7 @@ namespace pcpp * @return A pointer to the live device if this IP address is valid and exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; - std::shared_ptr getPcapLiveDeviceByIp(const std::string& ipAddrAsString, smart_ptr_api_tag) const; + std::shared_ptr getPcapLiveDeviceByIp(const std::string& ipAddrAsString, SmartPtrApiTag) const; /** * Get a pointer to the live device by its name @@ -103,7 +103,7 @@ namespace pcpp * @return A pointer to the live device if this name exists. NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; - std::shared_ptr getPcapLiveDeviceByName(const std::string& name, smart_ptr_api_tag) const; + std::shared_ptr getPcapLiveDeviceByName(const std::string& name, SmartPtrApiTag) const; /** * Get a pointer to the live device by its IP address or name @@ -111,7 +111,7 @@ namespace pcpp * @return A pointer to the live device if exists, NULL otherwise */ PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; - std::shared_ptr getPcapLiveDeviceByIpOrName(const std::string& ipOrName, smart_ptr_api_tag) const; + std::shared_ptr getPcapLiveDeviceByIpOrName(const std::string& ipOrName, SmartPtrApiTag) const; /** * @return A list of all DNS servers defined for this machine. If this list is empty it means no DNS servers were defined or they @@ -128,7 +128,7 @@ namespace pcpp * Copies the current live device list * @return A unique ptr managing the cloned device list */ - std::unique_ptr clone(smart_ptr_api_tag) const; + std::unique_ptr clone(SmartPtrApiTag) const; /** * Reset the live device list and DNS server list, meaning clear and refetch them diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 5c58b6a20e..e4300785cb 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -57,11 +57,11 @@ namespace pcpp typedef typename std::vector::const_iterator ConstRemoteDeviceListIterator; /* - * @class smart_ptr_tag - * Helper tag to disambiguate smart pointer api. + * @class SmartPtrApiTag + * Helper tag to disambiguate smart pointer API. */ - struct smart_ptr_api_tag {}; - const smart_ptr_api_tag smart_ptr_api{}; + struct SmartPtrApiTag {}; + const SmartPtrApiTag SmartPtrApi{}; /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also @@ -78,7 +78,7 @@ namespace pcpp */ static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); - static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, smart_ptr_api_tag); + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, SmartPtrApiTag); /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is suitable for connecting to @@ -113,7 +113,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; - std::shared_ptr getRemoteDeviceByIP(const IPv4Address &ip4Addr, smart_ptr_api_tag) const; + std::shared_ptr getRemoteDeviceByIP(const IPv4Address &ip4Addr, SmartPtrApiTag) const; /** * Search a PcapRemoteDevice in the list by its IPv6 address @@ -121,7 +121,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; - std::shared_ptr getRemoteDeviceByIP(const IPv6Address &ip4Addr, smart_ptr_api_tag) const; + std::shared_ptr getRemoteDeviceByIP(const IPv6Address &ip4Addr, SmartPtrApiTag) const; /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) @@ -129,7 +129,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; - std::shared_ptr getRemoteDeviceByIP(const IPAddress &ip4Addr, smart_ptr_api_tag) const; + std::shared_ptr getRemoteDeviceByIP(const IPAddress &ip4Addr, SmartPtrApiTag) const; /** * Search a PcapRemoteDevice in the list by its IP address @@ -137,7 +137,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; - std::shared_ptr getRemoteDeviceByIP(const std::string &ip4Addr, smart_ptr_api_tag) const; + std::shared_ptr getRemoteDeviceByIP(const std::string &ip4Addr, SmartPtrApiTag) const; /** * @return An iterator object pointing to the first PcapRemoteDevice in list diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 045dd5b55c..7212598387 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -274,18 +274,18 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAdd // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getPcapLiveDeviceByIp(ipAddr, smart_ptr_api).get(); + return getPcapLiveDeviceByIp(ipAddr, SmartPtrApi).get(); } -std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAddr, smart_ptr_api_tag) const +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAddr, SmartPtrApiTag) const { if (ipAddr.getType() == IPAddress::IPv4AddressType) { - return getPcapLiveDeviceByIp(ipAddr.getIPv4(), smart_ptr_api); + return getPcapLiveDeviceByIp(ipAddr.getIPv4(), SmartPtrApi); } else //IPAddress::IPv6AddressType { - return getPcapLiveDeviceByIp(ipAddr.getIPv6(), smart_ptr_api); + return getPcapLiveDeviceByIp(ipAddr.getIPv6(), SmartPtrApi); } } @@ -294,10 +294,10 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getPcapLiveDeviceByIp(ipAddr, smart_ptr_api).get(); + return getPcapLiveDeviceByIp(ipAddr, SmartPtrApi).get(); } -std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr, smart_ptr_api_tag) const +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all live devices..."); for(const auto &devIter : m_LiveDeviceList) @@ -335,10 +335,10 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getPcapLiveDeviceByIp(ip6Addr, smart_ptr_api).get(); + return getPcapLiveDeviceByIp(ip6Addr, SmartPtrApi).get(); } -std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, smart_ptr_api_tag) const +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all live devices..."); for(const auto &devIter : m_LiveDeviceList) @@ -376,10 +376,10 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipA // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getPcapLiveDeviceByIp(ipAddrAsString, smart_ptr_api).get(); + return getPcapLiveDeviceByIp(ipAddrAsString, SmartPtrApi).get(); } -std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipAddrAsString, smart_ptr_api_tag) const +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipAddrAsString, SmartPtrApiTag) const { IPAddress ipAddr; try @@ -392,7 +392,7 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const return nullptr; } - return PcapLiveDeviceList::getPcapLiveDeviceByIp(ipAddr, smart_ptr_api); + return PcapLiveDeviceList::getPcapLiveDeviceByIp(ipAddr, SmartPtrApi); } @@ -401,10 +401,10 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& n // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getPcapLiveDeviceByName(name, smart_ptr_api).get(); + return getPcapLiveDeviceByName(name, SmartPtrApi).get(); } -std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name, smart_ptr_api_tag) const +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all live devices..."); auto devIter = std::find_if(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), @@ -424,25 +424,25 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::strin // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getPcapLiveDeviceByIpOrName(ipOrName, smart_ptr_api).get(); + return getPcapLiveDeviceByIpOrName(ipOrName, SmartPtrApi).get(); } -std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName, smart_ptr_api_tag) const +std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName, SmartPtrApiTag) const { try { IPAddress interfaceIP = IPAddress(ipOrName); - return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP, smart_ptr_api); + return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP, SmartPtrApi); } catch (std::exception&) { - return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(ipOrName, smart_ptr_api); + return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(ipOrName, SmartPtrApi); } } -PcapLiveDeviceList* PcapLiveDeviceList::clone() const { return clone(smart_ptr_api).release(); } +PcapLiveDeviceList* PcapLiveDeviceList::clone() const { return clone(SmartPtrApi).release(); } -std::unique_ptr PcapLiveDeviceList::clone(smart_ptr_api_tag) const +std::unique_ptr PcapLiveDeviceList::clone(SmartPtrApiTag) const { return std::unique_ptr(new PcapLiveDeviceList()); } diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index f9a49d4d4d..c9bb770bc3 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -37,7 +37,7 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, nullptr); } -std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, PcapRemoteDeviceList::smart_ptr_api_tag) +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, PcapRemoteDeviceList::SmartPtrApiTag) { return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, std::unique_ptr()); } @@ -109,10 +109,10 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& i // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getRemoteDeviceByIP(ipAddrAsString, smart_ptr_api).get(); + return getRemoteDeviceByIP(ipAddrAsString, SmartPtrApi).get(); } -std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& ipAddrAsString, smart_ptr_api_tag) const +std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& ipAddrAsString, SmartPtrApiTag) const { IPAddress ipAddr; @@ -126,7 +126,7 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons return nullptr; } - return getRemoteDeviceByIP(ipAddr, smart_ptr_api); + return getRemoteDeviceByIP(ipAddr, SmartPtrApi); } PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipAddr) const @@ -134,18 +134,18 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipA // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getRemoteDeviceByIP(ipAddr, smart_ptr_api).get(); + return getRemoteDeviceByIP(ipAddr, SmartPtrApi).get(); } -std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipAddr, smart_ptr_api_tag) const +std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipAddr, SmartPtrApiTag) const { if (ipAddr.getType() == IPAddress::IPv4AddressType) { - return getRemoteDeviceByIP(ipAddr.getIPv4(), smart_ptr_api); + return getRemoteDeviceByIP(ipAddr.getIPv4(), SmartPtrApi); } else //IPAddress::IPv6AddressType { - return getRemoteDeviceByIP(ipAddr.getIPv6(), smart_ptr_api); + return getRemoteDeviceByIP(ipAddr.getIPv6(), SmartPtrApi); } } @@ -154,10 +154,10 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getRemoteDeviceByIP(ip4Addr, smart_ptr_api).get(); + return getRemoteDeviceByIP(ip4Addr, SmartPtrApi).get(); } -std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr, smart_ptr_api_tag) const +std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); for(auto& devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); ++devIter) @@ -196,10 +196,10 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address &i // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the // device alive. - return getRemoteDeviceByIP(ip6Addr, smart_ptr_api).get(); + return getRemoteDeviceByIP(ip6Addr, SmartPtrApi).get(); } -std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr, smart_ptr_api_tag) const +std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); for(auto& devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); ++devIter) From 9c26770a13c024c834e5713a3fe346b25b87870e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 09:57:14 +0300 Subject: [PATCH 39/81] Refactored device loop to for-each loop. --- Pcap++/src/PcapRemoteDeviceList.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index c9bb770bc3..422bba7ba7 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -160,10 +160,10 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); - for(auto& devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); ++devIter) + for(const auto& devIter : m_RemoteDeviceList) { - PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); - for(const auto &addrIter : (*devIter)->m_Addresses) + PCPP_LOG_DEBUG("Searching device '" << devIter->m_Name << "'. Searching all addresses..."); + for(const auto &addrIter : devIter->m_Addresses) { if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != NULL) { @@ -182,7 +182,7 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons if (currAddr->s_addr == ip4Addr.toInt()) { PCPP_LOG_DEBUG("Found matched address!"); - return *devIter; + return devIter; } } } @@ -202,10 +202,10 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address &i std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); - for(auto& devIter = m_RemoteDeviceList.begin(); devIter != m_RemoteDeviceList.end(); ++devIter) + for(const auto& devIter : m_RemoteDeviceList) { - PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); - for(const auto &addrIter : (*devIter)->m_Addresses) + PCPP_LOG_DEBUG("Searching device '" << devIter->m_Name << "'. Searching all addresses..."); + for(const auto& addrIter : devIter->m_Addresses) { if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != NULL) { @@ -224,7 +224,7 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { PCPP_LOG_DEBUG("Found matched address!"); - return *devIter; + return devIter; } } } From bf7516cb8596b02111dc1e8ad6572386f7b61c42 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 10:02:57 +0300 Subject: [PATCH 40/81] Renamed loop variables to reflect the new for-each loops, as the variables are no longer iterators. --- Pcap++/src/PcapLiveDeviceList.cpp | 28 ++++++++++++------------- Pcap++/src/PcapRemoteDeviceList.cpp | 32 ++++++++++++++--------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 7212598387..2e06c14ddb 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -300,19 +300,19 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(const auto &devIter : m_LiveDeviceList) + for(const auto& device : m_LiveDeviceList) { - PCPP_LOG_DEBUG("Searching device '" << devIter->m_Name << "'. Searching all addresses..."); - for(const auto &addrIter : devIter->m_Addresses) + PCPP_LOG_DEBUG("Searching device '" << device->m_Name << "'. Searching all addresses..."); + for(const auto& addressInfo : device->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addressInfo.addr != nullptr) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter.addr, addrAsString); + internal::sockaddr2string(addressInfo.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in_addr* currAddr = internal::sockaddr2in_addr(addrIter.addr); + in_addr* currAddr = internal::sockaddr2in_addr(addressInfo.addr); if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is nullptr"); @@ -322,7 +322,7 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const if (currAddr->s_addr == ipAddr.toInt()) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter; + return device; } } } @@ -341,19 +341,19 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(const auto &devIter : m_LiveDeviceList) + for(const auto& device : m_LiveDeviceList) { - PCPP_LOG_DEBUG("Searching device '" << devIter->m_Name << "'. Searching all addresses..."); - for(const auto &addrIter : devIter->m_Addresses) + PCPP_LOG_DEBUG("Searching device '" << device->m_Name << "'. Searching all addresses..."); + for(const auto& addressInfo : device->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addressInfo.addr != nullptr) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter.addr, addrAsString); + internal::sockaddr2string(addressInfo.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in6_addr* currAddr = internal::sockaddr2in6_addr(addrIter.addr); + in6_addr* currAddr = internal::sockaddr2in6_addr(addressInfo.addr); if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is nullptr"); @@ -363,7 +363,7 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter; + return device; } } } diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 422bba7ba7..ce8dd16a8a 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -160,20 +160,20 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); - for(const auto& devIter : m_RemoteDeviceList) + for(const auto& device : m_RemoteDeviceList) { - PCPP_LOG_DEBUG("Searching device '" << devIter->m_Name << "'. Searching all addresses..."); - for(const auto &addrIter : devIter->m_Addresses) + PCPP_LOG_DEBUG("Searching device '" << device->m_Name << "'. Searching all addresses..."); + for(const auto& addressInfo : device->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != NULL) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addressInfo.addr != nullptr) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter.addr, addrAsString); + internal::sockaddr2string(addressInfo.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in_addr* currAddr = internal::sockaddr2in_addr(addrIter.addr); - if (currAddr == NULL) + in_addr* currAddr = internal::sockaddr2in_addr(addressInfo.addr); + if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is NULL"); continue; @@ -182,7 +182,7 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons if (currAddr->s_addr == ip4Addr.toInt()) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter; + return device; } } } @@ -202,20 +202,20 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address &i std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr, SmartPtrApiTag) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); - for(const auto& devIter : m_RemoteDeviceList) + for(const auto& device : m_RemoteDeviceList) { - PCPP_LOG_DEBUG("Searching device '" << devIter->m_Name << "'. Searching all addresses..."); - for(const auto& addrIter : devIter->m_Addresses) + PCPP_LOG_DEBUG("Searching device '" << device->m_Name << "'. Searching all addresses..."); + for(const auto& addressInfo : device->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != NULL) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addressInfo.addr != nullptr) { char addrAsString[INET6_ADDRSTRLEN]; - internal::sockaddr2string(addrIter.addr, addrAsString); + internal::sockaddr2string(addressInfo.addr, addrAsString); PCPP_LOG_DEBUG("Searching address " << addrAsString); } - in6_addr* currAddr = internal::sockaddr2in6_addr(addrIter.addr); - if (currAddr == NULL) + in6_addr* currAddr = internal::sockaddr2in6_addr(addressInfo.addr); + if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is NULL"); continue; @@ -224,7 +224,7 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter; + return device; } } } From a8e97b0879b6f4dc029acb085a17a114004760cf Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 10:03:48 +0300 Subject: [PATCH 41/81] Fixed log message grammar. --- Pcap++/src/PcapLiveDeviceList.cpp | 4 ++-- Pcap++/src/PcapRemoteDeviceList.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 2e06c14ddb..79d24acf58 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -321,7 +321,7 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const if (currAddr->s_addr == ipAddr.toInt()) { - PCPP_LOG_DEBUG("Found matched address!"); + PCPP_LOG_DEBUG("Found matching address!"); return device; } } @@ -362,7 +362,7 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { - PCPP_LOG_DEBUG("Found matched address!"); + PCPP_LOG_DEBUG("Found matching address!"); return device; } } diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index ce8dd16a8a..2ec598051b 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -181,7 +181,7 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons if (currAddr->s_addr == ip4Addr.toInt()) { - PCPP_LOG_DEBUG("Found matched address!"); + PCPP_LOG_DEBUG("Found matching address!"); return device; } } @@ -223,7 +223,7 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons if (memcmp(currAddr, ip6Addr.toBytes(), sizeof(struct in6_addr)) == 0) { - PCPP_LOG_DEBUG("Found matched address!"); + PCPP_LOG_DEBUG("Found matching address!"); return device; } } From 6436b019af89a7d86f6ea22a9b3413becf874ec7 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 10:13:29 +0300 Subject: [PATCH 42/81] Explicitly marks "deleted" copy ctors as deleted as in C++11 standard. --- Pcap++/header/PcapLiveDeviceList.h | 6 +++--- Pcap++/header/PcapRemoteDeviceList.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 75ed7db6a2..93051cf946 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -32,9 +32,6 @@ namespace pcpp // private c'tor PcapLiveDeviceList(); - // private copy c'tor - PcapLiveDeviceList( const PcapLiveDeviceList& other ); - PcapLiveDeviceList& operator=(const PcapLiveDeviceList& other); void init(); @@ -42,6 +39,9 @@ namespace pcpp void updateLiveDeviceListView() const; public: + PcapLiveDeviceList(const PcapLiveDeviceList& other) = delete; + PcapLiveDeviceList& operator=(const PcapLiveDeviceList& other) = delete; + /* * @class SmartPtrApiTag * Helper tag to disambiguate smart pointer API. diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index e4300785cb..acac862032 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -37,15 +37,15 @@ namespace pcpp // private c'tor. User should create the list via static methods PcapRemoteDeviceList::getRemoteDeviceList() PcapRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList); - // private copy c'tor - PcapRemoteDeviceList(const PcapRemoteDeviceList& other); - PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other); void updateDeviceListView(); // Implementation that uses a shared ptr is private to guarantee that the remote auth object is not shared externally. It is used by the other overloads. static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth); public: + PcapRemoteDeviceList(const PcapRemoteDeviceList& other) = delete; + PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other) = delete; + /** * Iterator object that can be used for iterating all PcapRemoteDevice in list */ From 89d4b2486849f22329aa7d7d026c1b4b94ba523a Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 20:55:39 +0300 Subject: [PATCH 43/81] Updated remote device list documentation. --- Pcap++/header/PcapRemoteDeviceList.h | 91 ++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 13 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index acac862032..318b664190 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -40,7 +40,8 @@ namespace pcpp void updateDeviceListView(); - // Implementation that uses a shared ptr is private to guarantee that the remote auth object is not shared externally. It is used by the other overloads. + // Implementation that uses a shared ptr is private to guarantee that the remote auth object is not shared externally. + // It is used by the other overloads for casting different kinds of pointers/references into shared_ptr. static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth); public: PcapRemoteDeviceList(const PcapRemoteDeviceList& other) = delete; @@ -61,6 +62,9 @@ namespace pcpp * Helper tag to disambiguate smart pointer API. */ struct SmartPtrApiTag {}; + /** + * Helper tag constant for disambuguating smart pointer API. + */ const SmartPtrApiTag SmartPtrApi{}; /** @@ -71,13 +75,26 @@ namespace pcpp * use the other method overload * @param[in] ipAddress The IP address of the remote machine through which clients can connect to the rpcapd daemon * @param[in] port The port of the remote machine through which clients can connect to the rpcapd daemon - * @return A pointer to the newly created PcapRemoteDeviceList, or NULL if (an appropriate error will be printed to log in each case): - * - IP address provided is NULL or not valid + * @return A pointer to the newly created PcapRemoteDeviceList, or nullptr if (an appropriate error will be printed to log in each case): + * - IP address provided is not valid * - WinPcap/Npcap encountered an error in creating the remote connection string * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine */ static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); - + /** + * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also + * creates a list of PcapRemoteDevice instances stored in it, one for each remote network interface. Notice this method allocates + * the PcapRemoteDeviceList instance and returns a unique pointer to it.
+ * This method overload is for remote daemons which don't require authentication for accessing them. For daemons which do require authentication + * use the other method overload. + * @param[in] ipAddress The IP address of the remote machine through which clients can connect to the rpcapd daemon + * @param[in] port The port of the remote machine through which clients can connect to the rpcapd daemon + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return An unique pointer to the newly created PcapRemoteDeviceList or a nullptr if (an appropriate error will be printed to log in each case): + * - IP address provided is not valid + * - WinPcap/Npcap encountered an error in creating the remote connection string + * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine + */ static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, SmartPtrApiTag); /** @@ -88,13 +105,37 @@ namespace pcpp * @param[in] remoteAuth A pointer to the authentication object which contains the username and password for connecting to the remote * daemon * @return A pointer to the newly created PcapRemoteDeviceList, or NULL if (an appropriate error will be printed to log in each case): - * - IP address provided is NULL or not valid + * - IP address provided is not valid * - WinPcap/Npcap encountered an error in creating the remote connection string * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine */ static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); - + /** + * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is + * suitable for connecting to remote daemons which require authentication for accessing them + * @param[in] ipAddress The IP address of the remote machine through which clients can connect to the rpcapd daemon + * @param[in] port The port of the remote machine through which clients can connect to the rpcapd daemon + * @param[in] remoteAuth An unique pointer to the authentication object which contains the username and password for connecting to the remote daemon + * @return An unique pointer to the newly created PcapRemoteDeviceList, or nullptr if (an appropriate error will be printed to log in each case): + * - IP address provided is not valid + * - WinPcap/Npcap encountered an error in creating the remote connection string + * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving + * devices on the remote machine + */ static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::unique_ptr remoteAuth); + /** + * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is + * suitable for connecting to remote daemons which require authentication for accessing them + * @param[in] ipAddress The IP address of the remote machine through which clients can connect to the rpcapd daemon + * @param[in] port The port of the remote machine through which clients can connect to the rpcapd daemon + * @param[in] remoteAuth A reference to the authentication object which contains the username and password for connecting to the remote daemon + * @return An unique pointer to the newly created PcapRemoteDeviceList, or nullptr if (an appropriate error will be printed + * to log in each case): + * - IP address provided is not valid + * - WinPcap/Npcap encountered an error in creating the remote connection string + * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving + * devices on the remote machine + */ static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, const PcapRemoteAuthentication& remoteAuth); /** @@ -110,34 +151,58 @@ namespace pcpp /** * Search a PcapRemoteDevice in the list by its IPv4 address * @param[in] ip4Addr The IPv4 address - * @return The PcapRemoteDevice if found, NULL otherwise + * @return A pointer to PcapRemoteDevice if found, nullptr otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; + /** + * Search a PcapRemoteDevice in the list by its IPv4 address + * @param[in] ip4Addr The IPv4 address + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise + */ std::shared_ptr getRemoteDeviceByIP(const IPv4Address &ip4Addr, SmartPtrApiTag) const; /** * Search a PcapRemoteDevice in the list by its IPv6 address * @param[in] ip6Addr The IPv6 address - * @return The PcapRemoteDevice if found, NULL otherwise + * @return A pointer to PcapRemoteDevice if found, nullptr otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; - std::shared_ptr getRemoteDeviceByIP(const IPv6Address &ip4Addr, SmartPtrApiTag) const; + /** + * Search a PcapRemoteDevice in the list by its IPv6 address + * @param[in] ip6Addr The IPv6 address + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise + */ + std::shared_ptr getRemoteDeviceByIP(const IPv6Address& ip6Addr, SmartPtrApiTag) const; /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) * @param[in] ipAddr The IP address - * @return The PcapRemoteDevice if found, NULL otherwise + * @return A pointer to PcapRemoteDevice if found, nullptr otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; - std::shared_ptr getRemoteDeviceByIP(const IPAddress &ip4Addr, SmartPtrApiTag) const; + /** + * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) + * @param[in] ipAddr The IP address + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise + */ + std::shared_ptr getRemoteDeviceByIP(const IPAddress& ipAddr, SmartPtrApiTag) const; /** * Search a PcapRemoteDevice in the list by its IP address * @param[in] ipAddrAsString The IP address in string format - * @return The PcapRemoteDevice if found, NULL otherwise + * @return A pointer to PcapRemoteDevice if found, nullptr otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; - std::shared_ptr getRemoteDeviceByIP(const std::string &ip4Addr, SmartPtrApiTag) const; + /** + * Search a PcapRemoteDevice in the list by its IP address + * @param[in] ipAddrAsString The IP address in string format + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise + */ + std::shared_ptr getRemoteDeviceByIP(const std::string& ipAddrAsString, SmartPtrApiTag) const; /** * @return An iterator object pointing to the first PcapRemoteDevice in list From 9628d3de5528db35e2edcb7bc458aedc83e86d45 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 20:58:10 +0300 Subject: [PATCH 44/81] Added exception if IPAddress type is not IPv4 or IPv6 for some reason. --- Pcap++/src/PcapRemoteDeviceList.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 2ec598051b..e2d7bd2764 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -139,13 +139,14 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipA std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipAddr, SmartPtrApiTag) const { - if (ipAddr.getType() == IPAddress::IPv4AddressType) + switch (ipAddr.getType()) { + case IPAddress::IPv4AddressType: return getRemoteDeviceByIP(ipAddr.getIPv4(), SmartPtrApi); - } - else //IPAddress::IPv6AddressType - { + case IPAddress::IPv6AddressType: return getRemoteDeviceByIP(ipAddr.getIPv6(), SmartPtrApi); + default: + throw std::invalid_argument("Unsupported IP Address type."); } } From 2ac28f96d4d561858f63c58b3fa9f6389822b265 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:04:00 +0300 Subject: [PATCH 45/81] Updated documentation for LiveDeviceList --- Pcap++/header/PcapLiveDeviceList.h | 53 ++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 93051cf946..21c1ba0b9f 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -63,54 +63,93 @@ namespace pcpp * @return A vector containing pointers to all live devices currently installed on the machine */ const std::vector& getPcapLiveDevicesList() const; - const std::vector> &getPcapLiveDevicesList(SmartPtrApiTag) const { return m_LiveDeviceList; }; + /** + * @return A reference to a vector containing shared pointers to all live devices currently installed on the machine. + */ + const std::vector>& getPcapLiveDevicesList(SmartPtrApiTag) const { return m_LiveDeviceList; }; /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 * @param[in] ipAddr The IP address defined for the device - * @return A pointer to the live device if this IP address exists. NULL otherwise + * @return A pointer to the live device if this IP address exists, nullptr otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; + /** + * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 + * @param[in] ipAddr The IP address defined for the device + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the live device if this IP address exists, nullptr otherwise + */ std::shared_ptr getPcapLiveDeviceByIp(const IPAddress& ipAddr, SmartPtrApiTag) const; /** * Get a pointer to the live device by its IPv4 address * @param[in] ipAddr The IPv4 address defined for the device - * @return A pointer to the live device if this IPv4 address exists. NULL otherwise + * @return A pointer to the live device if this IPv4 address exists, nullptr otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; + /** + * Get a pointer to the live device by its IPv4 address + * @param[in] ipAddr The IPv4 address defined for the device + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the live device if this IPv4 address exists, nullptr otherwise + */ std::shared_ptr getPcapLiveDeviceByIp(const IPv4Address& ipAddr, SmartPtrApiTag) const; /** * Get a pointer to the live device by its IPv6 address * @param[in] ip6Addr The IPv6 address defined for the device - * @return A pointer to the live device if this IPv6 address exists. NULL otherwise + * @return A pointer to the live device if this IPv6 address exists, nullptr otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; + /** + * Get a pointer to the live device by its IPv6 address + * @param[in] ip6Addr The IPv6 address defined for the device + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the live device if this IPv6 address exists, nullptr otherwise + */ std::shared_ptr getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, SmartPtrApiTag) const; /** * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 * @param[in] ipAddrAsString The IP address defined for the device as string - * @return A pointer to the live device if this IP address is valid and exists. NULL otherwise + * @return A pointer to the live device if this IP address is valid and exists, nullptr otherwise */ PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; + /** + * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 + * @param[in] ipAddrAsString The IP address defined for the device as string + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the live device if this IP address is valid and exists, nullptr otherwise + */ std::shared_ptr getPcapLiveDeviceByIp(const std::string& ipAddrAsString, SmartPtrApiTag) const; /** * Get a pointer to the live device by its name * @param[in] name The name of the interface (e.g eth0) - * @return A pointer to the live device if this name exists. NULL otherwise + * @return A pointer to the live device if this name exists, nullprt otherwise */ PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; + /** + * Get a pointer to the live device by its name + * @param[in] name The name of the interface (e.g eth0) + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the live device if this name exists, nullptr otherwise + */ std::shared_ptr getPcapLiveDeviceByName(const std::string& name, SmartPtrApiTag) const; /** * Get a pointer to the live device by its IP address or name * @param[in] ipOrName An IP address or name of the interface - * @return A pointer to the live device if exists, NULL otherwise + * @return A pointer to the live device if exists, nullptr otherwise */ PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; + /** + * Get a pointer to the live device by its IP address or name + * @param[in] ipOrName An IP address or name of the interface + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return A shared pointer to the live device if exists, nullptr otherwise + */ std::shared_ptr getPcapLiveDeviceByIpOrName(const std::string& ipOrName, SmartPtrApiTag) const; /** From 11a4f63c012ea1cb2e98e54b2e664e3b32d6a528 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:12:03 +0300 Subject: [PATCH 46/81] Made the smart ptr api tag static constant. --- Pcap++/header/PcapLiveDeviceList.h | 5 ++++- Pcap++/header/PcapRemoteDeviceList.h | 2 +- Pcap++/src/PcapLiveDeviceList.cpp | 2 ++ Pcap++/src/PcapRemoteDeviceList.cpp | 2 ++ 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 21c1ba0b9f..1dcee56df2 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -47,7 +47,10 @@ namespace pcpp * Helper tag to disambiguate smart pointer API. */ struct SmartPtrApiTag {}; - const SmartPtrApiTag SmartPtrApi{}; + /** + * Helper tag constant for disambuguating smart pointer API. + */ + static const SmartPtrApiTag SmartPtrApi; /** * The access method to the singleton diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 318b664190..f8bea4fc9c 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -65,7 +65,7 @@ namespace pcpp /** * Helper tag constant for disambuguating smart pointer API. */ - const SmartPtrApiTag SmartPtrApi{}; + static const SmartPtrApiTag SmartPtrApi; /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 79d24acf58..300c703f1c 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -24,6 +24,8 @@ namespace pcpp { +const PcapLiveDeviceList::SmartPtrApiTag PcapLiveDeviceList::SmartPtrApi{}; + PcapLiveDeviceList::PcapLiveDeviceList() { init(); diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index e2d7bd2764..b1d211c51b 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -12,6 +12,8 @@ namespace pcpp { +const PcapRemoteDeviceList::SmartPtrApiTag PcapRemoteDeviceList::SmartPtrApi{}; + PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList) : m_RemoteDeviceList(std::move(deviceList)), m_RemoteMachineIpAddress(ipAddress), m_RemoteMachinePort(port), m_RemoteAuthentication(std::move(remoteAuth)) { From b0b2a8351b43247e00b25583f9442306ac8a6aa1 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:26:18 +0300 Subject: [PATCH 47/81] Added unique_ptr overload for remote authentication as a ptr. --- Pcap++/header/PcapRemoteDeviceList.h | 16 ++++++++++++++-- Pcap++/src/PcapRemoteDeviceList.cpp | 9 ++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index f8bea4fc9c..38d6391545 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -102,14 +102,26 @@ namespace pcpp * remote daemons which require authentication for accessing them * @param[in] ipAddress The IP address of the remote machine through which clients can connect to the rpcapd daemon * @param[in] port The port of the remote machine through which clients can connect to the rpcapd daemon - * @param[in] remoteAuth A pointer to the authentication object which contains the username and password for connecting to the remote - * daemon + * @param[in] remoteAuth A pointer to the authentication object which contains the username and password for connecting to the remote daemon * @return A pointer to the newly created PcapRemoteDeviceList, or NULL if (an appropriate error will be printed to log in each case): * - IP address provided is not valid * - WinPcap/Npcap encountered an error in creating the remote connection string * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine */ static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); + /** + * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is suitable for connecting to + * remote daemons which require authentication for accessing them + * @param[in] ipAddress The IP address of the remote machine through which clients can connect to the rpcapd daemon + * @param[in] port The port of the remote machine through which clients can connect to the rpcapd daemon + * @param[in] remoteAuth A pointer to the authentication object which contains the username and password for connecting to the remote daemon + * @param[in] Disambiguating tag for SmartPtrAPI. + * @return An unique pointer to the newly created PcapRemoteDeviceList, or NULL if (an appropriate error will be printed to log in each case): + * - IP address provided is not valid + * - WinPcap/Npcap encountered an error in creating the remote connection string + * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine + */ + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth, SmartPtrApiTag); /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is * suitable for connecting to remote daemons which require authentication for accessing them diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index b1d211c51b..6b3c16a30d 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -46,10 +46,13 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth) { - // Uses the smart pointer version and releases management of the object to the caller. + return getRemoteDeviceList(ipAddress, port, remoteAuth, SmartPtrApi).release(); +} + +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth, PcapRemoteDeviceList::SmartPtrApiTag) +{ std::unique_ptr auth = remoteAuth != nullptr ? std::unique_ptr(new PcapRemoteAuthentication(*remoteAuth)) : nullptr; - std::unique_ptr uPtr = getRemoteDeviceList(ipAddress, port, std::move(auth)); - return uPtr.release(); + return getRemoteDeviceList(ipAddress, port, std::move(auth)); } std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, const PcapRemoteAuthentication& remoteAuth) From dd09da03adfdbc0a4b60270dcd8554811a9c51b5 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:36:27 +0300 Subject: [PATCH 48/81] Added deprecation warnings to old Raw pointer API. --- Pcap++/header/PcapLiveDeviceList.h | 26 ++++++++++++++++++-------- Pcap++/header/PcapRemoteDeviceList.h | 21 ++++++++++++++------- Pcap++/src/PcapRemoteDeviceList.cpp | 2 +- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 1dcee56df2..4f672167a2 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -1,5 +1,6 @@ #pragma once +#include "DeprecationUtils.h" #include "IpAddress.h" #include "PcapLiveDevice.h" #include @@ -64,8 +65,9 @@ namespace pcpp /** * @return A vector containing pointers to all live devices currently installed on the machine + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - const std::vector& getPcapLiveDevicesList() const; + PCPP_DEPRECATED const std::vector& getPcapLiveDevicesList() const; /** * @return A reference to a vector containing shared pointers to all live devices currently installed on the machine. */ @@ -75,8 +77,9 @@ namespace pcpp * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 * @param[in] ipAddr The IP address defined for the device * @return A pointer to the live device if this IP address exists, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; + PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 * @param[in] ipAddr The IP address defined for the device @@ -89,8 +92,9 @@ namespace pcpp * Get a pointer to the live device by its IPv4 address * @param[in] ipAddr The IPv4 address defined for the device * @return A pointer to the live device if this IPv4 address exists, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; + PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; /** * Get a pointer to the live device by its IPv4 address * @param[in] ipAddr The IPv4 address defined for the device @@ -103,13 +107,15 @@ namespace pcpp * Get a pointer to the live device by its IPv6 address * @param[in] ip6Addr The IPv6 address defined for the device * @return A pointer to the live device if this IPv6 address exists, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; + PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; /** * Get a pointer to the live device by its IPv6 address * @param[in] ip6Addr The IPv6 address defined for the device * @param[in] Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the live device if this IPv6 address exists, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ std::shared_ptr getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, SmartPtrApiTag) const; @@ -117,8 +123,9 @@ namespace pcpp * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 * @param[in] ipAddrAsString The IP address defined for the device as string * @return A pointer to the live device if this IP address is valid and exists, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; + PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; /** * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 * @param[in] ipAddrAsString The IP address defined for the device as string @@ -131,8 +138,9 @@ namespace pcpp * Get a pointer to the live device by its name * @param[in] name The name of the interface (e.g eth0) * @return A pointer to the live device if this name exists, nullprt otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; + PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; /** * Get a pointer to the live device by its name * @param[in] name The name of the interface (e.g eth0) @@ -145,8 +153,9 @@ namespace pcpp * Get a pointer to the live device by its IP address or name * @param[in] ipOrName An IP address or name of the interface * @return A pointer to the live device if exists, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; + PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; /** * Get a pointer to the live device by its IP address or name * @param[in] ipOrName An IP address or name of the interface @@ -164,8 +173,9 @@ namespace pcpp /** * Copies the current live device list * @return A pointer to the cloned device list + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapLiveDeviceList* clone() const; + PCPP_DEPRECATED PcapLiveDeviceList* clone() const; /** * Copies the current live device list * @return A unique ptr managing the cloned device list diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 38d6391545..ede0c5ad22 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -3,6 +3,7 @@ #if defined(_WIN32) #include +#include "DeprecationUtils.h" #include "IpAddress.h" #include "PcapRemoteDevice.h" @@ -79,8 +80,9 @@ namespace pcpp * - IP address provided is not valid * - WinPcap/Npcap encountered an error in creating the remote connection string * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); + PCPP_DEPRECATED static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also * creates a list of PcapRemoteDevice instances stored in it, one for each remote network interface. Notice this method allocates @@ -107,8 +109,9 @@ namespace pcpp * - IP address provided is not valid * - WinPcap/Npcap encountered an error in creating the remote connection string * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); + PCPP_DEPRECATED static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is suitable for connecting to * remote daemons which require authentication for accessing them @@ -164,22 +167,24 @@ namespace pcpp * Search a PcapRemoteDevice in the list by its IPv4 address * @param[in] ip4Addr The IPv4 address * @return A pointer to PcapRemoteDevice if found, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; + PCPP_DEPRECATED PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; /** * Search a PcapRemoteDevice in the list by its IPv4 address * @param[in] ip4Addr The IPv4 address * @param[in] Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise */ - std::shared_ptr getRemoteDeviceByIP(const IPv4Address &ip4Addr, SmartPtrApiTag) const; + std::shared_ptr getRemoteDeviceByIP(const IPv4Address& ip4Addr, SmartPtrApiTag) const; /** * Search a PcapRemoteDevice in the list by its IPv6 address * @param[in] ip6Addr The IPv6 address * @return A pointer to PcapRemoteDevice if found, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; + PCPP_DEPRECATED PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; /** * Search a PcapRemoteDevice in the list by its IPv6 address * @param[in] ip6Addr The IPv6 address @@ -192,8 +197,9 @@ namespace pcpp * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) * @param[in] ipAddr The IP address * @return A pointer to PcapRemoteDevice if found, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; + PCPP_DEPRECATED PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) * @param[in] ipAddr The IP address @@ -206,8 +212,9 @@ namespace pcpp * Search a PcapRemoteDevice in the list by its IP address * @param[in] ipAddrAsString The IP address in string format * @return A pointer to PcapRemoteDevice if found, nullptr otherwise + * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; + PCPP_DEPRECATED PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; /** * Search a PcapRemoteDevice in the list by its IP address * @param[in] ipAddrAsString The IP address in string format diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 6b3c16a30d..ef87497fb2 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -36,7 +36,7 @@ void PcapRemoteDeviceList::updateDeviceListView() PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port) { - return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, nullptr); + return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, nullptr, SmartPtrApi).release(); } std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, PcapRemoteDeviceList::SmartPtrApiTag) From e6123dd3e12c8a5d024c484015df4ad38e35a363 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:39:50 +0300 Subject: [PATCH 49/81] Fixed class documentation. --- Pcap++/header/MemoryUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/MemoryUtils.h b/Pcap++/header/MemoryUtils.h index db058b1a0f..5459c70a01 100644 --- a/Pcap++/header/MemoryUtils.h +++ b/Pcap++/header/MemoryUtils.h @@ -11,7 +11,7 @@ namespace pcpp namespace internal { /** - * @class PcapTDeleter + * @class PcapCloseDeleter * A deleter that cleans up a pcap_t structure by calling pcap_close. */ struct PcapCloseDeleter From 0daf110b097639a5c34d31f8e22342abfb85ce86 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:41:40 +0300 Subject: [PATCH 50/81] Fixed class documentation. --- Pcap++/header/MemoryUtils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/MemoryUtils.h b/Pcap++/header/MemoryUtils.h index 5459c70a01..6e0eeb3431 100644 --- a/Pcap++/header/MemoryUtils.h +++ b/Pcap++/header/MemoryUtils.h @@ -20,7 +20,7 @@ namespace pcpp }; /** - * @class FreeAllDevsDeleter + * @class PcapFreeAllDevsDeleter * A deleter that frees an interface list of pcap_if_t ptr by calling 'pcap_freealldevs' function on it. */ struct PcapFreeAllDevsDeleter From 5d4703a92e9e89c1ed4e396223c01186d47d8d12 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:47:43 +0300 Subject: [PATCH 51/81] Added name to tag variable to keep doxygen happy. --- Pcap++/header/PcapLiveDeviceList.h | 23 ++++++++++++----------- Pcap++/header/PcapRemoteDeviceList.h | 22 +++++++++++----------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 4f672167a2..ff7ec31674 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -83,10 +83,10 @@ namespace pcpp /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 * @param[in] ipAddr The IP address defined for the device - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the live device if this IP address exists, nullptr otherwise */ - std::shared_ptr getPcapLiveDeviceByIp(const IPAddress& ipAddr, SmartPtrApiTag) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPAddress& ipAddr, SmartPtrApiTag apiTag) const; /** * Get a pointer to the live device by its IPv4 address @@ -98,10 +98,10 @@ namespace pcpp /** * Get a pointer to the live device by its IPv4 address * @param[in] ipAddr The IPv4 address defined for the device - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the live device if this IPv4 address exists, nullptr otherwise */ - std::shared_ptr getPcapLiveDeviceByIp(const IPv4Address& ipAddr, SmartPtrApiTag) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPv4Address& ipAddr, SmartPtrApiTag apiTag) const; /** * Get a pointer to the live device by its IPv6 address @@ -113,11 +113,11 @@ namespace pcpp /** * Get a pointer to the live device by its IPv6 address * @param[in] ip6Addr The IPv6 address defined for the device - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the live device if this IPv6 address exists, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - std::shared_ptr getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, SmartPtrApiTag) const; + std::shared_ptr getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, SmartPtrApiTag apiTag) const; /** * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 @@ -129,10 +129,10 @@ namespace pcpp /** * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 * @param[in] ipAddrAsString The IP address defined for the device as string - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the live device if this IP address is valid and exists, nullptr otherwise */ - std::shared_ptr getPcapLiveDeviceByIp(const std::string& ipAddrAsString, SmartPtrApiTag) const; + std::shared_ptr getPcapLiveDeviceByIp(const std::string& ipAddrAsString, SmartPtrApiTag apiTag) const; /** * Get a pointer to the live device by its name @@ -159,10 +159,10 @@ namespace pcpp /** * Get a pointer to the live device by its IP address or name * @param[in] ipOrName An IP address or name of the interface - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the live device if exists, nullptr otherwise */ - std::shared_ptr getPcapLiveDeviceByIpOrName(const std::string& ipOrName, SmartPtrApiTag) const; + std::shared_ptr getPcapLiveDeviceByIpOrName(const std::string& ipOrName, SmartPtrApiTag apiTag) const; /** * @return A list of all DNS servers defined for this machine. If this list is empty it means no DNS servers were defined or they @@ -178,9 +178,10 @@ namespace pcpp PCPP_DEPRECATED PcapLiveDeviceList* clone() const; /** * Copies the current live device list + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A unique ptr managing the cloned device list */ - std::unique_ptr clone(SmartPtrApiTag) const; + std::unique_ptr clone(SmartPtrApiTag apiTag) const; /** * Reset the live device list and DNS server list, meaning clear and refetch them diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index ede0c5ad22..3600a295e2 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -97,7 +97,7 @@ namespace pcpp * - WinPcap/Npcap encountered an error in creating the remote connection string * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine */ - static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, SmartPtrApiTag); + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, SmartPtrApiTag apiTag); /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is suitable for connecting to @@ -118,13 +118,13 @@ namespace pcpp * @param[in] ipAddress The IP address of the remote machine through which clients can connect to the rpcapd daemon * @param[in] port The port of the remote machine through which clients can connect to the rpcapd daemon * @param[in] remoteAuth A pointer to the authentication object which contains the username and password for connecting to the remote daemon - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return An unique pointer to the newly created PcapRemoteDeviceList, or NULL if (an appropriate error will be printed to log in each case): * - IP address provided is not valid * - WinPcap/Npcap encountered an error in creating the remote connection string * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine */ - static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth, SmartPtrApiTag); + static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth, SmartPtrApiTag apiTag); /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is * suitable for connecting to remote daemons which require authentication for accessing them @@ -173,10 +173,10 @@ namespace pcpp /** * Search a PcapRemoteDevice in the list by its IPv4 address * @param[in] ip4Addr The IPv4 address - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise */ - std::shared_ptr getRemoteDeviceByIP(const IPv4Address& ip4Addr, SmartPtrApiTag) const; + std::shared_ptr getRemoteDeviceByIP(const IPv4Address& ip4Addr, SmartPtrApiTag apiTag) const; /** * Search a PcapRemoteDevice in the list by its IPv6 address @@ -188,10 +188,10 @@ namespace pcpp /** * Search a PcapRemoteDevice in the list by its IPv6 address * @param[in] ip6Addr The IPv6 address - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise */ - std::shared_ptr getRemoteDeviceByIP(const IPv6Address& ip6Addr, SmartPtrApiTag) const; + std::shared_ptr getRemoteDeviceByIP(const IPv6Address& ip6Addr, SmartPtrApiTag apiTag) const; /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) @@ -203,10 +203,10 @@ namespace pcpp /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) * @param[in] ipAddr The IP address - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise */ - std::shared_ptr getRemoteDeviceByIP(const IPAddress& ipAddr, SmartPtrApiTag) const; + std::shared_ptr getRemoteDeviceByIP(const IPAddress& ipAddr, SmartPtrApiTag apiTag) const; /** * Search a PcapRemoteDevice in the list by its IP address @@ -218,10 +218,10 @@ namespace pcpp /** * Search a PcapRemoteDevice in the list by its IP address * @param[in] ipAddrAsString The IP address in string format - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the PcapRemoteDevice if found, nullptr otherwise */ - std::shared_ptr getRemoteDeviceByIP(const std::string& ipAddrAsString, SmartPtrApiTag) const; + std::shared_ptr getRemoteDeviceByIP(const std::string& ipAddrAsString, SmartPtrApiTag apiTag) const; /** * @return An iterator object pointing to the first PcapRemoteDevice in list From 1d2ed7119478c8fe9acdd55d35c1626d29fc3284 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:49:24 +0300 Subject: [PATCH 52/81] Added name to tag variable to keep doxygen happy. --- Pcap++/header/PcapLiveDeviceList.h | 3 ++- Pcap++/header/PcapRemoteDeviceList.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index ff7ec31674..200e29692b 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -69,9 +69,10 @@ namespace pcpp */ PCPP_DEPRECATED const std::vector& getPcapLiveDevicesList() const; /** + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A reference to a vector containing shared pointers to all live devices currently installed on the machine. */ - const std::vector>& getPcapLiveDevicesList(SmartPtrApiTag) const { return m_LiveDeviceList; }; + const std::vector>& getPcapLiveDevicesList(SmartPtrApiTag apiTag) const { return m_LiveDeviceList; }; /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 3600a295e2..09d88904bf 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -91,7 +91,7 @@ namespace pcpp * use the other method overload. * @param[in] ipAddress The IP address of the remote machine through which clients can connect to the rpcapd daemon * @param[in] port The port of the remote machine through which clients can connect to the rpcapd daemon - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return An unique pointer to the newly created PcapRemoteDeviceList or a nullptr if (an appropriate error will be printed to log in each case): * - IP address provided is not valid * - WinPcap/Npcap encountered an error in creating the remote connection string From 7930e5bf87817a546c346cb74dbcd11d43dbe30a Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 12 May 2024 21:52:47 +0300 Subject: [PATCH 53/81] more tag fixes... --- Pcap++/header/PcapLiveDeviceList.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 200e29692b..64e6363c57 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -145,10 +145,10 @@ namespace pcpp /** * Get a pointer to the live device by its name * @param[in] name The name of the interface (e.g eth0) - * @param[in] Disambiguating tag for SmartPtrAPI. + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A shared pointer to the live device if this name exists, nullptr otherwise */ - std::shared_ptr getPcapLiveDeviceByName(const std::string& name, SmartPtrApiTag) const; + std::shared_ptr getPcapLiveDeviceByName(const std::string& name, SmartPtrApiTag apiTag) const; /** * Get a pointer to the live device by its IP address or name From db3530d15a8e13a847be02e409f60a253fd2ff80 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 13 May 2024 13:15:48 +0300 Subject: [PATCH 54/81] Changed typedefs to use Cpp11 using declarations. --- Pcap++/header/PcapRemoteDeviceList.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 09d88904bf..73f803f857 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -51,12 +51,12 @@ namespace pcpp /** * Iterator object that can be used for iterating all PcapRemoteDevice in list */ - typedef typename std::vector::iterator RemoteDeviceListIterator; + using RemoteDeviceListIterator = std::vector::iterator; /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list */ - typedef typename std::vector::const_iterator ConstRemoteDeviceListIterator; + using ConstRemoteDeviceListIterator = std::vector::const_iterator; /* * @class SmartPtrApiTag From 54143cc90ffea9cbbb5d8a37e7bb2c0fd37fdb2f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 23 May 2024 10:15:00 +0300 Subject: [PATCH 55/81] Updated deprecation macros to have deprecation message. --- Pcap++/header/PcapLiveDeviceList.h | 26 +++++++++++++------ Pcap++/header/PcapRemoteDeviceList.h | 38 +++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 64e6363c57..b991f787be 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -9,6 +9,11 @@ /// @file +#ifndef PCPP_DEPRECATED_RAW_PTR_API +#define PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE 1 +#define PCPP_DEPRECATED_RAW_PTR_API PCPP_DEPRECATED("This method is deprecated in favor of the SmartPtrAPI overload.") +#endif // !PCPP_DEPRECATED_RAW_PTR_API + /** * \namespace pcpp * \brief The main namespace for the PcapPlusPlus lib @@ -67,7 +72,7 @@ namespace pcpp * @return A vector containing pointers to all live devices currently installed on the machine * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED const std::vector& getPcapLiveDevicesList() const; + PCPP_DEPRECATED_RAW_PTR_API const std::vector& getPcapLiveDevicesList() const; /** * @param[in] apiTag Disambiguating tag for SmartPtrAPI. * @return A reference to a vector containing shared pointers to all live devices currently installed on the machine. @@ -80,7 +85,7 @@ namespace pcpp * @return A pointer to the live device if this IP address exists, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; + PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 * @param[in] ipAddr The IP address defined for the device @@ -95,7 +100,7 @@ namespace pcpp * @return A pointer to the live device if this IPv4 address exists, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; + PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; /** * Get a pointer to the live device by its IPv4 address * @param[in] ipAddr The IPv4 address defined for the device @@ -110,7 +115,7 @@ namespace pcpp * @return A pointer to the live device if this IPv6 address exists, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; + PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; /** * Get a pointer to the live device by its IPv6 address * @param[in] ip6Addr The IPv6 address defined for the device @@ -126,7 +131,7 @@ namespace pcpp * @return A pointer to the live device if this IP address is valid and exists, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; + PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; /** * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 * @param[in] ipAddrAsString The IP address defined for the device as string @@ -141,7 +146,7 @@ namespace pcpp * @return A pointer to the live device if this name exists, nullprt otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; + PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; /** * Get a pointer to the live device by its name * @param[in] name The name of the interface (e.g eth0) @@ -156,7 +161,7 @@ namespace pcpp * @return A pointer to the live device if exists, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; + PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; /** * Get a pointer to the live device by its IP address or name * @param[in] ipOrName An IP address or name of the interface @@ -176,7 +181,7 @@ namespace pcpp * @return A pointer to the cloned device list * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapLiveDeviceList* clone() const; + PCPP_DEPRECATED_RAW_PTR_API PcapLiveDeviceList* clone() const; /** * Copies the current live device list * @param[in] apiTag Disambiguating tag for SmartPtrAPI. @@ -191,3 +196,8 @@ namespace pcpp }; } // namespace pcpp + +#ifdef PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE +#undef PCPP_DEPRECATED_RAW_PTR_API +#undef PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE +#endif // PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 73f803f857..38b3310de4 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -9,12 +9,31 @@ /// @file +#ifndef PCPP_DEPRECATED_RAW_PTR_API +#define PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE 1 +#define PCPP_DEPRECATED_RAW_PTR_API PCPP_DEPRECATED("This method is deprecated in favor of the SmartPtrAPI overload.") +#endif // !PCPP_DEPRECATED_RAW_PTR_API + /** * \namespace pcpp * \brief The main namespace for the PcapPlusPlus lib */ namespace pcpp { + namespace internal + { + // In internal namespace as you are not supposed to use this type directly, but through PcapRemoteDeviceList::RemoteDeviceListIterator. + template + class PcapDeviceListConstIterator + { + public: + using iterator_category = std::input_iterator_tag; + using value_type = T; + using difference_type = std::ptrdiff_t; + using pointer = const value_type*; + using reference = const value_type&; + }; + } /** * @class PcapRemoteDeviceList @@ -52,11 +71,13 @@ namespace pcpp * Iterator object that can be used for iterating all PcapRemoteDevice in list */ using RemoteDeviceListIterator = std::vector::iterator; + using iterator = std::vector::iterator; /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list */ using ConstRemoteDeviceListIterator = std::vector::const_iterator; + using const_iterator = std::vector::const_iterator; /* * @class SmartPtrApiTag @@ -82,7 +103,7 @@ namespace pcpp * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); + PCPP_DEPRECATED_RAW_PTR_API static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also * creates a list of PcapRemoteDevice instances stored in it, one for each remote network interface. Notice this method allocates @@ -111,7 +132,7 @@ namespace pcpp * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); + PCPP_DEPRECATED_RAW_PTR_API static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is suitable for connecting to * remote daemons which require authentication for accessing them @@ -169,7 +190,7 @@ namespace pcpp * @return A pointer to PcapRemoteDevice if found, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; + PCPP_DEPRECATED_RAW_PTR_API PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; /** * Search a PcapRemoteDevice in the list by its IPv4 address * @param[in] ip4Addr The IPv4 address @@ -184,7 +205,7 @@ namespace pcpp * @return A pointer to PcapRemoteDevice if found, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; + PCPP_DEPRECATED_RAW_PTR_API PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; /** * Search a PcapRemoteDevice in the list by its IPv6 address * @param[in] ip6Addr The IPv6 address @@ -199,7 +220,7 @@ namespace pcpp * @return A pointer to PcapRemoteDevice if found, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; + PCPP_DEPRECATED_RAW_PTR_API PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) * @param[in] ipAddr The IP address @@ -214,7 +235,7 @@ namespace pcpp * @return A pointer to PcapRemoteDevice if found, nullptr otherwise * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ - PCPP_DEPRECATED PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; + PCPP_DEPRECATED_RAW_PTR_API PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; /** * Search a PcapRemoteDevice in the list by its IP address * @param[in] ipAddrAsString The IP address in string format @@ -247,4 +268,9 @@ namespace pcpp } // namespace pcpp +#ifdef PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE +#undef PCPP_DEPRECATED_RAW_PTR_API +#undef PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE +#endif // PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE + #endif // _WIN32 From 28fba54a010838df713bab14b48958265f9fd85d Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 23 May 2024 18:28:33 +0300 Subject: [PATCH 56/81] Changed internal macro to have double underscore. --- Pcap++/header/PcapLiveDeviceList.h | 6 +++--- Pcap++/header/PcapRemoteDeviceList.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index b991f787be..d0960d001d 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -10,7 +10,7 @@ /// @file #ifndef PCPP_DEPRECATED_RAW_PTR_API -#define PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE 1 +#define PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #define PCPP_DEPRECATED_RAW_PTR_API PCPP_DEPRECATED("This method is deprecated in favor of the SmartPtrAPI overload.") #endif // !PCPP_DEPRECATED_RAW_PTR_API @@ -197,7 +197,7 @@ namespace pcpp } // namespace pcpp -#ifdef PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE +#ifdef PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #undef PCPP_DEPRECATED_RAW_PTR_API -#undef PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE +#undef PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #endif // PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 38b3310de4..3465142c59 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -10,7 +10,7 @@ /// @file #ifndef PCPP_DEPRECATED_RAW_PTR_API -#define PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE 1 +#define PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #define PCPP_DEPRECATED_RAW_PTR_API PCPP_DEPRECATED("This method is deprecated in favor of the SmartPtrAPI overload.") #endif // !PCPP_DEPRECATED_RAW_PTR_API @@ -268,9 +268,9 @@ namespace pcpp } // namespace pcpp -#ifdef PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE +#ifdef PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #undef PCPP_DEPRECATED_RAW_PTR_API -#undef PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE +#undef PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #endif // PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE #endif // _WIN32 From e82aad36820b2df48755e1a95d2cb34eee12930b Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 23 May 2024 20:13:49 +0300 Subject: [PATCH 57/81] Added a smart pointer overload for PcapLiveDevice clone. Marked clone method as `const`. --- Pcap++/header/PcapLiveDevice.h | 19 +++++++++++++- Pcap++/src/PcapLiveDevice.cpp | 45 +++++++++++++++++----------------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index 8abf91fd77..6e4d32630f 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -131,6 +132,16 @@ namespace pcpp static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet); public: + /* + * @class SmartPtrApiTag + * Helper tag to disambiguate smart pointer API. + */ + struct SmartPtrApiTag{}; + /** + * Helper tag constant for disambuguating smart pointer API. + */ + static const SmartPtrApiTag SmartPtrApi; + /** * The type of the live device */ @@ -570,7 +581,13 @@ namespace pcpp * Clones the current device class * @return Pointer to the copied class */ - PcapLiveDevice* clone(); + PcapLiveDevice* clone() const; + /** + * Clones the current device class + * @param[in] apiTag Disambiguating tag for SmartPtrAPI. + * @return Unique pointer to the copied device. + */ + std::unique_ptr clone(SmartPtrApiTag apiTag) const; void getStatistics(IPcapDevice::PcapStats& stats) const override; diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index 70a0c18ab7..1d70cd1ef2 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -11,6 +11,7 @@ #include #include "Logger.h" #include "SystemUtils.h" +#include "MemoryUtils.h" #include #include #include @@ -71,7 +72,7 @@ static pcap_direction_t directionTypeMap(PcapLiveDevice::PcapDirection direction } #endif - +const PcapLiveDevice::SmartPtrApiTag PcapLiveDevice::SmartPtrApi{}; PcapLiveDevice::PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway) : IPcapDevice(), m_PcapSelectableFd(-1), m_DefaultGateway(IPv4Address::Zero), m_UsePoll(false) @@ -401,34 +402,32 @@ void PcapLiveDevice::close() PCPP_LOG_DEBUG("Device '" << m_Name << "' closed"); } -PcapLiveDevice* PcapLiveDevice::clone() -{ - PcapLiveDevice *retval = nullptr; +PcapLiveDevice* PcapLiveDevice::clone() const { return clone(SmartPtrApi).release(); } - pcap_if_t *interfaceList; - char errbuf[PCAP_ERRBUF_SIZE]; - int err = pcap_findalldevs(&interfaceList, errbuf); - if (err < 0) +std::unique_ptr PcapLiveDevice::clone(SmartPtrApiTag apiTag) const +{ + std::unique_ptr interfaceList; { - PCPP_LOG_ERROR("Error searching for devices: " << errbuf); - return nullptr; + pcap_if_t* interfaceListRaw; + char errbuf[PCAP_ERRBUF_SIZE]; + int err = pcap_findalldevs(&interfaceListRaw, errbuf); + if (err < 0) + { + PCPP_LOG_ERROR("Error searching for devices: " << errbuf); + return nullptr; + } + interfaceList = std::unique_ptr(interfaceListRaw); } - pcap_if_t* currInterface = interfaceList; - while (currInterface != nullptr) + for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { - if(!strcmp(currInterface->name, getName().c_str())) - break; - currInterface = currInterface->next; + if (!strcmp(currInterface->name, getName().c_str())) + { + return std::unique_ptr(new PcapLiveDevice(currInterface, true, true, true)); + } } - - if(currInterface) - retval = new PcapLiveDevice(currInterface, true, true, true); - else - PCPP_LOG_ERROR("Can't find interface " << getName().c_str()); - - pcap_freealldevs(interfaceList); - return retval; + PCPP_LOG_ERROR("Can't find interface " << getName().c_str()); + return nullptr; } bool PcapLiveDevice::startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie) From 775b14a75f5495a439e723fa78b4c0bbde393c89 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 23 May 2024 20:15:01 +0300 Subject: [PATCH 58/81] Lint --- Pcap++/src/PcapLiveDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index 1d70cd1ef2..96a9ca4a3c 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -404,7 +404,7 @@ void PcapLiveDevice::close() PcapLiveDevice* PcapLiveDevice::clone() const { return clone(SmartPtrApi).release(); } -std::unique_ptr PcapLiveDevice::clone(SmartPtrApiTag apiTag) const +std::unique_ptr PcapLiveDevice::clone(SmartPtrApiTag apiTag) const { std::unique_ptr interfaceList; { From ec0d8a8e02bfd3b480198597a0380f3acd1c04cc Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 23 May 2024 20:25:01 +0300 Subject: [PATCH 59/81] Changed standard iterator and const_iterator type aliases to copy from original aliases. --- Pcap++/header/PcapRemoteDeviceList.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 3465142c59..5dc84c817e 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -71,13 +71,13 @@ namespace pcpp * Iterator object that can be used for iterating all PcapRemoteDevice in list */ using RemoteDeviceListIterator = std::vector::iterator; - using iterator = std::vector::iterator; + using iterator = RemoteDeviceListIterator; /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list */ using ConstRemoteDeviceListIterator = std::vector::const_iterator; - using const_iterator = std::vector::const_iterator; + using const_iterator = ConstRemoteDeviceListIterator; /* * @class SmartPtrApiTag From 6b64f65af177a8201e7e82d2d337ddbcbdfa169f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 23 May 2024 20:25:12 +0300 Subject: [PATCH 60/81] Lint --- Pcap++/header/PcapRemoteDeviceList.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 5dc84c817e..c7d60f3f94 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -263,7 +263,6 @@ namespace pcpp * @return A const iterator object pointing to the last PcapRemoteDevice in list */ ConstRemoteDeviceListIterator end() const { return m_RemoteDeviceListView.end(); } - }; } // namespace pcpp From 454b5f6d73a175fc78005d09957e104eddf264e5 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 24 May 2024 20:20:07 +0300 Subject: [PATCH 61/81] Removed duplicates of SmartPtrApiTag struct. The struct is now a top-level struct and the variable SpartPtrApi is a top-level namespace constant. --- Pcap++/header/PcapLiveDevice.h | 20 +++++++++----------- Pcap++/header/PcapLiveDeviceList.h | 10 ---------- Pcap++/header/PcapRemoteDeviceList.h | 10 ---------- Pcap++/src/PcapLiveDevice.cpp | 4 ++-- Pcap++/src/PcapLiveDeviceList.cpp | 2 -- Pcap++/src/PcapRemoteDeviceList.cpp | 6 ++---- 6 files changed, 13 insertions(+), 39 deletions(-) diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index 6e4d32630f..a02070296c 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -25,6 +25,15 @@ typedef struct pcap_addr pcap_addr_t; */ namespace pcpp { + /* + * @class SmartPtrApiTag + * Helper tag to disambiguate smart pointer API. + */ + struct SmartPtrApiTag{}; + /** + * Helper tag constant for disambuguating smart pointer API. + */ + extern const SmartPtrApiTag SmartPtrApi; class PcapLiveDevice; @@ -131,17 +140,6 @@ namespace pcpp static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet); static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet); public: - - /* - * @class SmartPtrApiTag - * Helper tag to disambiguate smart pointer API. - */ - struct SmartPtrApiTag{}; - /** - * Helper tag constant for disambuguating smart pointer API. - */ - static const SmartPtrApiTag SmartPtrApi; - /** * The type of the live device */ diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index d0960d001d..415eddc930 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -48,16 +48,6 @@ namespace pcpp PcapLiveDeviceList(const PcapLiveDeviceList& other) = delete; PcapLiveDeviceList& operator=(const PcapLiveDeviceList& other) = delete; - /* - * @class SmartPtrApiTag - * Helper tag to disambiguate smart pointer API. - */ - struct SmartPtrApiTag {}; - /** - * Helper tag constant for disambuguating smart pointer API. - */ - static const SmartPtrApiTag SmartPtrApi; - /** * The access method to the singleton * @return The singleton instance of this class diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index c7d60f3f94..b4ebf5189e 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -79,16 +79,6 @@ namespace pcpp using ConstRemoteDeviceListIterator = std::vector::const_iterator; using const_iterator = ConstRemoteDeviceListIterator; - /* - * @class SmartPtrApiTag - * Helper tag to disambiguate smart pointer API. - */ - struct SmartPtrApiTag {}; - /** - * Helper tag constant for disambuguating smart pointer API. - */ - static const SmartPtrApiTag SmartPtrApi; - /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also * creates a list of PcapRemoteDevice instances stored in it, one for each remote network interface. Notice this method allocates diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index 1cfb5ade7a..b8c6264cc6 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -60,6 +60,8 @@ static const int DEFAULT_SNAPLEN = 9000; namespace pcpp { +const SmartPtrApiTag SmartPtrApi{}; + #ifdef HAS_SET_DIRECTION_ENABLED static pcap_direction_t directionTypeMap(PcapLiveDevice::PcapDirection direction) { @@ -72,8 +74,6 @@ static pcap_direction_t directionTypeMap(PcapLiveDevice::PcapDirection direction } #endif -const PcapLiveDevice::SmartPtrApiTag PcapLiveDevice::SmartPtrApi{}; - PcapLiveDevice::PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway) : IPcapDevice(), m_PcapSelectableFd(-1), m_DefaultGateway(IPv4Address::Zero), m_UsePoll(false) { diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index d42fd25c9b..68f283b4a6 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -25,8 +25,6 @@ namespace pcpp { -const PcapLiveDeviceList::SmartPtrApiTag PcapLiveDeviceList::SmartPtrApi{}; - PcapLiveDeviceList::PcapLiveDeviceList() { init(); diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 09c311d3bf..68cec5aced 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -13,8 +13,6 @@ namespace pcpp { -const PcapRemoteDeviceList::SmartPtrApiTag PcapRemoteDeviceList::SmartPtrApi{}; - PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList) : m_RemoteDeviceList(std::move(deviceList)), m_RemoteMachineIpAddress(ipAddress), m_RemoteMachinePort(port), m_RemoteAuthentication(std::move(remoteAuth)) { @@ -40,7 +38,7 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, nullptr, SmartPtrApi).release(); } -std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, PcapRemoteDeviceList::SmartPtrApiTag) +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress &ipAddress, uint16_t port, SmartPtrApiTag) { return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, std::unique_ptr()); } @@ -50,7 +48,7 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& return getRemoteDeviceList(ipAddress, port, remoteAuth, SmartPtrApi).release(); } -std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth, PcapRemoteDeviceList::SmartPtrApiTag) +std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth, SmartPtrApiTag) { std::unique_ptr auth = remoteAuth != nullptr ? std::unique_ptr(new PcapRemoteAuthentication(*remoteAuth)) : nullptr; return getRemoteDeviceList(ipAddress, port, std::move(auth)); From 8bcdee913c99f652dfab9ad5e388d6c0cee96180 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 26 May 2024 12:02:23 +0300 Subject: [PATCH 62/81] Marked private copy ctros as 'deleted'. --- Pcap++/header/PcapLiveDevice.h | 6 +++--- Pcap++/header/PcapRemoteDevice.h | 8 +++----- Pcap++/header/WinPcapLiveDevice.h | 6 +++--- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index a02070296c..f4495d3474 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -124,9 +124,6 @@ namespace pcpp // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList) PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway); - // copy c'tor is not public - PcapLiveDevice( const PcapLiveDevice& other ); - PcapLiveDevice& operator=(const PcapLiveDevice& other); void setDeviceMtu(); void setDeviceMacAddress(); @@ -140,6 +137,9 @@ namespace pcpp static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet); static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet); public: + PcapLiveDevice(const PcapLiveDevice& other) = delete; + PcapLiveDevice& operator=(const PcapLiveDevice& other) = delete; + /** * The type of the live device */ diff --git a/Pcap++/header/PcapRemoteDevice.h b/Pcap++/header/PcapRemoteDevice.h index ad5a0f5f70..df133aeb9d 100644 --- a/Pcap++/header/PcapRemoteDevice.h +++ b/Pcap++/header/PcapRemoteDevice.h @@ -87,17 +87,15 @@ 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, std::shared_ptr 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); //overridden methods ThreadStart getCaptureThreadStart(); public: + PcapRemoteDevice(const PcapRemoteDevice& other) = delete; + PcapRemoteDevice& operator=(const PcapRemoteDevice& other) = delete; + virtual ~PcapRemoteDevice() {} /** diff --git a/Pcap++/header/WinPcapLiveDevice.h b/Pcap++/header/WinPcapLiveDevice.h index 38a258d541..cd34342ed8 100644 --- a/Pcap++/header/WinPcapLiveDevice.h +++ b/Pcap++/header/WinPcapLiveDevice.h @@ -27,11 +27,11 @@ namespace pcpp // c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList) WinPcapLiveDevice(pcap_if_t* iface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway); - // copy c'tor is not public - WinPcapLiveDevice( const WinPcapLiveDevice& other ); - WinPcapLiveDevice& operator=(const WinPcapLiveDevice& other); public: + WinPcapLiveDevice(const WinPcapLiveDevice& other) = delete; + WinPcapLiveDevice& operator=(const WinPcapLiveDevice& other) = delete; + virtual LiveDeviceType getDeviceType() const { return WinPcapDevice; } bool startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie, int intervalInSecondsToUpdateStats, OnStatsUpdateCallback onStatsUpdate, void* onStatsUpdateUserCookie); From b5be87971eaf820f43eb9473f524560428974974 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 26 May 2024 12:07:55 +0300 Subject: [PATCH 63/81] Added assert that remote device is not nullptr. (cherry picked from commit 07f2c432d9453a856c92f1d60aecf71d51d1fe64) --- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index c6d4e910ca..393724cab9 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -868,6 +868,7 @@ PTF_TEST_CASE(TestRemoteCapture) PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachinePort(), remoteDevicePort); pcpp::PcapRemoteDevice* remoteDevice = remoteDevices->getRemoteDeviceByIP(remoteDeviceIPAddr); + PTF_ASSERT_NOT_NULL(remoteDevice); PTF_ASSERT_EQUAL(remoteDevice->getDeviceType(), pcpp::PcapLiveDevice::RemoteDevice, enum); PTF_ASSERT_EQUAL(remoteDevice->getMtu(), 0); pcpp::Logger::getInstance().suppressLogs(); From 96606368ac731f30ef93ee6f40850248ca74bd15 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 27 May 2024 14:32:51 +0300 Subject: [PATCH 64/81] Fixed remote devices being lost on view update. --- Pcap++/src/PcapRemoteDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 68cec5aced..46b2f340f0 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -26,7 +26,7 @@ void PcapRemoteDeviceList::updateDeviceListView() // refresh which can easily be handled by clearing the view list too. if (m_RemoteDeviceList.size() != m_RemoteDeviceListView.size()) { - m_RemoteDeviceList.resize(m_RemoteDeviceListView.size()); + m_RemoteDeviceListView.resize(m_RemoteDeviceList.size()); // Full update of all elements of the view vector to synchronize them with the main vector. std::transform(m_RemoteDeviceList.begin(), m_RemoteDeviceList.end(), m_RemoteDeviceListView.begin(), [](const std::shared_ptr& ptr) { return ptr.get(); }); From 87c4855e670d59c2d001f7f2028bf6366879cc82 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 27 May 2024 16:06:41 +0300 Subject: [PATCH 65/81] Centralized duplicated code for fetching device interfaces from libpcap. --- Pcap++/CMakeLists.txt | 2 ++ Pcap++/header/DeviceUtils.h | 32 ++++++++++++++++++++ Pcap++/src/DeviceUtils.cpp | 47 +++++++++++++++++++++++++++++ Pcap++/src/PcapLiveDevice.cpp | 17 +++++------ Pcap++/src/PcapLiveDeviceList.cpp | 16 +++++----- Pcap++/src/PcapRemoteDeviceList.cpp | 28 ++++++----------- 6 files changed, 105 insertions(+), 37 deletions(-) create mode 100644 Pcap++/header/DeviceUtils.h create mode 100644 Pcap++/src/DeviceUtils.cpp diff --git a/Pcap++/CMakeLists.txt b/Pcap++/CMakeLists.txt index 3007f889eb..c85c96d6ae 100644 --- a/Pcap++/CMakeLists.txt +++ b/Pcap++/CMakeLists.txt @@ -1,5 +1,6 @@ add_library( Pcap++ + src/DeviceUtils.cpp $<$:src/DpdkDevice.cpp> $<$:src/DpdkDeviceList.cpp> $<$:src/KniDevice.cpp> @@ -25,6 +26,7 @@ add_library( set(public_headers header/Device.h + header/DeviceUtils.h header/MemoryUtils.h header/NetworkUtils.h header/PcapDevice.h diff --git a/Pcap++/header/DeviceUtils.h b/Pcap++/header/DeviceUtils.h new file mode 100644 index 0000000000..db7c2206df --- /dev/null +++ b/Pcap++/header/DeviceUtils.h @@ -0,0 +1,32 @@ +#pragma once + +/// @file + +#include +#include "IpAddress.h" +#include "MemoryUtils.h" + +// Forward declaration +struct pcap_rmtauth; + +namespace pcpp +{ + namespace internal + { + /** + * Fetches a list of all network devices on the local machine that LibPcap/WinPcap/NPcap can find. + * @return A smart pointer to an interface list structure. + * @throws std::runtime_error The system encountered an error fetching the devices. + */ + std::unique_ptr getAllLocalPcapDevices(); + /** + * Fetches a list of all network devices on a remote machine that LibPcap/WinPcap/NPcap can find. + * @param[in] ipAddress IP address of the remote machine. + * @param[in] port Port to use when connecting to the remote machine. + * @param[in] pRmAuth Pointer to an authentication structure to use when connecting to the remote machine. Nullptr if no authentication is required. + * @return A smart pointer to an interface list structure. + * @throws std::runtime_error The system encountered an error fetching the devices. + */ + std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth = nullptr); + } +} \ No newline at end of file diff --git a/Pcap++/src/DeviceUtils.cpp b/Pcap++/src/DeviceUtils.cpp new file mode 100644 index 0000000000..522fe8b60f --- /dev/null +++ b/Pcap++/src/DeviceUtils.cpp @@ -0,0 +1,47 @@ +#include "DeviceUtils.h" + +#include +#include + +#include "pcap.h" +#include "Logger.h" +#include "IpAddress.h" + +namespace pcpp +{ + namespace internal + { + std::unique_ptr getAllLocalPcapDevices() + { + pcap_if_t* interfaceListRaw; + std::array errbuf; + int err = pcap_findalldevs(&interfaceListRaw, errbuf.data()); + if (err < 0) + { + throw std::runtime_error("Error searching for devices: " + std::string(errbuf.begin(), errbuf.end())); + } + // Assigns the raw pointer to the smart pointer with specialized deleter. + return std::unique_ptr(interfaceListRaw); + } + std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth) + { + PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); + std::array remoteCaptureString; + std::array errorBuf; + if (pcap_createsrcstr(remoteCaptureString.data(), PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), + std::to_string(port).c_str(), nullptr, errorBuf.data()) != 0) + { + throw std::runtime_error("Error creating the remote connection string. Error: " + std::string(errorBuf.begin(), errorBuf.end())); + } + + PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString.data()); + + pcap_if_t* interfaceListRaw; + if (pcap_findalldevs_ex(remoteCaptureString.data(), pRmAuth, &interfaceListRaw, errorBuf.data()) < 0) + { + throw std::runtime_error("Error retrieving device on remote machine. Error: " + std::string(errorBuf.begin(), errorBuf.end())); + } + return std::unique_ptr(interfaceListRaw); + } + } +} \ No newline at end of file diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index d5cd4cdbfd..c6dc326beb 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -10,6 +10,7 @@ #include "pcap.h" #include #include "Logger.h" +#include "DeviceUtils.h" #include "SystemUtils.h" #include "MemoryUtils.h" #include @@ -408,16 +409,14 @@ PcapLiveDevice* PcapLiveDevice::clone() const { return clone(SmartPtrApi).releas std::unique_ptr PcapLiveDevice::clone(SmartPtrApiTag apiTag) const { std::unique_ptr interfaceList; + try { - pcap_if_t* interfaceListRaw; - char errbuf[PCAP_ERRBUF_SIZE]; - int err = pcap_findalldevs(&interfaceListRaw, errbuf); - if (err < 0) - { - PCPP_LOG_ERROR("Error searching for devices: " << errbuf); - return nullptr; - } - interfaceList = std::unique_ptr(interfaceListRaw); + interfaceList = internal::getAllLocalPcapDevices(); + } + catch (const std::exception& e) + { + PCPP_LOG_ERROR(e.what()); + return nullptr; } for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 68f283b4a6..9553d98152 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -4,6 +4,7 @@ #include "IpAddressUtils.h" #include "PcapLiveDeviceList.h" #include "Logger.h" +#include "DeviceUtils.h" #include "SystemUtils.h" #include "MemoryUtils.h" #include "pcap.h" @@ -33,16 +34,13 @@ PcapLiveDeviceList::PcapLiveDeviceList() void PcapLiveDeviceList::init() { std::unique_ptr interfaceList; + try { - pcap_if_t* interfaceListRaw; - char errbuf[PCAP_ERRBUF_SIZE]; - int err = pcap_findalldevs(&interfaceListRaw, errbuf); - if (err < 0) - { - PCPP_LOG_ERROR("Error searching for devices: " << errbuf); - } - // Assigns the raw pointer to the smart pointer with specialized deleter. - interfaceList = std::unique_ptr(interfaceListRaw); + interfaceList = internal::getAllLocalPcapDevices(); + } + catch (const std::exception& e) + { + PCPP_LOG_ERROR(e.what()); } PCPP_LOG_DEBUG("Pcap lib version info: " << IPcapDevice::getPcapLibVersionInfo()); diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 46b2f340f0..d2fb17c2b1 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -6,6 +6,7 @@ #include "Logger.h" #include "IpUtils.h" #include "IpAddressUtils.h" +#include "DeviceUtils.h" #include "MemoryUtils.h" #include "pcap.h" #include @@ -66,17 +67,6 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth) { - PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); - char remoteCaptureString[PCAP_BUF_SIZE]; - char errorBuf[PCAP_ERRBUF_SIZE]; - if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), std::to_string(port).c_str(), nullptr, errorBuf) != 0) - { - PCPP_LOG_ERROR("Error in creating the remote connection string. Error was: " << errorBuf); - return nullptr; - } - - PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString); - pcap_rmtauth* pRmAuth = nullptr; pcap_rmtauth rmAuth; if (remoteAuth != nullptr) @@ -87,16 +77,16 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( } std::unique_ptr interfaceList; + try { - pcap_if_t* interfaceListRaw; - if (pcap_findalldevs_ex(remoteCaptureString, pRmAuth, &interfaceListRaw, errorBuf) < 0) - { - PCPP_LOG_ERROR("Error retrieving device on remote machine. Error string is: " << errorBuf); - return nullptr; - } - interfaceList = std::unique_ptr(interfaceListRaw); + interfaceList = internal::getAllRemotePcapDevices(ipAddress, port, pRmAuth); } - + catch (const std::exception& e) + { + PCPP_LOG_ERROR(e.what()); + return nullptr; + } + std::vector> remoteDeviceList; for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { From fd595c6c5e5940edf706efe23bed6768a0843295 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 27 May 2024 16:12:46 +0300 Subject: [PATCH 66/81] Fixed remote device fetch being windows only. --- Pcap++/header/DeviceUtils.h | 4 +++- Pcap++/src/DeviceUtils.cpp | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Pcap++/header/DeviceUtils.h b/Pcap++/header/DeviceUtils.h index db7c2206df..61de8326e8 100644 --- a/Pcap++/header/DeviceUtils.h +++ b/Pcap++/header/DeviceUtils.h @@ -19,8 +19,9 @@ namespace pcpp * @throws std::runtime_error The system encountered an error fetching the devices. */ std::unique_ptr getAllLocalPcapDevices(); +#ifdef _WIN32 /** - * Fetches a list of all network devices on a remote machine that LibPcap/WinPcap/NPcap can find. + * Fetches a list of all network devices on a remote machine that WinPcap/NPcap can find. * @param[in] ipAddress IP address of the remote machine. * @param[in] port Port to use when connecting to the remote machine. * @param[in] pRmAuth Pointer to an authentication structure to use when connecting to the remote machine. Nullptr if no authentication is required. @@ -28,5 +29,6 @@ namespace pcpp * @throws std::runtime_error The system encountered an error fetching the devices. */ std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth = nullptr); +#endif // _WIN32 } } \ No newline at end of file diff --git a/Pcap++/src/DeviceUtils.cpp b/Pcap++/src/DeviceUtils.cpp index 522fe8b60f..79fd40eec4 100644 --- a/Pcap++/src/DeviceUtils.cpp +++ b/Pcap++/src/DeviceUtils.cpp @@ -23,6 +23,8 @@ namespace pcpp // Assigns the raw pointer to the smart pointer with specialized deleter. return std::unique_ptr(interfaceListRaw); } + +#ifdef _WIN32 std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth) { PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); @@ -43,5 +45,6 @@ namespace pcpp } return std::unique_ptr(interfaceListRaw); } +#endif // _WIN32 } } \ No newline at end of file From f03bb130c7484d9d970aef0b6cc817fb854d989b Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 27 May 2024 16:14:07 +0300 Subject: [PATCH 67/81] Lint --- Pcap++/header/DeviceUtils.h | 4 ++-- Pcap++/src/DeviceUtils.cpp | 2 +- Pcap++/src/PcapRemoteDeviceList.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Pcap++/header/DeviceUtils.h b/Pcap++/header/DeviceUtils.h index 61de8326e8..35e4f24057 100644 --- a/Pcap++/header/DeviceUtils.h +++ b/Pcap++/header/DeviceUtils.h @@ -13,7 +13,7 @@ namespace pcpp { namespace internal { - /** + /** * Fetches a list of all network devices on the local machine that LibPcap/WinPcap/NPcap can find. * @return A smart pointer to an interface list structure. * @throws std::runtime_error The system encountered an error fetching the devices. @@ -31,4 +31,4 @@ namespace pcpp std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth = nullptr); #endif // _WIN32 } -} \ No newline at end of file +} diff --git a/Pcap++/src/DeviceUtils.cpp b/Pcap++/src/DeviceUtils.cpp index 79fd40eec4..6bc2a86cd2 100644 --- a/Pcap++/src/DeviceUtils.cpp +++ b/Pcap++/src/DeviceUtils.cpp @@ -47,4 +47,4 @@ namespace pcpp } #endif // _WIN32 } -} \ No newline at end of file +} diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index d2fb17c2b1..974d969d01 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -86,7 +86,7 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( PCPP_LOG_ERROR(e.what()); return nullptr; } - + std::vector> remoteDeviceList; for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { From 541b1985e22b6c7ee778aa8b60f166e93dc1850d Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 27 May 2024 22:25:52 +0300 Subject: [PATCH 68/81] Removed ifdefs as it is assumed the deprecation macro to be undefined externally. --- Pcap++/header/PcapLiveDeviceList.h | 6 ------ Pcap++/header/PcapRemoteDeviceList.h | 6 ------ 2 files changed, 12 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 415eddc930..87ff2e93de 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -9,10 +9,7 @@ /// @file -#ifndef PCPP_DEPRECATED_RAW_PTR_API -#define PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #define PCPP_DEPRECATED_RAW_PTR_API PCPP_DEPRECATED("This method is deprecated in favor of the SmartPtrAPI overload.") -#endif // !PCPP_DEPRECATED_RAW_PTR_API /** * \namespace pcpp @@ -187,7 +184,4 @@ namespace pcpp } // namespace pcpp -#ifdef PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #undef PCPP_DEPRECATED_RAW_PTR_API -#undef PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE -#endif // PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index b4ebf5189e..4b428e9ccb 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -9,10 +9,7 @@ /// @file -#ifndef PCPP_DEPRECATED_RAW_PTR_API -#define PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #define PCPP_DEPRECATED_RAW_PTR_API PCPP_DEPRECATED("This method is deprecated in favor of the SmartPtrAPI overload.") -#endif // !PCPP_DEPRECATED_RAW_PTR_API /** * \namespace pcpp @@ -257,9 +254,6 @@ namespace pcpp } // namespace pcpp -#ifdef PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE #undef PCPP_DEPRECATED_RAW_PTR_API -#undef PCPP_DEPRECATED_RAW_PTR_API__LOCAL_DEFINE -#endif // PCPP_DEPRECATED_RAW_PTR_API_LOCAL_DEFINE #endif // _WIN32 From e9c7a9b009680b8ca223b394781fa7b14d9a73bf Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 28 May 2024 22:24:09 +0300 Subject: [PATCH 69/81] Added smart pointer api tests to TestPcapLiveDeviceList, TestPcapLiveDeviceListSearch, TestPcapLiveDevice, TestPcapLiveDeviceClone and TestRemoteCapture. --- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 464 +++++++++++++++------ 1 file changed, 326 insertions(+), 138 deletions(-) diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 393724cab9..367be5fa28 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -10,6 +10,7 @@ #include "../Common/GlobalTestArgs.h" #include "../Common/TestUtils.h" #include "../Common/PcapFileNamesDef.h" +#include #include #if defined(_WIN32) #include "PcapRemoteDevice.h" @@ -178,18 +179,22 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) PTF_ASSERT_FALSE(iter->getName().empty()); } - pcpp::PcapLiveDeviceList *clonedDevList = pcpp::PcapLiveDeviceList::getInstance().clone(); - PTF_ASSERT_NOT_NULL(clonedDevList); + pcpp::PcapLiveDeviceList* clonedDevList1 = pcpp::PcapLiveDeviceList::getInstance().clone(); + std::unique_ptr clonedDevList2 = pcpp::PcapLiveDeviceList::getInstance().clone(pcpp::SmartPtrApi); - std::vector clonedDevListVector = clonedDevList->getPcapLiveDevicesList(); - PTF_ASSERT_EQUAL(clonedDevListVector.size(), devList.size()); - - auto iterCloned = clonedDevListVector.begin(); - for(auto iter = devList.begin(); iter != devList.end(); ++iter, ++iterCloned) + for (pcpp::PcapLiveDeviceList* clonedDevList : { clonedDevList1, clonedDevList2.get() }) { - PTF_ASSERT_EQUAL((*iter)->getName(), (*iterCloned)->getName()); + PTF_ASSERT_NOT_NULL(clonedDevList); + + std::vector clonedDevListVector = clonedDevList->getPcapLiveDevicesList(); + PTF_ASSERT_EQUAL(clonedDevListVector.size(), devList.size()); + + for (auto iter = devList.begin(), iterCloned = clonedDevListVector.begin(); iter != devList.end(); ++iter, ++iterCloned) + { + PTF_ASSERT_EQUAL((*iter)->getName(), (*iterCloned)->getName()); + } } - delete clonedDevList; + delete clonedDevList1; PTF_ASSERT_EQUAL(pcpp::PcapLiveDeviceList::getInstance().getDnsServers().size(), dnsServerCount); } // TestPcapLiveDeviceList @@ -198,108 +203,216 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) PTF_TEST_CASE(TestPcapLiveDeviceListSearch) { - pcpp::PcapLiveDevice* liveDev = nullptr; - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - PTF_ASSERT_NOT_NULL(liveDev); + // Raw Pointer API + { + pcpp::PcapLiveDevice* liveDev = nullptr; + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + PTF_ASSERT_NOT_NULL(liveDev); - std::string devName(liveDev->getName()); - pcpp::PcapLiveDevice* liveDev2 = nullptr; - liveDev2 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(devName); - PTF_ASSERT_NOT_NULL(liveDev2); - PTF_ASSERT_EQUAL(liveDev->getName(), liveDev2->getName()); + std::string devName(liveDev->getName()); + pcpp::PcapLiveDevice* liveDev2 = nullptr; + liveDev2 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(devName); + PTF_ASSERT_NOT_NULL(liveDev2); + PTF_ASSERT_EQUAL(liveDev->getName(), liveDev2->getName()); - pcpp::PcapLiveDevice* liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(devName); - PTF_ASSERT_EQUAL(liveDev3, liveDev2, ptr); - liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets); - PTF_ASSERT_EQUAL(liveDev3, liveDev, ptr); + pcpp::PcapLiveDevice* liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(devName); + PTF_ASSERT_EQUAL(liveDev3, liveDev2, ptr); + liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets); + PTF_ASSERT_EQUAL(liveDev3, liveDev, ptr); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("255.255.255.250"); - PTF_ASSERT_NULL(liveDev); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("255.255.255.250"); + PTF_ASSERT_NULL(liveDev); + } + // Smart Pointer API + { + std::shared_ptr liveDev; + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str(), pcpp::SmartPtrApi); + PTF_ASSERT_NOT_NULL(liveDev); + + std::string devName(liveDev->getName()); + std::shared_ptr liveDev2 = nullptr; + liveDev2 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(devName, pcpp::SmartPtrApi); + PTF_ASSERT_NOT_NULL(liveDev2); + PTF_ASSERT_EQUAL(liveDev->getName(), liveDev2->getName()); + + std::shared_ptr liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(devName, pcpp::SmartPtrApi); + PTF_ASSERT_EQUAL(liveDev3, liveDev2, ptr); + liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets, pcpp::SmartPtrApi); + PTF_ASSERT_EQUAL(liveDev3, liveDev, ptr); + + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("255.255.255.250", pcpp::SmartPtrApi); + PTF_ASSERT_NULL(liveDev); + } } // TestPcapLiveDeviceListSearch PTF_TEST_CASE(TestPcapLiveDevice) { - pcpp::PcapLiveDevice* liveDev = nullptr; - pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); - PTF_ASSERT_NOT_NULL(liveDev); - PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0); - PTF_ASSERT_TRUE(liveDev->open()); - DeviceTeardown devTeardown(liveDev); - int packetCount = 0; - int numOfTimeStatsWereInvoked = 0; - PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); - int totalSleepTime = 0; - while (totalSleepTime <= 20) + // Raw Pointer API { - pcpp::multiPlatformSleep(2); - totalSleepTime += 2; - if (packetCount > 0) - break; + pcpp::PcapLiveDevice* liveDev = nullptr; + pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + PTF_ASSERT_NOT_NULL(liveDev); + PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0); + PTF_ASSERT_TRUE(liveDev->open()); + DeviceTeardown devTeardown(liveDev); + int packetCount = 0; + int numOfTimeStatsWereInvoked = 0; + PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, + static_cast(&numOfTimeStatsWereInvoked))); + int totalSleepTime = 0; + while (totalSleepTime <= 20) + { + pcpp::multiPlatformSleep(2); + totalSleepTime += 2; + if (packetCount > 0) + break; + } + + PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); + + liveDev->stopCapture(); + PTF_ASSERT_GREATER_THAN(packetCount, 0); + PTF_ASSERT_GREATER_OR_EQUAL_THAN(numOfTimeStatsWereInvoked, totalSleepTime - 2); + + pcpp::IPcapDevice::PcapStats statistics; + liveDev->getStatistics(statistics); + // Bad test - on high traffic libpcap/WinPcap/Npcap sometimes drop packets + // PTF_ASSERT_EQUALS((uint32_t)statistics.ps_drop, 0); + liveDev->close(); + PTF_ASSERT_FALSE(liveDev->isOpened()); + + // a negative test + pcpp::Logger::getInstance().suppressLogs(); + PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, + static_cast(&numOfTimeStatsWereInvoked))); + pcpp::Logger::getInstance().enableLogs(); } + // Smart Pointer API + { + std::shared_ptr liveDev = nullptr; + pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch, pcpp::SmartPtrApi); + PTF_ASSERT_NOT_NULL(liveDev); + PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0); + PTF_ASSERT_TRUE(liveDev->open()); + DeviceTeardown devTeardown(liveDev.get()); + int packetCount = 0; + int numOfTimeStatsWereInvoked = 0; + PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); + int totalSleepTime = 0; + while (totalSleepTime <= 20) + { + pcpp::multiPlatformSleep(2); + totalSleepTime += 2; + if (packetCount > 0) + break; + } - PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); + PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); - liveDev->stopCapture(); - PTF_ASSERT_GREATER_THAN(packetCount, 0); - PTF_ASSERT_GREATER_OR_EQUAL_THAN(numOfTimeStatsWereInvoked, totalSleepTime-2); + liveDev->stopCapture(); + PTF_ASSERT_GREATER_THAN(packetCount, 0); + PTF_ASSERT_GREATER_OR_EQUAL_THAN(numOfTimeStatsWereInvoked, totalSleepTime-2); - pcpp::IPcapDevice::PcapStats statistics; - liveDev->getStatistics(statistics); - //Bad test - on high traffic libpcap/WinPcap/Npcap sometimes drop packets - //PTF_ASSERT_EQUALS((uint32_t)statistics.ps_drop, 0); - liveDev->close(); - PTF_ASSERT_FALSE(liveDev->isOpened()); + pcpp::IPcapDevice::PcapStats statistics; + liveDev->getStatistics(statistics); + //Bad test - on high traffic libpcap/WinPcap/Npcap sometimes drop packets + //PTF_ASSERT_EQUALS((uint32_t)statistics.ps_drop, 0); + liveDev->close(); + PTF_ASSERT_FALSE(liveDev->isOpened()); - // a negative test - pcpp::Logger::getInstance().suppressLogs(); - PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); - pcpp::Logger::getInstance().enableLogs(); + // a negative test + pcpp::Logger::getInstance().suppressLogs(); + PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); + pcpp::Logger::getInstance().enableLogs(); + } } // TestPcapLiveDevice PTF_TEST_CASE(TestPcapLiveDeviceClone) { - // Test of clone device should be same with original - pcpp::PcapLiveDevice* liveDev = nullptr; - pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch)->clone(); - PTF_ASSERT_NOT_NULL(liveDev); - PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0); - PTF_ASSERT_TRUE(liveDev->open()); - DeviceTeardown devTeardown(liveDev, true); - int packetCount = 0; - int numOfTimeStatsWereInvoked = 0; - PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); - int totalSleepTime = 0; - while (totalSleepTime <= 20) + // Raw Pointer API { - pcpp::multiPlatformSleep(2); - totalSleepTime += 2; - if (packetCount > 0) - break; - } + // Test of clone device should be same with original + pcpp::PcapLiveDevice* liveDev = nullptr; + pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch)->clone(); + PTF_ASSERT_NOT_NULL(liveDev); + PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0); + PTF_ASSERT_TRUE(liveDev->open()); + DeviceTeardown devTeardown(liveDev, true); + int packetCount = 0; + int numOfTimeStatsWereInvoked = 0; + PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); + int totalSleepTime = 0; + while (totalSleepTime <= 20) + { + pcpp::multiPlatformSleep(2); + totalSleepTime += 2; + if (packetCount > 0) + break; + } - PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); + PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); - liveDev->stopCapture(); - PTF_ASSERT_GREATER_THAN(packetCount, 0); - PTF_ASSERT_GREATER_OR_EQUAL_THAN(numOfTimeStatsWereInvoked, totalSleepTime-1); - pcpp::IPcapDevice::PcapStats statistics; - liveDev->getStatistics(statistics); - //Bad test - on high traffic libpcap/WinPcap/Npcap sometimes drop packets - //PTF_ASSERT_EQUALS((uint32_t)statistics.ps_drop, 0); - liveDev->close(); - PTF_ASSERT_FALSE(liveDev->isOpened()); + liveDev->stopCapture(); + PTF_ASSERT_GREATER_THAN(packetCount, 0); + PTF_ASSERT_GREATER_OR_EQUAL_THAN(numOfTimeStatsWereInvoked, totalSleepTime-1); + pcpp::IPcapDevice::PcapStats statistics; + liveDev->getStatistics(statistics); + //Bad test - on high traffic libpcap/WinPcap/Npcap sometimes drop packets + //PTF_ASSERT_EQUALS((uint32_t)statistics.ps_drop, 0); + liveDev->close(); + PTF_ASSERT_FALSE(liveDev->isOpened()); - // a negative test - pcpp::Logger::getInstance().suppressLogs(); - PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); - pcpp::Logger::getInstance().enableLogs(); + // a negative test + pcpp::Logger::getInstance().suppressLogs(); + PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); + pcpp::Logger::getInstance().enableLogs(); + } + // Smart Pointer API + { + // Test of clone device should be same with original + std::unique_ptr liveDev = nullptr; + pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch, pcpp::SmartPtrApi)->clone(pcpp::SmartPtrApi); + PTF_ASSERT_NOT_NULL(liveDev); + PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0); + PTF_ASSERT_TRUE(liveDev->open()); + DeviceTeardown devTeardown(liveDev.get()); + int packetCount = 0; + int numOfTimeStatsWereInvoked = 0; + PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); + int totalSleepTime = 0; + while (totalSleepTime <= 20) + { + pcpp::multiPlatformSleep(2); + totalSleepTime += 2; + if (packetCount > 0) + break; + } + + PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); + liveDev->stopCapture(); + PTF_ASSERT_GREATER_THAN(packetCount, 0); + PTF_ASSERT_GREATER_OR_EQUAL_THAN(numOfTimeStatsWereInvoked, totalSleepTime-1); + pcpp::IPcapDevice::PcapStats statistics; + liveDev->getStatistics(statistics); + //Bad test - on high traffic libpcap/WinPcap/Npcap sometimes drop packets + //PTF_ASSERT_EQUALS((uint32_t)statistics.ps_drop, 0); + liveDev->close(); + PTF_ASSERT_FALSE(liveDev->isOpened()); + + // a negative test + pcpp::Logger::getInstance().suppressLogs(); + PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, static_cast(&packetCount), 1, &statsUpdate, static_cast(&numOfTimeStatsWereInvoked))); + pcpp::Logger::getInstance().enableLogs(); + } } // TestPcapLiveDeviceClone @@ -858,79 +971,154 @@ PTF_TEST_CASE(TestRemoteCapture) PTF_ASSERT_NOT_NULL(rpcapdInitializer.getHandle()); pcpp::IPv4Address remoteDeviceIPAddr(remoteDeviceIP); - pcpp::PcapRemoteDeviceList* remoteDevices = pcpp::PcapRemoteDeviceList::getRemoteDeviceList(remoteDeviceIPAddr, remoteDevicePort); - PTF_ASSERT_NOT_NULL(remoteDevices); - for (pcpp::PcapRemoteDeviceList::RemoteDeviceListIterator remoteDevIter = remoteDevices->begin(); remoteDevIter != remoteDevices->end(); remoteDevIter++) + + // Raw Pointer API { - PTF_ASSERT_FALSE((*remoteDevIter)->getName().empty()); - } - PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachineIpAddress().toString(), remoteDeviceIP); - PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachinePort(), remoteDevicePort); + pcpp::PcapRemoteDeviceList* remoteDevices = pcpp::PcapRemoteDeviceList::getRemoteDeviceList(remoteDeviceIPAddr, remoteDevicePort); + PTF_ASSERT_NOT_NULL(remoteDevices); + for (pcpp::PcapRemoteDeviceList::RemoteDeviceListIterator remoteDevIter = remoteDevices->begin(); remoteDevIter != remoteDevices->end(); remoteDevIter++) + { + PTF_ASSERT_FALSE((*remoteDevIter)->getName().empty()); + } + PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachineIpAddress().toString(), remoteDeviceIP); + PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachinePort(), remoteDevicePort); - pcpp::PcapRemoteDevice* remoteDevice = remoteDevices->getRemoteDeviceByIP(remoteDeviceIPAddr); - PTF_ASSERT_NOT_NULL(remoteDevice); - PTF_ASSERT_EQUAL(remoteDevice->getDeviceType(), pcpp::PcapLiveDevice::RemoteDevice, enum); - PTF_ASSERT_EQUAL(remoteDevice->getMtu(), 0); - pcpp::Logger::getInstance().suppressLogs(); - PTF_ASSERT_EQUAL(remoteDevice->getMacAddress(), pcpp::MacAddress::Zero); - pcpp::Logger::getInstance().enableLogs(); - PTF_ASSERT_TRUE(remoteDevice->open()); - DeviceTeardown devTeardown(remoteDevice); - pcpp::RawPacketVector capturedPackets; - PTF_ASSERT_TRUE(remoteDevice->startCapture(capturedPackets)); + pcpp::PcapRemoteDevice* remoteDevice = remoteDevices->getRemoteDeviceByIP(remoteDeviceIPAddr); + PTF_ASSERT_NOT_NULL(remoteDevice); + PTF_ASSERT_EQUAL(remoteDevice->getDeviceType(), pcpp::PcapLiveDevice::RemoteDevice, enum); + PTF_ASSERT_EQUAL(remoteDevice->getMtu(), 0); + pcpp::Logger::getInstance().suppressLogs(); + PTF_ASSERT_EQUAL(remoteDevice->getMacAddress(), pcpp::MacAddress::Zero); + pcpp::Logger::getInstance().enableLogs(); + PTF_ASSERT_TRUE(remoteDevice->open()); + DeviceTeardown devTeardown(remoteDevice); + pcpp::RawPacketVector capturedPackets; + PTF_ASSERT_TRUE(remoteDevice->startCapture(capturedPackets)); - if (!useRemoteDevicesFromArgs) - PTF_ASSERT_TRUE(sendURLRequest("www.yahoo.com")); + if (!useRemoteDevicesFromArgs) + PTF_ASSERT_TRUE(sendURLRequest("www.yahoo.com")); - int totalSleepTime = 0; - while (totalSleepTime < 10) - { - if (capturedPackets.size() > 2) + int totalSleepTime = 0; + while (totalSleepTime < 10) { - break; + if (capturedPackets.size() > 2) + { + break; + } + + pcpp::multiPlatformSleep(1); + totalSleepTime += 1; } - pcpp::multiPlatformSleep(1); - totalSleepTime += 1; - } + remoteDevice->stopCapture(); - remoteDevice->stopCapture(); + PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); - PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); + PTF_ASSERT_GREATER_THAN(capturedPackets.size(), 2); + + // send single packet + PTF_ASSERT_TRUE(remoteDevice->sendPacket(*capturedPackets.front())); + + // send multiple packets + pcpp::RawPacketVector packetsToSend; + std::vector::iterator iter = capturedPackets.begin(); + + size_t capturedPacketsSize = capturedPackets.size(); + while (iter != capturedPackets.end()) + { + if ((*iter)->getRawDataLen() <= (int)remoteDevice->getMtu()) + { + packetsToSend.pushBack(capturedPackets.getAndRemoveFromVector(iter)); + } + else + ++iter; + } + int packetsSent = remoteDevice->sendPackets(packetsToSend); + PTF_ASSERT_EQUAL(packetsSent, (int)packetsToSend.size()); + + //check statistics + pcpp::IPcapDevice::PcapStats stats; + remoteDevice->getStatistics(stats); + PTF_ASSERT_EQUAL((uint32_t)stats.packetsRecv, capturedPacketsSize); - PTF_ASSERT_GREATER_THAN(capturedPackets.size(), 2); + remoteDevice->close(); - // send single packet - PTF_ASSERT_TRUE(remoteDevice->sendPacket(*capturedPackets.front())); + delete remoteDevices; - // send multiple packets - pcpp::RawPacketVector packetsToSend; - std::vector::iterator iter = capturedPackets.begin(); + // the device object is already deleted, cannot close it + devTeardown.cancelTeardown(); + } - size_t capturedPacketsSize = capturedPackets.size(); - while (iter != capturedPackets.end()) + // Smart Pointer API { - if ((*iter)->getRawDataLen() <= (int)remoteDevice->getMtu()) + std::unique_ptr remoteDevices = pcpp::PcapRemoteDeviceList::getRemoteDeviceList(remoteDeviceIPAddr, remoteDevicePort, pcpp::SmartPtrApi); + PTF_ASSERT_NOT_NULL(remoteDevices); + for (pcpp::PcapRemoteDeviceList::RemoteDeviceListIterator remoteDevIter = remoteDevices->begin(); remoteDevIter != remoteDevices->end(); remoteDevIter++) { - packetsToSend.pushBack(capturedPackets.getAndRemoveFromVector(iter)); + PTF_ASSERT_FALSE((*remoteDevIter)->getName().empty()); } - else - ++iter; - } - int packetsSent = remoteDevice->sendPackets(packetsToSend); - PTF_ASSERT_EQUAL(packetsSent, (int)packetsToSend.size()); + PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachineIpAddress().toString(), remoteDeviceIP); + PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachinePort(), remoteDevicePort); + + std::shared_ptr remoteDevice = remoteDevices->getRemoteDeviceByIP(remoteDeviceIPAddr, pcpp::SmartPtrApi); + PTF_ASSERT_NOT_NULL(remoteDevice); + PTF_ASSERT_EQUAL(remoteDevice->getDeviceType(), pcpp::PcapLiveDevice::RemoteDevice, enum); + PTF_ASSERT_EQUAL(remoteDevice->getMtu(), 0); + pcpp::Logger::getInstance().suppressLogs(); + PTF_ASSERT_EQUAL(remoteDevice->getMacAddress(), pcpp::MacAddress::Zero); + pcpp::Logger::getInstance().enableLogs(); + PTF_ASSERT_TRUE(remoteDevice->open()); + DeviceTeardown devTeardown(remoteDevice.get()); + pcpp::RawPacketVector capturedPackets; + PTF_ASSERT_TRUE(remoteDevice->startCapture(capturedPackets)); - //check statistics - pcpp::IPcapDevice::PcapStats stats; - remoteDevice->getStatistics(stats); - PTF_ASSERT_EQUAL((uint32_t)stats.packetsRecv, capturedPacketsSize); + if (!useRemoteDevicesFromArgs) + PTF_ASSERT_TRUE(sendURLRequest("www.yahoo.com")); - remoteDevice->close(); + int totalSleepTime = 0; + while (totalSleepTime < 10) + { + if (capturedPackets.size() > 2) + { + break; + } + + pcpp::multiPlatformSleep(1); + totalSleepTime += 1; + } - delete remoteDevices; + remoteDevice->stopCapture(); + + PTF_PRINT_VERBOSE("Total sleep time: " << totalSleepTime << " secs"); + + PTF_ASSERT_GREATER_THAN(capturedPackets.size(), 2); + + // send single packet + PTF_ASSERT_TRUE(remoteDevice->sendPacket(*capturedPackets.front())); + + // send multiple packets + pcpp::RawPacketVector packetsToSend; + std::vector::iterator iter = capturedPackets.begin(); + + size_t capturedPacketsSize = capturedPackets.size(); + while (iter != capturedPackets.end()) + { + if ((*iter)->getRawDataLen() <= (int)remoteDevice->getMtu()) + { + packetsToSend.pushBack(capturedPackets.getAndRemoveFromVector(iter)); + } + else + ++iter; + } + int packetsSent = remoteDevice->sendPackets(packetsToSend); + PTF_ASSERT_EQUAL(packetsSent, (int)packetsToSend.size()); + + //check statistics + pcpp::IPcapDevice::PcapStats stats; + remoteDevice->getStatistics(stats); + PTF_ASSERT_EQUAL((uint32_t)stats.packetsRecv, capturedPacketsSize); + } - // the device object is already deleted, cannot close it - devTeardown.cancelTeardown(); #else PTF_SKIP_TEST("This test can only run in Windows environment"); #endif From 9a4003e4bd57720b83df57991a26aaf475976ff0 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 28 May 2024 22:36:28 +0300 Subject: [PATCH 70/81] Interlaced tests in device list search. --- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 65 +++++++++++----------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 367be5fa28..406d2b5d40 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -203,46 +203,45 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) PTF_TEST_CASE(TestPcapLiveDeviceListSearch) { - // Raw Pointer API - { - pcpp::PcapLiveDevice* liveDev = nullptr; - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - PTF_ASSERT_NOT_NULL(liveDev); + std::shared_ptr liveDev; + pcpp::PcapLiveDevice* liveDevRaw = nullptr; - std::string devName(liveDev->getName()); - pcpp::PcapLiveDevice* liveDev2 = nullptr; - liveDev2 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(devName); - PTF_ASSERT_NOT_NULL(liveDev2); - PTF_ASSERT_EQUAL(liveDev->getName(), liveDev2->getName()); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str(), pcpp::SmartPtrApi); + PTF_ASSERT_NOT_NULL(liveDev); - pcpp::PcapLiveDevice* liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(devName); - PTF_ASSERT_EQUAL(liveDev3, liveDev2, ptr); - liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets); - PTF_ASSERT_EQUAL(liveDev3, liveDev, ptr); + liveDevRaw = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + PTF_ASSERT_EQUAL(liveDevRaw, liveDev.get()) + + std::string devName(liveDev->getName()); + + std::shared_ptr liveDev2 = nullptr; + liveDev2 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(devName, pcpp::SmartPtrApi); + PTF_ASSERT_NOT_NULL(liveDev2); + PTF_ASSERT_EQUAL(liveDev->getName(), liveDev2->getName()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("255.255.255.250"); - PTF_ASSERT_NULL(liveDev); - } - // Smart Pointer API { - std::shared_ptr liveDev; - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str(), pcpp::SmartPtrApi); - PTF_ASSERT_NOT_NULL(liveDev); + pcpp::PcapLiveDevice* liveDev2Raw = nullptr; + liveDev2Raw = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(devName); + PTF_ASSERT_EQUAL(liveDev2Raw, liveDev2.get()) + } - std::string devName(liveDev->getName()); - std::shared_ptr liveDev2 = nullptr; - liveDev2 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(devName, pcpp::SmartPtrApi); - PTF_ASSERT_NOT_NULL(liveDev2); - PTF_ASSERT_EQUAL(liveDev->getName(), liveDev2->getName()); + std::shared_ptr liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(devName, pcpp::SmartPtrApi); + PTF_ASSERT_EQUAL(liveDev3, liveDev2, ptr); - std::shared_ptr liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(devName, pcpp::SmartPtrApi); - PTF_ASSERT_EQUAL(liveDev3, liveDev2, ptr); - liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets, pcpp::SmartPtrApi); - PTF_ASSERT_EQUAL(liveDev3, liveDev, ptr); + pcpp::PcapLiveDevice* liveDev3Raw = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(devName); + PTF_ASSERT_EQUAL(liveDev3Raw, liveDev2.get()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("255.255.255.250", pcpp::SmartPtrApi); - PTF_ASSERT_NULL(liveDev); - } + liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets, pcpp::SmartPtrApi); + PTF_ASSERT_EQUAL(liveDev3, liveDev, ptr); + + liveDev3Raw = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets); + PTF_ASSERT_EQUAL(liveDev3Raw, liveDev.get(), ptr); + + liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("255.255.255.250", pcpp::SmartPtrApi); + PTF_ASSERT_NULL(liveDev); + + liveDevRaw = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("255.255.255.250"); + PTF_ASSERT_NULL(liveDevRaw); } // TestPcapLiveDeviceListSearch From 65bc6e084531ac11b3fb647ec147394f7914bfe5 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 30 May 2024 10:00:47 +0300 Subject: [PATCH 71/81] Cleanup --- Pcap++/header/PcapRemoteDeviceList.h | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 4b428e9ccb..7323bb8960 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -17,21 +17,6 @@ */ namespace pcpp { - namespace internal - { - // In internal namespace as you are not supposed to use this type directly, but through PcapRemoteDeviceList::RemoteDeviceListIterator. - template - class PcapDeviceListConstIterator - { - public: - using iterator_category = std::input_iterator_tag; - using value_type = T; - using difference_type = std::ptrdiff_t; - using pointer = const value_type*; - using reference = const value_type&; - }; - } - /** * @class PcapRemoteDeviceList * A class that creates, stores and provides access to all instances of PcapRemoteDevice for a certain remote machine. To get an instance From f6ba4a199d69c055bf947193ca6045026e082dc5 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 2 Jun 2024 20:28:13 +0300 Subject: [PATCH 72/81] Marked the device list view as mutable for consistency with PcapLiveDeviceList. --- Pcap++/header/PcapRemoteDeviceList.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 7323bb8960..90c15d298a 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -32,7 +32,7 @@ namespace pcpp private: std::vector> m_RemoteDeviceList; // View vector to help keep backward compatibility of iteration. - std::vector m_RemoteDeviceListView; + mutable std::vector m_RemoteDeviceListView; IPAddress m_RemoteMachineIpAddress; uint16_t m_RemoteMachinePort; std::shared_ptr m_RemoteAuthentication; @@ -40,7 +40,7 @@ namespace pcpp // private c'tor. User should create the list via static methods PcapRemoteDeviceList::getRemoteDeviceList() PcapRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth, std::vector> deviceList); - void updateDeviceListView(); + void updateDeviceListView() const; // Implementation that uses a shared ptr is private to guarantee that the remote auth object is not shared externally. // It is used by the other overloads for casting different kinds of pointers/references into shared_ptr. From 6e7f8cb894b1211454477f367c4214d027a96488 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 2 Jun 2024 21:02:17 +0300 Subject: [PATCH 73/81] Changed clone internal to return smart pointer. --- Pcap++/header/PcapLiveDevice.h | 2 +- Pcap++/header/WinPcapLiveDevice.h | 2 +- Pcap++/src/PcapLiveDevice.cpp | 6 +++--- Pcap++/src/WinPcapLiveDevice.cpp | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index ca63f18ab8..ce6149a5ee 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -592,7 +592,7 @@ namespace pcpp protected: pcap_t* doOpen(const DeviceConfiguration& config); - virtual PcapLiveDevice* cloneInternal(pcap_if_t& devInterface) const; + virtual std::unique_ptr cloneInternal(pcap_if_t& devInterface) const; }; } // namespace pcpp diff --git a/Pcap++/header/WinPcapLiveDevice.h b/Pcap++/header/WinPcapLiveDevice.h index 01a5e77020..f273f5ed43 100644 --- a/Pcap++/header/WinPcapLiveDevice.h +++ b/Pcap++/header/WinPcapLiveDevice.h @@ -57,7 +57,7 @@ namespace pcpp int getMinAmountOfDataToCopyFromKernelToApplication() const { return m_MinAmountOfDataToCopyFromKernelToApplication; } protected: - WinPcapLiveDevice* cloneInternal(pcap_if_t& devInterface) const override; + std::unique_ptr cloneInternal(pcap_if_t& devInterface) const override; }; } // namespace pcpp diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index e589d9778e..daa11df5d6 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -423,16 +423,16 @@ std::unique_ptr PcapLiveDevice::clone(SmartPtrApiTag apiTag) con { if (!strcmp(currInterface->name, getName().c_str())) { - return std::unique_ptr(cloneInternal(*currInterface)); + return cloneInternal(*currInterface); } } PCPP_LOG_ERROR("Can't find interface " << getName().c_str()); return nullptr; } -PcapLiveDevice* PcapLiveDevice::cloneInternal(pcap_if_t& devInterface) const +std::unique_ptr PcapLiveDevice::cloneInternal(pcap_if_t& devInterface) const { - return new PcapLiveDevice(&devInterface, true, true, true); + return std::unique_ptr(new PcapLiveDevice(&devInterface, true, true, true)); } bool PcapLiveDevice::startCapture(OnPacketArrivesCallback onPacketArrives, void* onPacketArrivesUserCookie) diff --git a/Pcap++/src/WinPcapLiveDevice.cpp b/Pcap++/src/WinPcapLiveDevice.cpp index c6a6a1121f..d989e20b64 100644 --- a/Pcap++/src/WinPcapLiveDevice.cpp +++ b/Pcap++/src/WinPcapLiveDevice.cpp @@ -126,9 +126,9 @@ bool WinPcapLiveDevice::setMinAmountOfDataToCopyFromKernelToApplication(int size return true; } -WinPcapLiveDevice* WinPcapLiveDevice::cloneInternal(pcap_if_t& devInterface) const +std::unique_ptr WinPcapLiveDevice::cloneInternal(pcap_if_t& devInterface) const { - return new WinPcapLiveDevice(&devInterface, true, true, true); + return std::unique_ptr(new WinPcapLiveDevice(&devInterface, true, true, true)); } } // namespace pcpp From c9e37ee5cdd7cdb97c2508414b441193c79446ac Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 2 Jun 2024 21:11:21 +0300 Subject: [PATCH 74/81] Fixed method not being const. --- Pcap++/src/PcapRemoteDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 974d969d01..f42c3ed9c2 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -20,7 +20,7 @@ PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress &ipAddress, uint16_t updateDeviceListView(); } -void PcapRemoteDeviceList::updateDeviceListView() +void PcapRemoteDeviceList::updateDeviceListView() const { // Technically if a device is removed and a different device is added, it might cause issues, // but as far as I can see the LiveDeviceList is only modified on construction and reset, and that is a whole list From 2efeaec9a8cd9160e18d846931c8ac14f8db8435 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 2 Jun 2024 21:11:36 +0300 Subject: [PATCH 75/81] Fixed duplicate clone. --- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 9d8b3076f4..ac7d26149e 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -401,7 +401,6 @@ PTF_TEST_CASE(TestPcapLiveDeviceClone) // Test of clone device should be same with original pcpp::PcapLiveDevice* liveDev = nullptr; pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch)->clone(); { pcpp::PcapLiveDevice* originalDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(originalDev); From 0528e92f57c7513a1c814e3e86c4600b99dbd66c Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 21:37:16 +0300 Subject: [PATCH 76/81] Lint --- Pcap++/header/PcapLiveDeviceList.h | 7 +++++++ Pcap++/header/PcapRemoteDeviceList.h | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 87ff2e93de..af1fe7dd7b 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -73,6 +73,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; + /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 * @param[in] ipAddr The IP address defined for the device @@ -88,6 +89,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; + /** * Get a pointer to the live device by its IPv4 address * @param[in] ipAddr The IPv4 address defined for the device @@ -103,6 +105,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; + /** * Get a pointer to the live device by its IPv6 address * @param[in] ip6Addr The IPv6 address defined for the device @@ -119,6 +122,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; + /** * Get a pointer to the live device by its IP address represented as string. IP address can be both IPv4 or IPv6 * @param[in] ipAddrAsString The IP address defined for the device as string @@ -134,6 +138,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; + /** * Get a pointer to the live device by its name * @param[in] name The name of the interface (e.g eth0) @@ -149,6 +154,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; + /** * Get a pointer to the live device by its IP address or name * @param[in] ipOrName An IP address or name of the interface @@ -169,6 +175,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapLiveDeviceList* clone() const; + /** * Copies the current live device list * @param[in] apiTag Disambiguating tag for SmartPtrAPI. diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 90c15d298a..479a5d72da 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -76,6 +76,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port); + /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods creates the instance, and also * creates a list of PcapRemoteDevice instances stored in it, one for each remote network interface. Notice this method allocates @@ -105,6 +106,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API static PcapRemoteDeviceList* getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth); + /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is suitable for connecting to * remote daemons which require authentication for accessing them @@ -118,6 +120,7 @@ namespace pcpp * - WinPcap/Npcap encountered an error connecting to the rpcapd daemon on the remote machine or retrieving devices on the remote machine */ static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth, SmartPtrApiTag apiTag); + /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is * suitable for connecting to remote daemons which require authentication for accessing them @@ -131,6 +134,7 @@ namespace pcpp * devices on the remote machine */ static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::unique_ptr remoteAuth); + /** * An overload of the previous getRemoteDeviceList() method but with authentication support. This method is * suitable for connecting to remote daemons which require authentication for accessing them @@ -163,6 +167,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; + /** * Search a PcapRemoteDevice in the list by its IPv4 address * @param[in] ip4Addr The IPv4 address @@ -178,6 +183,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; + /** * Search a PcapRemoteDevice in the list by its IPv6 address * @param[in] ip6Addr The IPv6 address @@ -193,6 +199,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; + /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) * @param[in] ipAddr The IP address @@ -208,6 +215,7 @@ namespace pcpp * @deprecated This method is deprecated in favor of the SmartPtrAPI overload. */ PCPP_DEPRECATED_RAW_PTR_API PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; + /** * Search a PcapRemoteDevice in the list by its IP address * @param[in] ipAddrAsString The IP address in string format From a95c946ee548830b1d64fa028ca127610bf1bb61 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 21:37:39 +0300 Subject: [PATCH 77/81] Improved documentation. --- Pcap++/header/PcapLiveDevice.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index ce6149a5ee..921d86d0cb 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -27,11 +27,11 @@ namespace pcpp { /* * @class SmartPtrApiTag - * Helper tag to disambiguate smart pointer API. + * Helper tag to disambiguate smart pointer API methods. */ struct SmartPtrApiTag{}; /** - * Helper tag constant for disambuguating smart pointer API. + * Helper tag constant for disambiguating smart pointer API methods. */ extern const SmartPtrApiTag SmartPtrApi; From 4ae2edb652ad68199c61fc51b32551e02025abb9 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 21:40:12 +0300 Subject: [PATCH 78/81] Changed strcmp to std::strcmp --- Pcap++/src/PcapLiveDevice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index daa11df5d6..208dc25bb4 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -13,7 +13,7 @@ #include "DeviceUtils.h" #include "SystemUtils.h" #include "MemoryUtils.h" -#include +#include #include #include #include @@ -421,7 +421,7 @@ std::unique_ptr PcapLiveDevice::clone(SmartPtrApiTag apiTag) con for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { - if (!strcmp(currInterface->name, getName().c_str())) + if (!std::strcmp(currInterface->name, getName().c_str())) { return cloneInternal(*currInterface); } From 5d9dcfd0404c94b4a1b8c4ae4ab5aa920fa44069 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 21:41:57 +0300 Subject: [PATCH 79/81] Shortened double declaration to auto. --- Pcap++/src/PcapFilter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapFilter.cpp b/Pcap++/src/PcapFilter.cpp index 775ce5b317..780dfda84c 100644 --- a/Pcap++/src/PcapFilter.cpp +++ b/Pcap++/src/PcapFilter.cpp @@ -58,7 +58,7 @@ bool BpfFilterWrapper::setFilter(const std::string& filter, LinkLayerType linkTy if (filter != m_FilterStr || linkType != m_LinkType) { - std::unique_ptr pcap = std::unique_ptr(pcap_open_dead(linkType, DEFAULT_SNAPLEN)); + auto pcap = std::unique_ptr(pcap_open_dead(linkType, DEFAULT_SNAPLEN)); if (pcap == nullptr) { return false; From 5e25d9fdb906c6b3999c0e1440773f7645c65bc8 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 21:52:20 +0300 Subject: [PATCH 80/81] Documentation fixes. --- Pcap++/src/PcapLiveDeviceList.cpp | 35 ++++++++++++----------------- Pcap++/src/PcapRemoteDeviceList.cpp | 20 +++++++---------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 9553d98152..7744c4729f 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -249,9 +249,8 @@ void PcapLiveDeviceList::setDnsServers() void PcapLiveDeviceList::updateLiveDeviceListView() const { - // Technically if a device is removed and a different device is added, it might cause issues, - // but as far as I can see the LiveDeviceList is only modified on construction and reset, and that is a whole list refresh - // which can easily be handled by clearing the view list too. + // There is a potential issue if a device is removed and another one is added between updates, + // but as far as I can see the LiveDeviceList is never partially modified. if (m_LiveDeviceList.size() != m_LiveDeviceListView.size()) { m_LiveDeviceListView.resize(m_LiveDeviceList.size()); @@ -270,9 +269,8 @@ const std::vector& PcapLiveDeviceList::getPcapLiveDevicesList() PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAddr) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getPcapLiveDeviceByIp(ipAddr, SmartPtrApi).get(); } @@ -290,9 +288,8 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getPcapLiveDeviceByIp(ipAddr, SmartPtrApi).get(); } @@ -331,9 +328,8 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getPcapLiveDeviceByIp(ip6Addr, SmartPtrApi).get(); } @@ -372,9 +368,8 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getPcapLiveDeviceByIp(ipAddrAsString, SmartPtrApi).get(); } @@ -397,9 +392,8 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByIp(const PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getPcapLiveDeviceByName(name, SmartPtrApi).get(); } @@ -420,9 +414,8 @@ std::shared_ptr PcapLiveDeviceList::getPcapLiveDeviceByName(cons PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference count. + // As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getPcapLiveDeviceByIpOrName(ipOrName, SmartPtrApi).get(); } diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index f42c3ed9c2..c31af4d694 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -100,9 +100,8 @@ std::unique_ptr PcapRemoteDeviceList::getRemoteDeviceList( PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& ipAddrAsString) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getRemoteDeviceByIP(ipAddrAsString, SmartPtrApi).get(); } @@ -125,9 +124,8 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipAddr) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getRemoteDeviceByIP(ipAddr, SmartPtrApi).get(); } @@ -146,9 +144,8 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getRemoteDeviceByIP(ip4Addr, SmartPtrApi).get(); } @@ -188,9 +185,8 @@ std::shared_ptr PcapRemoteDeviceList::getRemoteDeviceByIP(cons PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address &ip6Addr) const { - // Technically this creates and deconstructs an extra shared ptr leading to some inneficiencies but its shorter. - // As the current function is to return a non-owning pointer, the shared pointer in the list is left to keep the - // device alive. + // This line creates and immediately deconstructs an extra shared pointer which is slow due to the atomic reference + // count. As the current function returns a non-owning pointer, the list's shared pointer copy is the only one keeping the device object alive. return getRemoteDeviceByIP(ip6Addr, SmartPtrApi).get(); } From 4bcb50eec815e82122009283cda429c4042bfd2c Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 4 Jun 2024 19:43:23 +0300 Subject: [PATCH 81/81] Marked move ctors/assignment as deleted. --- Pcap++/header/PcapLiveDevice.h | 6 ++++-- Pcap++/header/PcapLiveDeviceList.h | 6 ++++-- Pcap++/header/PcapRemoteDevice.h | 6 ++++-- Pcap++/header/PcapRemoteDeviceList.h | 6 ++++-- Pcap++/header/WinPcapLiveDevice.h | 6 ++++-- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index 921d86d0cb..d1867f3998 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -137,8 +137,10 @@ namespace pcpp static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet); static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet); public: - PcapLiveDevice(const PcapLiveDevice& other) = delete; - PcapLiveDevice& operator=(const PcapLiveDevice& other) = delete; + PcapLiveDevice(const PcapLiveDevice&) = delete; + PcapLiveDevice(PcapLiveDevice&&) noexcept = delete; + PcapLiveDevice& operator=(const PcapLiveDevice&) = delete; + PcapLiveDevice& operator=(PcapLiveDevice&&) noexcept = delete; /** * The type of the live device diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index af1fe7dd7b..5813b6e65f 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -42,8 +42,10 @@ namespace pcpp void updateLiveDeviceListView() const; public: - PcapLiveDeviceList(const PcapLiveDeviceList& other) = delete; - PcapLiveDeviceList& operator=(const PcapLiveDeviceList& other) = delete; + PcapLiveDeviceList(const PcapLiveDeviceList&) = delete; + PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete; + PcapLiveDeviceList& operator=(const PcapLiveDeviceList&) = delete; + PcapLiveDeviceList& operator=(PcapLiveDeviceList&&) noexcept = delete; /** * The access method to the singleton diff --git a/Pcap++/header/PcapRemoteDevice.h b/Pcap++/header/PcapRemoteDevice.h index df133aeb9d..339405571b 100644 --- a/Pcap++/header/PcapRemoteDevice.h +++ b/Pcap++/header/PcapRemoteDevice.h @@ -93,8 +93,10 @@ namespace pcpp ThreadStart getCaptureThreadStart(); public: - PcapRemoteDevice(const PcapRemoteDevice& other) = delete; - PcapRemoteDevice& operator=(const PcapRemoteDevice& other) = delete; + 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 479a5d72da..a4359ab013 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -46,8 +46,10 @@ namespace pcpp // It is used by the other overloads for casting different kinds of pointers/references into shared_ptr. static std::unique_ptr getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth); public: - PcapRemoteDeviceList(const PcapRemoteDeviceList& other) = delete; - PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other) = delete; + PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; + PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; + PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList&) = delete; + PcapRemoteDeviceList& operator=(PcapRemoteDeviceList&&) noexcept = delete; /** * Iterator object that can be used for iterating all PcapRemoteDevice in list diff --git a/Pcap++/header/WinPcapLiveDevice.h b/Pcap++/header/WinPcapLiveDevice.h index f273f5ed43..4622f018bf 100644 --- a/Pcap++/header/WinPcapLiveDevice.h +++ b/Pcap++/header/WinPcapLiveDevice.h @@ -29,8 +29,10 @@ namespace pcpp WinPcapLiveDevice(pcap_if_t* iface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway); public: - WinPcapLiveDevice(const WinPcapLiveDevice& other) = delete; - WinPcapLiveDevice& operator=(const WinPcapLiveDevice& other) = delete; + WinPcapLiveDevice(const WinPcapLiveDevice&) = delete; + WinPcapLiveDevice(WinPcapLiveDevice&&) noexcept = delete; + WinPcapLiveDevice& operator=(const WinPcapLiveDevice&) = delete; + WinPcapLiveDevice& operator=(WinPcapLiveDevice&&) noexcept = delete; virtual LiveDeviceType getDeviceType() const { return WinPcapDevice; }