From 6a2fefa83b6d72bb11a4b357a9fda709ecd6ff49 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 01:41:03 +0300 Subject: [PATCH 01/10] Added device utilities header for pcap device utility functions. Added memory utilities header for smart pointer utilities. --- Pcap++/CMakeLists.txt | 4 +++ Pcap++/header/DeviceUtils.h | 34 +++++++++++++++++++++++++ Pcap++/header/MemoryUtils.h | 31 +++++++++++++++++++++++ Pcap++/src/DeviceUtils.cpp | 50 +++++++++++++++++++++++++++++++++++++ Pcap++/src/MemoryUtils.cpp | 13 ++++++++++ 5 files changed, 132 insertions(+) create mode 100644 Pcap++/header/DeviceUtils.h create mode 100644 Pcap++/header/MemoryUtils.h create mode 100644 Pcap++/src/DeviceUtils.cpp create mode 100644 Pcap++/src/MemoryUtils.cpp diff --git a/Pcap++/CMakeLists.txt b/Pcap++/CMakeLists.txt index a23efa7e17..c85c96d6ae 100644 --- a/Pcap++/CMakeLists.txt +++ b/Pcap++/CMakeLists.txt @@ -1,11 +1,13 @@ add_library( Pcap++ + src/DeviceUtils.cpp $<$:src/DpdkDevice.cpp> $<$:src/DpdkDeviceList.cpp> $<$:src/KniDevice.cpp> $<$:src/KniDeviceList.cpp> $<$:src/LinuxNicInformationSocket.cpp> $<$:src/MBufRawPacket.cpp> + src/MemoryUtils.cpp src/NetworkUtils.cpp src/PcapFileDevice.cpp src/PcapDevice.cpp @@ -24,6 +26,8 @@ add_library( set(public_headers header/Device.h + header/DeviceUtils.h + header/MemoryUtils.h header/NetworkUtils.h header/PcapDevice.h header/PcapFileDevice.h diff --git a/Pcap++/header/DeviceUtils.h b/Pcap++/header/DeviceUtils.h new file mode 100644 index 0000000000..35e4f24057 --- /dev/null +++ b/Pcap++/header/DeviceUtils.h @@ -0,0 +1,34 @@ +#pragma once + +/// @file + +#include +#include "IpAddress.h" +#include "MemoryUtils.h" + +// Forward declaration +struct pcap_rmtauth; + +namespace pcpp +{ + namespace internal + { + /** + * Fetches a list of all network devices on the local machine that LibPcap/WinPcap/NPcap can find. + * @return A smart pointer to an interface list structure. + * @throws std::runtime_error The system encountered an error fetching the devices. + */ + std::unique_ptr getAllLocalPcapDevices(); +#ifdef _WIN32 + /** + * Fetches a list of all network devices on a remote machine that WinPcap/NPcap can find. + * @param[in] ipAddress IP address of the remote machine. + * @param[in] port Port to use when connecting to the remote machine. + * @param[in] pRmAuth Pointer to an authentication structure to use when connecting to the remote machine. Nullptr if no authentication is required. + * @return A smart pointer to an interface list structure. + * @throws std::runtime_error The system encountered an error fetching the devices. + */ + std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth = nullptr); +#endif // _WIN32 + } +} diff --git a/Pcap++/header/MemoryUtils.h b/Pcap++/header/MemoryUtils.h new file mode 100644 index 0000000000..6e0eeb3431 --- /dev/null +++ b/Pcap++/header/MemoryUtils.h @@ -0,0 +1,31 @@ +#pragma once + +// Forward declarations +struct pcap; +typedef pcap pcap_t; +struct pcap_if; +typedef pcap_if pcap_if_t; + +namespace pcpp +{ + namespace internal + { + /** + * @class PcapCloseDeleter + * A deleter that cleans up a pcap_t structure by calling pcap_close. + */ + struct PcapCloseDeleter + { + void operator()(pcap_t* ptr) const; + }; + + /** + * @class PcapFreeAllDevsDeleter + * A deleter that frees an interface list of pcap_if_t ptr by calling 'pcap_freealldevs' function on it. + */ + struct PcapFreeAllDevsDeleter + { + void operator()(pcap_if_t* ptr) const; + }; + } +} diff --git a/Pcap++/src/DeviceUtils.cpp b/Pcap++/src/DeviceUtils.cpp new file mode 100644 index 0000000000..6bc2a86cd2 --- /dev/null +++ b/Pcap++/src/DeviceUtils.cpp @@ -0,0 +1,50 @@ +#include "DeviceUtils.h" + +#include +#include + +#include "pcap.h" +#include "Logger.h" +#include "IpAddress.h" + +namespace pcpp +{ + namespace internal + { + std::unique_ptr getAllLocalPcapDevices() + { + pcap_if_t* interfaceListRaw; + std::array errbuf; + int err = pcap_findalldevs(&interfaceListRaw, errbuf.data()); + if (err < 0) + { + throw std::runtime_error("Error searching for devices: " + std::string(errbuf.begin(), errbuf.end())); + } + // Assigns the raw pointer to the smart pointer with specialized deleter. + return std::unique_ptr(interfaceListRaw); + } + +#ifdef _WIN32 + std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth) + { + PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); + std::array remoteCaptureString; + std::array errorBuf; + if (pcap_createsrcstr(remoteCaptureString.data(), PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), + std::to_string(port).c_str(), nullptr, errorBuf.data()) != 0) + { + throw std::runtime_error("Error creating the remote connection string. Error: " + std::string(errorBuf.begin(), errorBuf.end())); + } + + PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString.data()); + + pcap_if_t* interfaceListRaw; + if (pcap_findalldevs_ex(remoteCaptureString.data(), pRmAuth, &interfaceListRaw, errorBuf.data()) < 0) + { + throw std::runtime_error("Error retrieving device on remote machine. Error: " + std::string(errorBuf.begin(), errorBuf.end())); + } + return std::unique_ptr(interfaceListRaw); + } +#endif // _WIN32 + } +} diff --git a/Pcap++/src/MemoryUtils.cpp b/Pcap++/src/MemoryUtils.cpp new file mode 100644 index 0000000000..7716046497 --- /dev/null +++ b/Pcap++/src/MemoryUtils.cpp @@ -0,0 +1,13 @@ +#include "MemoryUtils.h" + +#include "pcap.h" + +namespace pcpp +{ + namespace internal + { + void PcapCloseDeleter::operator()(pcap_t* ptr) const { pcap_close(ptr); } + + void PcapFreeAllDevsDeleter::operator()(pcap_if_t* ptr) const { pcap_freealldevs(ptr); } + } +} From e98e6b3fa51b5d679f6c501fc05a4e353dfbcb86 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 02:06:47 +0300 Subject: [PATCH 02/10] Replaced usages of pcap_findalldevs(_ex) with internal::getAll(Local/Remote)PcapDevices. --- Pcap++/src/PcapLiveDevice.cpp | 38 +++++++++++++---------------- Pcap++/src/PcapLiveDeviceList.cpp | 22 ++++++++--------- Pcap++/src/PcapRemoteDeviceList.cpp | 21 +++++++++------- 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index dcc32f0b74..1d9d8c2c8e 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -1,6 +1,8 @@ #define LOG_MODULE PcapLogModuleLiveDevice #include "IpUtils.h" +#include "DeviceUtils.h" +#include "MemoryUtils.h" #include "PcapLiveDevice.h" #include "PcapLiveDeviceList.h" #include "Packet.h" @@ -11,7 +13,7 @@ #include #include "Logger.h" #include "SystemUtils.h" -#include +#include #include #include #include @@ -405,32 +407,26 @@ void PcapLiveDevice::close() PcapLiveDevice* PcapLiveDevice::clone() const { - PcapLiveDevice* retval = nullptr; - - pcap_if_t* interfaceList; - char errbuf[PCAP_ERRBUF_SIZE]; - int err = pcap_findalldevs(&interfaceList, errbuf); - if (err < 0) + std::unique_ptr interfaceList; + try { - PCPP_LOG_ERROR("Error searching for devices: " << errbuf); - return nullptr; + interfaceList = internal::getAllLocalPcapDevices(); } - - pcap_if_t* currInterface = interfaceList; - while (currInterface != nullptr) + catch (const std::exception& e) { - if(!strcmp(currInterface->name, getName().c_str())) - break; - currInterface = currInterface->next; + PCPP_LOG_ERROR(e.what()); } - if(currInterface) - retval = cloneInternal(*currInterface); - else - PCPP_LOG_ERROR("Can't find interface " << getName().c_str()); + for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) + { + if (!std::strcmp(currInterface->name, getName().c_str())) + { + return cloneInternal(*currInterface); + } + } - pcap_freealldevs(interfaceList); - return retval; + PCPP_LOG_ERROR("Can't find interface " << getName().c_str()); + return nullptr; } PcapLiveDevice* PcapLiveDevice::cloneInternal(pcap_if_t& devInterface) const diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 47f179f479..2d705f715f 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -4,6 +4,8 @@ #include "IpAddressUtils.h" #include "PcapLiveDeviceList.h" #include "Logger.h" +#include "MemoryUtils.h" +#include "DeviceUtils.h" #include "SystemUtils.h" #include "pcap.h" #include @@ -39,32 +41,30 @@ PcapLiveDeviceList::~PcapLiveDeviceList() void PcapLiveDeviceList::init() { - pcap_if_t* interfaceList; - char errbuf[PCAP_ERRBUF_SIZE]; - int err = pcap_findalldevs(&interfaceList, errbuf); - if (err < 0) + std::unique_ptr interfaceList; + try + { + interfaceList = internal::getAllLocalPcapDevices(); + } + catch (const std::exception& e) { - PCPP_LOG_ERROR("Error searching for devices: " << errbuf); + PCPP_LOG_ERROR(e.what()); } PCPP_LOG_DEBUG("Pcap lib version info: " << IPcapDevice::getPcapLibVersionInfo()); - pcap_if_t* currInterface = interfaceList; - while (currInterface != nullptr) + + for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) { #if defined(_WIN32) PcapLiveDevice* dev = new WinPcapLiveDevice(currInterface, true, true, true); #else //__linux__, __APPLE__, __FreeBSD__ PcapLiveDevice* dev = new PcapLiveDevice(currInterface, true, true, true); #endif - currInterface = currInterface->next; m_LiveDeviceList.insert(m_LiveDeviceList.end(), dev); } setDnsServers(); - - PCPP_LOG_DEBUG("Freeing live device data"); - pcap_freealldevs(interfaceList); } void PcapLiveDeviceList::setDnsServers() diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index af107fee91..b6b71829f1 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -5,6 +5,8 @@ #include "PcapRemoteDeviceList.h" #include "Logger.h" #include "IpUtils.h" +#include "DeviceUtils.h" +#include "MemoryUtils.h" #include "IpAddressUtils.h" #include "pcap.h" #include @@ -42,12 +44,15 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& pRmAuth = &rmAuth; } - pcap_if_t* interfaceList; - char errorBuf[PCAP_ERRBUF_SIZE]; - if (pcap_findalldevs_ex(remoteCaptureString, pRmAuth, &interfaceList, errorBuf) < 0) + std::unique_ptr interfaceList; + try { - PCPP_LOG_ERROR("Error retrieving device on remote machine. Error string is: " << errorBuf); - return NULL; + interfaceList = internal::getAllRemotePcapDevices(ipAddress, port, pRmAuth); + } + catch (const std::exception& e) + { + PCPP_LOG_ERROR(e.what()); + return nullptr; } PcapRemoteDeviceList* resultList = new PcapRemoteDeviceList(); @@ -55,16 +60,14 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& resultList->setRemoteMachinePort(port); resultList->setRemoteAuthentication(remoteAuth); - pcap_if_t* currInterface = interfaceList; - while (currInterface != NULL) + + 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); - currInterface = currInterface->next; } - pcap_freealldevs(interfaceList); return resultList; } From 1167bd15bc858f9c8dce60fc194b819be5c0d51b Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 02:19:04 +0300 Subject: [PATCH 03/10] Removed code creating pcap_capture string as it is unused. --- Pcap++/src/PcapRemoteDeviceList.cpp | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index b6b71829f1..51f519ea35 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -22,22 +22,9 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth) { - PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); - char remoteCaptureString[PCAP_BUF_SIZE]; - char errbuf[PCAP_ERRBUF_SIZE]; - std::ostringstream portAsString; - portAsString << port; - if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), portAsString.str().c_str(), NULL, errbuf) != 0) - { - PCPP_LOG_ERROR("Error in creating the remote connection string. Error was: " << errbuf); - return NULL; - } - - PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString); - - pcap_rmtauth* pRmAuth = NULL; + pcap_rmtauth* pRmAuth = nullptr; pcap_rmtauth rmAuth; - if (remoteAuth != NULL) + if (remoteAuth != nullptr) { PCPP_LOG_DEBUG("Authentication requested. Username: " << remoteAuth->userName << ", Password: " << remoteAuth->password); rmAuth = remoteAuth->getPcapRmAuth(); From 535bc76ebfc32f73e10ae53df511c7bcd8042f1f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 02:19:56 +0300 Subject: [PATCH 04/10] Fixed returning nullptr on clone fail. --- Pcap++/src/PcapLiveDevice.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index 1d9d8c2c8e..3a21e0471a 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -415,6 +415,7 @@ PcapLiveDevice* PcapLiveDevice::clone() const catch (const std::exception& e) { PCPP_LOG_ERROR(e.what()); + return nullptr; } for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) From 0d91ddf7088e3f5a4f86582927183b9a14e49c4e Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 12:45:54 +0300 Subject: [PATCH 05/10] Renamed MemoryUtils to PcapUtils. --- Pcap++/CMakeLists.txt | 4 ++-- Pcap++/header/DeviceUtils.h | 2 +- Pcap++/header/{MemoryUtils.h => PcapUtils.h} | 0 Pcap++/src/PcapLiveDevice.cpp | 2 +- Pcap++/src/PcapLiveDeviceList.cpp | 2 +- Pcap++/src/PcapRemoteDeviceList.cpp | 2 +- Pcap++/src/{MemoryUtils.cpp => PcapUtils.cpp} | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) rename Pcap++/header/{MemoryUtils.h => PcapUtils.h} (100%) rename Pcap++/src/{MemoryUtils.cpp => PcapUtils.cpp} (90%) diff --git a/Pcap++/CMakeLists.txt b/Pcap++/CMakeLists.txt index c85c96d6ae..6b95c8f78c 100644 --- a/Pcap++/CMakeLists.txt +++ b/Pcap++/CMakeLists.txt @@ -7,7 +7,7 @@ add_library( $<$:src/KniDeviceList.cpp> $<$:src/LinuxNicInformationSocket.cpp> $<$:src/MBufRawPacket.cpp> - src/MemoryUtils.cpp + src/PcapUtils.cpp src/NetworkUtils.cpp src/PcapFileDevice.cpp src/PcapDevice.cpp @@ -27,7 +27,7 @@ add_library( set(public_headers header/Device.h header/DeviceUtils.h - header/MemoryUtils.h + header/PcapUtils.h header/NetworkUtils.h header/PcapDevice.h header/PcapFileDevice.h diff --git a/Pcap++/header/DeviceUtils.h b/Pcap++/header/DeviceUtils.h index 35e4f24057..ece596c82b 100644 --- a/Pcap++/header/DeviceUtils.h +++ b/Pcap++/header/DeviceUtils.h @@ -4,7 +4,7 @@ #include #include "IpAddress.h" -#include "MemoryUtils.h" +#include "PcapUtils.h" // Forward declaration struct pcap_rmtauth; diff --git a/Pcap++/header/MemoryUtils.h b/Pcap++/header/PcapUtils.h similarity index 100% rename from Pcap++/header/MemoryUtils.h rename to Pcap++/header/PcapUtils.h diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index 3a21e0471a..c3a690bd95 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -2,7 +2,7 @@ #include "IpUtils.h" #include "DeviceUtils.h" -#include "MemoryUtils.h" +#include "PcapUtils.h" #include "PcapLiveDevice.h" #include "PcapLiveDeviceList.h" #include "Packet.h" diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 2d705f715f..82efc59703 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -4,7 +4,7 @@ #include "IpAddressUtils.h" #include "PcapLiveDeviceList.h" #include "Logger.h" -#include "MemoryUtils.h" +#include "PcapUtils.h" #include "DeviceUtils.h" #include "SystemUtils.h" #include "pcap.h" diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 51f519ea35..576fa5204d 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -6,7 +6,7 @@ #include "Logger.h" #include "IpUtils.h" #include "DeviceUtils.h" -#include "MemoryUtils.h" +#include "PcapUtils.h" #include "IpAddressUtils.h" #include "pcap.h" #include diff --git a/Pcap++/src/MemoryUtils.cpp b/Pcap++/src/PcapUtils.cpp similarity index 90% rename from Pcap++/src/MemoryUtils.cpp rename to Pcap++/src/PcapUtils.cpp index 7716046497..b3df4eca44 100644 --- a/Pcap++/src/MemoryUtils.cpp +++ b/Pcap++/src/PcapUtils.cpp @@ -1,4 +1,4 @@ -#include "MemoryUtils.h" +#include "PcapUtils.h" #include "pcap.h" From 9a5e63897c4dc8e8a06feeb6b630649106786581 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 21:29:39 +0300 Subject: [PATCH 06/10] Moved getAllRemotePcapDevices to anonymous namespace in PcapRemoteDeviceList.cpp --- Pcap++/header/DeviceUtils.h | 11 --------- Pcap++/src/DeviceUtils.cpp | 23 ------------------ Pcap++/src/PcapRemoteDeviceList.cpp | 37 +++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 36 deletions(-) diff --git a/Pcap++/header/DeviceUtils.h b/Pcap++/header/DeviceUtils.h index ece596c82b..1923d17007 100644 --- a/Pcap++/header/DeviceUtils.h +++ b/Pcap++/header/DeviceUtils.h @@ -19,16 +19,5 @@ namespace pcpp * @throws std::runtime_error The system encountered an error fetching the devices. */ std::unique_ptr getAllLocalPcapDevices(); -#ifdef _WIN32 - /** - * Fetches a list of all network devices on a remote machine that WinPcap/NPcap can find. - * @param[in] ipAddress IP address of the remote machine. - * @param[in] port Port to use when connecting to the remote machine. - * @param[in] pRmAuth Pointer to an authentication structure to use when connecting to the remote machine. Nullptr if no authentication is required. - * @return A smart pointer to an interface list structure. - * @throws std::runtime_error The system encountered an error fetching the devices. - */ - std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth = nullptr); -#endif // _WIN32 } } diff --git a/Pcap++/src/DeviceUtils.cpp b/Pcap++/src/DeviceUtils.cpp index 6bc2a86cd2..785c726b47 100644 --- a/Pcap++/src/DeviceUtils.cpp +++ b/Pcap++/src/DeviceUtils.cpp @@ -23,28 +23,5 @@ namespace pcpp // Assigns the raw pointer to the smart pointer with specialized deleter. return std::unique_ptr(interfaceListRaw); } - -#ifdef _WIN32 - std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth) - { - PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); - std::array remoteCaptureString; - std::array errorBuf; - if (pcap_createsrcstr(remoteCaptureString.data(), PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), - std::to_string(port).c_str(), nullptr, errorBuf.data()) != 0) - { - throw std::runtime_error("Error creating the remote connection string. Error: " + std::string(errorBuf.begin(), errorBuf.end())); - } - - PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString.data()); - - pcap_if_t* interfaceListRaw; - if (pcap_findalldevs_ex(remoteCaptureString.data(), pRmAuth, &interfaceListRaw, errorBuf.data()) < 0) - { - throw std::runtime_error("Error retrieving device on remote machine. Error: " + std::string(errorBuf.begin(), errorBuf.end())); - } - return std::unique_ptr(interfaceListRaw); - } -#endif // _WIN32 } } diff --git a/Pcap++/src/PcapRemoteDeviceList.cpp b/Pcap++/src/PcapRemoteDeviceList.cpp index 576fa5204d..e6ff31205a 100644 --- a/Pcap++/src/PcapRemoteDeviceList.cpp +++ b/Pcap++/src/PcapRemoteDeviceList.cpp @@ -5,7 +5,6 @@ #include "PcapRemoteDeviceList.h" #include "Logger.h" #include "IpUtils.h" -#include "DeviceUtils.h" #include "PcapUtils.h" #include "IpAddressUtils.h" #include "pcap.h" @@ -15,6 +14,40 @@ namespace pcpp { + namespace + { + /** + * Fetches a list of all network devices on a remote machine that WinPcap/NPcap can find. + * @param[in] ipAddress IP address of the remote machine. + * @param[in] port Port to use when connecting to the remote machine. + * @param[in] pRmAuth Pointer to an authentication structure to use when connecting to the remote machine. Nullptr if no authentication is required. + * @return A smart pointer to an interface list structure. + * @throws std::runtime_error The system encountered an error fetching the devices. + */ + std::unique_ptr getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth = nullptr) + { + PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); + std::array remoteCaptureString; + std::array errorBuf; + if (pcap_createsrcstr(remoteCaptureString.data(), PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), + std::to_string(port).c_str(), nullptr, errorBuf.data()) != 0) + { + throw std::runtime_error("Error creating the remote connection string. Error: " + + std::string(errorBuf.begin(), errorBuf.end())); + } + + PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString.data()); + + pcap_if_t* interfaceListRaw; + if (pcap_findalldevs_ex(remoteCaptureString.data(), pRmAuth, &interfaceListRaw, errorBuf.data()) < 0) + { + throw std::runtime_error("Error retrieving device on remote machine. Error: " + + std::string(errorBuf.begin(), errorBuf.end())); + } + return std::unique_ptr(interfaceListRaw); + } + } + PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port) { return PcapRemoteDeviceList::getRemoteDeviceList(ipAddress, port, NULL); @@ -34,7 +67,7 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& std::unique_ptr interfaceList; try { - interfaceList = internal::getAllRemotePcapDevices(ipAddress, port, pRmAuth); + interfaceList = getAllRemotePcapDevices(ipAddress, port, pRmAuth); } catch (const std::exception& e) { From 68b8632977513515c3f1255ae402e7f8cbbd7941 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 21:41:40 +0300 Subject: [PATCH 07/10] Removed duplicate of PcapCloseDeleter. --- Pcap++/src/PcapFilter.cpp | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Pcap++/src/PcapFilter.cpp b/Pcap++/src/PcapFilter.cpp index 28e83f4a75..eb850077bf 100644 --- a/Pcap++/src/PcapFilter.cpp +++ b/Pcap++/src/PcapFilter.cpp @@ -3,6 +3,7 @@ #include "PcapFilter.h" #include "Logger.h" #include "IPv4Layer.h" +#include "PcapUtils.h" #include #include #if defined(_WIN32) @@ -35,15 +36,6 @@ namespace internal pcap_freecode(ptr); delete ptr; } - - /** - * @class PcapTDeleter - * A deleter that cleans up a pcap_t structure by calling pcap_close. - */ - struct PcapTDeleter - { - void operator()(pcap_t* ptr) const { pcap_close(ptr); } - }; } BpfFilterWrapper::BpfFilterWrapper() : m_LinkType(LinkLayerType::LINKTYPE_ETHERNET) {} @@ -66,7 +58,7 @@ bool BpfFilterWrapper::setFilter(const std::string& filter, LinkLayerType linkTy if (filter != m_FilterStr || linkType != m_LinkType) { - std::unique_ptr pcap = std::unique_ptr(pcap_open_dead(linkType, DEFAULT_SNAPLEN)); + auto pcap = std::unique_ptr(pcap_open_dead(linkType, DEFAULT_SNAPLEN)); if (pcap == nullptr) { return false; From fb685bf5b414ac3b316fb524dee861a34889a86f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Thu, 6 Jun 2024 22:11:01 +0300 Subject: [PATCH 08/10] Remoted internal utilitiy headers from the public header list. --- Pcap++/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/Pcap++/CMakeLists.txt b/Pcap++/CMakeLists.txt index 6b95c8f78c..f28fcb6be6 100644 --- a/Pcap++/CMakeLists.txt +++ b/Pcap++/CMakeLists.txt @@ -26,8 +26,6 @@ add_library( set(public_headers header/Device.h - header/DeviceUtils.h - header/PcapUtils.h header/NetworkUtils.h header/PcapDevice.h header/PcapFileDevice.h From f343771bc735bcf04848f41481a989ab435f9d65 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 7 Jun 2024 13:13:31 +0300 Subject: [PATCH 09/10] Removed unused forward declare. --- Pcap++/header/DeviceUtils.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/Pcap++/header/DeviceUtils.h b/Pcap++/header/DeviceUtils.h index 1923d17007..c00b4fda37 100644 --- a/Pcap++/header/DeviceUtils.h +++ b/Pcap++/header/DeviceUtils.h @@ -6,9 +6,6 @@ #include "IpAddress.h" #include "PcapUtils.h" -// Forward declaration -struct pcap_rmtauth; - namespace pcpp { namespace internal From 617273e15bedd459bc9e1bf4a8acd5e8e6051d8f Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Fri, 7 Jun 2024 13:16:42 +0300 Subject: [PATCH 10/10] Added doxygen conditionals to exclude the internal classes from the public documentation. --- Pcap++/header/DeviceUtils.h | 4 ++++ Pcap++/header/PcapUtils.h | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/Pcap++/header/DeviceUtils.h b/Pcap++/header/DeviceUtils.h index c00b4fda37..7a33e2e277 100644 --- a/Pcap++/header/DeviceUtils.h +++ b/Pcap++/header/DeviceUtils.h @@ -8,6 +8,8 @@ namespace pcpp { + /// @cond PCPP_INTERNAL + namespace internal { /** @@ -17,4 +19,6 @@ namespace pcpp */ std::unique_ptr getAllLocalPcapDevices(); } + + /// @endcond } diff --git a/Pcap++/header/PcapUtils.h b/Pcap++/header/PcapUtils.h index 6e0eeb3431..d9f76033fe 100644 --- a/Pcap++/header/PcapUtils.h +++ b/Pcap++/header/PcapUtils.h @@ -8,6 +8,8 @@ typedef pcap_if pcap_if_t; namespace pcpp { + /// @cond PCPP_INTERNAL + namespace internal { /** @@ -28,4 +30,6 @@ namespace pcpp void operator()(pcap_if_t* ptr) const; }; } + + /// @endcond }