Skip to content

Commit 964eab7

Browse files
authored
Added internal::DeviceListBase as a common base for device list classes. (#1790)
* Added base class for device lists. * Converted `PcapLiveDeviceList` to use `internal::DeviceListBase`. * Lint * Removed unused typedefs. * Revert "Removed unused typedefs." This reverts commit c051dd3. * Removed unused typedefs.
1 parent 7db783e commit 964eab7

File tree

4 files changed

+114
-30
lines changed

4 files changed

+114
-30
lines changed

Pcap++/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ add_library(
2828
set(
2929
public_headers
3030
header/Device.h
31+
header/DeviceListBase.h
3132
header/NetworkUtils.h
3233
header/PcapDevice.h
3334
header/PcapFileDevice.h

Pcap++/header/DeviceListBase.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#pragma once
2+
#include "PointerVector.h"
3+
4+
namespace pcpp
5+
{
6+
namespace internal
7+
{
8+
/// @brief A base class for device lists, providing common functionality for managing a list of devices.
9+
template <typename T, typename Deleter = std::default_delete<T>> class DeviceListBase
10+
{
11+
protected:
12+
DeviceListBase() = default;
13+
14+
explicit DeviceListBase(PointerVector<T, Deleter> devices) : m_DeviceList(std::move(devices))
15+
{}
16+
17+
DeviceListBase(DeviceListBase const&) = default;
18+
DeviceListBase(DeviceListBase&&) = default;
19+
// Protected destructor to disallow deletion of derived class through a base class pointer
20+
~DeviceListBase() = default;
21+
22+
DeviceListBase& operator=(DeviceListBase const&) = default;
23+
DeviceListBase& operator=(DeviceListBase&&) = default;
24+
25+
public:
26+
using size_type = std::size_t;
27+
28+
using iterator = typename PointerVector<T, Deleter>::VectorIterator;
29+
using const_iterator = typename PointerVector<T, Deleter>::ConstVectorIterator;
30+
31+
/// @brief Get an iterator to the beginning of the device list
32+
iterator begin()
33+
{
34+
return m_DeviceList.begin();
35+
}
36+
37+
/// @brief Get an iterator to the beginning of the device list
38+
const_iterator begin() const
39+
{
40+
return m_DeviceList.begin();
41+
}
42+
43+
/// @brief Get a const iterator to the beginning of the device list
44+
const_iterator cbegin() const
45+
{
46+
return m_DeviceList.cbegin();
47+
}
48+
49+
/// @brief Get an iterator to the end of the device list
50+
iterator end()
51+
{
52+
return m_DeviceList.end();
53+
}
54+
55+
/// @brief Get an iterator to the end of the device list
56+
const_iterator end() const
57+
{
58+
return m_DeviceList.end();
59+
}
60+
61+
/// @brief Get a const iterator to the end of the device list
62+
const_iterator cend() const
63+
{
64+
return m_DeviceList.cend();
65+
}
66+
67+
/// @brief Check if the device list is empty
68+
/// @return True if the device list is empty, false otherwise
69+
bool empty() const
70+
{
71+
return m_DeviceList.empty();
72+
}
73+
74+
/// @brief Get the number of devices in the list
75+
/// @return The number of devices in the list
76+
size_type size() const
77+
{
78+
return m_DeviceList.size();
79+
}
80+
81+
protected:
82+
PointerVector<T, Deleter> m_DeviceList;
83+
};
84+
} // namespace internal
85+
} // namespace pcpp

Pcap++/header/PcapLiveDeviceList.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "DeprecationUtils.h"
44
#include "IpAddress.h"
5+
#include "DeviceListBase.h"
56
#include "PcapLiveDevice.h"
67
#include <vector>
78
#include <memory>
@@ -17,10 +18,11 @@ namespace pcpp
1718
/// (on Windows) instances. All live devices are initialized on startup and wrap the network interfaces installed on
1819
/// the machine. This class enables access to them through their IP addresses or get a vector of all of them so the
1920
/// user can search them in some other way
20-
class PcapLiveDeviceList
21+
class PcapLiveDeviceList : public internal::DeviceListBase<PcapLiveDevice>
2122
{
2223
private:
23-
std::vector<std::unique_ptr<PcapLiveDevice>> m_LiveDeviceList;
24+
using Base = internal::DeviceListBase<PcapLiveDevice>;
25+
2426
// Vector of raw device pointers to keep the signature of getPcapLiveDevicesList, as it returns a reference.
2527
std::vector<PcapLiveDevice*> m_LiveDeviceListView;
2628

@@ -29,7 +31,7 @@ namespace pcpp
2931
// private c'tor
3032
PcapLiveDeviceList();
3133

32-
static std::vector<std::unique_ptr<PcapLiveDevice>> fetchAllLocalDevices();
34+
static PointerVector<PcapLiveDevice> fetchAllLocalDevices();
3335
static std::vector<IPv4Address> fetchDnsServers();
3436

3537
public:

Pcap++/src/PcapLiveDeviceList.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,23 @@ namespace pcpp
2626
{
2727
namespace
2828
{
29-
void syncPointerVectors(std::vector<std::unique_ptr<PcapLiveDevice>> const& mainVector,
29+
void syncPointerVectors(PointerVector<PcapLiveDevice> const& mainVector,
3030
std::vector<PcapLiveDevice*>& viewVector)
3131
{
3232
viewVector.resize(mainVector.size());
3333
// Full update of all elements of the view vector to synchronize them with the main vector.
34-
std::transform(mainVector.begin(), mainVector.end(), viewVector.begin(),
35-
[](const std::unique_ptr<PcapLiveDevice>& ptr) { return ptr.get(); });
34+
std::copy(mainVector.begin(), mainVector.end(), viewVector.begin());
3635
}
3736
} // namespace
3837

39-
PcapLiveDeviceList::PcapLiveDeviceList() : m_LiveDeviceList(fetchAllLocalDevices()), m_DnsServers(fetchDnsServers())
38+
PcapLiveDeviceList::PcapLiveDeviceList() : Base(fetchAllLocalDevices()), m_DnsServers(fetchDnsServers())
4039
{
41-
syncPointerVectors(m_LiveDeviceList, m_LiveDeviceListView);
40+
syncPointerVectors(m_DeviceList, m_LiveDeviceListView);
4241
}
4342

44-
std::vector<std::unique_ptr<PcapLiveDevice>> PcapLiveDeviceList::fetchAllLocalDevices()
43+
PointerVector<PcapLiveDevice> PcapLiveDeviceList::fetchAllLocalDevices()
4544
{
46-
std::vector<std::unique_ptr<PcapLiveDevice>> deviceList;
45+
PointerVector<PcapLiveDevice> deviceList;
4746
std::unique_ptr<pcap_if_t, internal::PcapFreeAllDevsDeleter> interfaceList;
4847
try
4948
{
@@ -65,7 +64,7 @@ namespace pcpp
6564
#else //__linux__, __APPLE__, __FreeBSD__
6665
auto dev = std::unique_ptr<PcapLiveDevice>(new PcapLiveDevice(currInterface, true, true, true));
6766
#endif
68-
deviceList.push_back(std::move(dev));
67+
deviceList.pushBack(std::move(dev));
6968
}
7069
return deviceList;
7170
}
@@ -289,12 +288,11 @@ namespace pcpp
289288

290289
PcapLiveDevice* PcapLiveDeviceList::getDeviceByIp(const IPv4Address& ipAddr) const
291290
{
292-
auto it = std::find_if(m_LiveDeviceList.begin(), m_LiveDeviceList.end(),
293-
[&ipAddr](std::unique_ptr<PcapLiveDevice> const& devPtr) {
294-
auto devIP = devPtr->getIPv4Address();
295-
return devIP == ipAddr;
296-
});
297-
return it != m_LiveDeviceList.end() ? it->get() : nullptr;
291+
auto it = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), [&ipAddr](PcapLiveDevice const* devPtr) {
292+
auto devIP = devPtr->getIPv4Address();
293+
return devIP == ipAddr;
294+
});
295+
return it != m_DeviceList.end() ? *it : nullptr;
298296
}
299297

300298
PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const
@@ -304,12 +302,11 @@ namespace pcpp
304302

305303
PcapLiveDevice* PcapLiveDeviceList::getDeviceByIp(const IPv6Address& ip6Addr) const
306304
{
307-
auto it = std::find_if(m_LiveDeviceList.begin(), m_LiveDeviceList.end(),
308-
[&ip6Addr](std::unique_ptr<PcapLiveDevice> const& devPtr) {
309-
auto devIP = devPtr->getIPv6Address();
310-
return devIP == ip6Addr;
311-
});
312-
return it != m_LiveDeviceList.end() ? it->get() : nullptr;
305+
auto it = std::find_if(m_DeviceList.begin(), m_DeviceList.end(), [&ip6Addr](PcapLiveDevice const* devPtr) {
306+
auto devIP = devPtr->getIPv6Address();
307+
return devIP == ip6Addr;
308+
});
309+
return it != m_DeviceList.end() ? *it : nullptr;
313310
}
314311

315312
PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const
@@ -342,17 +339,16 @@ namespace pcpp
342339
PcapLiveDevice* PcapLiveDeviceList::getDeviceByName(const std::string& name) const
343340
{
344341
PCPP_LOG_DEBUG("Searching all live devices...");
345-
auto devIter =
346-
std::find_if(m_LiveDeviceList.begin(), m_LiveDeviceList.end(),
347-
[&name](const std::unique_ptr<PcapLiveDevice>& dev) { return dev->getName() == name; });
342+
auto devIter = std::find_if(m_DeviceList.begin(), m_DeviceList.end(),
343+
[&name](PcapLiveDevice* dev) { return dev->getName() == name; });
348344

349-
if (devIter == m_LiveDeviceList.end())
345+
if (devIter == m_DeviceList.end())
350346
{
351347
PCPP_LOG_DEBUG("Found no live device with name '" << name << "'");
352348
return nullptr;
353349
}
354350

355-
return devIter->get();
351+
return *devIter;
356352
}
357353

358354
PcapLiveDevice* PcapLiveDeviceList::getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const
@@ -382,10 +378,10 @@ namespace pcpp
382378
{
383379
m_LiveDeviceListView.clear();
384380

385-
m_LiveDeviceList = fetchAllLocalDevices();
381+
m_DeviceList = fetchAllLocalDevices();
386382
m_DnsServers = fetchDnsServers();
387383

388-
syncPointerVectors(m_LiveDeviceList, m_LiveDeviceListView);
384+
syncPointerVectors(m_DeviceList, m_LiveDeviceListView);
389385
}
390386

391387
} // namespace pcpp

0 commit comments

Comments
 (0)