From d78f143758e967899544735c500f8bbdd55aacc4 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 22:20:59 +0300 Subject: [PATCH 1/5] Updated 'private deleted' constructors to cpp11 standard. --- Pcap++/header/PcapLiveDeviceList.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 22d3b02c55..071b8c3e8b 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -29,14 +29,16 @@ namespace pcpp // private c'tor PcapLiveDeviceList(); - // private copy c'tor - PcapLiveDeviceList( const PcapLiveDeviceList& other ); - PcapLiveDeviceList& operator=(const PcapLiveDeviceList& other); void init(); void setDnsServers(); public: + PcapLiveDeviceList(const PcapLiveDeviceList&) = delete; + PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete; + PcapLiveDeviceList& operator=(const PcapLiveDeviceList&) = delete; + PcapLiveDeviceList& operator=(PcapLiveDeviceList&&) noexcept = delete; + /** * The access method to the singleton * @return The singleton instance of this class From f85ef1ecf00f4b722fadf77d29023804b04fdd73 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 22:33:55 +0300 Subject: [PATCH 2/5] Changed PcapLiveDeviceList to hold its PcapLiveDevices in shared pointers instead of raw pointers. --- Pcap++/header/PcapLiveDeviceList.h | 12 ++++---- Pcap++/src/PcapLiveDeviceList.cpp | 47 +++++++++++++++++------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 071b8c3e8b..fc61212b4e 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; @@ -33,6 +36,8 @@ namespace pcpp void init(); void setDnsServers(); + + void updateLiveDeviceListView() const; public: PcapLiveDeviceList(const PcapLiveDeviceList&) = delete; PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete; @@ -52,7 +57,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 @@ -112,9 +117,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 47f179f479..a56cd44444 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -29,14 +29,6 @@ PcapLiveDeviceList::PcapLiveDeviceList() init(); } -PcapLiveDeviceList::~PcapLiveDeviceList() -{ - for(const auto &devIter : m_LiveDeviceList) - { - delete devIter; - } -} - void PcapLiveDeviceList::init() { pcap_if_t* interfaceList; @@ -53,12 +45,12 @@ void PcapLiveDeviceList::init() while (currInterface != nullptr) { #if defined(_WIN32) - PcapLiveDevice* dev = new WinPcapLiveDevice(currInterface, true, true, true); + auto dev = std::unique_ptr(new WinPcapLiveDevice(currInterface, true, true, true)); #else //__linux__, __APPLE__, __FreeBSD__ - PcapLiveDevice* dev = new PcapLiveDevice(currInterface, true, true, true); + auto dev = std::unique_ptr(new PcapLiveDevice(currInterface, true, true, true)); #endif currInterface = currInterface->next; - m_LiveDeviceList.insert(m_LiveDeviceList.end(), dev); + m_LiveDeviceList.push_back(std::move(dev)); } setDnsServers(); @@ -253,6 +245,25 @@ void PcapLiveDeviceList::setDnsServers() #endif } +void PcapLiveDeviceList::updateLiveDeviceListView() const +{ + // 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()); + // 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::shared_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) @@ -290,7 +301,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA if (*currAddr == ipAddr) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter; + return devIter.get(); } } } @@ -323,7 +334,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 if (*currAddr == ip6Addr) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter; + return devIter.get(); } } } @@ -353,7 +364,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::shared_ptr& dev) { return dev->getName() == name; }); if (devIter == m_LiveDeviceList.end()) { @@ -361,7 +372,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& n return nullptr; } - return *devIter; + return devIter->get(); } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const @@ -384,11 +395,7 @@ PcapLiveDeviceList* PcapLiveDeviceList::clone() void PcapLiveDeviceList::reset() { - for(auto devIter : m_LiveDeviceList) - { - delete devIter; - } - + m_LiveDeviceListView.clear(); m_LiveDeviceList.clear(); m_DnsServers.clear(); From f559505ca4801a0753f1c5c67e348b893dc87bef Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 22:43:57 +0300 Subject: [PATCH 3/5] Renamed variables in loops to be more clear. --- Pcap++/src/PcapLiveDeviceList.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index a56cd44444..2ab008ffcd 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -279,19 +279,19 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAdd PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(const auto &devIter : m_LiveDeviceList) + for(const auto& devicePtr : 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 '" << devicePtr->m_Name << "'. Searching all addresses..."); + for(const auto& addressInfo : devicePtr->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addressInfo.addr != nullptr) { std::array addrAsString; - internal::sockaddr2string(addrIter.addr, addrAsString.data(), addrAsString.size()); + internal::sockaddr2string(addressInfo.addr, addrAsString.data(), addrAsString.size()); PCPP_LOG_DEBUG("Searching address " << addrAsString.data()); } - in_addr* currAddr = internal::try_sockaddr2in_addr(addrIter.addr); + in_addr* currAddr = internal::try_sockaddr2in_addr(addressInfo.addr); if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is nullptr"); @@ -301,7 +301,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA if (*currAddr == ipAddr) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter.get(); + return devicePtr.get(); } } } @@ -312,19 +312,19 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(const auto &devIter : m_LiveDeviceList) + for(const auto& devicePtr : 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 '" << devicePtr->m_Name << "'. Searching all addresses..."); + for(const auto& addressInfo : devicePtr->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addrIter.addr != nullptr) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleLiveDevice) && addressInfo.addr != nullptr) { std::array addrAsString; - internal::sockaddr2string(addrIter.addr, addrAsString.data(), addrAsString.size()); + internal::sockaddr2string(addressInfo.addr, addrAsString.data(), addrAsString.size()); PCPP_LOG_DEBUG("Searching address " << addrAsString.data()); } - in6_addr* currAddr = internal::try_sockaddr2in6_addr(addrIter.addr); + in6_addr* currAddr = internal::try_sockaddr2in6_addr(addressInfo.addr); if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is nullptr"); @@ -334,7 +334,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 if (*currAddr == ip6Addr) { PCPP_LOG_DEBUG("Found matched address!"); - return devIter.get(); + return devicePtr.get(); } } } From 5e688f2271343a55f4a4529357f6676a51a3b1e0 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 7 Jun 2024 12:58:34 +0300 Subject: [PATCH 4/5] Moved view vector to be constructed directly in initialization. --- Pcap++/header/PcapLiveDeviceList.h | 4 ++-- Pcap++/src/PcapLiveDeviceList.cpp | 24 +++++------------------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index fc61212b4e..b5343f569e 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -26,7 +26,7 @@ namespace pcpp private: 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_LiveDeviceListView; std::vector m_DnsServers; @@ -57,7 +57,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() const { return m_LiveDeviceListView; }; /** * Get a pointer to the live device by its IP address. IP address can be both IPv4 or IPv6 diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 2ab008ffcd..aabf8e2912 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -53,6 +53,11 @@ void PcapLiveDeviceList::init() m_LiveDeviceList.push_back(std::move(dev)); } + 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::shared_ptr& ptr) { return ptr.get(); }); + setDnsServers(); PCPP_LOG_DEBUG("Freeing live device data"); @@ -245,25 +250,6 @@ void PcapLiveDeviceList::setDnsServers() #endif } -void PcapLiveDeviceList::updateLiveDeviceListView() const -{ - // 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()); - // 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::shared_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) From ad77379898d7101e71d93dec1ea6fbd7f21dfaac Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 7 Jun 2024 13:05:52 +0300 Subject: [PATCH 5/5] Changed shared_ptr to unique_ptr --- Pcap++/header/PcapLiveDeviceList.h | 2 +- Pcap++/src/PcapLiveDeviceList.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index b5343f569e..7a93fe9d74 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. std::vector m_LiveDeviceListView; diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index aabf8e2912..8e2eb120f2 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -56,7 +56,7 @@ void PcapLiveDeviceList::init() 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::shared_ptr& ptr) { return ptr.get(); }); + [](const std::unique_ptr& ptr) { return ptr.get(); }); setDnsServers(); @@ -350,7 +350,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 std::shared_ptr& dev) { return dev->getName() == name; }); + [&name](const std::unique_ptr& dev) { return dev->getName() == name; }); if (devIter == m_LiveDeviceList.end()) {