From 59c8647b1101990ee09d9a2c300cc8b1b9466d36 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 30 Jun 2024 19:50:43 +0300 Subject: [PATCH 01/64] Changed RemoteDeviceList to hold devices in a PointerVector. --- Pcap++/header/PcapRemoteDeviceList.h | 7 ++++--- Pcap++/src/PcapRemoteDeviceList.cpp | 16 +++++----------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 4d9e09a3d3..c365478426 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -4,6 +4,7 @@ #include #include "IpAddress.h" +#include "PointerVector.h" #include "PcapRemoteDevice.h" /// @file @@ -28,7 +29,7 @@ namespace pcpp class PcapRemoteDeviceList { private: - std::vector m_RemoteDeviceList; + PointerVector m_RemoteDeviceList; IPAddress m_RemoteMachineIpAddress; uint16_t m_RemoteMachinePort; std::shared_ptr m_RemoteAuthentication; @@ -44,12 +45,12 @@ namespace pcpp /** * Iterator object that can be used for iterating all PcapRemoteDevice in list */ - typedef typename std::vector::iterator RemoteDeviceListIterator; + using RemoteDeviceListIterator = PointerVector::VectorIterator; /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list */ - typedef typename std::vector::const_iterator ConstRemoteDeviceListIterator; + using ConstRemoteDeviceListIterator = PointerVector::ConstVectorIterator; PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index e852e6c297..c35f1b5850 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -9,6 +9,7 @@ #include "IpAddressUtils.h" #include "pcap.h" #include +#include #include namespace pcpp @@ -83,9 +84,9 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& 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); + std::unique_ptr pNewRemoteDevice = std::unique_ptr( + new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort())); + resultList->m_RemoteDeviceList.pushBack(std::move(pNewRemoteDevice)); } return resultList; @@ -211,14 +212,7 @@ void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthenticatio } PcapRemoteDeviceList::~PcapRemoteDeviceList() -{ - while (m_RemoteDeviceList.size() > 0) - { - RemoteDeviceListIterator devIter = m_RemoteDeviceList.begin(); - delete (*devIter); - m_RemoteDeviceList.erase(devIter); - } -} +{} } // namespace pcpp From 415880ece3a60854fdf065b50c50adc148b2f4e9 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 7 Jul 2024 09:34:38 +0300 Subject: [PATCH 02/64] Added iterator member type definitions conforming to the standard library. --- Pcap++/header/PcapRemoteDeviceList.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index c365478426..ce4191e1ee 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -46,11 +46,13 @@ namespace pcpp * Iterator object that can be used for iterating all PcapRemoteDevice in list */ using RemoteDeviceListIterator = PointerVector::VectorIterator; + using iterator = RemoteDeviceListIterator; /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list */ using ConstRemoteDeviceListIterator = PointerVector::ConstVectorIterator; + using const_iterator = ConstRemoteDeviceListIterator; PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; @@ -129,22 +131,22 @@ namespace pcpp /** * @return An iterator object pointing to the first PcapRemoteDevice in list */ - RemoteDeviceListIterator begin() { return m_RemoteDeviceList.begin(); } + iterator begin() { return m_RemoteDeviceList.begin(); } /** * @return A const iterator object pointing to the first PcapRemoteDevice in list */ - ConstRemoteDeviceListIterator begin() const { return m_RemoteDeviceList.begin(); } + const_iterator begin() const { return m_RemoteDeviceList.begin(); } /** * @return An iterator object pointing to the last PcapRemoteDevice in list */ - RemoteDeviceListIterator end() { return m_RemoteDeviceList.end(); } + iterator end() { return m_RemoteDeviceList.end(); } /** * @return A const iterator object pointing to the last PcapRemoteDevice in list */ - ConstRemoteDeviceListIterator end() const { return m_RemoteDeviceList.end(); } + const_iterator end() const { return m_RemoteDeviceList.end(); } }; From 1a678bb1f814bacb15f5ab1cf901ce28ee6f1bd8 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 7 Jul 2024 09:36:38 +0300 Subject: [PATCH 03/64] Changed PcapLiveDeviceList to utilize a PointerVector iternally. --- Pcap++/header/PcapLiveDeviceList.h | 3 ++- Pcap++/src/PcapLiveDeviceList.cpp | 13 ++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 7a93fe9d74..effa914a53 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -2,6 +2,7 @@ #include "IpAddress.h" #include "PcapLiveDevice.h" +#include "PointerVector.h" #include #include @@ -24,7 +25,7 @@ namespace pcpp class PcapLiveDeviceList { private: - std::vector> m_LiveDeviceList; + PointerVector 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 5d0650d731..b2f1b1581e 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -54,13 +54,12 @@ void PcapLiveDeviceList::init() #else //__linux__, __APPLE__, __FreeBSD__ auto dev = std::unique_ptr(new PcapLiveDevice(currInterface, true, true, true)); #endif - m_LiveDeviceList.push_back(std::move(dev)); + m_LiveDeviceList.pushBack(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::unique_ptr& ptr) { return ptr.get(); }); + std::copy(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), m_LiveDeviceListView.begin()); setDnsServers(); } @@ -291,7 +290,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA if (*currAddr == ipAddr) { PCPP_LOG_DEBUG("Found matched address!"); - return devicePtr.get(); + return devicePtr; } } } @@ -324,7 +323,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 if (*currAddr == ip6Addr) { PCPP_LOG_DEBUG("Found matched address!"); - return devicePtr.get(); + return devicePtr; } } } @@ -354,7 +353,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::unique_ptr& dev) { return dev->getName() == name; }); + [&name](PcapLiveDevice* const dev) { return dev->getName() == name; }); if (devIter == m_LiveDeviceList.end()) { @@ -362,7 +361,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& n return nullptr; } - return devIter->get(); + return *devIter; } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const From 40da178e9d02ae06d480d38380709d1b2950fd31 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 7 Jul 2024 09:37:55 +0300 Subject: [PATCH 04/64] Added iteration API to LiveDeviceList. - Deprecated `getPcapLiveDevicesList` in favor of the iteration API. --- Pcap++/header/PcapLiveDeviceList.h | 45 ++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index effa914a53..a505d1eb13 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -5,6 +5,7 @@ #include "PointerVector.h" #include #include +#include "DeprecationUtils.h" /// @file @@ -40,6 +41,16 @@ namespace pcpp void updateLiveDeviceListView() const; public: + /** + * Iterator object that can be used for iterating all PcapLiveDevice in list + */ + using iterator = PointerVector::VectorIterator; + + /** + * Const iterator object that can be used for iterating all PcapLiveDevice in a constant list + */ + using const_iterator = PointerVector::ConstVectorIterator; + PcapLiveDeviceList(const PcapLiveDeviceList&) = delete; PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete; PcapLiveDeviceList& operator=(const PcapLiveDeviceList&) = delete; @@ -57,7 +68,9 @@ namespace pcpp /** * @return A vector containing pointers to all live devices currently installed on the machine + * @deprecated This method has been deprecated in favor of begin/end iteration API. */ + PCPP_DEPRECATED("Deprecated in favor of begin/end iteration API") const std::vector& getPcapLiveDevicesList() const { return m_LiveDeviceListView; }; /** @@ -118,6 +131,38 @@ namespace pcpp * Reset the live device list and DNS server list, meaning clear and refetch them */ void reset(); + + /** + * @return An iterator object pointing to the first PcapRemoteDevice in list + */ + iterator begin() + { + return m_LiveDeviceList.begin(); + } + + /** + * @return A const iterator object pointing to the first PcapRemoteDevice in list + */ + const_iterator begin() const + { + return m_LiveDeviceList.begin(); + } + + /** + * @return An iterator object pointing to the last PcapRemoteDevice in list + */ + iterator end() + { + return m_LiveDeviceList.end(); + } + + /** + * @return A const iterator object pointing to the last PcapRemoteDevice in list + */ + const_iterator end() const + { + return m_LiveDeviceList.end(); + } }; } // namespace pcpp From e0eed6ce6ea0808d61b63c8f0d36d1023bf14637 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 7 Jul 2024 09:47:45 +0300 Subject: [PATCH 05/64] Updated usages of deprecated `getPcapLiveDevicesList`. --- Examples/Arping/main.cpp | 2 +- Examples/DNSResolver/main.cpp | 6 ++---- Examples/DnsSpoofing/main.cpp | 2 +- Examples/IcmpFileTransfer/Common.cpp | 2 +- Examples/SSLAnalyzer/main.cpp | 2 +- Examples/TLSFingerprinting/main.cpp | 2 +- 6 files changed, 7 insertions(+), 9 deletions(-) diff --git a/Examples/Arping/main.cpp b/Examples/Arping/main.cpp index 9b252de361..b0b0f14a85 100644 --- a/Examples/Arping/main.cpp +++ b/Examples/Arping/main.cpp @@ -86,7 +86,7 @@ void printAppVersion() */ void listInterfaces() { - const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); + const auto& devList = pcpp::PcapLiveDeviceList::getInstance(); std::cout << std::endl << "Network interfaces:" << std::endl; for (const auto &dev : devList) diff --git a/Examples/DNSResolver/main.cpp b/Examples/DNSResolver/main.cpp index 10ce187135..25d12f5355 100644 --- a/Examples/DNSResolver/main.cpp +++ b/Examples/DNSResolver/main.cpp @@ -73,10 +73,8 @@ void printAppVersion() */ void listInterfaces() { - const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); - std::cout << std::endl << "Network interfaces:" << std::endl; - for (const auto &dev : devList) + for (const auto& dev : pcpp::PcapLiveDeviceList::getInstance()) { std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl; } @@ -187,7 +185,7 @@ int main(int argc, char* argv[]) // if interface name or IP was not provided - find a device that has a default gateway else { - const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); + const auto& devList = pcpp::PcapLiveDeviceList::getInstance(); auto iter = std::find_if(devList.begin(), devList.end(), [](pcpp::PcapLiveDevice *dev) { return dev->getDefaultGateway() != pcpp::IPv4Address::Zero; }); diff --git a/Examples/DnsSpoofing/main.cpp b/Examples/DnsSpoofing/main.cpp index d19d014b24..a5ac81b043 100644 --- a/Examples/DnsSpoofing/main.cpp +++ b/Examples/DnsSpoofing/main.cpp @@ -125,7 +125,7 @@ void printAppVersion() */ void listInterfaces() { - const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); + const auto& devList = pcpp::PcapLiveDeviceList::getInstance(); std::cout << std::endl << "Network interfaces:" << std::endl; for (const auto &dev : devList) diff --git a/Examples/IcmpFileTransfer/Common.cpp b/Examples/IcmpFileTransfer/Common.cpp index 4e3037e7d3..eec32b36c0 100644 --- a/Examples/IcmpFileTransfer/Common.cpp +++ b/Examples/IcmpFileTransfer/Common.cpp @@ -88,7 +88,7 @@ void printAppVersion() */ void listInterfaces() { - const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); + const auto& devList = pcpp::PcapLiveDeviceList::getInstance(); std::cout << std::endl << "Network interfaces:" << std::endl; for (const auto &dev : devList) diff --git a/Examples/SSLAnalyzer/main.cpp b/Examples/SSLAnalyzer/main.cpp index 0bba4d8155..4196f00518 100644 --- a/Examples/SSLAnalyzer/main.cpp +++ b/Examples/SSLAnalyzer/main.cpp @@ -121,7 +121,7 @@ void printAppVersion() */ void listInterfaces() { - const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); + const auto& devList = pcpp::PcapLiveDeviceList::getInstance(); std::cout << std::endl << "Network interfaces:" << std::endl; for (const auto &dev : devList) diff --git a/Examples/TLSFingerprinting/main.cpp b/Examples/TLSFingerprinting/main.cpp index 683f480a2d..d75a76bdef 100644 --- a/Examples/TLSFingerprinting/main.cpp +++ b/Examples/TLSFingerprinting/main.cpp @@ -125,7 +125,7 @@ void printAppVersion() */ void listInterfaces() { - const std::vector& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); + const auto& devList = pcpp::PcapLiveDeviceList::getInstance(); std::cout << std::endl << "Network interfaces:" << std::endl; for (const auto &dev : devList) From 265865cbeb91e5d96db5269a5374795b64a181d4 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 7 Jul 2024 10:20:24 +0300 Subject: [PATCH 06/64] Removed empty destructor. --- Pcap++/header/PcapRemoteDeviceList.h | 2 -- Pcap++/src/PcapRemoteDeviceList.cpp | 3 --- 2 files changed, 5 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index ce4191e1ee..2d952a3277 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -59,8 +59,6 @@ namespace pcpp PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList& operator=(PcapRemoteDeviceList&&) noexcept = delete; - ~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 diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index c35f1b5850..02a6f33430 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -211,9 +211,6 @@ void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthenticatio } } -PcapRemoteDeviceList::~PcapRemoteDeviceList() -{} - } // namespace pcpp #endif // _WIN32 From b479b088993bfd891ba34e5f6956868b0c436cec Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 9 Jul 2024 01:16:37 +0300 Subject: [PATCH 07/64] Split overly long line. --- 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 02a6f33430..bda5b8c18f 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -84,8 +84,9 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { - std::unique_ptr pNewRemoteDevice = std::unique_ptr( - new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort())); + auto pNewRemoteDevice = std::unique_ptr( + new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, + resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort())); resultList->m_RemoteDeviceList.pushBack(std::move(pNewRemoteDevice)); } From 6a4c3306b2e1c09f6ab5929a5bc02e72a821a9da Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 10 Jul 2024 11:09:47 +0300 Subject: [PATCH 08/64] Formatting fixes. --- Pcap++/src/PcapRemoteDeviceList.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index bda5b8c18f..f77ab72f1c 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -84,9 +84,16 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { + // clang-format off auto pNewRemoteDevice = std::unique_ptr( - new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, - resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort())); + new PcapRemoteDevice( + currInterface, + resultList->m_RemoteAuthentication, + resultList->getRemoteMachineIpAddress(), + resultList->getRemoteMachinePort() + ) + ); + // clang-format on resultList->m_RemoteDeviceList.pushBack(std::move(pNewRemoteDevice)); } From f28307a3d2eaad99344076f09173087edcf09256 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 10 Jul 2024 11:21:43 +0300 Subject: [PATCH 09/64] Fixed documentation of iterator API. --- Pcap++/header/PcapLiveDeviceList.h | 8 ++++---- Pcap++/header/PcapRemoteDeviceList.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index a505d1eb13..20bf2dc638 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -133,7 +133,7 @@ namespace pcpp void reset(); /** - * @return An iterator object pointing to the first PcapRemoteDevice in list + * @return An iterator object pointing to the first device in the list */ iterator begin() { @@ -141,7 +141,7 @@ namespace pcpp } /** - * @return A const iterator object pointing to the first PcapRemoteDevice in list + * @return A constant iterator object pointing to the first device in the list */ const_iterator begin() const { @@ -149,7 +149,7 @@ namespace pcpp } /** - * @return An iterator object pointing to the last PcapRemoteDevice in list + * @return An iterator object pointing to the last device in the list */ iterator end() { @@ -157,7 +157,7 @@ namespace pcpp } /** - * @return A const iterator object pointing to the last PcapRemoteDevice in list + * @return A constant iterator object pointing to the last device in the list */ const_iterator end() const { diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 2d952a3277..f2bff6f43e 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -127,22 +127,22 @@ namespace pcpp PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; /** - * @return An iterator object pointing to the first PcapRemoteDevice in list + * @return An iterator object pointing to the first device in the list */ iterator begin() { return m_RemoteDeviceList.begin(); } /** - * @return A const iterator object pointing to the first PcapRemoteDevice in list + * @return A constant iterator object pointing to the first device in the list */ const_iterator begin() const { return m_RemoteDeviceList.begin(); } /** - * @return An iterator object pointing to the last PcapRemoteDevice in list + * @return An iterator object pointing to the last device in the list */ iterator end() { return m_RemoteDeviceList.end(); } /** - * @return A const iterator object pointing to the last PcapRemoteDevice in list + * @return A constant iterator object pointing to the last device in the list */ const_iterator end() const { return m_RemoteDeviceList.end(); } From 326d2cc935fd800c85b6504b05772595dabe3b62 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 10 Jul 2024 11:30:58 +0300 Subject: [PATCH 10/64] Updated PfRingDeviceList to use PointerVector internally. - Added iterator API - Added iterator type aliases. - Deprecated getPfRingDevicesList. - Updated calls to getPfRingDevicesList. --- Examples/PfRingExample-FilterTraffic/main.cpp | 2 +- Pcap++/header/PfRingDeviceList.h | 48 ++++++++++++++++++- Pcap++/src/PfRingDeviceList.cpp | 9 ++-- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/Examples/PfRingExample-FilterTraffic/main.cpp b/Examples/PfRingExample-FilterTraffic/main.cpp index d9b2cdbc9f..e920bd6cb9 100644 --- a/Examples/PfRingExample-FilterTraffic/main.cpp +++ b/Examples/PfRingExample-FilterTraffic/main.cpp @@ -134,7 +134,7 @@ void listPfRingDevices() // suppress errors as there may be devices (like lo) that their MAC address can't be read, etc. pcpp::Logger::getInstance().suppressLogs(); - const std::vector& devList = pcpp::PfRingDeviceList::getInstance().getPfRingDevicesList(); + const auto& devList = pcpp::PfRingDeviceList::getInstance(); for (const auto &dev : devList) { std::ostringstream interfaceIndex; diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index d800862c21..d812df2334 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -4,6 +4,8 @@ #include #include "PfRingDevice.h" +#include "PointerVector.h" +#include "DeprecationUtils.h" /// @file @@ -22,12 +24,22 @@ namespace pcpp class PfRingDeviceList { private: - std::vector> m_PfRingDeviceList; + PointerVector m_PfRingDeviceList; std::vector m_PfRingDeviceListView; std::string m_PfRingVersion; PfRingDeviceList(); public: + /** + * Iterator object that can be used for iterating all PfRingDevice in list + */ + using iterator = PointerVector::VectorIterator; + + /** + * Const iterator object that can be used for iterating all PfRingDevice in a constant list + */ + using const_iterator = PointerVector::ConstVectorIterator; + PfRingDeviceList(const PfRingDeviceList&) = delete; PfRingDeviceList(PfRingDeviceList&&) noexcept = delete; PfRingDeviceList& operator=(const PfRingDeviceList&) = delete; @@ -46,7 +58,9 @@ namespace pcpp /** * Return a list of all available PF_RING devices * @return a list of all available PF_RING devices + * @deprecated This method has been deprecated in favor of begin/end iteration API. */ + PCPP_DEPRECATED("Deprecated in favor of begin/end iteration API") const std::vector& getPfRingDevicesList() const { return m_PfRingDeviceListView; } /** @@ -61,6 +75,38 @@ namespace pcpp * @return A string representing PF_RING version */ std::string getPfRingVersion() const { return m_PfRingVersion; } + + /** + * @return An iterator object pointing to the first device in the list + */ + iterator begin() + { + return m_PfRingDeviceList.begin(); + } + + /** + * @return A constant iterator object pointing to the first device in the list + */ + const_iterator begin() const + { + return m_PfRingDeviceList.begin(); + } + + /** + * @return An iterator object pointing to the last device in the list + */ + iterator end() + { + return m_PfRingDeviceList.end(); + } + + /** + * @return A constant iterator object pointing to the last device in the list + */ + const_iterator end() const + { + return m_PfRingDeviceList.end(); + } }; } // namespace pcpp diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index e2f1489c48..f8574eae65 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -95,7 +95,7 @@ PfRingDeviceList::PfRingDeviceList() PCPP_LOG_DEBUG("PF_RING version is: " << m_PfRingVersion); } std::unique_ptr newDev = std::unique_ptr(new PfRingDevice(currInterface->name)); - m_PfRingDeviceList.push_back(std::move(newDev)); + m_PfRingDeviceList.pushBack(std::move(newDev)); PCPP_LOG_DEBUG("Found interface: " << currInterface->name); } } @@ -109,15 +109,14 @@ PfRingDeviceList::PfRingDeviceList() // Full update of all elements of the view vector to synchronize them with the main vector. m_PfRingDeviceListView.resize(m_PfRingDeviceList.size()); - std::transform(m_PfRingDeviceList.begin(), m_PfRingDeviceList.end(), m_PfRingDeviceListView.begin(), - [](const std::unique_ptr& ptr) { return ptr.get(); }); + std::copy(m_PfRingDeviceList.begin(), m_PfRingDeviceList.end(), m_PfRingDeviceListView.begin()); } PfRingDevice* PfRingDeviceList::getPfRingDeviceByName(const std::string &devName) const { PCPP_LOG_DEBUG("Searching all live devices..."); auto devIter = std::find_if(m_PfRingDeviceList.begin(), m_PfRingDeviceList.end(), - [&devName](const std::unique_ptr& dev) { return dev->getDeviceName() == devName; }); + [&devName](PfRingDevice const* dev) { return dev->getDeviceName() == devName; }); if (devIter == m_PfRingDeviceList.end()) { @@ -125,7 +124,7 @@ PfRingDevice* PfRingDeviceList::getPfRingDeviceByName(const std::string &devName return nullptr; } - return devIter->get(); + return *devIter; } } // namespace pcpp From d940946d0ca29685dfbafa89408a508b56409205 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 10 Jul 2024 11:33:51 +0300 Subject: [PATCH 11/64] Moved include to cpp file. --- Pcap++/header/PfRingDeviceList.h | 1 - Pcap++/src/PfRingDeviceList.cpp | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index d812df2334..4c739dd866 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -2,7 +2,6 @@ // GCOVR_EXCL_START -#include #include "PfRingDevice.h" #include "PointerVector.h" #include "DeprecationUtils.h" diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index f8574eae65..02eabb99f4 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "PfRingDeviceList.h" #include "SystemUtils.h" #include "DeviceUtils.h" From 921dda4c01926c2e32c6ee4136ea8d0c04806a94 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 19:54:48 +0300 Subject: [PATCH 12/64] Added a base device list class to contain common functionality between different device lists. --- Pcap++/CMakeLists.txt | 1 + Pcap++/header/DeviceListBase.h | 165 +++++++++++++++++++++++++++++ Pcap++/header/PcapLiveDeviceList.h | 40 +------ Pcap++/src/PcapLiveDeviceList.cpp | 16 +-- 4 files changed, 178 insertions(+), 44 deletions(-) create mode 100644 Pcap++/header/DeviceListBase.h diff --git a/Pcap++/CMakeLists.txt b/Pcap++/CMakeLists.txt index f28fcb6be6..f0a974b28c 100644 --- a/Pcap++/CMakeLists.txt +++ b/Pcap++/CMakeLists.txt @@ -26,6 +26,7 @@ add_library( set(public_headers header/Device.h + header/DeviceListBase.h header/NetworkUtils.h header/PcapDevice.h header/PcapFileDevice.h diff --git a/Pcap++/header/DeviceListBase.h b/Pcap++/header/DeviceListBase.h new file mode 100644 index 0000000000..39e57b3707 --- /dev/null +++ b/Pcap++/header/DeviceListBase.h @@ -0,0 +1,165 @@ +#pragma once + +/// @file + +#include "PointerVector.h" + +namespace pcpp +{ + namespace internal + { + /** + * @class DeviceListBase + * A base class for all device lists in PcapPlusPlus. This class is used to store a list of devices and provide + * access to them + */ + template class DeviceListBase + { + protected: + PointerVector m_DeviceList; + + DeviceListBase() = default; + DeviceListBase(DeviceListBase const&) = default; + DeviceListBase(DeviceListBase&&) = default; + // Protected destructor to disallow deletion of derived class through a base class pointer + ~DeviceListBase() = default; + + DeviceListBase& operator=(DeviceListBase const&) = default; + DeviceListBase& operator=(DeviceListBase&&) = default; + + public: + using value_type = DeviceType*; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + + /** + * Iterator object that can be used to iterate all devices. + */ + using iterator = typename PointerVector::VectorIterator; + + /** + * Const iterator object that can be used to iterate all devices. + */ + using const_iterator = typename PointerVector::ConstVectorIterator; + + /** + * @brief Returns a pointer to the device at the specified position in the container. + * @param pos The position of the device. + * @return A pointer to the specified device. + */ + DeviceType* at(size_type pos) + { + return m_DeviceList.at(pos); + } + + /** + * @brief Returns a pointer to the device at the specified position in the container. + * @param pos The position of the device. + * @return A pointer to the specified device. + */ + DeviceType const* at(size_type pos) const + { + return m_DeviceList.at(pos); + } + + /** + * @brief Returns a pointer to first device. + * @return A pointer to the specified device. + */ + DeviceType* front() + { + return m_DeviceList.front(); + } + /** + * @brief Returns a pointer to first device. + * @return A pointer to the specified device. + */ + DeviceType const* front() const + { + return m_DeviceList.front(); + } + /** + * @brief Returns a pointer to last device. + * @return A pointer to the specified device. + */ + DeviceType* back() + { + return m_DeviceList.back(); + } + /** + * @brief Returns a pointer to last device. + * @return A pointer to the specified device. + */ + DeviceType const* back() const + { + return m_DeviceList.back(); + } + + /** + * @brief Returns an iterator to the first device. + * @return An iterator to the specified device. + */ + iterator begin() + { + return m_DeviceList.begin(); + } + /** + * @brief Returns an iterator to the first device. + * @return An iterator to the specified device. + */ + const_iterator begin() const + { + return cbegin(); + } + /** + * @brief Returns an iterator to the first device. + * @return An iterator to the specified device. + */ + const_iterator cbegin() const + { + return m_DeviceList.begin(); + } + /** + * @brief Returns an iterator past the last device. + * @return An iterator past the last device. + */ + iterator end() + { + return m_DeviceList.end(); + } + /** + * @brief Returns an iterator past the last device. + * @return An iterator past the last device. + */ + const_iterator end() const + { + return cend(); + } + /** + * @brief Returns an iterator past the last device. + * @return An iterator past the last device. + */ + const_iterator cend() const + { + return m_DeviceList.end(); + } + + /** + * @brief Checks if the container is empty. + * @return True if the container is empty, false otherwise. + */ + bool empty() const + { + return m_DeviceList.size() == 0; + } + /** + * @brief Returns the number of devices. + * @return The number of devices in the container. + */ + size_type size() const + { + return m_DeviceList.size(); + } + }; + } // namespace internal +} // namespace pcpp diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 20bf2dc638..42716fa2a7 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -3,6 +3,7 @@ #include "IpAddress.h" #include "PcapLiveDevice.h" #include "PointerVector.h" +#include "DeviceListBase.h" #include #include #include "DeprecationUtils.h" @@ -23,10 +24,9 @@ namespace pcpp * devices are initialized on startup and wrap the network interfaces installed on the machine. This class enables access to them through * their IP addresses or get a vector of all of them so the user can search them in some other way */ - class PcapLiveDeviceList + class PcapLiveDeviceList : public internal::DeviceListBase { private: - PointerVector m_LiveDeviceList; // Vector of raw device pointers to keep the signature of getPcapLiveDevicesList, as it returns a reference. std::vector m_LiveDeviceListView; @@ -68,9 +68,9 @@ namespace pcpp /** * @return A vector containing pointers to all live devices currently installed on the machine - * @deprecated This method has been deprecated in favor of begin/end iteration API. + * @deprecated This method has been deprecated in favor of direct accessor API. */ - PCPP_DEPRECATED("Deprecated in favor of begin/end iteration API") + PCPP_DEPRECATED("Deprecated in favor of direct accessor API") const std::vector& getPcapLiveDevicesList() const { return m_LiveDeviceListView; }; /** @@ -131,38 +131,6 @@ namespace pcpp * Reset the live device list and DNS server list, meaning clear and refetch them */ void reset(); - - /** - * @return An iterator object pointing to the first device in the list - */ - iterator begin() - { - return m_LiveDeviceList.begin(); - } - - /** - * @return A constant iterator object pointing to the first device in the list - */ - const_iterator begin() const - { - return m_LiveDeviceList.begin(); - } - - /** - * @return An iterator object pointing to the last device in the list - */ - iterator end() - { - return m_LiveDeviceList.end(); - } - - /** - * @return A constant iterator object pointing to the last device in the list - */ - const_iterator end() const - { - return m_LiveDeviceList.end(); - } }; } // namespace pcpp diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index b2f1b1581e..9647bb8eba 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -54,12 +54,12 @@ void PcapLiveDeviceList::init() #else //__linux__, __APPLE__, __FreeBSD__ auto dev = std::unique_ptr(new PcapLiveDevice(currInterface, true, true, true)); #endif - m_LiveDeviceList.pushBack(std::move(dev)); + m_DeviceList.pushBack(std::move(dev)); } - m_LiveDeviceListView.resize(m_LiveDeviceList.size()); + m_LiveDeviceListView.resize(m_DeviceList.size()); // Full update of all elements of the view vector to synchronize them with the main vector. - std::copy(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), m_LiveDeviceListView.begin()); + std::copy(m_DeviceList.begin(), m_DeviceList.end(), m_LiveDeviceListView.begin()); setDnsServers(); } @@ -268,7 +268,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAdd PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(const auto& devicePtr : m_LiveDeviceList) + for(const auto& devicePtr : m_DeviceList) { PCPP_LOG_DEBUG("Searching device '" << devicePtr->m_Name << "'. Searching all addresses..."); for(const auto& addressInfo : devicePtr->m_Addresses) @@ -301,7 +301,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const { PCPP_LOG_DEBUG("Searching all live devices..."); - for(const auto& devicePtr : m_LiveDeviceList) + for(const auto& devicePtr : m_DeviceList) { PCPP_LOG_DEBUG("Searching device '" << devicePtr->m_Name << "'. Searching all addresses..."); for(const auto& addressInfo : devicePtr->m_Addresses) @@ -352,10 +352,10 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipA PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name) const { PCPP_LOG_DEBUG("Searching all live devices..."); - auto devIter = std::find_if(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), + auto devIter = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), [&name](PcapLiveDevice* const dev) { return dev->getName() == name; }); - if (devIter == m_LiveDeviceList.end()) + if (devIter == m_DeviceList.end()) { PCPP_LOG_DEBUG("Found no live device with name '" << name << "'"); return nullptr; @@ -385,7 +385,7 @@ PcapLiveDeviceList* PcapLiveDeviceList::clone() void PcapLiveDeviceList::reset() { m_LiveDeviceListView.clear(); - m_LiveDeviceList.clear(); + m_DeviceList.clear(); m_DnsServers.clear(); init(); From a3c533a49db2b14129c2b19ed1a48f6142d1a00f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 20:11:25 +0300 Subject: [PATCH 13/64] Updated PcapRemoteDeviceList and PfRingDeviceList to utilize DeviceListBase. --- Pcap++/header/DeviceListBase.h | 1 + Pcap++/header/PcapRemoteDeviceList.h | 27 +++----------------- Pcap++/header/PfRingDeviceList.h | 38 +++------------------------- Pcap++/src/PcapRemoteDeviceList.cpp | 6 ++--- Pcap++/src/PfRingDeviceList.cpp | 10 ++++---- 5 files changed, 17 insertions(+), 65 deletions(-) diff --git a/Pcap++/header/DeviceListBase.h b/Pcap++/header/DeviceListBase.h index 39e57b3707..d96ed774c1 100644 --- a/Pcap++/header/DeviceListBase.h +++ b/Pcap++/header/DeviceListBase.h @@ -19,6 +19,7 @@ namespace pcpp PointerVector m_DeviceList; DeviceListBase() = default; + DeviceListBase(PointerVector devices) : m_DeviceList(std::move(devices)) {} DeviceListBase(DeviceListBase const&) = default; DeviceListBase(DeviceListBase&&) = default; // Protected destructor to disallow deletion of derived class through a base class pointer diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 74052350de..4b211a0967 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -5,6 +5,7 @@ #include #include "IpAddress.h" #include "PointerVector.h" +#include "DeviceListBase.h" #include "PcapRemoteDevice.h" #include "DeprecationUtils.h" @@ -27,10 +28,11 @@ namespace pcpp * by iterating the PcapRemoteDevice instances (through the PcapRemoteDeviceList#RemoteDeviceListIterator iterator)
* Since Remote Capture is supported in WinPcap and Npcap only, this class is available in Windows only */ - class PcapRemoteDeviceList + class PcapRemoteDeviceList : public internal::DeviceListBase { private: - PointerVector m_RemoteDeviceList; + using Base = internal::DeviceListBase; + IPAddress m_RemoteMachineIpAddress; uint16_t m_RemoteMachinePort; std::shared_ptr m_RemoteAuthentication; @@ -168,27 +170,6 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; - - /** - * @return An iterator object pointing to the first device in the list - */ - iterator begin() { return m_RemoteDeviceList.begin(); } - - /** - * @return A constant iterator object pointing to the first device in the list - */ - const_iterator begin() const { return m_RemoteDeviceList.begin(); } - - /** - * @return An iterator object pointing to the last device in the list - */ - iterator end() { return m_RemoteDeviceList.end(); } - - /** - * @return A constant iterator object pointing to the last device in the list - */ - const_iterator end() const { return m_RemoteDeviceList.end(); } - }; } // namespace pcpp diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index 4c739dd866..85f335b6c2 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -4,6 +4,7 @@ #include "PfRingDevice.h" #include "PointerVector.h" +#include "DeviceListBase.h" #include "DeprecationUtils.h" /// @file @@ -20,10 +21,11 @@ namespace pcpp * A singleton class that holds all available PF_RING devices. Through this class the user can iterate all PF_RING devices or find a specific * device by name */ - class PfRingDeviceList + class PfRingDeviceList : public internal::DeviceListBase { private: - PointerVector m_PfRingDeviceList; + using Base = internal::DeviceListBase; + std::vector m_PfRingDeviceListView; std::string m_PfRingVersion; @@ -74,38 +76,6 @@ namespace pcpp * @return A string representing PF_RING version */ std::string getPfRingVersion() const { return m_PfRingVersion; } - - /** - * @return An iterator object pointing to the first device in the list - */ - iterator begin() - { - return m_PfRingDeviceList.begin(); - } - - /** - * @return A constant iterator object pointing to the first device in the list - */ - const_iterator begin() const - { - return m_PfRingDeviceList.begin(); - } - - /** - * @return An iterator object pointing to the last device in the list - */ - iterator end() - { - return m_PfRingDeviceList.end(); - } - - /** - * @return A constant iterator object pointing to the last device in the list - */ - const_iterator end() const - { - return m_PfRingDeviceList.end(); - } }; } // namespace pcpp diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 6118ffccfc..637ab8ba36 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -50,7 +50,7 @@ namespace pcpp } PcapRemoteDeviceList::PcapRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, std::shared_ptr remoteAuth, PointerVector deviceList) - : m_RemoteDeviceList(std::move(deviceList)) + : Base(std::move(deviceList)) , m_RemoteMachineIpAddress(ipAddress) , m_RemoteMachinePort(port) , m_RemoteAuthentication(std::move(remoteAuth)) @@ -149,7 +149,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(ConstRemoteDeviceListIterator devIter = m_DeviceList.begin(); devIter != m_DeviceList.end(); devIter++) { PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); for(const auto &addrIter : (*devIter)->m_Addresses) @@ -183,7 +183,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(ConstRemoteDeviceListIterator devIter = m_DeviceList.begin(); devIter != m_DeviceList.end(); devIter++) { PCPP_LOG_DEBUG("Searching device '" << (*devIter)->m_Name << "'. Searching all addresses..."); for(const auto &addrIter : (*devIter)->m_Addresses) diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index 02eabb99f4..24763be009 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -96,7 +96,7 @@ PfRingDeviceList::PfRingDeviceList() PCPP_LOG_DEBUG("PF_RING version is: " << m_PfRingVersion); } std::unique_ptr newDev = std::unique_ptr(new PfRingDevice(currInterface->name)); - m_PfRingDeviceList.pushBack(std::move(newDev)); + m_DeviceList.pushBack(std::move(newDev)); PCPP_LOG_DEBUG("Found interface: " << currInterface->name); } } @@ -109,17 +109,17 @@ PfRingDeviceList::PfRingDeviceList() PCPP_LOG_DEBUG("PfRingDeviceList init end"); // Full update of all elements of the view vector to synchronize them with the main vector. - m_PfRingDeviceListView.resize(m_PfRingDeviceList.size()); - std::copy(m_PfRingDeviceList.begin(), m_PfRingDeviceList.end(), m_PfRingDeviceListView.begin()); + m_PfRingDeviceListView.resize(m_DeviceList.size()); + std::copy(m_DeviceList.begin(), m_DeviceList.end(), m_PfRingDeviceListView.begin()); } PfRingDevice* PfRingDeviceList::getPfRingDeviceByName(const std::string &devName) const { PCPP_LOG_DEBUG("Searching all live devices..."); - auto devIter = std::find_if(m_PfRingDeviceList.begin(), m_PfRingDeviceList.end(), + auto devIter = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), [&devName](PfRingDevice const* dev) { return dev->getDeviceName() == devName; }); - if (devIter == m_PfRingDeviceList.end()) + if (devIter == m_DeviceList.end()) { PCPP_LOG_DEBUG("Found no PF_RING devices with name '" << devName << "'"); return nullptr; From 6e990e9513ce4df3889d77b3466839721068035f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 20:39:52 +0300 Subject: [PATCH 14/64] Fixes and cleanup. - Added explicit to DeviceListBase's 1 param ctors. - Removed 'PointerVector.h' from headers it is no longer directly used. - Removed 'iterator' and 'const_iterator' definitions as they are defined in the base class. - Changed RemoteDeviceListIterator and ConstRemoteDeviceListIterator to be aliases of iterator and const_iterator. --- Pcap++/header/DeviceListBase.h | 2 +- Pcap++/header/PcapLiveDeviceList.h | 17 ++--------------- Pcap++/header/PcapRemoteDeviceList.h | 7 ++----- Pcap++/header/PfRingDeviceList.h | 11 ----------- 4 files changed, 5 insertions(+), 32 deletions(-) diff --git a/Pcap++/header/DeviceListBase.h b/Pcap++/header/DeviceListBase.h index d96ed774c1..a8a719d938 100644 --- a/Pcap++/header/DeviceListBase.h +++ b/Pcap++/header/DeviceListBase.h @@ -19,7 +19,7 @@ namespace pcpp PointerVector m_DeviceList; DeviceListBase() = default; - DeviceListBase(PointerVector devices) : m_DeviceList(std::move(devices)) {} + explicit DeviceListBase(PointerVector devices) : m_DeviceList(std::move(devices)) {} DeviceListBase(DeviceListBase const&) = default; DeviceListBase(DeviceListBase&&) = default; // Protected destructor to disallow deletion of derived class through a base class pointer diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 42716fa2a7..9ffabea0df 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -1,11 +1,10 @@ #pragma once +#include +#include #include "IpAddress.h" #include "PcapLiveDevice.h" -#include "PointerVector.h" #include "DeviceListBase.h" -#include -#include #include "DeprecationUtils.h" @@ -38,19 +37,7 @@ namespace pcpp void init(); void setDnsServers(); - - void updateLiveDeviceListView() const; public: - /** - * Iterator object that can be used for iterating all PcapLiveDevice in list - */ - using iterator = PointerVector::VectorIterator; - - /** - * Const iterator object that can be used for iterating all PcapLiveDevice in a constant list - */ - using const_iterator = PointerVector::ConstVectorIterator; - PcapLiveDeviceList(const PcapLiveDeviceList&) = delete; PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete; PcapLiveDeviceList& operator=(const PcapLiveDeviceList&) = delete; diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 4b211a0967..430a80a958 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -4,7 +4,6 @@ #include #include "IpAddress.h" -#include "PointerVector.h" #include "DeviceListBase.h" #include "PcapRemoteDevice.h" #include "DeprecationUtils.h" @@ -46,14 +45,12 @@ namespace pcpp /** * Iterator object that can be used for iterating all PcapRemoteDevice in list */ - using RemoteDeviceListIterator = PointerVector::VectorIterator; - using iterator = RemoteDeviceListIterator; + using RemoteDeviceListIterator = iterator; /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list */ - using ConstRemoteDeviceListIterator = PointerVector::ConstVectorIterator; - using const_iterator = ConstRemoteDeviceListIterator; + using ConstRemoteDeviceListIterator = const_iterator; PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index 85f335b6c2..73085d32cb 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -3,7 +3,6 @@ // GCOVR_EXCL_START #include "PfRingDevice.h" -#include "PointerVector.h" #include "DeviceListBase.h" #include "DeprecationUtils.h" @@ -31,16 +30,6 @@ namespace pcpp PfRingDeviceList(); public: - /** - * Iterator object that can be used for iterating all PfRingDevice in list - */ - using iterator = PointerVector::VectorIterator; - - /** - * Const iterator object that can be used for iterating all PfRingDevice in a constant list - */ - using const_iterator = PointerVector::ConstVectorIterator; - PfRingDeviceList(const PfRingDeviceList&) = delete; PfRingDeviceList(PfRingDeviceList&&) noexcept = delete; PfRingDeviceList& operator=(const PfRingDeviceList&) = delete; From b31b65fe556250f53c72fa96cd6a0ca7498f8f62 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 20:43:46 +0300 Subject: [PATCH 15/64] Deprecation message fixes. --- Pcap++/header/PfRingDeviceList.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index 73085d32cb..fdef450f0b 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -48,9 +48,9 @@ namespace pcpp /** * Return a list of all available PF_RING devices * @return a list of all available PF_RING devices - * @deprecated This method has been deprecated in favor of begin/end iteration API. + * @deprecated This method has been deprecated in favor of direct accessor API. */ - PCPP_DEPRECATED("Deprecated in favor of begin/end iteration API") + PCPP_DEPRECATED("Deprecated in favor of direct accessor API") const std::vector& getPfRingDevicesList() const { return m_PfRingDeviceListView; } /** From d649dd9eb57075a30c7416e1f856349845d96dff Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 20:56:47 +0300 Subject: [PATCH 16/64] Updated DpdkDeviceList to utilize DeviceListBase. --- Pcap++/header/DpdkDeviceList.h | 11 ++++++----- Pcap++/src/DpdkDeviceList.cpp | 31 +++++++++++++------------------ 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/Pcap++/header/DpdkDeviceList.h b/Pcap++/header/DpdkDeviceList.h index e1336e982d..cadbf254b4 100644 --- a/Pcap++/header/DpdkDeviceList.h +++ b/Pcap++/header/DpdkDeviceList.h @@ -4,6 +4,7 @@ #include "SystemUtils.h" #include "DpdkDevice.h" +#include "DeviceListBase.h" #include "Logger.h" #include @@ -69,7 +70,7 @@ namespace pcpp * - it contains the list of DpdkDevice instances and enables access to them * - it has methods to start and stop worker threads. See more details in startDpdkWorkerThreads() */ - class DpdkDeviceList + class DpdkDeviceList : public internal::DeviceListBase { friend class KniDeviceList; private: @@ -78,7 +79,7 @@ namespace pcpp static uint32_t m_MBufPoolSizePerDevice; static uint16_t m_MBufDataSize; static CoreMask m_CoreMask; - std::vector m_DpdkDeviceList; + std::vector m_DpdkDeviceListView; std::vector m_WorkerThreads; DpdkDeviceList(); @@ -90,8 +91,6 @@ namespace pcpp static int dpdkWorkerThreadStart(void* ptr); public: - ~DpdkDeviceList(); - /** * As DpdkDeviceList is a singleton, this is the static getter to retrieve its instance. Note that if the static method * initDpdk() was not called or returned false this instance won't be initialized and DpdkDevices won't be initialized either @@ -149,8 +148,10 @@ namespace pcpp /** * @return A vector of all DpdkDevice instances + * @deprecated This method has been deprecated in favor of direct accessor API. */ - const std::vector& getDpdkDeviceList() const { return m_DpdkDeviceList; } + PCPP_DEPRECATED("Deprecated in favor of direct accessor API") + const std::vector& getDpdkDeviceList() const { return m_DpdkDeviceListView; } /** * @return DPDK master core which is the core that initializes the application diff --git a/Pcap++/src/DpdkDeviceList.cpp b/Pcap++/src/DpdkDeviceList.cpp index f2b139129b..bbe06a637d 100644 --- a/Pcap++/src/DpdkDeviceList.cpp +++ b/Pcap++/src/DpdkDeviceList.cpp @@ -36,6 +36,7 @@ #include #include +#include #include #include #include @@ -63,16 +64,6 @@ DpdkDeviceList::DpdkDeviceList() m_IsInitialized = false; } -DpdkDeviceList::~DpdkDeviceList() -{ - for (auto dev : m_DpdkDeviceList) - { - delete dev; - } - - m_DpdkDeviceList.clear(); -} - bool DpdkDeviceList::initDpdk(CoreMask coreMask, uint32_t mBufPoolSizePerDevice, uint16_t mBufDataSize, uint8_t masterCore, uint32_t initDpdkArgc, char **initDpdkArgv, const std::string& appName, bool verifyHugePagesAndDriver) { char **initDpdkArgvBuffer; @@ -192,11 +183,15 @@ bool DpdkDeviceList::initDpdkDevices(uint32_t mBufPoolSizePerDevice, uint16_t mB // Initialize a DpdkDevice per port for (int i = 0; i < numOfPorts; i++) { - DpdkDevice* newDevice = new DpdkDevice(i, mBufPoolSizePerDevice, mBufDataSize); + auto newDevice = std::unique_ptr(new DpdkDevice(i, mBufPoolSizePerDevice, mBufDataSize)); PCPP_LOG_DEBUG("DpdkDevice #" << i << ": Name='" << newDevice->getDeviceName() << "', PCI-slot='" << newDevice->getPciAddress() << "', PMD='" << newDevice->getPMDName() << "', MAC Addr='" << newDevice->getMacAddress() << "'"); - m_DpdkDeviceList.push_back(newDevice); + m_DeviceList.pushBack(std::move(newDevice)); } + // Full update of all elements of the view vector to synchronize them with the main vector. + m_DpdkDeviceListView.resize(m_DeviceList.size()); + std::copy(m_DeviceList.begin(), m_DeviceList.end(), m_DpdkDeviceListView.begin()); + m_IsInitialized = true; return true; } @@ -206,15 +201,15 @@ DpdkDevice* DpdkDeviceList::getDeviceByPort(int portId) const if (!isInitialized()) { PCPP_LOG_ERROR("DpdkDeviceList not initialized"); - return NULL; + return nullptr; } - if ((uint32_t)portId >= m_DpdkDeviceList.size()) + if (static_cast(portId) >= m_DeviceList.size()) { - return NULL; + return nullptr; } - return m_DpdkDeviceList.at(portId); + return m_DeviceList.at(portId); } DpdkDevice* DpdkDeviceList::getDeviceByPciAddress(const std::string& pciAddr) const @@ -225,10 +220,10 @@ DpdkDevice* DpdkDeviceList::getDeviceByPciAddress(const std::string& pciAddr) co return NULL; } - auto devIter = std::find_if(m_DpdkDeviceList.begin(), m_DpdkDeviceList.end(), + auto devIter = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), [&pciAddr](const DpdkDevice *dev) { return dev->getPciAddress() == pciAddr; }); - if (devIter == m_DpdkDeviceList.end()) + if (devIter == m_DeviceList.end()) { PCPP_LOG_DEBUG("Found no DPDK devices with PCI address '" << pciAddr << "'"); return nullptr; From e9143d1e385c480d343adbe2f790cad11812b8c6 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 21:16:06 +0300 Subject: [PATCH 17/64] Updated KniDeviceList to utilize DeviceListBase. --- Pcap++/header/KniDeviceList.h | 6 ++--- Pcap++/src/KniDeviceList.cpp | 49 ++++++++++++++--------------------- 2 files changed, 22 insertions(+), 33 deletions(-) diff --git a/Pcap++/header/KniDeviceList.h b/Pcap++/header/KniDeviceList.h index 41a1a3273d..8a3e0005de 100644 --- a/Pcap++/header/KniDeviceList.h +++ b/Pcap++/header/KniDeviceList.h @@ -2,10 +2,9 @@ // GCOVR_EXCL_START -#include - #include "KniDevice.h" #include "DpdkDeviceList.h" +#include "DeviceListBase.h" /** * \namespace pcpp @@ -20,7 +19,7 @@ namespace pcpp * and holds the list of KniDevice instances. * As it's a singleton, it has only one active instance doesn't have a public c'tor. */ - class KniDeviceList + class KniDeviceList : public internal::DeviceListBase { KniDeviceList(); @@ -120,7 +119,6 @@ namespace pcpp */ static bool isCallbackSupported(const KniCallbackType cbType); private: - std::vector m_Devices; bool m_Initialized; int m_KniUniqueId; }; diff --git a/Pcap++/src/KniDeviceList.cpp b/Pcap++/src/KniDeviceList.cpp index 546cb3a607..e866c98014 100644 --- a/Pcap++/src/KniDeviceList.cpp +++ b/Pcap++/src/KniDeviceList.cpp @@ -5,6 +5,7 @@ #define LOG_MODULE PcapLogModuleKniDevice #include +#include #include #include "KniDeviceList.h" @@ -41,11 +42,8 @@ static inline bool checkKniDriver() return true; } -KniDeviceList::KniDeviceList() : - m_Devices(), - m_Initialized(true), m_KniUniqueId(0) +KniDeviceList::KniDeviceList() : m_Initialized(true), m_KniUniqueId(0) { - m_Devices.reserve(MAX_KNI_DEVICES); if (!checkKniDriver()) { m_Initialized = false; @@ -69,8 +67,7 @@ KniDeviceList::KniDeviceList() : KniDeviceList::~KniDeviceList() { - for (size_t i = 0; i < m_Devices.size(); ++i) - delete m_Devices[i]; + m_DeviceList.clear(); rte_kni_close(); } @@ -86,40 +83,36 @@ KniDevice* KniDeviceList::createDevice( ) { if (!isInitialized()) - return NULL; + return nullptr; KniDevice* kniDevice = getDeviceByName(std::string(config.name)); - if (kniDevice != NULL) + if (kniDevice != nullptr) { PCPP_LOG_ERROR("Attempt to create DPDK KNI device with same name: '" << config.name << "'"); PCPP_LOG_DEBUG("Use KniDeviceList::getDeviceByName or KniDeviceList::getDeviceByPort."); - return NULL; + return nullptr; } if (config.portId != UINT16_MAX) { kniDevice = getDeviceByPort(config.portId); - if (kniDevice != NULL) + if (kniDevice != nullptr) { PCPP_LOG_ERROR("Attempt to create DPDK KNI device with same port ID: " << config.portId); PCPP_LOG_DEBUG("Use KniDeviceList::getDeviceByName or KniDeviceList::getDeviceByPort."); - return NULL; + return nullptr; } } kniDevice = new KniDevice(config, mempoolSize, m_KniUniqueId++); - m_Devices.push_back(kniDevice); + m_DeviceList.pushBack(kniDevice); return kniDevice; } void KniDeviceList::destroyDevice(KniDevice* kniDevice) { - m_Devices.erase( - std::remove( - m_Devices.begin(), - m_Devices.end(), - kniDevice - ), - m_Devices.end() - ); - delete kniDevice; + auto it = std::find(m_DeviceList.begin(), m_DeviceList.end(), kniDevice); + if (it != m_DeviceList.end()) + { + m_DeviceList.erase(it); + } } KniDevice* KniDeviceList::getDeviceByPort(const uint16_t portId) @@ -127,30 +120,28 @@ KniDevice* KniDeviceList::getDeviceByPort(const uint16_t portId) //? Linear search here is optimal for low count of devices. //? We assume that no one will create large count of devices or will rapidly search them. //? Same for function - KniDevice* kniDevice = NULL; + KniDevice* kniDevice = nullptr; if (!isInitialized()) return kniDevice; - for (size_t i = 0; i < m_Devices.size(); ++i) + for (auto const kniDevice : m_DeviceList) { - kniDevice = m_Devices[i]; if (kniDevice && kniDevice->m_DeviceInfo.portId == portId) return kniDevice; } - return kniDevice = NULL; + return kniDevice = nullptr; } KniDevice* KniDeviceList::getDeviceByName(const std::string& name) { - KniDevice* kniDevice = NULL; + KniDevice* kniDevice = nullptr; if (!isInitialized()) return kniDevice; - for (size_t i = 0; i < m_Devices.size(); ++i) + for (auto const kniDevice : m_DeviceList) { - kniDevice = m_Devices[i]; if (kniDevice && kniDevice->m_DeviceInfo.name == name) return kniDevice; } - return kniDevice = NULL; + return kniDevice = nullptr; } KniDeviceList::KniCallbackVersion KniDeviceList::callbackVersion() From c956f674374981493dc026b97b57b02575185e8f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 21:28:47 +0300 Subject: [PATCH 18/64] Fixed KniDevice device search. --- Pcap++/src/KniDeviceList.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/Pcap++/src/KniDeviceList.cpp b/Pcap++/src/KniDeviceList.cpp index e866c98014..530cc54f24 100644 --- a/Pcap++/src/KniDeviceList.cpp +++ b/Pcap++/src/KniDeviceList.cpp @@ -120,28 +120,23 @@ KniDevice* KniDeviceList::getDeviceByPort(const uint16_t portId) //? Linear search here is optimal for low count of devices. //? We assume that no one will create large count of devices or will rapidly search them. //? Same for function - KniDevice* kniDevice = nullptr; if (!isInitialized()) - return kniDevice; - for (auto const kniDevice : m_DeviceList) - { - if (kniDevice && kniDevice->m_DeviceInfo.portId == portId) - return kniDevice; - } - return kniDevice = nullptr; + return nullptr; + + auto const foundIt = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), + [&portId](KniDevice* kniDevice) { return kniDevice && kniDevice->m_DeviceInfo.portId == portId; } + ); + return foundIt != m_DeviceList.end() ? *foundIt : nullptr; } KniDevice* KniDeviceList::getDeviceByName(const std::string& name) { - KniDevice* kniDevice = nullptr; if (!isInitialized()) - return kniDevice; - for (auto const kniDevice : m_DeviceList) - { - if (kniDevice && kniDevice->m_DeviceInfo.name == name) - return kniDevice; - } - return kniDevice = nullptr; + return nullptr; + auto const foundIt = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), + [&name](KniDevice* kniDevice) { return kniDevice && kniDevice->m_DeviceInfo.name == name; } + ); + return foundIt != m_DeviceList.end() ? *foundIt : nullptr; } KniDeviceList::KniCallbackVersion KniDeviceList::callbackVersion() From ccaac4e90b922e90aa4d4f53073fb44994e58783 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 21:32:36 +0300 Subject: [PATCH 19/64] Refactored KniDeviceList::createDevice to use smart pointers during creation. --- Pcap++/src/KniDeviceList.cpp | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Pcap++/src/KniDeviceList.cpp b/Pcap++/src/KniDeviceList.cpp index 530cc54f24..8747513fa4 100644 --- a/Pcap++/src/KniDeviceList.cpp +++ b/Pcap++/src/KniDeviceList.cpp @@ -84,25 +84,21 @@ KniDevice* KniDeviceList::createDevice( { if (!isInitialized()) return nullptr; - KniDevice* kniDevice = getDeviceByName(std::string(config.name)); - if (kniDevice != nullptr) + + if (getDeviceByName(std::string(config.name)) != nullptr) { PCPP_LOG_ERROR("Attempt to create DPDK KNI device with same name: '" << config.name << "'"); PCPP_LOG_DEBUG("Use KniDeviceList::getDeviceByName or KniDeviceList::getDeviceByPort."); return nullptr; } - if (config.portId != UINT16_MAX) + if (config.portId != UINT16_MAX && getDeviceByPort(config.portId) != nullptr) { - kniDevice = getDeviceByPort(config.portId); - if (kniDevice != nullptr) - { - PCPP_LOG_ERROR("Attempt to create DPDK KNI device with same port ID: " << config.portId); - PCPP_LOG_DEBUG("Use KniDeviceList::getDeviceByName or KniDeviceList::getDeviceByPort."); - return nullptr; - } + PCPP_LOG_ERROR("Attempt to create DPDK KNI device with same port ID: " << config.portId); + PCPP_LOG_DEBUG("Use KniDeviceList::getDeviceByName or KniDeviceList::getDeviceByPort."); + return nullptr; } - kniDevice = new KniDevice(config, mempoolSize, m_KniUniqueId++); - m_DeviceList.pushBack(kniDevice); + auto kniDevice = std::unique_ptr(new KniDevice(config, mempoolSize, m_KniUniqueId++)); + m_DeviceList.pushBack(std::move(kniDevice)); return kniDevice; } From df807f7f3532765bf7ff87a8dddab8e9572aee95 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 21:56:42 +0300 Subject: [PATCH 20/64] Fixed KniDevice deletion errors. - Added extension point to PointerVector to specialize deleter. - Added extension point to DeviceListBase to specialize deleter. - Added internal::KniDeviceDeleter to be used in KniDeviceList to release KniDevice. --- Common++/header/PointerVector.h | 13 +++++++------ Pcap++/header/DeviceListBase.h | 15 ++++++++------- Pcap++/header/KniDevice.h | 6 ++++++ Pcap++/header/KniDeviceList.h | 16 +++++++++++++++- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Common++/header/PointerVector.h b/Common++/header/PointerVector.h index 299c190033..39f0e27fc4 100644 --- a/Common++/header/PointerVector.h +++ b/Common++/header/PointerVector.h @@ -25,9 +25,10 @@ namespace pcpp * removed from the vector This class wraps std::vector and adds the capability of freeing objects once they're * removed from it */ - template class PointerVector + template > class PointerVector { public: + using deleter_type = typename Deleter; /** * Iterator object that is used for iterating all elements in the vector */ @@ -148,7 +149,7 @@ namespace pcpp { if (freeElementOnError) { - delete element; + Deleter()(element); } throw; } @@ -259,7 +260,7 @@ namespace pcpp */ VectorIterator erase(VectorIterator position) { - delete (*position); + Deleter()(*position); return m_Vector.erase(position); } @@ -343,7 +344,7 @@ namespace pcpp } catch (const std::exception&) { - delete objCopy; + Deleter()(objCopy); throw; } } @@ -352,7 +353,7 @@ namespace pcpp { for (auto obj : copyVec) { - delete obj; + Deleter()(obj); } throw; } @@ -370,7 +371,7 @@ namespace pcpp { for (auto& obj : origin) { - delete obj; + Deleter()(obj); } } diff --git a/Pcap++/header/DeviceListBase.h b/Pcap++/header/DeviceListBase.h index a8a719d938..3cd82d9fcc 100644 --- a/Pcap++/header/DeviceListBase.h +++ b/Pcap++/header/DeviceListBase.h @@ -13,20 +13,21 @@ namespace pcpp * A base class for all device lists in PcapPlusPlus. This class is used to store a list of devices and provide * access to them */ - template class DeviceListBase + template > class DeviceListBase { protected: - PointerVector m_DeviceList; + PointerVector m_DeviceList; DeviceListBase() = default; - explicit DeviceListBase(PointerVector devices) : m_DeviceList(std::move(devices)) {} - DeviceListBase(DeviceListBase const&) = default; - DeviceListBase(DeviceListBase&&) = default; + explicit DeviceListBase(PointerVector devices) : m_DeviceList(std::move(devices)) + {} + DeviceListBase(DeviceListBase const&) = default; + DeviceListBase(DeviceListBase&&) = default; // Protected destructor to disallow deletion of derived class through a base class pointer ~DeviceListBase() = default; - DeviceListBase& operator=(DeviceListBase const&) = default; - DeviceListBase& operator=(DeviceListBase&&) = default; + DeviceListBase& operator=(DeviceListBase const&) = default; + DeviceListBase& operator=(DeviceListBase&&) = default; public: using value_type = DeviceType*; diff --git a/Pcap++/header/KniDevice.h b/Pcap++/header/KniDevice.h index 57d8640edd..a3fa76f298 100644 --- a/Pcap++/header/KniDevice.h +++ b/Pcap++/header/KniDevice.h @@ -88,6 +88,11 @@ namespace pcpp { class KniDevice; class KniDeviceList; + + namespace internal + { + class KniDeviceDeleter; + } /** * Defines the signature callback used by capturing API on KNI device @@ -122,6 +127,7 @@ namespace pcpp class KniDevice : public IDevice { friend class KniDeviceList; + friend class internal::KniDeviceDeleter; friend class MBufRawPacket; public: /** diff --git a/Pcap++/header/KniDeviceList.h b/Pcap++/header/KniDeviceList.h index 8a3e0005de..c9c0f33a6e 100644 --- a/Pcap++/header/KniDeviceList.h +++ b/Pcap++/header/KniDeviceList.h @@ -13,13 +13,27 @@ namespace pcpp { + namespace internal + { + /** + * @class Specialized deleter for KniDevice. + * @remarks Do not use outside of KniDeviceList. + */ + struct KniDeviceDeleter + { + void operator()(KniDevice* kniDevice) + { + delete kniDevice; + } + }; + /** * @class KniDeviceList * A singleton class that encapsulates DPDK KNI module initialization * and holds the list of KniDevice instances. * As it's a singleton, it has only one active instance doesn't have a public c'tor. */ - class KniDeviceList : public internal::DeviceListBase + class KniDeviceList : public internal::DeviceListBase { KniDeviceList(); From 38b5633de98af4eadd12db41ec97ea859236166e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 21:57:26 +0300 Subject: [PATCH 21/64] Lint --- Pcap++/header/KniDevice.h | 2 +- Pcap++/src/KniDeviceList.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Pcap++/header/KniDevice.h b/Pcap++/header/KniDevice.h index a3fa76f298..c4922c56e6 100644 --- a/Pcap++/header/KniDevice.h +++ b/Pcap++/header/KniDevice.h @@ -88,7 +88,7 @@ namespace pcpp { class KniDevice; class KniDeviceList; - + namespace internal { class KniDeviceDeleter; diff --git a/Pcap++/src/KniDeviceList.cpp b/Pcap++/src/KniDeviceList.cpp index 8747513fa4..3e2eb3663c 100644 --- a/Pcap++/src/KniDeviceList.cpp +++ b/Pcap++/src/KniDeviceList.cpp @@ -84,7 +84,7 @@ KniDevice* KniDeviceList::createDevice( { if (!isInitialized()) return nullptr; - + if (getDeviceByName(std::string(config.name)) != nullptr) { PCPP_LOG_ERROR("Attempt to create DPDK KNI device with same name: '" << config.name << "'"); @@ -129,7 +129,7 @@ KniDevice* KniDeviceList::getDeviceByName(const std::string& name) { if (!isInitialized()) return nullptr; - auto const foundIt = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), + auto const foundIt = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), [&name](KniDevice* kniDevice) { return kniDevice && kniDevice->m_DeviceInfo.name == name; } ); return foundIt != m_DeviceList.end() ? *foundIt : nullptr; From 7668b9f7977c115679cd9260be5a538aaf01f862 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 22:01:05 +0300 Subject: [PATCH 22/64] Fixed namespace closure. --- Pcap++/header/KniDeviceList.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Pcap++/header/KniDeviceList.h b/Pcap++/header/KniDeviceList.h index c9c0f33a6e..95ac24805f 100644 --- a/Pcap++/header/KniDeviceList.h +++ b/Pcap++/header/KniDeviceList.h @@ -26,6 +26,7 @@ namespace pcpp delete kniDevice; } }; + } // namespace internal /** * @class KniDeviceList From 91ccd0e53f48f2e4bbd15000310da22f53fc06b5 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 22:05:05 +0300 Subject: [PATCH 23/64] Fixed alias. --- Common++/header/PointerVector.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Common++/header/PointerVector.h b/Common++/header/PointerVector.h index 39f0e27fc4..10f428c8c8 100644 --- a/Common++/header/PointerVector.h +++ b/Common++/header/PointerVector.h @@ -28,7 +28,7 @@ namespace pcpp template > class PointerVector { public: - using deleter_type = typename Deleter; + using deleter_type = Deleter; /** * Iterator object that is used for iterating all elements in the vector */ From df2730f3f352f0f3fa590b2bf70550568eb262aa Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 22:07:57 +0300 Subject: [PATCH 24/64] Fixed KniDeviceDeleter documentation. --- Pcap++/header/KniDeviceList.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/KniDeviceList.h b/Pcap++/header/KniDeviceList.h index 95ac24805f..a6f9620621 100644 --- a/Pcap++/header/KniDeviceList.h +++ b/Pcap++/header/KniDeviceList.h @@ -16,7 +16,7 @@ namespace pcpp namespace internal { /** - * @class Specialized deleter for KniDevice. + * @class KniDeviceDeleter Specialized deleter for KniDevice. * @remarks Do not use outside of KniDeviceList. */ struct KniDeviceDeleter From e43425859f98475c2eaed5c460820d44a46f8502 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 22:12:08 +0300 Subject: [PATCH 25/64] Reverted creation of KniDevice to raw ptr. --- Pcap++/src/KniDeviceList.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Pcap++/src/KniDeviceList.cpp b/Pcap++/src/KniDeviceList.cpp index 3e2eb3663c..02ad6c8baa 100644 --- a/Pcap++/src/KniDeviceList.cpp +++ b/Pcap++/src/KniDeviceList.cpp @@ -5,7 +5,6 @@ #define LOG_MODULE PcapLogModuleKniDevice #include -#include #include #include "KniDeviceList.h" @@ -97,8 +96,8 @@ KniDevice* KniDeviceList::createDevice( PCPP_LOG_DEBUG("Use KniDeviceList::getDeviceByName or KniDeviceList::getDeviceByPort."); return nullptr; } - auto kniDevice = std::unique_ptr(new KniDevice(config, mempoolSize, m_KniUniqueId++)); - m_DeviceList.pushBack(std::move(kniDevice)); + auto kniDevice = new KniDevice(config, mempoolSize, m_KniUniqueId++); + m_DeviceList.pushBack(kniDevice); return kniDevice; } From 85a35f536273298f5be75611def681af3da56c1c Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 22:17:12 +0300 Subject: [PATCH 26/64] Fixed KniDeviceDeleter documentation again. --- Pcap++/header/KniDeviceList.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/KniDeviceList.h b/Pcap++/header/KniDeviceList.h index a6f9620621..add02bd41d 100644 --- a/Pcap++/header/KniDeviceList.h +++ b/Pcap++/header/KniDeviceList.h @@ -16,8 +16,8 @@ namespace pcpp namespace internal { /** - * @class KniDeviceDeleter Specialized deleter for KniDevice. - * @remarks Do not use outside of KniDeviceList. + * @class KniDeviceDeleter + * Specialized deleter for KniDevice. Do not use outside of KniDeviceList. */ struct KniDeviceDeleter { From ed143d7ad1a66dab8fd8073357b98fb21b426323 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 22:28:15 +0300 Subject: [PATCH 27/64] Attempt to fix dpdk device fetch by port. --- Pcap++/src/DpdkDeviceList.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Pcap++/src/DpdkDeviceList.cpp b/Pcap++/src/DpdkDeviceList.cpp index bbe06a637d..558525a77f 100644 --- a/Pcap++/src/DpdkDeviceList.cpp +++ b/Pcap++/src/DpdkDeviceList.cpp @@ -209,7 +209,8 @@ DpdkDevice* DpdkDeviceList::getDeviceByPort(int portId) const return nullptr; } - return m_DeviceList.at(portId); + // Hack because the device list returns a pointer to const, when the device list is const qualified. + return const_cast(m_DeviceList.at(portId)); } DpdkDevice* DpdkDeviceList::getDeviceByPciAddress(const std::string& pciAddr) const From e5dc2da3f04637084c19c25ede66f8abe26249e0 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 29 Jul 2024 22:29:55 +0300 Subject: [PATCH 28/64] Lint --- Pcap++/header/KniDeviceList.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/KniDeviceList.h b/Pcap++/header/KniDeviceList.h index add02bd41d..bbd1d9358d 100644 --- a/Pcap++/header/KniDeviceList.h +++ b/Pcap++/header/KniDeviceList.h @@ -16,7 +16,7 @@ namespace pcpp namespace internal { /** - * @class KniDeviceDeleter + * @class KniDeviceDeleter * Specialized deleter for KniDevice. Do not use outside of KniDeviceList. */ struct KniDeviceDeleter From 2ae51d24cc81103afa70d28995011cfe965d829e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 30 Jul 2024 17:39:14 +0300 Subject: [PATCH 29/64] Minor fixes. - Fixed cast to size_t instead of uint32_t. - Style fixes. --- Pcap++/src/DpdkDeviceList.cpp | 2 +- Pcap++/src/KniDeviceList.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Pcap++/src/DpdkDeviceList.cpp b/Pcap++/src/DpdkDeviceList.cpp index 558525a77f..dfe64c48b1 100644 --- a/Pcap++/src/DpdkDeviceList.cpp +++ b/Pcap++/src/DpdkDeviceList.cpp @@ -204,7 +204,7 @@ DpdkDevice* DpdkDeviceList::getDeviceByPort(int portId) const return nullptr; } - if (static_cast(portId) >= m_DeviceList.size()) + if (static_cast(portId) >= m_DeviceList.size()) { return nullptr; } diff --git a/Pcap++/src/KniDeviceList.cpp b/Pcap++/src/KniDeviceList.cpp index 02ad6c8baa..58878de276 100644 --- a/Pcap++/src/KniDeviceList.cpp +++ b/Pcap++/src/KniDeviceList.cpp @@ -82,7 +82,9 @@ KniDevice* KniDeviceList::createDevice( ) { if (!isInitialized()) + { return nullptr; + } if (getDeviceByName(std::string(config.name)) != nullptr) { From a73d4b91931bdb6a3bf8c783578a80692e329bc7 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 13:12:15 +0300 Subject: [PATCH 30/64] Changed PcapLiveDeviceList getters to 'getDeviceBy*' syntax. - Added new device getter functions in the pattern 'getDeviceBy*'. - Deprecated the old getter functions 'getPcapLiveDeviceBy'. --- Examples/ArpSpoofing/main.cpp | 2 +- Examples/Arping/main.cpp | 2 +- Examples/DNSResolver/main.cpp | 2 +- Examples/DnsSpoofing/main.cpp | 2 +- Examples/HttpAnalyzer/main.cpp | 2 +- Examples/IcmpFileTransfer/Common.cpp | 2 +- .../IcmpFileTransfer-catcher.cpp | 4 +- .../IcmpFileTransfer-pitcher.cpp | 4 +- Examples/SSLAnalyzer/main.cpp | 2 +- Examples/TLSFingerprinting/main.cpp | 2 +- Examples/TcpReassembly/main.cpp | 2 +- Pcap++/header/PcapLiveDevice.h | 2 +- Pcap++/header/PcapLiveDeviceList.h | 48 ++++++++++++++++--- Pcap++/src/PcapLiveDeviceList.cpp | 22 ++++----- Tests/Pcap++Test/Tests/FilterTests.cpp | 2 +- Tests/Pcap++Test/Tests/IpMacTests.cpp | 2 +- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 36 +++++++------- Tests/Pcap++Test/Tests/PfRingTests.cpp | 12 ++--- Tests/Pcap++Test/Tests/XdpTests.cpp | 2 +- 19 files changed, 94 insertions(+), 58 deletions(-) diff --git a/Examples/ArpSpoofing/main.cpp b/Examples/ArpSpoofing/main.cpp index d25e1a6eb3..55bdc934f6 100644 --- a/Examples/ArpSpoofing/main.cpp +++ b/Examples/ArpSpoofing/main.cpp @@ -241,7 +241,7 @@ int main(int argc, char* argv[]) EXIT_WITH_ERROR("Gateway address is not valid"); } - pcpp::PcapLiveDevice* pIfaceDevice = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ifaceAddr); + pcpp::PcapLiveDevice* pIfaceDevice = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ifaceAddr); // Verifying interface is valid if (pIfaceDevice == nullptr) diff --git a/Examples/Arping/main.cpp b/Examples/Arping/main.cpp index 5117441e92..0040e0f06f 100644 --- a/Examples/Arping/main.cpp +++ b/Examples/Arping/main.cpp @@ -198,7 +198,7 @@ int main(int argc, char* argv[]) // Search interface by name or IP if (!ifaceNameOrIP.empty()) { - dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(ifaceNameOrIP); + dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(ifaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); } diff --git a/Examples/DNSResolver/main.cpp b/Examples/DNSResolver/main.cpp index 2ad9fce0fe..9e50e4dd1a 100644 --- a/Examples/DNSResolver/main.cpp +++ b/Examples/DNSResolver/main.cpp @@ -180,7 +180,7 @@ int main(int argc, char* argv[]) // if interface name or IP was provided - find the device accordingly if (interfaceNameOrIPProvided) { - dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(interfaceNameOrIP); + dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); } diff --git a/Examples/DnsSpoofing/main.cpp b/Examples/DnsSpoofing/main.cpp index 22f7b77c04..1863b854ef 100644 --- a/Examples/DnsSpoofing/main.cpp +++ b/Examples/DnsSpoofing/main.cpp @@ -444,7 +444,7 @@ int main(int argc, char* argv[]) EXIT_WITH_ERROR("Interface name or IP weren't provided. Please use the -i switch or -h for help"); } - dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(interfaceNameOrIP); + dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); diff --git a/Examples/HttpAnalyzer/main.cpp b/Examples/HttpAnalyzer/main.cpp index b1f6803177..74d88bc7f5 100644 --- a/Examples/HttpAnalyzer/main.cpp +++ b/Examples/HttpAnalyzer/main.cpp @@ -575,7 +575,7 @@ int main(int argc, char* argv[]) else // analyze in live traffic mode { pcpp::PcapLiveDevice* dev = - pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(interfaceNameOrIP); + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); diff --git a/Examples/IcmpFileTransfer/Common.cpp b/Examples/IcmpFileTransfer/Common.cpp index c2fea0061f..da947ad191 100644 --- a/Examples/IcmpFileTransfer/Common.cpp +++ b/Examples/IcmpFileTransfer/Common.cpp @@ -174,7 +174,7 @@ void readCommandLineArguments(int argc, char* argv[], const std::string& thisSid } catch (const std::exception&) { - pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(interfaceNameOrIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR_PRINT_USAGE("Cannot find interface by provided name"); diff --git a/Examples/IcmpFileTransfer/IcmpFileTransfer-catcher.cpp b/Examples/IcmpFileTransfer/IcmpFileTransfer-catcher.cpp index a1ecd4703d..9f40b04eba 100644 --- a/Examples/IcmpFileTransfer/IcmpFileTransfer-catcher.cpp +++ b/Examples/IcmpFileTransfer/IcmpFileTransfer-catcher.cpp @@ -204,7 +204,7 @@ static bool getFileContent(pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* dev void receiveFile(pcpp::IPv4Address pitcherIP, pcpp::IPv4Address catcherIP) { // identify the interface to listen and send packets to - pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(catcherIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(catcherIP); if (dev == nullptr) EXIT_WITH_ERROR("Cannot find network interface with IP '" << catcherIP << "'"); @@ -417,7 +417,7 @@ static bool sendContent(pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* dev, v void sendFile(const std::string& filePath, pcpp::IPv4Address pitcherIP, pcpp::IPv4Address catcherIP, size_t blockSize) { // identify the interface to listen and send packets to - pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(catcherIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(catcherIP); if (dev == nullptr) EXIT_WITH_ERROR("Cannot find network interface with IP '" << catcherIP << "'"); diff --git a/Examples/IcmpFileTransfer/IcmpFileTransfer-pitcher.cpp b/Examples/IcmpFileTransfer/IcmpFileTransfer-pitcher.cpp index 2702f3b0c0..388717d401 100644 --- a/Examples/IcmpFileTransfer/IcmpFileTransfer-pitcher.cpp +++ b/Examples/IcmpFileTransfer/IcmpFileTransfer-pitcher.cpp @@ -214,7 +214,7 @@ static void getFileContent(pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* dev void receiveFile(pcpp::IPv4Address pitcherIP, pcpp::IPv4Address catcherIP, int packetPerSec) { // identify the interface to listen and send packets to - pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(pitcherIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(pitcherIP); if (dev == nullptr) EXIT_WITH_ERROR("Cannot find network interface with IP '" << pitcherIP << "'"); @@ -397,7 +397,7 @@ void sendFile(const std::string& filePath, pcpp::IPv4Address pitcherIP, pcpp::IP int packetPerSec) { // identify the interface to listen and send packets to - pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(pitcherIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(pitcherIP); if (dev == nullptr) EXIT_WITH_ERROR("Cannot find network interface with IP '" << pitcherIP << "'"); diff --git a/Examples/SSLAnalyzer/main.cpp b/Examples/SSLAnalyzer/main.cpp index fe7be4fd1b..58645fcd66 100644 --- a/Examples/SSLAnalyzer/main.cpp +++ b/Examples/SSLAnalyzer/main.cpp @@ -536,7 +536,7 @@ int main(int argc, char* argv[]) { // extract pcap live device by interface name or IP address pcpp::PcapLiveDevice* dev = - pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(interfaceNameOrIP); + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); diff --git a/Examples/TLSFingerprinting/main.cpp b/Examples/TLSFingerprinting/main.cpp index 6b6830d0fd..1d331bf8ca 100644 --- a/Examples/TLSFingerprinting/main.cpp +++ b/Examples/TLSFingerprinting/main.cpp @@ -461,7 +461,7 @@ void doTlsFingerprintingOnLiveTraffic(const std::string& interfaceNameOrIP, std: const std::string& separator, bool chFP, bool shFP, const std::string& bpfFilter) { // extract pcap live device by interface name or IP address - pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(interfaceNameOrIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by given IP address or name"); diff --git a/Examples/TcpReassembly/main.cpp b/Examples/TcpReassembly/main.cpp index 8a19f86e60..beb1fbd12c 100644 --- a/Examples/TcpReassembly/main.cpp +++ b/Examples/TcpReassembly/main.cpp @@ -691,7 +691,7 @@ int main(int argc, char* argv[]) { // extract pcap live device by interface name or IP address pcpp::PcapLiveDevice* dev = - pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(interfaceNameOrIP); + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index 868af6a3a7..b693a962df 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -63,7 +63,7 @@ namespace pcpp * almost similar in capabilities, the main difference between them is adapting some capabilities to the specific OS. * This class cannot be instantiated by the user (it has a private constructor), as network interfaces aren't dynamic. Instances of * this class (one instance per network interface) are created by PcapLiveDeviceList singleton on application startup and the user can get - * access to them by using PcapLiveDeviceList public methods such as PcapLiveDeviceList#getPcapLiveDeviceByIp()
+ * access to them by using PcapLiveDeviceList public methods such as PcapLiveDeviceList#getDeviceByIp()
* Main capabilities of this class: * - Get all available information for this network interfaces such as name, IP addresses, MAC address, MTU, etc. This information is taken * from both libpcap and the OS diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 9ffabea0df..d61b351888 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -65,42 +65,78 @@ namespace pcpp * @param[in] ipAddr The IP address defined for the device * @return A pointer to the live device if this IP address exists. NULL otherwise */ - PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; + PcapLiveDevice* getDeviceByIp(const IPAddress& ipAddr) const; + /** + * @copydoc PcapLiveDeviceList::getDeviceByIp(IPAddress const&) + * @deprecated This method has been deprecated in favor of getDeviceByIp(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const { return getDeviceByIp(ipAddr); } /** * 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 */ - PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const; + PcapLiveDevice* getDeviceByIp(const IPv4Address& ipAddr) const; + /* + * @copydoc getDeviceByIp(IPv4Address const&) + * @deprecated This method has been deprecated in favor of getDeviceByIp(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const { return getDeviceByIp(ipAddr); } /** * 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 */ - PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const; + PcapLiveDevice* getDeviceByIp(const IPv6Address& ip6Addr) const; + /** + * @copydoc getDeviceByIp(IPv6Address const&) + * @deprecated This method has been deprecated in favor of getDeviceByIp(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const { return getDeviceByIp(ip6Addr); } /** * 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 */ - PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const; + PcapLiveDevice* getDeviceByIp(const std::string& ipAddrAsString) const; + /** + * @copydoc getDeviceByIp(std::string const&) + * @deprecated This method has been deprecated in favor of getDeviceByIp(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const { return getDeviceByIp(ipAddrAsString); } /** * 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 */ - PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const; + PcapLiveDevice* getDeviceByName(const std::string& name) const; + /** + * @copydoc getDeviceByName(std::string const&) + * @deprecated This method has been deprecated in favor of getDeviceByName(...). + */ + PCPP_DEPRECATED("Please use getPcapLiveDeviceByName(...) instead.") + PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const { return getDeviceByName(name); } /** * 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 */ - PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; + PcapLiveDevice* getDeviceByIpOrName(const std::string& ipOrName) const; + /** + * @copydoc getDeviceByIpOrName(std::string const&) + * @deprecated This method has been deprecated in favor of getDeviceByIpOrName(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const { return getDeviceByIpOrName(ipOrName); } /** * @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 9647bb8eba..85f8f0a6fe 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -253,19 +253,19 @@ void PcapLiveDeviceList::setDnsServers() #endif } -PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAddr) const +PcapLiveDevice* PcapLiveDeviceList::getDeviceByIp(const IPAddress& ipAddr) const { if (ipAddr.getType() == IPAddress::IPv4AddressType) { - return getPcapLiveDeviceByIp(ipAddr.getIPv4()); + return getDeviceByIp(ipAddr.getIPv4()); } else //IPAddress::IPv6AddressType { - return getPcapLiveDeviceByIp(ipAddr.getIPv6()); + return getDeviceByIp(ipAddr.getIPv6()); } } -PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const +PcapLiveDevice* PcapLiveDeviceList::getDeviceByIp(const IPv4Address& ipAddr) const { PCPP_LOG_DEBUG("Searching all live devices..."); for(const auto& devicePtr : m_DeviceList) @@ -298,7 +298,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA return nullptr; } -PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const +PcapLiveDevice* PcapLiveDeviceList::getDeviceByIp(const IPv6Address& ip6Addr) const { PCPP_LOG_DEBUG("Searching all live devices..."); for(const auto& devicePtr : m_DeviceList) @@ -331,7 +331,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6 return nullptr; } -PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const +PcapLiveDevice* PcapLiveDeviceList::getDeviceByIp(const std::string& ipAddrAsString) const { IPAddress ipAddr; try @@ -344,12 +344,12 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipA return nullptr; } - PcapLiveDevice* result = PcapLiveDeviceList::getPcapLiveDeviceByIp(ipAddr); + PcapLiveDevice* result = PcapLiveDeviceList::getDeviceByIp(ipAddr); return result; } -PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& name) const +PcapLiveDevice* PcapLiveDeviceList::getDeviceByName(const std::string& name) const { PCPP_LOG_DEBUG("Searching all live devices..."); auto devIter = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), @@ -364,16 +364,16 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByName(const std::string& n return *devIter; } -PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const +PcapLiveDevice* PcapLiveDeviceList::getDeviceByIpOrName(const std::string& ipOrName) const { try { IPAddress interfaceIP = IPAddress(ipOrName); - return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(interfaceIP); + return PcapLiveDeviceList::getInstance().getDeviceByIp(interfaceIP); } catch (std::exception&) { - return PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(ipOrName); + return PcapLiveDeviceList::getInstance().getDeviceByName(ipOrName); } } diff --git a/Tests/Pcap++Test/Tests/FilterTests.cpp b/Tests/Pcap++Test/Tests/FilterTests.cpp index 13f656fbb0..7b4e5e298d 100644 --- a/Tests/Pcap++Test/Tests/FilterTests.cpp +++ b/Tests/Pcap++Test/Tests/FilterTests.cpp @@ -36,7 +36,7 @@ PTF_TEST_CASE(TestPcapFiltersLive) { pcpp::PcapLiveDevice* liveDev = nullptr; pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(liveDev); std::string filterAsString; diff --git a/Tests/Pcap++Test/Tests/IpMacTests.cpp b/Tests/Pcap++Test/Tests/IpMacTests.cpp index 7fa15a470f..48b24f1e6a 100644 --- a/Tests/Pcap++Test/Tests/IpMacTests.cpp +++ b/Tests/Pcap++Test/Tests/IpMacTests.cpp @@ -355,7 +355,7 @@ PTF_TEST_CASE(TestGetMacAddress) { pcpp::PcapLiveDevice* liveDev = nullptr; pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open()); DeviceTeardown devTeardown(liveDev); diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 1536131312..4cee3ca205 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -269,23 +269,23 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) PTF_TEST_CASE(TestPcapLiveDeviceListSearch) { pcpp::PcapLiveDevice* liveDev = nullptr; - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); std::string devName(liveDev->getName()); pcpp::PcapLiveDevice* liveDev2 = nullptr; - liveDev2 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByName(devName); + liveDev2 = pcpp::PcapLiveDeviceList::getInstance().getDeviceByName(devName); PTF_ASSERT_NOT_NULL(liveDev2); PTF_ASSERT_EQUAL(liveDev->getName(), liveDev2->getName()); - pcpp::PcapLiveDevice* liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(devName); + pcpp::PcapLiveDevice* liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(devName); PTF_ASSERT_EQUAL(liveDev3, liveDev2, ptr); liveDev3 = - pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets); + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets); PTF_ASSERT_EQUAL(liveDev3, liveDev, ptr); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("255.255.255.250"); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp("255.255.255.250"); PTF_ASSERT_NULL(liveDev); } // TestPcapLiveDeviceListSearch @@ -293,7 +293,7 @@ PTF_TEST_CASE(TestPcapLiveDevice) { pcpp::PcapLiveDevice* liveDev = nullptr; pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0); PTF_ASSERT_TRUE(liveDev->open()); @@ -347,7 +347,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceClone) pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); { - pcpp::PcapLiveDevice* originalDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + pcpp::PcapLiveDevice* originalDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(originalDev); #ifdef _WIN32 @@ -426,7 +426,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceNoNetworking) // a negative test - check invalid IP address liveDev = nullptr; pcpp::Logger::getInstance().suppressLogs(); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp("eth0"); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp("eth0"); pcpp::Logger::getInstance().enableLogs(); PTF_ASSERT_NULL(liveDev); @@ -434,7 +434,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceNoNetworking) PTF_TEST_CASE(TestPcapLiveDeviceStatsMode) { - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open()); @@ -485,7 +485,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceBlockingMode) for (const auto& config : configs) { // open device - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open(config)); @@ -576,7 +576,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceWithLambda) { pcpp::PcapLiveDevice* liveDev = nullptr; pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_GREATER_THAN(liveDev->getMtu(), 0); PTF_ASSERT_TRUE(liveDev->open()); @@ -623,7 +623,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceBlockingModeWithLambda) }; // open device - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open()); @@ -643,7 +643,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceBlockingModeWithLambda) PTF_TEST_CASE(TestPcapLiveDeviceSpecialCfg) { - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); @@ -715,7 +715,7 @@ PTF_TEST_CASE(TestWinPcapLiveDevice) { #if defined(_WIN32) - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_EQUAL(liveDev->getDeviceType(), pcpp::PcapLiveDevice::WinPcapDevice, enum); @@ -752,7 +752,7 @@ PTF_TEST_CASE(TestWinPcapLiveDevice) pcpp::Logger::getInstance().enableLogs(); #else - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_EQUAL(liveDev->getDeviceType(), pcpp::PcapLiveDevice::LibPcapDevice, enum); @@ -764,7 +764,7 @@ PTF_TEST_CASE(TestSendPacket) { pcpp::PcapLiveDevice* liveDev = nullptr; pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open()); DeviceTeardown devTeardown(liveDev); @@ -813,7 +813,7 @@ PTF_TEST_CASE(TestSendPackets) { pcpp::PcapLiveDevice* liveDev = nullptr; pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open()); DeviceTeardown devTeardown(liveDev); @@ -849,7 +849,7 @@ PTF_TEST_CASE(TestMtuSize) { pcpp::PcapLiveDevice* liveDev = nullptr; pcpp::IPv4Address ipToSearch(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); - liveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp(ipToSearch); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(ipToSearch); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open()); DeviceTeardown devTearDown(liveDev); diff --git a/Tests/Pcap++Test/Tests/PfRingTests.cpp b/Tests/Pcap++Test/Tests/PfRingTests.cpp index b97ad500b4..c98d3802c0 100644 --- a/Tests/Pcap++Test/Tests/PfRingTests.cpp +++ b/Tests/Pcap++Test/Tests/PfRingTests.cpp @@ -202,7 +202,7 @@ PTF_TEST_CASE(TestPfRingDevice) pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); PTF_ASSERT_GREATER_THAN(devList.getPfRingDevicesList().size(), 0); PTF_ASSERT_NOT_EQUAL(devList.getPfRingVersion(), ""); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); @@ -252,7 +252,7 @@ PTF_TEST_CASE(TestPfRingDeviceSingleChannel) #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); @@ -293,7 +293,7 @@ PTF_TEST_CASE(TestPfRingDeviceMultiThread) { #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); @@ -434,7 +434,7 @@ PTF_TEST_CASE(TestPfRingSendPacket) { #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); @@ -499,7 +499,7 @@ PTF_TEST_CASE(TestPfRingSendPackets) { #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); @@ -542,7 +542,7 @@ PTF_TEST_CASE(TestPfRingFilters) { #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); diff --git a/Tests/Pcap++Test/Tests/XdpTests.cpp b/Tests/Pcap++Test/Tests/XdpTests.cpp index 13676c3873..4120a56f19 100644 --- a/Tests/Pcap++Test/Tests/XdpTests.cpp +++ b/Tests/Pcap++Test/Tests/XdpTests.cpp @@ -35,7 +35,7 @@ bool assertConfig(const pcpp::XdpDevice::XdpDeviceConfiguration* config, std::string getDeviceName() { - auto pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDeviceByIp( + auto pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); if (pcapLiveDev) { From 75c4e324dedcc6646584e07517413f6174b8a64c Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 13:24:43 +0300 Subject: [PATCH 31/64] Replaced usages of deprecated method 'getPcapLiveDevicesList()'. --- Examples/HttpAnalyzer/main.cpp | 5 +---- Examples/TcpReassembly/main.cpp | 4 +--- Examples/XdpExample-FilterTraffic/main.cpp | 2 +- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 25 +++++++++++----------- 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/Examples/HttpAnalyzer/main.cpp b/Examples/HttpAnalyzer/main.cpp index 74d88bc7f5..fe887abd82 100644 --- a/Examples/HttpAnalyzer/main.cpp +++ b/Examples/HttpAnalyzer/main.cpp @@ -124,11 +124,8 @@ void printAppVersion() */ void listInterfaces() { - const std::vector& liveDevices = - pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); - std::cout << std::endl << "Network interfaces:" << std::endl; - for (const auto& device : liveDevices) + for (const auto& device : pcpp::PcapLiveDeviceList::getInstance()) { std::cout << " -> Name: '" << device->getName() << "' IP address: " << device->getIPv4Address().toString() << std::endl; diff --git a/Examples/TcpReassembly/main.cpp b/Examples/TcpReassembly/main.cpp index beb1fbd12c..3978d1a89b 100644 --- a/Examples/TcpReassembly/main.cpp +++ b/Examples/TcpReassembly/main.cpp @@ -330,11 +330,9 @@ void printAppVersion() */ void listInterfaces() { - auto const& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); - std::cout << std::endl << "Network interfaces:" << std::endl; - for (auto dev : devList) + for (auto dev : pcpp::PcapLiveDeviceList::getInstance()) { std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl; diff --git a/Examples/XdpExample-FilterTraffic/main.cpp b/Examples/XdpExample-FilterTraffic/main.cpp index f08b180c44..7fa7edc560 100644 --- a/Examples/XdpExample-FilterTraffic/main.cpp +++ b/Examples/XdpExample-FilterTraffic/main.cpp @@ -356,7 +356,7 @@ void printAppVersion() void listInterfaces() { std::cout << std::endl << "Network interfaces:" << std::endl; - for (const auto& device : pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList()) + for (const auto& device : pcpp::PcapLiveDeviceList::getInstance()) { if (device->getIPv4Address() != pcpp::IPv4Address::Zero) { diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 4cee3ca205..d9438e927f 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -11,6 +11,8 @@ #include "../Common/TestUtils.h" #include "../Common/PcapFileNamesDef.h" #include +#include +#include #include #include #if defined(_WIN32) @@ -223,7 +225,7 @@ class RpcapdServerInitializer PTF_TEST_CASE(TestPcapLiveDeviceList) { - std::vector devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); + auto& devList = pcpp::PcapLiveDeviceList::getInstance(); PTF_ASSERT_FALSE(devList.empty()); pcpp::IPv4Address defaultGateway = pcpp::IPv4Address::Zero; @@ -242,7 +244,6 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) // reset the device list and make sure devices are back and there is no memory leak pcpp::PcapLiveDeviceList::getInstance().reset(); - devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); PTF_ASSERT_FALSE(devList.empty()); for (const auto& iter : devList) @@ -250,18 +251,18 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) PTF_ASSERT_FALSE(iter->getName().empty()); } - pcpp::PcapLiveDeviceList* clonedDevList = pcpp::PcapLiveDeviceList::getInstance().clone(); - PTF_ASSERT_NOT_NULL(clonedDevList); + { + auto clonedDevList = std::unique_ptr(devList.clone()); + PTF_ASSERT_NOT_NULL(clonedDevList); - std::vector clonedDevListVector = clonedDevList->getPcapLiveDevicesList(); - PTF_ASSERT_EQUAL(clonedDevListVector.size(), devList.size()); + PTF_ASSERT_EQUAL(clonedDevList->size(), devList.size()); - auto iterCloned = clonedDevListVector.begin(); - for (auto iter = devList.begin(); iter != devList.end(); ++iter, ++iterCloned) - { - PTF_ASSERT_EQUAL((*iter)->getName(), (*iterCloned)->getName()); + for (auto itPair = std::make_pair(devList.begin(), clonedDevList->begin()); itPair.first != devList.end(); + ++itPair.first, ++itPair.second) + { + PTF_ASSERT_EQUAL((*itPair.first)->getName(), (*itPair.second)->getName()); + } } - delete clonedDevList; PTF_ASSERT_EQUAL(pcpp::PcapLiveDeviceList::getInstance().getDnsServers().size(), dnsServerCount); } // TestPcapLiveDeviceList @@ -407,7 +408,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceNoNetworking) pcpp::PcapLiveDevice* liveDev = nullptr; - std::vector devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList(); + auto& devList = pcpp::PcapLiveDeviceList::getInstance(); PTF_ASSERT_FALSE(devList.empty()); auto iter = std::find_if(devList.begin(), devList.end(), [](const pcpp::PcapLiveDevice* dev) { From ab45201ec4219fbc338a2fd70d8402d4c5dfec25 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 13:32:25 +0300 Subject: [PATCH 32/64] Replaced usages of deprecated method 'getPfRingDevicesList()'. --- Tests/Pcap++Test/Tests/PfRingTests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Pcap++Test/Tests/PfRingTests.cpp b/Tests/Pcap++Test/Tests/PfRingTests.cpp index c98d3802c0..a427f3f494 100644 --- a/Tests/Pcap++Test/Tests/PfRingTests.cpp +++ b/Tests/Pcap++Test/Tests/PfRingTests.cpp @@ -200,7 +200,7 @@ PTF_TEST_CASE(TestPfRingDevice) #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - PTF_ASSERT_GREATER_THAN(devList.getPfRingDevicesList().size(), 0); + PTF_ASSERT_GREATER_THAN(devList.size(), 0); PTF_ASSERT_NOT_EQUAL(devList.getPfRingVersion(), ""); pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); From cee1e43ed01364739e9c9e61e29d7eec6f085caa Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 13:45:38 +0300 Subject: [PATCH 33/64] Deprecated old iterator aliases for RemoteDeviceList and updated usages. --- Pcap++/header/PcapRemoteDeviceList.h | 6 +++-- Pcap++/src/PcapRemoteDeviceList.cpp | 28 +++++++++++----------- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 5 ++-- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 430a80a958..c0ceb2187c 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -44,13 +44,15 @@ namespace pcpp public: /** * Iterator object that can be used for iterating all PcapRemoteDevice in list + * @deprecated Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'. */ - using RemoteDeviceListIterator = iterator; + using RemoteDeviceListIterator = PCPP_DEPRECATED("Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'.") iterator; /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list + * @deprecated Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'. */ - using ConstRemoteDeviceListIterator = const_iterator; + using ConstRemoteDeviceListIterator = PCPP_DEPRECATED("Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'.") const_iterator; PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 637ab8ba36..e93ad19f5d 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -149,12 +149,12 @@ 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_DeviceList.begin(); devIter != m_DeviceList.end(); devIter++) + for(auto const devicePtr : m_DeviceList) { - 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& addrIter : devicePtr->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != NULL) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != nullptr) { std::array addrAsString; internal::sockaddr2string(addrIter.addr, addrAsString.data(), addrAsString.size()); @@ -162,7 +162,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i } in_addr* currAddr = internal::try_sockaddr2in_addr(addrIter.addr); - if (currAddr == NULL) + if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is NULL"); continue; @@ -171,24 +171,24 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i if (*currAddr == ip4Addr) { PCPP_LOG_DEBUG("Found matched address!"); - return (*devIter); + return devicePtr; } } } - return NULL; + return nullptr; } PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); - for(ConstRemoteDeviceListIterator devIter = m_DeviceList.begin(); devIter != m_DeviceList.end(); devIter++) + for (auto devicePtr : m_DeviceList) { - 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& addrIter : devicePtr->m_Addresses) { - if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != NULL) + if (Logger::getInstance().isDebugEnabled(PcapLogModuleRemoteDevice) && addrIter.addr != nullptr) { std::array addrAsString; internal::sockaddr2string(addrIter.addr, addrAsString.data(), addrAsString.size()); @@ -196,7 +196,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& i } in6_addr* currAddr = internal::try_sockaddr2in6_addr(addrIter.addr); - if (currAddr == NULL) + if (currAddr == nullptr) { PCPP_LOG_DEBUG("Address is NULL"); continue; @@ -205,12 +205,12 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& i if (*currAddr == ip6Addr) { PCPP_LOG_DEBUG("Found matched address!"); - return (*devIter); + return devicePtr; } } } - return NULL; + return nullptr; } diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index d9438e927f..047a78d8b9 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -952,10 +952,9 @@ PTF_TEST_CASE(TestRemoteCapture) pcpp::PcapRemoteDeviceList* remoteDevices = pcpp::PcapRemoteDeviceList::getRemoteDeviceList(remoteDeviceIPAddr, remoteDevicePort); PTF_ASSERT_NOT_NULL(remoteDevices); - for (pcpp::PcapRemoteDeviceList::RemoteDeviceListIterator remoteDevIter = remoteDevices->begin(); - remoteDevIter != remoteDevices->end(); remoteDevIter++) + for (auto const remoteDevicePtr : *remoteDevices) { - PTF_ASSERT_FALSE((*remoteDevIter)->getName().empty()); + PTF_ASSERT_FALSE(remoteDevicePtr->getName().empty()); } PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachineIpAddress().toString(), remoteDeviceIP); PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachinePort(), remoteDevicePort); From 3b72143353ae2713f60d78f9aeb5a05ceb9938d1 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 13:53:38 +0300 Subject: [PATCH 34/64] Replaced deprecated function 'getRemoteDeviceList' usages. --- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 047a78d8b9..65ab02da20 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -949,8 +949,7 @@ PTF_TEST_CASE(TestRemoteCapture) PTF_ASSERT_NOT_NULL(rpcapdInitializer.getHandle()); pcpp::IPv4Address remoteDeviceIPAddr(remoteDeviceIP); - pcpp::PcapRemoteDeviceList* remoteDevices = - pcpp::PcapRemoteDeviceList::getRemoteDeviceList(remoteDeviceIPAddr, remoteDevicePort); + auto remoteDevices = pcpp::PcapRemoteDeviceList::createRemoteDeviceList(remoteDeviceIPAddr, remoteDevicePort); PTF_ASSERT_NOT_NULL(remoteDevices); for (auto const remoteDevicePtr : *remoteDevices) { @@ -1019,8 +1018,6 @@ PTF_TEST_CASE(TestRemoteCapture) remoteDevice->close(); - delete remoteDevices; - // the device object is already deleted, cannot close it devTeardown.cancelTeardown(); #else From ffac4327cc97c461d9be37db6dcd6487ec6181a8 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 14:08:12 +0300 Subject: [PATCH 35/64] Changed PcapRemoteDeviceList getters to 'getDeviceBy*' syntax. - Added new device getter functions in the pattern 'getDeviceBy*'. - Deprecated the old getter functions 'getPcapLiveDeviceBy*'. --- Pcap++/header/PcapRemoteDeviceList.h | 32 +++++++++++++++++++--- Pcap++/src/PcapRemoteDeviceList.cpp | 14 +++++----- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 2 +- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index c0ceb2187c..4b0bd00c59 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -147,28 +147,52 @@ namespace pcpp * @param[in] ip4Addr The IPv4 address * @return The PcapRemoteDevice if found, NULL otherwise */ - PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const; + PcapRemoteDevice* getDeviceByIp(const IPv4Address& ip4Addr) const; + /* + * @copydoc getDeviceByIp(IPv4Address const&) + * @deprecated This method has been deprecated in favor of getDeviceByIp(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapRemoteDevice* getRemoteDeviceByIP(const IPv4Address& ip4Addr) const { return getDeviceByIp(ip4Addr); } /** * Search a PcapRemoteDevice in the list by its IPv6 address * @param[in] ip6Addr The IPv6 address * @return The PcapRemoteDevice if found, NULL otherwise */ - PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const; + PcapRemoteDevice* getDeviceByIp(const IPv6Address& ip6Addr) const; + /** + * @copydoc getDeviceByIp(IPv6Address const&) + * @deprecated This method has been deprecated in favor of getDeviceByIp(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapRemoteDevice* getRemoteDeviceByIP(const IPv6Address& ip6Addr) const { return getDeviceByIp(ip6Addr); } /** * 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 */ - PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const; + PcapRemoteDevice* getDeviceByIp(const IPAddress& ipAddr) const; + /** + * @copydoc getDeviceByIp(IPAddress const&) + * @deprecated This method has been deprecated in favor of getDeviceByIp(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapRemoteDevice* getRemoteDeviceByIP(const IPAddress& ipAddr) const { return getDeviceByIp(ipAddr); }; /** * 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 */ - PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const; + PcapRemoteDevice* getDeviceByIp(const std::string& ipAddrAsString) const; + /** + * @copydoc getDeviceByIp(std::string const&) + * @deprecated This method has been deprecated in favor of getDeviceByIp(...). + */ + PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") + PcapRemoteDevice* getRemoteDeviceByIP(const std::string& ipAddrAsString) const { return getDeviceByIp(ipAddrAsString); }; }; } // namespace pcpp diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index e93ad19f5d..201afcca3d 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -115,7 +115,7 @@ std::unique_ptr PcapRemoteDeviceList::createRemoteDeviceLi return std::unique_ptr(new PcapRemoteDeviceList(ipAddress, port, pRemoteAuthCopy, std::move(devices))); } -PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& ipAddrAsString) const +PcapRemoteDevice* PcapRemoteDeviceList::getDeviceByIp(const std::string& ipAddrAsString) const { IPAddress ipAddr; @@ -129,24 +129,24 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const std::string& i return nullptr; } - PcapRemoteDevice* result = getRemoteDeviceByIP(ipAddr); + PcapRemoteDevice* result = getDeviceByIp(ipAddr); return result; } -PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPAddress& ipAddr) const +PcapRemoteDevice* PcapRemoteDeviceList::getDeviceByIp(const IPAddress& ipAddr) const { if (ipAddr.getType() == IPAddress::IPv4AddressType) { - return getRemoteDeviceByIP(ipAddr.getIPv4()); + return getDeviceByIp(ipAddr.getIPv4()); } else //IPAddress::IPv6AddressType { - return getRemoteDeviceByIP(ipAddr.getIPv6()); + return getDeviceByIp(ipAddr.getIPv6()); } } -PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& ip4Addr) const +PcapRemoteDevice* PcapRemoteDeviceList::getDeviceByIp(const IPv4Address& ip4Addr) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); for(auto const devicePtr : m_DeviceList) @@ -180,7 +180,7 @@ PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv4Address& i } -PcapRemoteDevice* PcapRemoteDeviceList::getRemoteDeviceByIP(const IPv6Address& ip6Addr) const +PcapRemoteDevice* PcapRemoteDeviceList::getDeviceByIp(const IPv6Address& ip6Addr) const { PCPP_LOG_DEBUG("Searching all remote devices in list..."); for (auto devicePtr : m_DeviceList) diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index 65ab02da20..deeac9cbf0 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -958,7 +958,7 @@ PTF_TEST_CASE(TestRemoteCapture) PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachineIpAddress().toString(), remoteDeviceIP); PTF_ASSERT_EQUAL(remoteDevices->getRemoteMachinePort(), remoteDevicePort); - pcpp::PcapRemoteDevice* remoteDevice = remoteDevices->getRemoteDeviceByIP(remoteDeviceIPAddr); + pcpp::PcapRemoteDevice* remoteDevice = remoteDevices->getDeviceByIp(remoteDeviceIPAddr); PTF_ASSERT_NOT_NULL(remoteDevice); PTF_ASSERT_EQUAL(remoteDevice->getDeviceType(), pcpp::PcapLiveDevice::RemoteDevice, enum); PTF_ASSERT_EQUAL(remoteDevice->getMtu(), 0); From 2245bb5bd6f92aae05eb0068f32ec97b881e85bf Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 14:12:06 +0300 Subject: [PATCH 36/64] Fixed deprecation message. --- Pcap++/header/PcapLiveDeviceList.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index d61b351888..81968b52d8 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -122,7 +122,7 @@ namespace pcpp * @copydoc getDeviceByName(std::string const&) * @deprecated This method has been deprecated in favor of getDeviceByName(...). */ - PCPP_DEPRECATED("Please use getPcapLiveDeviceByName(...) instead.") + PCPP_DEPRECATED("Please use getDeviceByName(...) instead.") PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const { return getDeviceByName(name); } /** From 151f09fba68c5cf0aa660e42a69812dd1a3f98b1 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 14:12:46 +0300 Subject: [PATCH 37/64] Changed PfRingDeviceList getters to 'getDeviceBy*' syntax. - Added new device getter functions in the pattern 'getDeviceBy*'. - Deprecated the old getter functions 'getPfRingDeviceBy*'. --- Pcap++/header/PfRingDeviceList.h | 8 +++++++- Pcap++/src/PfRingDeviceList.cpp | 2 +- Tests/Pcap++Test/Tests/PfRingTests.cpp | 12 ++++++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index fdef450f0b..155d143f45 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -58,7 +58,13 @@ namespace pcpp * (e.g eth0, eth1, etc.) * @return A pointer to the PF_RING device */ - PfRingDevice* getPfRingDeviceByName(const std::string &devName) const; + PfRingDevice* getDeviceByName(const std::string& devName) const; + /** + * @copydoc getDeviceByName(std::string const&) + * @deprecated This method has been deprecated in favor of getDeviceByName(...). + */ + PCPP_DEPRECATED("Please use getDeviceByName(...) instead.") + PfRingDevice* getPfRingDeviceByName(const std::string& devName) const { return getDeviceByName(devName); }; /** * Get installed PF_RING version diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index 24763be009..f433919279 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -113,7 +113,7 @@ PfRingDeviceList::PfRingDeviceList() std::copy(m_DeviceList.begin(), m_DeviceList.end(), m_PfRingDeviceListView.begin()); } -PfRingDevice* PfRingDeviceList::getPfRingDeviceByName(const std::string &devName) const +PfRingDevice* PfRingDeviceList::getDeviceByName(const std::string &devName) const { PCPP_LOG_DEBUG("Searching all live devices..."); auto devIter = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), diff --git a/Tests/Pcap++Test/Tests/PfRingTests.cpp b/Tests/Pcap++Test/Tests/PfRingTests.cpp index a427f3f494..e5e6a32175 100644 --- a/Tests/Pcap++Test/Tests/PfRingTests.cpp +++ b/Tests/Pcap++Test/Tests/PfRingTests.cpp @@ -205,7 +205,7 @@ PTF_TEST_CASE(TestPfRingDevice) pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); - pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); + pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); PTF_ASSERT_GREATER_THAN(dev->getInterfaceIndex(), 0); @@ -255,7 +255,7 @@ PTF_TEST_CASE(TestPfRingDeviceSingleChannel) pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); - pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); + pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); PfRingPacketData packetData; @@ -296,7 +296,7 @@ PTF_TEST_CASE(TestPfRingDeviceMultiThread) pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); - pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); + pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); uint8_t numOfChannels = dev->getTotalNumOfRxChannels(); @@ -437,7 +437,7 @@ PTF_TEST_CASE(TestPfRingSendPacket) pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); - pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); + pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); PTF_ASSERT_TRUE(dev->open()); DeviceTeardown devTeardown(dev); @@ -502,7 +502,7 @@ PTF_TEST_CASE(TestPfRingSendPackets) pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); - pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); + pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); PTF_ASSERT_TRUE(dev->open()); DeviceTeardown devTeardown(dev); @@ -545,7 +545,7 @@ PTF_TEST_CASE(TestPfRingFilters) pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); - pcpp::PfRingDevice* dev = devList.getPfRingDeviceByName(pcapLiveDev->getName()); + pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); PTF_ASSERT_FALSE(dev->isFilterCurrentlySet()); From 0dbeb1f899b2159896b8871b5526803a27daab79 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 14:32:56 +0300 Subject: [PATCH 38/64] Lint --- Examples/HttpAnalyzer/main.cpp | 3 +-- Examples/SSLAnalyzer/main.cpp | 3 +-- Examples/TcpReassembly/main.cpp | 3 +-- Tests/Pcap++Test/Tests/LiveDeviceTests.cpp | 30 ++++++++++------------ Tests/Pcap++Test/Tests/PfRingTests.cpp | 24 ++++++++--------- Tests/Pcap++Test/Tests/XdpTests.cpp | 4 +-- 6 files changed, 31 insertions(+), 36 deletions(-) diff --git a/Examples/HttpAnalyzer/main.cpp b/Examples/HttpAnalyzer/main.cpp index fe887abd82..960458032a 100644 --- a/Examples/HttpAnalyzer/main.cpp +++ b/Examples/HttpAnalyzer/main.cpp @@ -571,8 +571,7 @@ int main(int argc, char* argv[]) } else // analyze in live traffic mode { - pcpp::PcapLiveDevice* dev = - pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); diff --git a/Examples/SSLAnalyzer/main.cpp b/Examples/SSLAnalyzer/main.cpp index 58645fcd66..fc5fa2c896 100644 --- a/Examples/SSLAnalyzer/main.cpp +++ b/Examples/SSLAnalyzer/main.cpp @@ -535,8 +535,7 @@ int main(int argc, char* argv[]) else // analyze in live traffic mode { // extract pcap live device by interface name or IP address - pcpp::PcapLiveDevice* dev = - pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); diff --git a/Examples/TcpReassembly/main.cpp b/Examples/TcpReassembly/main.cpp index 3978d1a89b..92a6cd64bd 100644 --- a/Examples/TcpReassembly/main.cpp +++ b/Examples/TcpReassembly/main.cpp @@ -688,8 +688,7 @@ int main(int argc, char* argv[]) else // analyze in live traffic mode { // extract pcap live device by interface name or IP address - pcpp::PcapLiveDevice* dev = - pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); + pcpp::PcapLiveDevice* dev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(interfaceNameOrIP); if (dev == nullptr) EXIT_WITH_ERROR("Couldn't find interface by provided IP address or name"); diff --git a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp index deeac9cbf0..aa2042e4f7 100644 --- a/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp +++ b/Tests/Pcap++Test/Tests/LiveDeviceTests.cpp @@ -270,8 +270,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceList) PTF_TEST_CASE(TestPcapLiveDeviceListSearch) { pcpp::PcapLiveDevice* liveDev = nullptr; - liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); std::string devName(liveDev->getName()); @@ -282,8 +281,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceListSearch) pcpp::PcapLiveDevice* liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(devName); PTF_ASSERT_EQUAL(liveDev3, liveDev2, ptr); - liveDev3 = - pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets); + liveDev3 = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIpOrName(PcapTestGlobalArgs.ipToSendReceivePackets); PTF_ASSERT_EQUAL(liveDev3, liveDev, ptr); liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp("255.255.255.250"); @@ -435,8 +433,8 @@ PTF_TEST_CASE(TestPcapLiveDeviceNoNetworking) PTF_TEST_CASE(TestPcapLiveDeviceStatsMode) { - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* liveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open()); DeviceTeardown devTeardown(liveDev); @@ -486,8 +484,8 @@ PTF_TEST_CASE(TestPcapLiveDeviceBlockingMode) for (const auto& config : configs) { // open device - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* liveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open(config)); DeviceTeardown devTeardown(liveDev); @@ -624,8 +622,8 @@ PTF_TEST_CASE(TestPcapLiveDeviceBlockingModeWithLambda) }; // open device - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* liveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_TRUE(liveDev->open()); DeviceTeardown devTeardown(liveDev); @@ -644,8 +642,8 @@ PTF_TEST_CASE(TestPcapLiveDeviceBlockingModeWithLambda) PTF_TEST_CASE(TestPcapLiveDeviceSpecialCfg) { - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* liveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); // open device in default mode @@ -716,8 +714,8 @@ PTF_TEST_CASE(TestWinPcapLiveDevice) { #if defined(_WIN32) - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* liveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_EQUAL(liveDev->getDeviceType(), pcpp::PcapLiveDevice::WinPcapDevice, enum); @@ -753,8 +751,8 @@ PTF_TEST_CASE(TestWinPcapLiveDevice) pcpp::Logger::getInstance().enableLogs(); #else - pcpp::PcapLiveDevice* liveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* liveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(liveDev); PTF_ASSERT_EQUAL(liveDev->getDeviceType(), pcpp::PcapLiveDevice::LibPcapDevice, enum); #endif diff --git a/Tests/Pcap++Test/Tests/PfRingTests.cpp b/Tests/Pcap++Test/Tests/PfRingTests.cpp index e5e6a32175..871dfeb5fc 100644 --- a/Tests/Pcap++Test/Tests/PfRingTests.cpp +++ b/Tests/Pcap++Test/Tests/PfRingTests.cpp @@ -202,8 +202,8 @@ PTF_TEST_CASE(TestPfRingDevice) pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); PTF_ASSERT_GREATER_THAN(devList.size(), 0); PTF_ASSERT_NOT_EQUAL(devList.getPfRingVersion(), ""); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* pcapLiveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); @@ -252,8 +252,8 @@ PTF_TEST_CASE(TestPfRingDeviceSingleChannel) #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* pcapLiveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); @@ -293,8 +293,8 @@ PTF_TEST_CASE(TestPfRingDeviceMultiThread) { #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* pcapLiveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); @@ -434,8 +434,8 @@ PTF_TEST_CASE(TestPfRingSendPacket) { #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* pcapLiveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); @@ -499,8 +499,8 @@ PTF_TEST_CASE(TestPfRingSendPackets) { #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* pcapLiveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); @@ -542,8 +542,8 @@ PTF_TEST_CASE(TestPfRingFilters) { #ifdef USE_PF_RING pcpp::PfRingDeviceList& devList = pcpp::PfRingDeviceList::getInstance(); - pcpp::PcapLiveDevice* pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + pcpp::PcapLiveDevice* pcapLiveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); PTF_ASSERT_NOT_NULL(pcapLiveDev); pcpp::PfRingDevice* dev = devList.getDeviceByName(pcapLiveDev->getName()); PTF_ASSERT_NOT_NULL(dev); diff --git a/Tests/Pcap++Test/Tests/XdpTests.cpp b/Tests/Pcap++Test/Tests/XdpTests.cpp index 4120a56f19..200fe4bbde 100644 --- a/Tests/Pcap++Test/Tests/XdpTests.cpp +++ b/Tests/Pcap++Test/Tests/XdpTests.cpp @@ -35,8 +35,8 @@ bool assertConfig(const pcpp::XdpDevice::XdpDeviceConfiguration* config, std::string getDeviceName() { - auto pcapLiveDev = pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp( - PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); + auto pcapLiveDev = + pcpp::PcapLiveDeviceList::getInstance().getDeviceByIp(PcapTestGlobalArgs.ipToSendReceivePackets.c_str()); if (pcapLiveDev) { return pcapLiveDev->getName(); From 0317b919b0f1bab86a04b191061d86946634a5d0 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 31 Jul 2024 16:27:10 +0300 Subject: [PATCH 39/64] Fixed documentation error. --- Pcap++/header/PcapLiveDeviceList.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 81968b52d8..10309ea9fd 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -67,7 +67,7 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const IPAddress& ipAddr) const; /** - * @copydoc PcapLiveDeviceList::getDeviceByIp(IPAddress const&) + * @copydoc getDeviceByIp(IPAddress const&) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") From bb695b879675fa6b5a9276708433dcb6a60a2bd9 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 1 Aug 2024 13:59:46 +0300 Subject: [PATCH 40/64] Attempt to fix the deprecation warning error. --- Common++/header/DeprecationUtils.h | 20 ++++++++++++++++++-- Pcap++/header/PcapRemoteDeviceList.h | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Common++/header/DeprecationUtils.h b/Common++/header/DeprecationUtils.h index 2d600d88d6..7dad6c44cf 100644 --- a/Common++/header/DeprecationUtils.h +++ b/Common++/header/DeprecationUtils.h @@ -2,11 +2,27 @@ /// @file +#ifndef __PCPP_DEPRECATED_GNU +# if defined(__GUNC__) || defined(__clang__) +# define __PCPP_DEPRECATED_GNU(msg) __attribute__((deprecated(msg))) +# else +# define __PCPP_DEPRECATED_GNU(msg) +# endif // defined(__GUNC__) || defined(__clang__) +#endif // !__PCPP_DEPRECATED_GNU + +#ifndef __PCPP_DEPRECATED_MSVC +# ifdef _MSC_VER +# define __PCPP_DEPRECATED_MSVC(msg) __declspec(deprecated(msg)) +# else +# define __PCPP_DEPRECATED_MSVC(msg) +# endif // __MSC_VER +#endif // !__PCPP_DEPRECATED_MSVC + #ifndef PCPP_DEPRECATED # if defined(__GNUC__) || defined(__clang__) -# define PCPP_DEPRECATED(msg) __attribute__((deprecated(msg))) +# define PCPP_DEPRECATED(msg) __PCPP_DEPRECATED_GNU(msg) # elif defined(_MSC_VER) -# define PCPP_DEPRECATED(msg) __declspec(deprecated(msg)) +# define PCPP_DEPRECATED(msg) __PCPP_DEPRECATED_MSVC(msg) # else # pragma message("WARNING: DEPRECATED feature is not implemented for this compiler") # define PCPP_DEPRECATED(msg) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 4b0bd00c59..71c941578c 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -42,17 +42,28 @@ namespace pcpp PointerVector deviceList); public: +# define __PCPP_REMOTE_LIST_ITERATOR_MSG "Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'." + // Preprocessor hacks because GNU/Clang and MSVC expect their compiler specific attributes differently for aliases. + // I wish the minimum supported standard was Cpp14 :'( + /** * Iterator object that can be used for iterating all PcapRemoteDevice in list * @deprecated Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'. */ - using RemoteDeviceListIterator = PCPP_DEPRECATED("Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'.") iterator; + using RemoteDeviceListIterator __PCPP_DEPRECATED_GNU(__PCPP_REMOTE_LIST_ITERATOR_MSG) = + __PCPP_DEPRECATED_MSVC(__PCPP_REMOTE_LIST_ITERATOR_MSG) iterator; +# undef __PCPP_REMOTE_LIST_ITERATOR_MSG + +# define __PCPP_REMOTE_LIST_ITERATOR_MSG \ + "Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'." /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list * @deprecated Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'. */ - using ConstRemoteDeviceListIterator = PCPP_DEPRECATED("Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'.") const_iterator; + using ConstRemoteDeviceListIterator __PCPP_DEPRECATED_GNU(__PCPP_REMOTE_LIST_ITERATOR_MSG) = + __PCPP_DEPRECATED_MSVC(__PCPP_REMOTE_LIST_ITERATOR_MSG) const_iterator; +# undef __PCPP_REMOTE_LIST_ITERATOR_MSG PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; From 3dfe776cb02bbba5dac070cec040fa5c93517daa Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 1 Aug 2024 14:16:11 +0300 Subject: [PATCH 41/64] Lint --- Common++/header/DeprecationUtils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Common++/header/DeprecationUtils.h b/Common++/header/DeprecationUtils.h index 7dad6c44cf..7483a817c7 100644 --- a/Common++/header/DeprecationUtils.h +++ b/Common++/header/DeprecationUtils.h @@ -8,7 +8,7 @@ # else # define __PCPP_DEPRECATED_GNU(msg) # endif // defined(__GUNC__) || defined(__clang__) -#endif // !__PCPP_DEPRECATED_GNU +#endif // !__PCPP_DEPRECATED_GNU #ifndef __PCPP_DEPRECATED_MSVC # ifdef _MSC_VER @@ -16,7 +16,7 @@ # else # define __PCPP_DEPRECATED_MSVC(msg) # endif // __MSC_VER -#endif // !__PCPP_DEPRECATED_MSVC +#endif // !__PCPP_DEPRECATED_MSVC #ifndef PCPP_DEPRECATED # if defined(__GNUC__) || defined(__clang__) From 5c3f9d3924fdacc92cc55c6b099979fc74538d17 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 1 Aug 2024 14:26:39 +0300 Subject: [PATCH 42/64] Attempt to fix doxygen copydoc errors. --- Pcap++/header/PcapLiveDeviceList.h | 8 ++++---- Pcap++/header/PcapRemoteDeviceList.h | 8 ++++---- Pcap++/header/PfRingDeviceList.h | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 10309ea9fd..ac4c218e1a 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -67,7 +67,7 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const IPAddress& ipAddr) const; /** - * @copydoc getDeviceByIp(IPAddress const&) + * @copydoc getDeviceByIp(IPAddress) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -80,7 +80,7 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const IPv4Address& ipAddr) const; /* - * @copydoc getDeviceByIp(IPv4Address const&) + * @copydoc getDeviceByIp(IPv4Address) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -93,7 +93,7 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const IPv6Address& ip6Addr) const; /** - * @copydoc getDeviceByIp(IPv6Address const&) + * @copydoc getDeviceByIp(IPv6Address) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -106,7 +106,7 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const std::string& ipAddrAsString) const; /** - * @copydoc getDeviceByIp(std::string const&) + * @copydoc getDeviceByIp(std::string) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 71c941578c..67cddd1925 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -160,7 +160,7 @@ namespace pcpp */ PcapRemoteDevice* getDeviceByIp(const IPv4Address& ip4Addr) const; /* - * @copydoc getDeviceByIp(IPv4Address const&) + * @copydoc getDeviceByIp(IPv4Address) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -173,7 +173,7 @@ namespace pcpp */ PcapRemoteDevice* getDeviceByIp(const IPv6Address& ip6Addr) const; /** - * @copydoc getDeviceByIp(IPv6Address const&) + * @copydoc getDeviceByIp(IPv6Address) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -186,7 +186,7 @@ namespace pcpp */ PcapRemoteDevice* getDeviceByIp(const IPAddress& ipAddr) const; /** - * @copydoc getDeviceByIp(IPAddress const&) + * @copydoc getDeviceByIp(IPAddress) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -199,7 +199,7 @@ namespace pcpp */ PcapRemoteDevice* getDeviceByIp(const std::string& ipAddrAsString) const; /** - * @copydoc getDeviceByIp(std::string const&) + * @copydoc getDeviceByIp(std::string) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index 155d143f45..07624ef735 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -60,7 +60,7 @@ namespace pcpp */ PfRingDevice* getDeviceByName(const std::string& devName) const; /** - * @copydoc getDeviceByName(std::string const&) + * @copydoc getDeviceByName(std::string) * @deprecated This method has been deprecated in favor of getDeviceByName(...). */ PCPP_DEPRECATED("Please use getDeviceByName(...) instead.") From f5bd24fea1ee252e603e31c3eae925084b7ac243 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 1 Aug 2024 14:35:26 +0300 Subject: [PATCH 43/64] Copied the documentation manually as copydoc was causing issues. --- Pcap++/header/PcapLiveDeviceList.h | 22 ++++++++++++++++------ Pcap++/header/PcapRemoteDeviceList.h | 16 ++++++++++++---- Pcap++/header/PfRingDeviceList.h | 4 +++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index ac4c218e1a..c9124b906f 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -67,7 +67,7 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const IPAddress& ipAddr) const; /** - * @copydoc getDeviceByIp(IPAddress) + * @copydoc getDeviceByIp(IPAddress const&) * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -80,7 +80,9 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const IPv4Address& ipAddr) const; /* - * @copydoc getDeviceByIp(IPv4Address) + * 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 * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -93,7 +95,9 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const IPv6Address& ip6Addr) const; /** - * @copydoc getDeviceByIp(IPv6Address) + * 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 * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -106,7 +110,9 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const std::string& ipAddrAsString) const; /** - * @copydoc getDeviceByIp(std::string) + * 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 * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -119,7 +125,9 @@ namespace pcpp */ PcapLiveDevice* getDeviceByName(const std::string& name) const; /** - * @copydoc getDeviceByName(std::string 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 * @deprecated This method has been deprecated in favor of getDeviceByName(...). */ PCPP_DEPRECATED("Please use getDeviceByName(...) instead.") @@ -132,7 +140,9 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIpOrName(const std::string& ipOrName) const; /** - * @copydoc getDeviceByIpOrName(std::string 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 * @deprecated This method has been deprecated in favor of getDeviceByIpOrName(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 67cddd1925..594e6274f1 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -160,7 +160,9 @@ namespace pcpp */ PcapRemoteDevice* getDeviceByIp(const IPv4Address& ip4Addr) const; /* - * @copydoc getDeviceByIp(IPv4Address) + * Search a PcapRemoteDevice in the list by its IPv4 address + * @param[in] ip4Addr The IPv4 address + * @return The PcapRemoteDevice if found, NULL otherwise * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -173,7 +175,9 @@ namespace pcpp */ PcapRemoteDevice* getDeviceByIp(const IPv6Address& ip6Addr) const; /** - * @copydoc getDeviceByIp(IPv6Address) + * Search a PcapRemoteDevice in the list by its IPv6 address + * @param[in] ip6Addr The IPv6 address + * @return The PcapRemoteDevice if found, NULL otherwise * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -186,7 +190,9 @@ namespace pcpp */ PcapRemoteDevice* getDeviceByIp(const IPAddress& ipAddr) const; /** - * @copydoc getDeviceByIp(IPAddress) + * 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 * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") @@ -199,7 +205,9 @@ namespace pcpp */ PcapRemoteDevice* getDeviceByIp(const std::string& ipAddrAsString) const; /** - * @copydoc getDeviceByIp(std::string) + * 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 * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index 07624ef735..ec2803f2ec 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -60,7 +60,9 @@ namespace pcpp */ PfRingDevice* getDeviceByName(const std::string& devName) const; /** - * @copydoc getDeviceByName(std::string) + * Get a PF_RING device by name. The name is the Linux interface name which appears in ifconfig + * (e.g eth0, eth1, etc.) + * @return A pointer to the PF_RING device * @deprecated This method has been deprecated in favor of getDeviceByName(...). */ PCPP_DEPRECATED("Please use getDeviceByName(...) instead.") From 49ffbd6ef712a33ad9728e9e718b915c35048560 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 1 Aug 2024 14:55:49 +0300 Subject: [PATCH 44/64] Fixed missed copydoc. --- Pcap++/header/PcapLiveDeviceList.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index c9124b906f..2f16987079 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -67,7 +67,9 @@ namespace pcpp */ PcapLiveDevice* getDeviceByIp(const IPAddress& ipAddr) const; /** - * @copydoc getDeviceByIp(IPAddress 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 + * @return A pointer to the live device if this IP address exists. NULL otherwise * @deprecated This method has been deprecated in favor of getDeviceByIp(...). */ PCPP_DEPRECATED("Please use getDeviceByIp(...) instead.") From e8e86ca5e5ea106c6aeab011312a833d18dcb123 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 7 Aug 2024 12:18:58 +0300 Subject: [PATCH 45/64] Rename preprocessor macros to remove leading underscores Renamed macros in `DeprecationUtils.h`: - `__PCPP_DEPRECATED_GNU` to `PCPP_DEPRECATED_GNU` - `__PCPP_DEPRECATED_MSVC` to `PCPP_DEPRECATED_MSVC` - Updated usage within `PCPP_DEPRECATED` macro Renamed macros in `PcapRemoteDeviceList.h`: - `__PCPP_REMOTE_LIST_ITERATOR_MSG` to `PCPP_REMOTE_LIST_ITERATOR_MSG` - Updated usage within `RemoteDeviceListIterator` and `ConstRemoteDeviceListIterator` type aliases --- Common++/header/DeprecationUtils.h | 20 ++++++++++---------- Pcap++/header/PcapRemoteDeviceList.h | 16 ++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Common++/header/DeprecationUtils.h b/Common++/header/DeprecationUtils.h index 7483a817c7..375bc54c69 100644 --- a/Common++/header/DeprecationUtils.h +++ b/Common++/header/DeprecationUtils.h @@ -2,27 +2,27 @@ /// @file -#ifndef __PCPP_DEPRECATED_GNU +#ifndef PCPP_DEPRECATED_GNU # if defined(__GUNC__) || defined(__clang__) -# define __PCPP_DEPRECATED_GNU(msg) __attribute__((deprecated(msg))) +# define PCPP_DEPRECATED_GNU(msg) __attribute__((deprecated(msg))) # else -# define __PCPP_DEPRECATED_GNU(msg) +# define PCPP_DEPRECATED_GNU(msg) # endif // defined(__GUNC__) || defined(__clang__) -#endif // !__PCPP_DEPRECATED_GNU +#endif // !PCPP_DEPRECATED_GNU -#ifndef __PCPP_DEPRECATED_MSVC +#ifndef PCPP_DEPRECATED_MSVC # ifdef _MSC_VER -# define __PCPP_DEPRECATED_MSVC(msg) __declspec(deprecated(msg)) +# define PCPP_DEPRECATED_MSVC(msg) __declspec(deprecated(msg)) # else -# define __PCPP_DEPRECATED_MSVC(msg) +# define PCPP_DEPRECATED_MSVC(msg) # endif // __MSC_VER -#endif // !__PCPP_DEPRECATED_MSVC +#endif // !PCPP_DEPRECATED_MSVC #ifndef PCPP_DEPRECATED # if defined(__GNUC__) || defined(__clang__) -# define PCPP_DEPRECATED(msg) __PCPP_DEPRECATED_GNU(msg) +# define PCPP_DEPRECATED(msg) PCPP_DEPRECATED_GNU(msg) # elif defined(_MSC_VER) -# define PCPP_DEPRECATED(msg) __PCPP_DEPRECATED_MSVC(msg) +# define PCPP_DEPRECATED(msg) PCPP_DEPRECATED_MSVC(msg) # else # pragma message("WARNING: DEPRECATED feature is not implemented for this compiler") # define PCPP_DEPRECATED(msg) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 594e6274f1..b6d7da7f80 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -42,7 +42,7 @@ namespace pcpp PointerVector deviceList); public: -# define __PCPP_REMOTE_LIST_ITERATOR_MSG "Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'." +# define PCPP_REMOTE_LIST_ITERATOR_MSG "Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'." // Preprocessor hacks because GNU/Clang and MSVC expect their compiler specific attributes differently for aliases. // I wish the minimum supported standard was Cpp14 :'( @@ -50,20 +50,20 @@ namespace pcpp * Iterator object that can be used for iterating all PcapRemoteDevice in list * @deprecated Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'. */ - using RemoteDeviceListIterator __PCPP_DEPRECATED_GNU(__PCPP_REMOTE_LIST_ITERATOR_MSG) = - __PCPP_DEPRECATED_MSVC(__PCPP_REMOTE_LIST_ITERATOR_MSG) iterator; -# undef __PCPP_REMOTE_LIST_ITERATOR_MSG + using RemoteDeviceListIterator PCPP_DEPRECATED_GNU(PCPP_REMOTE_LIST_ITERATOR_MSG) = + PCPP_DEPRECATED_MSVC(PCPP_REMOTE_LIST_ITERATOR_MSG) iterator; +# undef PCPP_REMOTE_LIST_ITERATOR_MSG -# define __PCPP_REMOTE_LIST_ITERATOR_MSG \ +# define PCPP_REMOTE_LIST_ITERATOR_MSG \ "Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'." /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list * @deprecated Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'. */ - using ConstRemoteDeviceListIterator __PCPP_DEPRECATED_GNU(__PCPP_REMOTE_LIST_ITERATOR_MSG) = - __PCPP_DEPRECATED_MSVC(__PCPP_REMOTE_LIST_ITERATOR_MSG) const_iterator; -# undef __PCPP_REMOTE_LIST_ITERATOR_MSG + using ConstRemoteDeviceListIterator PCPP_DEPRECATED_GNU(PCPP_REMOTE_LIST_ITERATOR_MSG) = + PCPP_DEPRECATED_MSVC(PCPP_REMOTE_LIST_ITERATOR_MSG) const_iterator; +# undef PCPP_REMOTE_LIST_ITERATOR_MSG PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; From abf11716aaa404c984cb7cf82f6401a9733a8f88 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 12 Aug 2024 21:25:57 +0300 Subject: [PATCH 46/64] Updated code to use PointerVector. --- Pcap++/header/PcapLiveDeviceList.h | 4 +++- Pcap++/src/PcapLiveDeviceList.cpp | 22 +++++++++++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 3739c209af..1d6e2e031c 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -26,6 +26,8 @@ namespace pcpp class PcapLiveDeviceList : public internal::DeviceListBase { private: + using Base = internal::DeviceListBase; + // Vector of raw device pointers to keep the signature of getPcapLiveDevicesList, as it returns a reference. std::vector m_LiveDeviceListView; @@ -34,7 +36,7 @@ namespace pcpp // private c'tor PcapLiveDeviceList(); - static std::vector> fetchAllLocalDevices(); + static PointerVector fetchAllLocalDevices(); static std::vector fetchDnsServers(); public: PcapLiveDeviceList(const PcapLiveDeviceList&) = delete; diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 916cb471c9..22246a96b5 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -28,23 +28,23 @@ namespace pcpp { namespace { - void syncPointerVectors(std::vector> const& mainVector, std::vector& viewVector) + void syncPointerVectors(PointerVector const& mainVector, std::vector& viewVector) { viewVector.resize(mainVector.size()); // Full update of all elements of the view vector to synchronize them with the main vector. - std::transform(mainVector.begin(), mainVector.end(), viewVector.begin(), - [](const std::unique_ptr& ptr) { return ptr.get(); }); + std::copy(mainVector.begin(), mainVector.end(), viewVector.begin()); } } -PcapLiveDeviceList::PcapLiveDeviceList() : m_LiveDeviceList(fetchAllLocalDevices()), m_DnsServers(fetchDnsServers()) -{ - syncPointerVectors(m_LiveDeviceList, m_LiveDeviceListView); +PcapLiveDeviceList::PcapLiveDeviceList() + : Base(fetchAllLocalDevices()), m_DnsServers(fetchDnsServers()) + { + syncPointerVectors(m_DeviceList, m_LiveDeviceListView); } -std::vector> PcapLiveDeviceList::fetchAllLocalDevices() +PointerVector PcapLiveDeviceList::fetchAllLocalDevices() { - std::vector> deviceList; + PointerVector deviceList; std::unique_ptr interfaceList; try { @@ -65,7 +65,7 @@ std::vector> PcapLiveDeviceList::fetchAllLocalDe #else //__linux__, __APPLE__, __FreeBSD__ auto dev = std::unique_ptr(new PcapLiveDevice(currInterface, true, true, true)); #endif - deviceList.push_back(std::move(dev)); + deviceList.pushBack(std::move(dev)); } return deviceList; } @@ -394,10 +394,10 @@ void PcapLiveDeviceList::reset() { m_LiveDeviceListView.clear(); - m_LiveDeviceList = fetchAllLocalDevices(); + m_DeviceList = fetchAllLocalDevices(); m_DnsServers = fetchDnsServers(); - syncPointerVectors(m_LiveDeviceList, m_LiveDeviceListView); + syncPointerVectors(m_DeviceList, m_LiveDeviceListView); } } // namespace pcpp From 133c433dc517bbf403570a0e66ea0d84d5b7af02 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 23 Aug 2024 10:02:13 +0300 Subject: [PATCH 47/64] Fixed merge errors. --- Pcap++/src/DpdkDeviceList.cpp | 5 +++-- Pcap++/src/PfRingDeviceList.cpp | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Pcap++/src/DpdkDeviceList.cpp b/Pcap++/src/DpdkDeviceList.cpp index 32591362ae..a3b5abdad5 100644 --- a/Pcap++/src/DpdkDeviceList.cpp +++ b/Pcap++/src/DpdkDeviceList.cpp @@ -188,9 +188,10 @@ namespace pcpp for (int i = 0; i < numOfPorts; i++) { auto newDevice = std::unique_ptr(new DpdkDevice(i, mBufPoolSizePerDevice, mBufDataSize)); - PCPP_LOG_DEBUG("DpdkDevice #" << i << ": Name='" << newDevice->getDeviceName() - << "', PCI-slot='" m_DeviceList.pushBack(std::move(newDevice)); + PCPP_LOG_DEBUG("DpdkDevice #" << i << ": Name='" << newDevice->getDeviceName() << "', PCI-slot='" + << newDevice->getPciAddress() << "', PMD='" << newDevice->getPMDName() << "', MAC Addr='" << newDevice->getMacAddress() << "'"); + m_DeviceList.pushBack(std::move(newDevice)); } // Full update of all elements of the view vector to synchronize them with the main vector. diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index 0d9732d149..6dbbf91cd7 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -98,7 +98,9 @@ namespace pcpp m_PfRingVersion = readPfRingVersion(ring.get()); PCPP_LOG_DEBUG("PF_RING version is: " << m_PfRingVersion); } - std::unique_ptr newDev = m_DeviceList.pushBack(std::move(newDev)); + std::unique_ptr newDev = + std::unique_ptr(new PfRingDevice(currInterface->name)); + m_PfRingDeviceList.push_back(std::move(newDev)); PCPP_LOG_DEBUG("Found interface: " << currInterface->name); } } From 87bd25d2eb5629539494a02332d18e46045b821e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 23 Aug 2024 10:12:56 +0300 Subject: [PATCH 48/64] Lint --- Pcap++/src/DpdkDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/DpdkDeviceList.cpp b/Pcap++/src/DpdkDeviceList.cpp index a3b5abdad5..d39893c76a 100644 --- a/Pcap++/src/DpdkDeviceList.cpp +++ b/Pcap++/src/DpdkDeviceList.cpp @@ -190,7 +190,7 @@ namespace pcpp auto newDevice = std::unique_ptr(new DpdkDevice(i, mBufPoolSizePerDevice, mBufDataSize)); PCPP_LOG_DEBUG("DpdkDevice #" << i << ": Name='" << newDevice->getDeviceName() << "', PCI-slot='" << newDevice->getPciAddress() << "', PMD='" << newDevice->getPMDName() - << "', MAC Addr='" << newDevice->getMacAddress() << "'"); + << "', MAC Addr='" << newDevice->getMacAddress() << "'"); m_DeviceList.pushBack(std::move(newDevice)); } From 17f742e5a22572e0a867ec7c0468f01a68df7ff0 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 23 Aug 2024 10:43:12 +0300 Subject: [PATCH 49/64] Fixed device list name. --- Pcap++/src/PfRingDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index 6dbbf91cd7..fc71b843e4 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -100,7 +100,7 @@ namespace pcpp } std::unique_ptr newDev = std::unique_ptr(new PfRingDevice(currInterface->name)); - m_PfRingDeviceList.push_back(std::move(newDev)); + m_DeviceList.push_back(std::move(newDev)); PCPP_LOG_DEBUG("Found interface: " << currInterface->name); } } From be2db4b4af488f94c8cbb6ba10441a24ef7b942b Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 23 Aug 2024 18:29:02 +0300 Subject: [PATCH 50/64] Fixed pushBack signature. --- Pcap++/src/PfRingDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index fc71b843e4..323806b376 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -100,7 +100,7 @@ namespace pcpp } std::unique_ptr newDev = std::unique_ptr(new PfRingDevice(currInterface->name)); - m_DeviceList.push_back(std::move(newDev)); + m_DeviceList.pushBack(std::move(newDev)); PCPP_LOG_DEBUG("Found interface: " << currInterface->name); } } From 1a1c0b882ca42698cbb373fc4fe4796b45881e62 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 6 Oct 2024 16:13:18 +0300 Subject: [PATCH 51/64] Lint --- Pcap++/src/DpdkDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/DpdkDeviceList.cpp b/Pcap++/src/DpdkDeviceList.cpp index 1f527a9a47..33bd185814 100644 --- a/Pcap++/src/DpdkDeviceList.cpp +++ b/Pcap++/src/DpdkDeviceList.cpp @@ -34,7 +34,7 @@ #include #include -# include +#include #include #include #include From 2f890a4166225dc40be5c9a3ee2abf348ba8f963 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sun, 6 Oct 2024 16:13:28 +0300 Subject: [PATCH 52/64] Lint --- Pcap++/src/PfRingDeviceList.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PfRingDeviceList.cpp b/Pcap++/src/PfRingDeviceList.cpp index 819a0aad78..58c8d049ac 100644 --- a/Pcap++/src/PfRingDeviceList.cpp +++ b/Pcap++/src/PfRingDeviceList.cpp @@ -4,7 +4,7 @@ #include #include -# include +#include #include "PfRingDeviceList.h" #include "SystemUtils.h" #include "DeviceUtils.h" From 75a1df3baad62178001a02197ce91908ea07eaf1 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 7 Oct 2024 02:02:33 +0300 Subject: [PATCH 53/64] Lint --- Pcap++/header/PcapRemoteDeviceList.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 402fbc6a8d..de21e3de85 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -42,7 +42,7 @@ namespace pcpp PointerVector deviceList); public: -# define PCPP_REMOTE_LIST_ITERATOR_MSG "Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'." +#define PCPP_REMOTE_LIST_ITERATOR_MSG "Please use the 'iterator' alias instead of 'RemoteDeviceListIterator'." // Preprocessor hacks because GNU/Clang and MSVC expect their compiler specific attributes differently for // aliases. I wish the minimum supported standard was Cpp14 :'( @@ -52,10 +52,10 @@ namespace pcpp */ using RemoteDeviceListIterator PCPP_DEPRECATED_GNU(PCPP_REMOTE_LIST_ITERATOR_MSG) = PCPP_DEPRECATED_MSVC(PCPP_REMOTE_LIST_ITERATOR_MSG) iterator; -# undef PCPP_REMOTE_LIST_ITERATOR_MSG +#undef PCPP_REMOTE_LIST_ITERATOR_MSG -# define PCPP_REMOTE_LIST_ITERATOR_MSG \ - "Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'." +#define PCPP_REMOTE_LIST_ITERATOR_MSG \ + "Please use the 'const_iterator' alias instead of 'ConstRemoteDeviceListIterator'." /** * Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list @@ -63,7 +63,7 @@ namespace pcpp */ using ConstRemoteDeviceListIterator PCPP_DEPRECATED_GNU(PCPP_REMOTE_LIST_ITERATOR_MSG) = PCPP_DEPRECATED_MSVC(PCPP_REMOTE_LIST_ITERATOR_MSG) const_iterator; -# undef PCPP_REMOTE_LIST_ITERATOR_MSG +#undef PCPP_REMOTE_LIST_ITERATOR_MSG PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; From 344a65690f1e3bb2a41b86723564e051cd22c982 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 11 Oct 2024 22:51:18 +0300 Subject: [PATCH 54/64] Fixed empty line between methods. --- Pcap++/header/DeviceListBase.h | 8 ++++++++ Pcap++/header/KniDeviceList.h | 3 +++ Pcap++/header/PcapLiveDeviceList.h | 6 ++++++ Pcap++/header/PcapRemoteDeviceList.h | 4 ++++ Pcap++/header/PfRingDeviceList.h | 1 + 5 files changed, 22 insertions(+) diff --git a/Pcap++/header/DeviceListBase.h b/Pcap++/header/DeviceListBase.h index 3cd82d9fcc..9d1933fe3b 100644 --- a/Pcap++/header/DeviceListBase.h +++ b/Pcap++/header/DeviceListBase.h @@ -80,6 +80,7 @@ namespace pcpp { return m_DeviceList.front(); } + /** * @brief Returns a pointer to last device. * @return A pointer to the specified device. @@ -88,6 +89,7 @@ namespace pcpp { return m_DeviceList.back(); } + /** * @brief Returns a pointer to last device. * @return A pointer to the specified device. @@ -105,6 +107,7 @@ namespace pcpp { return m_DeviceList.begin(); } + /** * @brief Returns an iterator to the first device. * @return An iterator to the specified device. @@ -113,6 +116,7 @@ namespace pcpp { return cbegin(); } + /** * @brief Returns an iterator to the first device. * @return An iterator to the specified device. @@ -121,6 +125,7 @@ namespace pcpp { return m_DeviceList.begin(); } + /** * @brief Returns an iterator past the last device. * @return An iterator past the last device. @@ -129,6 +134,7 @@ namespace pcpp { return m_DeviceList.end(); } + /** * @brief Returns an iterator past the last device. * @return An iterator past the last device. @@ -137,6 +143,7 @@ namespace pcpp { return cend(); } + /** * @brief Returns an iterator past the last device. * @return An iterator past the last device. @@ -154,6 +161,7 @@ namespace pcpp { return m_DeviceList.size() == 0; } + /** * @brief Returns the number of devices. * @return The number of devices in the container. diff --git a/Pcap++/header/KniDeviceList.h b/Pcap++/header/KniDeviceList.h index c2dd1727ed..c144c68dc2 100644 --- a/Pcap++/header/KniDeviceList.h +++ b/Pcap++/header/KniDeviceList.h @@ -109,6 +109,7 @@ namespace pcpp * @return Pointer to new KNI device or nullptr in case of error */ KniDevice* createDevice(const KniDevice::KniDeviceConfiguration& config, const size_t mempoolSize); + /** * @brief Returns KNI device with specified portId. * @note MT SAFE if createDevice or destroyDevice is not called concurrently @@ -116,6 +117,7 @@ namespace pcpp * @return Pointer to KNI device or nullptr if device not found */ KniDevice* getDeviceByPort(const uint16_t portId); + /** * @brief Returns KNI device with specified name. * @note MT SAFE if createDevice or destroyDevice is not called concurrently @@ -132,6 +134,7 @@ namespace pcpp * @note MT SAFE */ static KniCallbackVersion callbackVersion(); + /** * Returns true if provided callback type is supported by used DPDK version * @note MT SAFE diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 2c27e866bc..ce4a558efa 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -71,6 +71,7 @@ namespace pcpp * @return A pointer to the live device if this IP address exists. nullptr otherwise */ PcapLiveDevice* getDeviceByIp(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,6 +90,7 @@ namespace pcpp * @return A pointer to the live device if this IPv4 address exists. nullptr otherwise */ PcapLiveDevice* getDeviceByIp(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 @@ -107,6 +109,7 @@ namespace pcpp * @return A pointer to the live device if this IPv6 address exists. nullptr otherwise */ PcapLiveDevice* getDeviceByIp(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 @@ -125,6 +128,7 @@ namespace pcpp * @return A pointer to the live device if this IP address is valid and exists. nullptr otherwise */ PcapLiveDevice* getDeviceByIp(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 @@ -143,6 +147,7 @@ namespace pcpp * @return A pointer to the live device if this name exists. nullptr otherwise */ PcapLiveDevice* getDeviceByName(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) @@ -161,6 +166,7 @@ namespace pcpp * @return A pointer to the live device if exists, nullptr otherwise */ PcapLiveDevice* getDeviceByIpOrName(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 diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index de21e3de85..17fc8da3e2 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -177,6 +177,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, nullptr otherwise */ PcapRemoteDevice* getDeviceByIp(const IPv4Address& ip4Addr) const; + /* * Search a PcapRemoteDevice in the list by its IPv4 address * @param[in] ip4Addr The IPv4 address @@ -195,6 +196,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, nullptr otherwise */ PcapRemoteDevice* getDeviceByIp(const IPv6Address& ip6Addr) const; + /** * Search a PcapRemoteDevice in the list by its IPv6 address * @param[in] ip6Addr The IPv6 address @@ -213,6 +215,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, nullptr otherwise */ PcapRemoteDevice* getDeviceByIp(const IPAddress& ipAddr) const; + /** * Search a PcapRemoteDevice in the list by its IP address (IPv4 or IPv6) * @param[in] ipAddr The IP address @@ -231,6 +234,7 @@ namespace pcpp * @return The PcapRemoteDevice if found, NULL otherwise */ PcapRemoteDevice* getDeviceByIp(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++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index 072532d760..a1e734f659 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -63,6 +63,7 @@ namespace pcpp * @return A pointer to the PF_RING device */ PfRingDevice* getDeviceByName(const std::string& devName) const; + /** * Get a PF_RING device by name. The name is the Linux interface name which appears in ifconfig * (e.g eth0, eth1, etc.) From e9ae72e0bcd7572ef90b982f717e5ccf0f652988 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 5 Nov 2024 20:23:22 +0200 Subject: [PATCH 55/64] Lint --- Pcap++/src/PcapRemoteDeviceList.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 612cb4adbc..430ef8c3c9 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -154,21 +154,19 @@ namespace pcpp PcapRemoteDevice* PcapRemoteDeviceList::getDeviceByIp(const IPv4Address& ip4Addr) const { - auto it = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), - [&ip4Addr](PcapRemoteDevice const* devPtr) { - auto devIP = devPtr->getIPv4Address(); - return devIP == ip4Addr; - }); + auto it = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), [&ip4Addr](PcapRemoteDevice const* devPtr) { + auto devIP = devPtr->getIPv4Address(); + return devIP == ip4Addr; + }); return it != m_DeviceList.end() ? *it : nullptr; } PcapRemoteDevice* PcapRemoteDeviceList::getDeviceByIp(const IPv6Address& ip6Addr) const { - auto it = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), - [&ip6Addr](PcapRemoteDevice const* devPtr) { - auto devIP = devPtr->getIPv6Address(); - return devIP == ip6Addr; - }); + auto it = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), [&ip6Addr](PcapRemoteDevice const* devPtr) { + auto devIP = devPtr->getIPv6Address(); + return devIP == ip6Addr; + }); return it != m_DeviceList.end() ? *it : nullptr; } From b0d2744dc82d5d4e0d64a7fdb39cd4f2d5cbed4f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 25 Nov 2024 14:09:02 +0200 Subject: [PATCH 56/64] Added self-copy guards. --- Common++/header/PointerVector.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Common++/header/PointerVector.h b/Common++/header/PointerVector.h index 4097009daa..104cb8b202 100644 --- a/Common++/header/PointerVector.h +++ b/Common++/header/PointerVector.h @@ -109,8 +109,11 @@ namespace pcpp */ PointerVector& operator=(const PointerVector& other) { - // Saves a copy of the old pointer to defer cleanup. - auto oldValues = m_Vector; + if (this == &other) + return *this; + + // Moves the old values to a temporary to defer cleanup. + auto oldValues = std::move(m_Vector); try { m_Vector = deepCopyUnsafe(other.m_Vector); @@ -134,6 +137,9 @@ namespace pcpp */ PointerVector& operator=(PointerVector&& other) noexcept { + if (this == &other) + return *this; + // Releases all current elements. clear(); // Moves the elements of the other vector. @@ -387,10 +393,7 @@ namespace pcpp } catch (const std::exception&) { - for (auto obj : copyVec) - { - Deleter()(obj); - } + freeVectorUnsafe(copyVec); throw; } From 9863b1191fc653cb7534100dfe78b48964b19507 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 25 Nov 2024 14:18:38 +0200 Subject: [PATCH 57/64] Fixed warnings. --- Examples/PfRingExample-FilterTraffic/main.cpp | 4 ++-- Pcap++/header/KniDevice.h | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Examples/PfRingExample-FilterTraffic/main.cpp b/Examples/PfRingExample-FilterTraffic/main.cpp index ee9241660e..eeb8e4a390 100644 --- a/Examples/PfRingExample-FilterTraffic/main.cpp +++ b/Examples/PfRingExample-FilterTraffic/main.cpp @@ -277,7 +277,7 @@ int main(int argc, char* argv[]) case 'n': { std::string ifaceName = std::string(optarg); - dev = pcpp::PfRingDeviceList::getInstance().getPfRingDeviceByName(ifaceName); + dev = pcpp::PfRingDeviceList::getInstance().getDeviceByName(ifaceName); if (dev == nullptr) EXIT_WITH_ERROR("Could not find PF_RING device '" << ifaceName << "'"); break; @@ -285,7 +285,7 @@ int main(int argc, char* argv[]) case 's': { std::string sendPacketsToIfaceName = std::string(optarg); - sendPacketsToIface = pcpp::PfRingDeviceList::getInstance().getPfRingDeviceByName(sendPacketsToIfaceName); + sendPacketsToIface = pcpp::PfRingDeviceList::getInstance().getDeviceByName(sendPacketsToIfaceName); if (sendPacketsToIface == nullptr) EXIT_WITH_ERROR("Could not find PF_RING device '" << sendPacketsToIfaceName << "'"); diff --git a/Pcap++/header/KniDevice.h b/Pcap++/header/KniDevice.h index fc37ce65aa..5eaac3a0f1 100644 --- a/Pcap++/header/KniDevice.h +++ b/Pcap++/header/KniDevice.h @@ -129,7 +129,7 @@ namespace pcpp class KniDevice : public IDevice { friend class KniDeviceList; - friend class internal::KniDeviceDeleter; + friend struct internal::KniDeviceDeleter; friend class MBufRawPacket; public: @@ -284,7 +284,7 @@ namespace pcpp /** This class is not copyable */ KniDevice& operator=(const KniDevice&); /** All instances of this class MUST be destroyed by KniDeviceList class */ - ~KniDevice(); + ~KniDevice() override; public: /* Information getters */ @@ -636,13 +636,13 @@ namespace pcpp * @return true if the device was opened successfully, false if device is already opened, * or KNI device configuration and startup failed */ - bool open(); + bool open() override; /** * @brief Close the KNI device. * When device is closed it's not possible to work with it. * Stops asynchronous packet capture if it is running. */ - void close(); + void close() override; private: struct rte_kni* m_Device; From fc738792520b2903f4b3a6cf38bc50b1be67e8a3 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 25 Nov 2024 20:49:26 +0200 Subject: [PATCH 58/64] Added tests for DeviceListBase. --- Tests/Pcap++Test/CMakeLists.txt | 4 +- Tests/Pcap++Test/TestDefinition.h | 3 + .../Pcap++Test/Tests/DeviceListBaseTests.cpp | 69 +++++++++++++++++++ Tests/Pcap++Test/main.cpp | 1 + 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp diff --git a/Tests/Pcap++Test/CMakeLists.txt b/Tests/Pcap++Test/CMakeLists.txt index 73c6fdcc06..e0ccf84455 100644 --- a/Tests/Pcap++Test/CMakeLists.txt +++ b/Tests/Pcap++Test/CMakeLists.txt @@ -2,6 +2,7 @@ add_executable( Pcap++Test Common/TestUtils.cpp main.cpp + Tests/DeviceListBaseTests.cpp Tests/DpdkTests.cpp Tests/FileTests.cpp Tests/FilterTests.cpp @@ -15,7 +16,8 @@ add_executable( Tests/RawSocketTests.cpp Tests/SystemUtilsTests.cpp Tests/TcpReassemblyTests.cpp - Tests/XdpTests.cpp) + Tests/XdpTests.cpp +) target_link_libraries( Pcap++Test diff --git a/Tests/Pcap++Test/TestDefinition.h b/Tests/Pcap++Test/TestDefinition.h index ccaaec4756..469e280bcd 100644 --- a/Tests/Pcap++Test/TestDefinition.h +++ b/Tests/Pcap++Test/TestDefinition.h @@ -32,6 +32,9 @@ PTF_TEST_CASE(TestSolarisSnoopFileRead); PTF_TEST_CASE(TestPcapNgFilePrecision); PTF_TEST_CASE(TestPcapFileWriterDeviceDestructor); +// Implemented in DeviceListBaseTests.cpp +PTF_TEST_CASE(TestDeviceListBase); + // Implemented in LiveDeviceTests.cpp PTF_TEST_CASE(TestPcapLiveDeviceList); PTF_TEST_CASE(TestPcapLiveDeviceListSearch); diff --git a/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp b/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp new file mode 100644 index 0000000000..37b1491aee --- /dev/null +++ b/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp @@ -0,0 +1,69 @@ +#include "../TestDefinition.h" + +#include "DeviceListBase.h" + +namespace +{ + /// @brief A derived class of DeviceListBase used for testing purposes. + template + struct DerivedDeviceList : public pcpp::internal::DeviceListBase + { + DerivedDeviceList() = default; + DerivedDeviceList(pcpp::PointerVector devices) : pcpp::internal::DeviceListBase(std::move(devices)) + {} + }; + +} + +PTF_TEST_CASE(TestDeviceListBase) +{ + using pcpp::internal::DeviceListBase; + + // Test the default constructor. + DerivedDeviceList deviceList; + PTF_ASSERT_EQUAL(deviceList.size(), 0); + PTF_ASSERT_TRUE(deviceList.begin() == deviceList.end()); + PTF_ASSERT_TRUE(deviceList.empty()); + + // Test the constructor with a list of devices. + pcpp::PointerVector devices; + int* dev0 = new int(0); + int* dev1 = new int(1); + int* dev2 = new int(2); + devices.pushBack(dev0); + devices.pushBack(dev1); + devices.pushBack(dev2); + DerivedDeviceList deviceList2(std::move(devices)); + + PTF_ASSERT_EQUAL(deviceList2.size(), 3); + PTF_ASSERT_FALSE(deviceList2.empty()); + PTF_ASSERT_EQUAL(deviceList2.at(0), dev0); + PTF_ASSERT_EQUAL(deviceList2.at(1), dev1); + PTF_ASSERT_EQUAL(deviceList2.at(2), dev2); + PTF_ASSERT_EQUAL(deviceList2.front(), dev0); + PTF_ASSERT_EQUAL(deviceList2.back(), dev2); + + // Test iterators. + { + auto it = deviceList2.begin(); + PTF_ASSERT_EQUAL(*it, dev0); + ++it; + PTF_ASSERT_EQUAL(*it, dev1); + ++it; + PTF_ASSERT_EQUAL(*it, dev2); + ++it; + PTF_ASSERT_TRUE(it == deviceList2.end()); + } + + // Test const iterators. + { + auto it = deviceList2.cbegin(); + PTF_ASSERT_EQUAL(*it, dev0); + ++it; + PTF_ASSERT_EQUAL(*it, dev1); + ++it; + PTF_ASSERT_EQUAL(*it, dev2); + ++it; + PTF_ASSERT_TRUE(it == deviceList2.cend()); + } +} \ No newline at end of file diff --git a/Tests/Pcap++Test/main.cpp b/Tests/Pcap++Test/main.cpp index 784a9e0d6f..821bab0856 100644 --- a/Tests/Pcap++Test/main.cpp +++ b/Tests/Pcap++Test/main.cpp @@ -227,6 +227,7 @@ int main(int argc, char* argv[]) PTF_RUN_TEST(TestSolarisSnoopFileRead, "no_network;pcap;snoop"); PTF_RUN_TEST(TestPcapFileWriterDeviceDestructor, "no_network;pcap"); + PTF_RUN_TEST(TestDeviceListBase, "no_network"); PTF_RUN_TEST(TestPcapLiveDeviceList, "no_network;live_device;skip_mem_leak_check"); PTF_RUN_TEST(TestPcapLiveDeviceListSearch, "live_device"); PTF_RUN_TEST(TestPcapLiveDevice, "live_device"); From 6bd0cbfb3cfbd672e4949d7ecbbbc330c07664b6 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 25 Nov 2024 20:58:06 +0200 Subject: [PATCH 59/64] Lint --- Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp b/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp index 37b1491aee..af5e326691 100644 --- a/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp +++ b/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp @@ -5,15 +5,14 @@ namespace { /// @brief A derived class of DeviceListBase used for testing purposes. - template - struct DerivedDeviceList : public pcpp::internal::DeviceListBase + template struct DerivedDeviceList : public pcpp::internal::DeviceListBase { DerivedDeviceList() = default; DerivedDeviceList(pcpp::PointerVector devices) : pcpp::internal::DeviceListBase(std::move(devices)) {} }; -} +} // namespace PTF_TEST_CASE(TestDeviceListBase) { @@ -66,4 +65,4 @@ PTF_TEST_CASE(TestDeviceListBase) ++it; PTF_ASSERT_TRUE(it == deviceList2.cend()); } -} \ No newline at end of file +} From 1d84a3cee9adff32de3cb88035495ed5546c2f51 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 27 Nov 2024 19:31:53 +0200 Subject: [PATCH 60/64] Add "venv" and "./out" directories to be ignored by codespell. --- .codespellrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.codespellrc b/.codespellrc index c7019c766a..7506fd03df 100644 --- a/.codespellrc +++ b/.codespellrc @@ -1,4 +1,4 @@ [codespell] -skip = *.dat,typos-config.toml,.git,.venv,./ci,./Dist,./mk,./Tests/ExamplesTest/expected_output,./Tests/ExamplesTest/pcap_examples,./Tests/Packet++Test/PacketExamples,./Tests/Pcap++Test/PcapExamples,./3rdParty,./Examples/PcapSearch/dirent-for-Visual-Studio +skip = *.dat,typos-config.toml,.git,.venv,venv,./out,./ci,./Dist,./mk,./Tests/ExamplesTest/expected_output,./Tests/ExamplesTest/pcap_examples,./Tests/Packet++Test/PacketExamples,./Tests/Pcap++Test/PcapExamples,./3rdParty,./Examples/PcapSearch/dirent-for-Visual-Studio ignore-words = codespell-ignore-list.txt count = From 68130d53b1446d4da8f74607e0691dd31ebb3ef7 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 27 Nov 2024 19:32:16 +0200 Subject: [PATCH 61/64] Suppress checkersReport messages for cppcheck. --- cppcheckSuppressions.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/cppcheckSuppressions.txt b/cppcheckSuppressions.txt index 771a24fba0..9d4e3a876c 100644 --- a/cppcheckSuppressions.txt +++ b/cppcheckSuppressions.txt @@ -1,5 +1,6 @@ *:3rdParty/* +checkersReport:* ConfigurationNotChecked:* ctuOneDefinitionRuleViolation:Examples/* missingInclude:* From 9ad70afd0162f4d42cff77fffa3015ffd756f4c7 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 27 Nov 2024 19:33:30 +0200 Subject: [PATCH 62/64] Lint --- Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp b/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp index af5e326691..0a40115d39 100644 --- a/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp +++ b/Tests/Pcap++Test/Tests/DeviceListBaseTests.cpp @@ -8,7 +8,8 @@ namespace template struct DerivedDeviceList : public pcpp::internal::DeviceListBase { DerivedDeviceList() = default; - DerivedDeviceList(pcpp::PointerVector devices) : pcpp::internal::DeviceListBase(std::move(devices)) + explicit DerivedDeviceList(pcpp::PointerVector devices) + : pcpp::internal::DeviceListBase(std::move(devices)) {} }; From 871cca79d6f4e8f4243425f261a13c1052c2e870 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Wed, 27 Nov 2024 20:18:12 +0200 Subject: [PATCH 63/64] Added explicit defaulted public destructors to device lists. --- Pcap++/header/DpdkDeviceList.h | 2 ++ Pcap++/header/PcapLiveDeviceList.h | 1 + Pcap++/header/PcapRemoteDeviceList.h | 1 + Pcap++/header/PfRingDeviceList.h | 1 + 4 files changed, 5 insertions(+) diff --git a/Pcap++/header/DpdkDeviceList.h b/Pcap++/header/DpdkDeviceList.h index 613e4bd8c3..222ea2074f 100644 --- a/Pcap++/header/DpdkDeviceList.h +++ b/Pcap++/header/DpdkDeviceList.h @@ -98,6 +98,8 @@ namespace pcpp static int dpdkWorkerThreadStart(void* ptr); public: + ~DpdkDeviceList() = default; + /** * As DpdkDeviceList is a singleton, this is the static getter to retrieve its instance. Note that if the static * method initDpdk() was not called or returned false this instance won't be initialized and DpdkDevices won't diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index ce4a558efa..c5685592b4 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -44,6 +44,7 @@ namespace pcpp PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete; PcapLiveDeviceList& operator=(const PcapLiveDeviceList&) = delete; PcapLiveDeviceList& operator=(PcapLiveDeviceList&&) noexcept = delete; + ~PcapLiveDeviceList() = default; /** * The access method to the singleton diff --git a/Pcap++/header/PcapRemoteDeviceList.h b/Pcap++/header/PcapRemoteDeviceList.h index 17fc8da3e2..d274fdb473 100644 --- a/Pcap++/header/PcapRemoteDeviceList.h +++ b/Pcap++/header/PcapRemoteDeviceList.h @@ -69,6 +69,7 @@ namespace pcpp PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete; PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList&) = delete; PcapRemoteDeviceList& operator=(PcapRemoteDeviceList&&) noexcept = delete; + ~PcapRemoteDeviceList() = default; /** * A static method for creating a PcapRemoteDeviceList instance for a certain remote machine. This methods diff --git a/Pcap++/header/PfRingDeviceList.h b/Pcap++/header/PfRingDeviceList.h index a1e734f659..e27521fccd 100644 --- a/Pcap++/header/PfRingDeviceList.h +++ b/Pcap++/header/PfRingDeviceList.h @@ -35,6 +35,7 @@ namespace pcpp PfRingDeviceList(PfRingDeviceList&&) noexcept = delete; PfRingDeviceList& operator=(const PfRingDeviceList&) = delete; PfRingDeviceList& operator=(PfRingDeviceList&&) noexcept = delete; + ~PfRingDeviceList() = default; /** * A static method that returns the singleton object for PfRingDeviceList From b58284b2a4141bb76c00685f35c617ff8cab73e4 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Sat, 4 Jan 2025 13:54:39 +0200 Subject: [PATCH 64/64] Lint --- Pcap++/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/CMakeLists.txt b/Pcap++/CMakeLists.txt index 0f5b347dfc..7687e4c601 100644 --- a/Pcap++/CMakeLists.txt +++ b/Pcap++/CMakeLists.txt @@ -28,7 +28,7 @@ add_library( set( public_headers header/Device.h - header/DeviceListBase.h + header/DeviceListBase.h header/NetworkUtils.h header/PcapDevice.h header/PcapFileDevice.h