Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate shared DeviceList functionality under a common base class. #1488

Open
wants to merge 86 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
59c8647
Changed RemoteDeviceList to hold devices in a PointerVector.
Dimi1010 Jun 30, 2024
2ce99c0
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Jul 7, 2024
415880e
Added iterator member type definitions conforming to the standard lib…
Dimi1010 Jul 7, 2024
1a678bb
Changed PcapLiveDeviceList to utilize a PointerVector iternally.
Dimi1010 Jul 7, 2024
40da178
Added iteration API to LiveDeviceList.
Dimi1010 Jul 7, 2024
e0eed6c
Updated usages of deprecated `getPcapLiveDevicesList`.
Dimi1010 Jul 7, 2024
265865c
Removed empty destructor.
Dimi1010 Jul 7, 2024
b479b08
Split overly long line.
Dimi1010 Jul 8, 2024
6a4c330
Formatting fixes.
Dimi1010 Jul 10, 2024
f28307a
Fixed documentation of iterator API.
Dimi1010 Jul 10, 2024
326d2cc
Updated PfRingDeviceList to use PointerVector internally.
Dimi1010 Jul 10, 2024
d940946
Moved include to cpp file.
Dimi1010 Jul 10, 2024
21391cb
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Jul 21, 2024
54c0efe
Merge branch 'dev' into refactor/device-list-management
seladb Jul 21, 2024
0b4230c
Merge branch 'dev' into refactor/device-list-management
seladb Jul 25, 2024
118578c
Merge branch 'dev' into refactor/device-list-management
seladb Jul 29, 2024
921dda4
Added a base device list class to contain common functionality betwee…
Dimi1010 Jul 29, 2024
7f3d810
Merge remote-tracking branch 'origin/refactor/device-list-management'…
Dimi1010 Jul 29, 2024
a3c533a
Updated PcapRemoteDeviceList and PfRingDeviceList to utilize DeviceLi…
Dimi1010 Jul 29, 2024
faeb1ee
Merge branch 'dev' into refactor/device-list-management
Dimi1010 Jul 29, 2024
6e990e9
Fixes and cleanup.
Dimi1010 Jul 29, 2024
b31b65f
Deprecation message fixes.
Dimi1010 Jul 29, 2024
d649dd9
Updated DpdkDeviceList to utilize DeviceListBase.
Dimi1010 Jul 29, 2024
e9143d1
Updated KniDeviceList to utilize DeviceListBase.
Dimi1010 Jul 29, 2024
c956f67
Fixed KniDevice device search.
Dimi1010 Jul 29, 2024
ccaac4e
Refactored KniDeviceList::createDevice to use smart pointers during c…
Dimi1010 Jul 29, 2024
df807f7
Fixed KniDevice deletion errors.
Dimi1010 Jul 29, 2024
38b5633
Lint
Dimi1010 Jul 29, 2024
7668b9f
Fixed namespace closure.
Dimi1010 Jul 29, 2024
91ccd0e
Fixed alias.
Dimi1010 Jul 29, 2024
df2730f
Fixed KniDeviceDeleter documentation.
Dimi1010 Jul 29, 2024
e434258
Reverted creation of KniDevice to raw ptr.
Dimi1010 Jul 29, 2024
85a35f5
Fixed KniDeviceDeleter documentation again.
Dimi1010 Jul 29, 2024
ed143d7
Attempt to fix dpdk device fetch by port.
Dimi1010 Jul 29, 2024
e5dc2da
Lint
Dimi1010 Jul 29, 2024
9cf44ce
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Jul 30, 2024
2ae51d2
Minor fixes.
Dimi1010 Jul 30, 2024
a73d4b9
Changed PcapLiveDeviceList getters to 'getDeviceBy*' syntax.
Dimi1010 Jul 31, 2024
75c4e32
Replaced usages of deprecated method 'getPcapLiveDevicesList()'.
Dimi1010 Jul 31, 2024
ab45201
Replaced usages of deprecated method 'getPfRingDevicesList()'.
Dimi1010 Jul 31, 2024
cee1e43
Deprecated old iterator aliases for RemoteDeviceList and updated usages.
Dimi1010 Jul 31, 2024
3b72143
Replaced deprecated function 'getRemoteDeviceList' usages.
Dimi1010 Jul 31, 2024
ffac432
Changed PcapRemoteDeviceList getters to 'getDeviceBy*' syntax.
Dimi1010 Jul 31, 2024
2245bb5
Fixed deprecation message.
Dimi1010 Jul 31, 2024
151f09f
Changed PfRingDeviceList getters to 'getDeviceBy*' syntax.
Dimi1010 Jul 31, 2024
0dbeb1f
Lint
Dimi1010 Jul 31, 2024
0317b91
Fixed documentation error.
Dimi1010 Jul 31, 2024
bb695b8
Attempt to fix the deprecation warning error.
Dimi1010 Aug 1, 2024
3dfe776
Lint
Dimi1010 Aug 1, 2024
5c3f9d3
Attempt to fix doxygen copydoc errors.
Dimi1010 Aug 1, 2024
f5bd24f
Copied the documentation manually as copydoc was causing issues.
Dimi1010 Aug 1, 2024
49ffbd6
Fixed missed copydoc.
Dimi1010 Aug 1, 2024
e473b64
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Aug 7, 2024
e8e86ca
Rename preprocessor macros to remove leading underscores
Dimi1010 Aug 7, 2024
1fabdd1
Merge branch 'dev' into refactor/device-list-management
Dimi1010 Aug 7, 2024
9d515c4
Merge branch 'dev' into refactor/device-list-management
Dimi1010 Aug 12, 2024
abf1171
Updated code to use PointerVector.
Dimi1010 Aug 12, 2024
6ffdc41
Merge branch 'dev' into refactor/device-list-management
Dimi1010 Aug 14, 2024
5215614
Merge branch 'dev' into refactor/device-list-management
Dimi1010 Aug 21, 2024
133c433
Fixed merge errors.
Dimi1010 Aug 23, 2024
87bd25d
Lint
Dimi1010 Aug 23, 2024
848afd5
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Aug 23, 2024
17f742e
Fixed device list name.
Dimi1010 Aug 23, 2024
be2db4b
Fixed pushBack signature.
Dimi1010 Aug 23, 2024
2ea0628
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Aug 25, 2024
8841b6b
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Oct 4, 2024
1a1c0b8
Lint
Dimi1010 Oct 6, 2024
2f890a4
Lint
Dimi1010 Oct 6, 2024
75a1df3
Lint
Dimi1010 Oct 6, 2024
2bdfd2a
Merge branch 'dev' into refactor/device-list-management
Dimi1010 Oct 11, 2024
344a656
Fixed empty line between methods.
Dimi1010 Oct 11, 2024
0cfbdd4
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Oct 24, 2024
274c02c
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Nov 5, 2024
e9ae72e
Lint
Dimi1010 Nov 5, 2024
afd6e92
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Nov 25, 2024
b0d2744
Added self-copy guards.
Dimi1010 Nov 25, 2024
9863b11
Fixed warnings.
Dimi1010 Nov 25, 2024
fc73879
Added tests for DeviceListBase.
Dimi1010 Nov 25, 2024
6bd0cbf
Lint
Dimi1010 Nov 25, 2024
1d84a3c
Add "venv" and "./out" directories to be ignored by codespell.
Dimi1010 Nov 27, 2024
68130d5
Suppress checkersReport messages for cppcheck.
Dimi1010 Nov 27, 2024
9ad70af
Lint
Dimi1010 Nov 27, 2024
871cca7
Added explicit defaulted public destructors to device lists.
Dimi1010 Nov 27, 2024
8eb5f62
Merge branch 'dev' into refactor/device-list-management
Dimi1010 Dec 31, 2024
1b917bf
Merge remote-tracking branch 'upstream/dev' into refactor/device-list…
Dimi1010 Jan 4, 2025
b58284b
Lint
Dimi1010 Jan 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/Arping/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void printAppVersion()
*/
void listInterfaces()
{
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
const auto& devList = pcpp::PcapLiveDeviceList::getInstance();

std::cout << std::endl << "Network interfaces:" << std::endl;
for (const auto &dev : devList)
Expand Down
6 changes: 2 additions & 4 deletions Examples/DNSResolver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ void printAppVersion()
*/
void listInterfaces()
{
const std::vector<pcpp::PcapLiveDevice*>& 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;
}
Expand Down Expand Up @@ -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<pcpp::PcapLiveDevice*>& 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; });
Expand Down
2 changes: 1 addition & 1 deletion Examples/DnsSpoofing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void printAppVersion()
*/
void listInterfaces()
{
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
const auto& devList = pcpp::PcapLiveDeviceList::getInstance();

std::cout << std::endl << "Network interfaces:" << std::endl;
for (const auto &dev : devList)
Expand Down
2 changes: 1 addition & 1 deletion Examples/IcmpFileTransfer/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void printAppVersion()
*/
void listInterfaces()
{
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
const auto& devList = pcpp::PcapLiveDeviceList::getInstance();

std::cout << std::endl << "Network interfaces:" << std::endl;
for (const auto &dev : devList)
Expand Down
2 changes: 1 addition & 1 deletion Examples/SSLAnalyzer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void printAppVersion()
*/
void listInterfaces()
{
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
const auto& devList = pcpp::PcapLiveDeviceList::getInstance();

std::cout << std::endl << "Network interfaces:" << std::endl;
for (const auto &dev : devList)
Expand Down
2 changes: 1 addition & 1 deletion Examples/TLSFingerprinting/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void printAppVersion()
*/
void listInterfaces()
{
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();
const auto& devList = pcpp::PcapLiveDeviceList::getInstance();

std::cout << std::endl << "Network interfaces:" << std::endl;
for (const auto &dev : devList)
Expand Down
48 changes: 47 additions & 1 deletion Pcap++/header/PcapLiveDeviceList.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include "IpAddress.h"
#include "PcapLiveDevice.h"
#include "PointerVector.h"
#include <vector>
#include <memory>
#include "DeprecationUtils.h"


/// @file
Expand All @@ -24,7 +26,7 @@ namespace pcpp
class PcapLiveDeviceList
{
private:
std::vector<std::unique_ptr<PcapLiveDevice>> m_LiveDeviceList;
PointerVector<PcapLiveDevice> m_LiveDeviceList;
// Vector of raw device pointers to keep the signature of getPcapLiveDevicesList, as it returns a reference.
std::vector<PcapLiveDevice*> m_LiveDeviceListView;

Expand All @@ -39,6 +41,16 @@ namespace pcpp

void updateLiveDeviceListView() const;
public:
/**
* Iterator object that can be used for iterating all PcapLiveDevice in list
*/
using iterator = PointerVector<PcapLiveDevice>::VectorIterator;

/**
* Const iterator object that can be used for iterating all PcapLiveDevice in a constant list
*/
using const_iterator = PointerVector<PcapLiveDevice>::ConstVectorIterator;

PcapLiveDeviceList(const PcapLiveDeviceList&) = delete;
PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete;
PcapLiveDeviceList& operator=(const PcapLiveDeviceList&) = delete;
Expand All @@ -56,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<PcapLiveDevice*>& getPcapLiveDevicesList() const { return m_LiveDeviceListView; };
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved

/**
Expand Down Expand Up @@ -117,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
19 changes: 10 additions & 9 deletions Pcap++/header/PcapRemoteDeviceList.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <memory>
#include "IpAddress.h"
#include "PointerVector.h"
#include "PcapRemoteDevice.h"

/// @file
Expand All @@ -28,7 +29,7 @@ namespace pcpp
class PcapRemoteDeviceList
{
private:
std::vector<PcapRemoteDevice*> m_RemoteDeviceList;
PointerVector<PcapRemoteDevice> m_RemoteDeviceList;
IPAddress m_RemoteMachineIpAddress;
uint16_t m_RemoteMachinePort;
std::shared_ptr<PcapRemoteAuthentication> m_RemoteAuthentication;
Expand All @@ -44,20 +45,20 @@ namespace pcpp
/**
* Iterator object that can be used for iterating all PcapRemoteDevice in list
*/
typedef typename std::vector<PcapRemoteDevice*>::iterator RemoteDeviceListIterator;
using RemoteDeviceListIterator = PointerVector<PcapRemoteDevice>::VectorIterator;
using iterator = RemoteDeviceListIterator;

/**
* Const iterator object that can be used for iterating all PcapRemoteDevice in a constant list
*/
typedef typename std::vector<PcapRemoteDevice*>::const_iterator ConstRemoteDeviceListIterator;
using ConstRemoteDeviceListIterator = PointerVector<PcapRemoteDevice>::ConstVectorIterator;
using const_iterator = ConstRemoteDeviceListIterator;

PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete;
PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete;
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
Expand Down Expand Up @@ -128,22 +129,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(); }

};

Expand Down
13 changes: 6 additions & 7 deletions Pcap++/src/PcapLiveDeviceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ void PcapLiveDeviceList::init()
#else //__linux__, __APPLE__, __FreeBSD__
auto dev = std::unique_ptr<PcapLiveDevice>(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<PcapLiveDevice>& ptr) { return ptr.get(); });
std::copy(m_LiveDeviceList.begin(), m_LiveDeviceList.end(), m_LiveDeviceListView.begin());

setDnsServers();
}
Expand Down Expand Up @@ -291,7 +290,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv4Address& ipA
if (*currAddr == ipAddr)
{
PCPP_LOG_DEBUG("Found matched address!");
return devicePtr.get();
return devicePtr;
}
}
}
Expand Down Expand Up @@ -324,7 +323,7 @@ PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6
if (*currAddr == ip6Addr)
{
PCPP_LOG_DEBUG("Found matched address!");
return devicePtr.get();
return devicePtr;
}
}
}
Expand Down Expand Up @@ -354,15 +353,15 @@ 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<PcapLiveDevice>& dev) { return dev->getName() == name; });
[&name](PcapLiveDevice* const dev) { return dev->getName() == name; });

if (devIter == m_LiveDeviceList.end())
{
PCPP_LOG_DEBUG("Found no live device with name '" << name << "'");
return nullptr;
}

return devIter->get();
return *devIter;
}

PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const
Expand Down
17 changes: 4 additions & 13 deletions Pcap++/src/PcapRemoteDeviceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "IpAddressUtils.h"
#include "pcap.h"
#include <array>
#include <memory>
#include <ws2tcpip.h>

namespace pcpp
Expand Down Expand Up @@ -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<PcapRemoteDevice> pNewRemoteDevice = std::unique_ptr<PcapRemoteDevice>(
tigercosmos marked this conversation as resolved.
Show resolved Hide resolved
new PcapRemoteDevice(currInterface, resultList->m_RemoteAuthentication, resultList->getRemoteMachineIpAddress(), resultList->getRemoteMachinePort()));
tigercosmos marked this conversation as resolved.
Show resolved Hide resolved
resultList->m_RemoteDeviceList.pushBack(std::move(pNewRemoteDevice));
}

return resultList;
Expand Down Expand Up @@ -210,16 +211,6 @@ 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

#endif // _WIN32
Loading