From 650e6da6b7f86bd7704bf4f3eb163160c19f33df Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 12 Aug 2024 06:02:26 +0300 Subject: [PATCH] Broke up PcapLiveDeviceList initialization into steps. (#1515) * Broke up PcapLiveDeviceList initialization into steps. * Fixed fetchDnsServers returns. * Lint --- Pcap++/header/PcapLiveDeviceList.h | 6 +-- Pcap++/src/PcapLiveDeviceList.cpp | 60 +++++++++++++++++------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/Pcap++/header/PcapLiveDeviceList.h b/Pcap++/header/PcapLiveDeviceList.h index 7a93fe9d74..6fc976889a 100644 --- a/Pcap++/header/PcapLiveDeviceList.h +++ b/Pcap++/header/PcapLiveDeviceList.h @@ -33,11 +33,9 @@ namespace pcpp // private c'tor PcapLiveDeviceList(); - void init(); + static std::vector> fetchAllLocalDevices(); + static std::vector fetchDnsServers(); - void setDnsServers(); - - void updateLiveDeviceListView() const; public: PcapLiveDeviceList(const PcapLiveDeviceList&) = delete; PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete; diff --git a/Pcap++/src/PcapLiveDeviceList.cpp b/Pcap++/src/PcapLiveDeviceList.cpp index 5d0650d731..8763d536a1 100644 --- a/Pcap++/src/PcapLiveDeviceList.cpp +++ b/Pcap++/src/PcapLiveDeviceList.cpp @@ -26,14 +26,25 @@ namespace pcpp { + namespace + { + void syncPointerVectors(std::vector> 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(); }); + } + } -PcapLiveDeviceList::PcapLiveDeviceList() +PcapLiveDeviceList::PcapLiveDeviceList() : m_LiveDeviceList(fetchAllLocalDevices()), m_DnsServers(fetchDnsServers()) { - init(); + syncPointerVectors(m_LiveDeviceList, m_LiveDeviceListView); } -void PcapLiveDeviceList::init() +std::vector> PcapLiveDeviceList::fetchAllLocalDevices() { + std::vector> deviceList; std::unique_ptr interfaceList; try { @@ -54,19 +65,14 @@ 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)); + deviceList.push_back(std::move(dev)); } - - m_LiveDeviceListView.resize(m_LiveDeviceList.size()); - // Full update of all elements of the view vector to synchronize them with the main vector. - std::transform(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), m_LiveDeviceListView.begin(), - [](const std::unique_ptr& ptr) { return ptr.get(); }); - - setDnsServers(); + return deviceList; } -void PcapLiveDeviceList::setDnsServers() +std::vector PcapLiveDeviceList::fetchDnsServers() { + std::vector dnsServers; #if defined(_WIN32) FIXED_INFO* fixedInfo; ULONG ulOutBufLen; @@ -96,7 +102,7 @@ void PcapLiveDeviceList::setDnsServers() int dnsServerCounter = 0; try { - m_DnsServers.push_back(IPv4Address(fixedInfo->DnsServerList.IpAddress.String)); + dnsServers.push_back(IPv4Address(fixedInfo->DnsServerList.IpAddress.String)); PCPP_LOG_DEBUG("Default DNS server IP #" << dnsServerCounter++ << ": " << fixedInfo->DnsServerList.IpAddress.String); } catch(const std::exception&) @@ -109,7 +115,7 @@ void PcapLiveDeviceList::setDnsServers() { try { - m_DnsServers.push_back(IPv4Address(pIPAddr->IpAddress.String)); + dnsServers.push_back(IPv4Address(pIPAddr->IpAddress.String)); PCPP_LOG_DEBUG("Default DNS server IP #" << dnsServerCounter++ << ": " << pIPAddr->IpAddress.String); } catch(const std::exception&) @@ -126,7 +132,7 @@ void PcapLiveDeviceList::setDnsServers() if (nmcliExists != "") { PCPP_LOG_DEBUG("Error retrieving DNS server list: nmcli doesn't exist"); - return; + return {}; } // check nmcli major version (0 or 1) @@ -145,7 +151,7 @@ void PcapLiveDeviceList::setDnsServers() if (dnsServersInfo == "") { PCPP_LOG_DEBUG("Error retrieving DNS server list: call to nmcli gave no output"); - return; + return {}; } std::istringstream stream(dnsServersInfo); @@ -169,9 +175,9 @@ void PcapLiveDeviceList::setDnsServers() continue; } - if (std::find(m_DnsServers.begin(), m_DnsServers.end(), dnsIPAddr) == m_DnsServers.end()) + if (std::find(dnsServers.begin(), dnsServers.end(), dnsIPAddr) == dnsServers.end()) { - m_DnsServers.push_back(dnsIPAddr); + dnsServers.push_back(dnsIPAddr); PCPP_LOG_DEBUG("Default DNS server IP #" << i++ << ": " << dnsIPAddr); } } @@ -181,7 +187,7 @@ void PcapLiveDeviceList::setDnsServers() if (dynRef == nullptr) { PCPP_LOG_DEBUG("Couldn't set DNS server list: failed to retrieve SCDynamicStore"); - return; + return {}; } CFDictionaryRef dnsDict = (CFDictionaryRef)SCDynamicStoreCopyValue(dynRef,CFSTR("State:/Network/Global/DNS")); @@ -190,7 +196,7 @@ void PcapLiveDeviceList::setDnsServers() { PCPP_LOG_DEBUG("Couldn't set DNS server list: failed to get DNS dictionary"); CFRelease(dynRef); - return; + return {}; } CFArrayRef serverAddresses = (CFArrayRef)CFDictionaryGetValue(dnsDict, CFSTR("ServerAddresses")); @@ -200,7 +206,7 @@ void PcapLiveDeviceList::setDnsServers() PCPP_LOG_DEBUG("Couldn't set DNS server list: server addresses array is null"); CFRelease(dynRef); CFRelease(dnsDict); - return; + return {}; } CFIndex count = CFArrayGetCount(serverAddresses); @@ -216,7 +222,7 @@ void PcapLiveDeviceList::setDnsServers() CFStringGetCString(serverAddress, serverAddressCString, 20, kCFStringEncodingUTF8); try { - m_DnsServers.push_back(IPv4Address(serverAddressCString)); + dnsServers.push_back(IPv4Address(serverAddressCString)); PCPP_LOG_DEBUG("Default DNS server IP #" << (int)(i+1) << ": " << serverAddressCString); } catch(const std::exception& e) @@ -243,7 +249,7 @@ void PcapLiveDeviceList::setDnsServers() try { - m_DnsServers.push_back(IPv4Address(internal::in_addr2int(*inaddr))); + dnsServers.push_back(IPv4Address(internal::in_addr2int(*inaddr))); } catch(const std::exception& e) { @@ -252,6 +258,7 @@ void PcapLiveDeviceList::setDnsServers() } #endif + return dnsServers; } PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPAddress& ipAddr) const @@ -386,10 +393,11 @@ PcapLiveDeviceList* PcapLiveDeviceList::clone() void PcapLiveDeviceList::reset() { m_LiveDeviceListView.clear(); - m_LiveDeviceList.clear(); - m_DnsServers.clear(); - init(); + m_LiveDeviceList = fetchAllLocalDevices(); + m_DnsServers = fetchDnsServers(); + + syncPointerVectors(m_LiveDeviceList, m_LiveDeviceListView); } } // namespace pcpp