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

Pcap device list refactoring to use smart pointers. #1386

Closed
Closed
Show file tree
Hide file tree
Changes from 83 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
5d10a89
Replaced the container of owning raw pointers of PcapDevices in PcapD…
Dimi1010 May 6, 2024
9146330
Replaced raw pcap_if_t pointers with smart pointers in LiveDeviceList…
Dimi1010 May 6, 2024
bbbcd71
Simplified device insertion call.
Dimi1010 May 6, 2024
19c93f3
Rafactored while-loop into a for-loop.
Dimi1010 May 6, 2024
9359c2c
Refactored to use a bytes view of IPv6 address when comparing the add…
Dimi1010 May 6, 2024
226b313
Merge branch 'dev' into feature/device-list-smart-pointers
Dimi1010 May 7, 2024
0ca74d9
Changed remote authentication structure in PcapRemoteDeviceList and P…
Dimi1010 May 7, 2024
276fdfb
Moved variable setup to initialization list.
Dimi1010 May 7, 2024
2c1bfef
C-cast -> Cpp cast.
Dimi1010 May 7, 2024
074de0a
Replaced NULL with nullptr.
Dimi1010 May 7, 2024
61fcfa8
Changed shared_ptr params to const ref
Dimi1010 May 7, 2024
0027113
Added a smart pointer version of getRemoteDeviceList.
Dimi1010 May 7, 2024
bd1677f
Changed while-loop to for-loop.
Dimi1010 May 7, 2024
ca0602b
Fixed possible copy from nullptr.
Dimi1010 May 7, 2024
9551dbe
Added smart pointer factory overload for non-auth verison.
Dimi1010 May 7, 2024
6cedf1b
Changed setRemoteAuth to receive smart ptr by value as that enables m…
Dimi1010 May 7, 2024
0a437be
Changed remote device to receive shared ptr by value, to enable move …
Dimi1010 May 7, 2024
5f683c4
Added overload from authentication struct reference.
Dimi1010 May 7, 2024
0a16197
Cenrtalized deleters that do not need to be declared in the public he…
Dimi1010 May 7, 2024
14c951f
Simplified conversion to string.
Dimi1010 May 7, 2024
e8b0086
Removed duplicated error buffer.
Dimi1010 May 7, 2024
b5cd2a2
Changed interfaceList to be held by smart pointer.
Dimi1010 May 7, 2024
412d5df
Liter fixes.
Dimi1010 May 7, 2024
be3a169
Refactored RemoteDeviceList to use RAII constructor initialization.
Dimi1010 May 7, 2024
820d4aa
Lint
Dimi1010 May 7, 2024
e0cdeda
Removed duplicate auth structure.
Dimi1010 May 7, 2024
74d8be5
Changed PcapRemoteDevice raw pointers to smart pointers for memory ma…
Dimi1010 May 7, 2024
e755209
Added API for getting PcapRemoteDevice as a shared pointer instead of…
Dimi1010 May 7, 2024
a83d9b1
Lint
Dimi1010 May 7, 2024
25cf25f
Documentation fix.
Dimi1010 May 7, 2024
7b41ece
Added API for getting PcapLiveDevice as a shared pointer instead of a…
Dimi1010 May 7, 2024
49d7736
Added smart pointer API of PcapLiveDeviceList::clone
Dimi1010 May 7, 2024
ff165e7
Marked clone method as const.
Dimi1010 May 7, 2024
0037b97
Added smart ptr API for getLiveDevicesList.
Dimi1010 May 7, 2024
8240730
Removed unused private setters.
Dimi1010 May 7, 2024
f3ab634
Fixed getByIpOrName calling pure IP implementation.
Dimi1010 May 7, 2024
639b85d
Fixed getByName calling pure IP implementation.
Dimi1010 May 7, 2024
786d59e
Revert "Removed duplicate auth structure."
Dimi1010 May 7, 2024
c7195c2
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 May 12, 2024
55a427b
Renamed the smart API tag from snake_case to PascalCase.
Dimi1010 May 12, 2024
9c26770
Refactored device loop to for-each loop.
Dimi1010 May 12, 2024
bf7516c
Renamed loop variables to reflect the new for-each loops, as the vari…
Dimi1010 May 12, 2024
a8e97b0
Fixed log message grammar.
Dimi1010 May 12, 2024
6436b01
Explicitly marks "deleted" copy ctors as deleted as in C++11 standard.
Dimi1010 May 12, 2024
89d4b24
Updated remote device list documentation.
Dimi1010 May 12, 2024
9628d3d
Added exception if IPAddress type is not IPv4 or IPv6 for some reason.
Dimi1010 May 12, 2024
2ac28f9
Updated documentation for LiveDeviceList
Dimi1010 May 12, 2024
11a4f63
Made the smart ptr api tag static constant.
Dimi1010 May 12, 2024
b0b2a83
Added unique_ptr overload for remote authentication as a ptr.
Dimi1010 May 12, 2024
dd09da0
Added deprecation warnings to old Raw pointer API.
Dimi1010 May 12, 2024
e6123dd
Fixed class documentation.
Dimi1010 May 12, 2024
0daf110
Fixed class documentation.
Dimi1010 May 12, 2024
5d4703a
Added name to tag variable to keep doxygen happy.
Dimi1010 May 12, 2024
1d2ed71
Added name to tag variable to keep doxygen happy.
Dimi1010 May 12, 2024
7930e5b
more tag fixes...
Dimi1010 May 12, 2024
db3530d
Changed typedefs to use Cpp11 using declarations.
Dimi1010 May 13, 2024
ddf3e21
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 May 23, 2024
54143cc
Updated deprecation macros to have deprecation message.
Dimi1010 May 23, 2024
28fba54
Changed internal macro to have double underscore.
Dimi1010 May 23, 2024
e82aad3
Added a smart pointer overload for PcapLiveDevice clone.
Dimi1010 May 23, 2024
775b14a
Lint
Dimi1010 May 23, 2024
ec0d8a8
Changed standard iterator and const_iterator type aliases to copy fro…
Dimi1010 May 23, 2024
6b64f65
Lint
Dimi1010 May 23, 2024
da0a6c4
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 May 24, 2024
454b5f6
Removed duplicates of SmartPtrApiTag struct. The struct is now a top-…
Dimi1010 May 24, 2024
27caf36
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 May 26, 2024
8bcdee9
Marked private copy ctros as 'deleted'.
Dimi1010 May 26, 2024
6dee560
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 May 27, 2024
b5be879
Added assert that remote device is not nullptr.
Dimi1010 May 26, 2024
9660636
Fixed remote devices being lost on view update.
Dimi1010 May 27, 2024
87c4855
Centralized duplicated code for fetching device interfaces from libpcap.
Dimi1010 May 27, 2024
fd595c6
Fixed remote device fetch being windows only.
Dimi1010 May 27, 2024
f03bb13
Lint
Dimi1010 May 27, 2024
541b198
Removed ifdefs as it is assumed the deprecation macro to be undefined…
Dimi1010 May 27, 2024
e9c7a9b
Added smart pointer api tests to TestPcapLiveDeviceList, TestPcapLive…
Dimi1010 May 28, 2024
9a4003e
Interlaced tests in device list search.
Dimi1010 May 28, 2024
65bc6e0
Cleanup
Dimi1010 May 30, 2024
979273f
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 May 30, 2024
f6ba4a1
Marked the device list view as mutable for consistency with PcapLiveD…
Dimi1010 Jun 2, 2024
8736f42
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 Jun 2, 2024
6e7f8cb
Changed clone internal to return smart pointer.
Dimi1010 Jun 2, 2024
c9e37ee
Fixed method not being const.
Dimi1010 Jun 2, 2024
2efeaec
Fixed duplicate clone.
Dimi1010 Jun 2, 2024
39cee9b
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 Jun 3, 2024
0528e92
Lint
Dimi1010 Jun 3, 2024
a95c946
Improved documentation.
Dimi1010 Jun 3, 2024
4ae2edb
Changed strcmp to std::strcmp
Dimi1010 Jun 3, 2024
5d9dcfd
Shortened double declaration to auto.
Dimi1010 Jun 3, 2024
5e25d9f
Documentation fixes.
Dimi1010 Jun 3, 2024
4bcb50e
Marked move ctors/assignment as deleted.
Dimi1010 Jun 4, 2024
905f871
Merge remote-tracking branch 'upstream/dev' into feature/device-list-…
Dimi1010 Jun 4, 2024
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
4 changes: 4 additions & 0 deletions Pcap++/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
add_library(
Pcap++
src/DeviceUtils.cpp
$<$<BOOL:${PCAPPP_USE_DPDK}>:src/DpdkDevice.cpp>
$<$<BOOL:${PCAPPP_USE_DPDK}>:src/DpdkDeviceList.cpp>
$<$<BOOL:${PCAPPP_USE_DPDK_KNI}>:src/KniDevice.cpp>
$<$<BOOL:${PCAPPP_USE_DPDK_KNI}>:src/KniDeviceList.cpp>
$<$<BOOL:${LINUX}>:src/LinuxNicInformationSocket.cpp>
$<$<BOOL:${PCAPPP_USE_DPDK}>:src/MBufRawPacket.cpp>
src/MemoryUtils.cpp
src/NetworkUtils.cpp
src/PcapFileDevice.cpp
src/PcapDevice.cpp
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions Pcap++/header/DeviceUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest to put all util functions in one file, as there is not much code.

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which one to merge into the other tho? I initially separated them as they have different use cases.

MemoryUtils is for general utilities w.r.t. smart pointer utilities and deleters.
DeviceUtils is for utility functions for Pcap devices, like fetching devices, etc.

MemoryUtils will be used a lot more as any time a smart pointer to a Pcap handle is used, it would need to be included for the deleter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. originally I thought we could merge MemoryUtils and DeviceUtils, but it did make sense to be separated.


/// @file

#include <memory>
#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<pcap_if_t, PcapFreeAllDevsDeleter> 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<pcap_if_t, PcapFreeAllDevsDeleter> getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth = nullptr);
#endif // _WIN32
}
}
31 changes: 31 additions & 0 deletions Pcap++/header/MemoryUtils.h
Original file line number Diff line number Diff line change
@@ -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;
};
}
}
23 changes: 19 additions & 4 deletions Pcap++/header/PcapLiveDevice.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <atomic>
#include <memory>
#include <vector>
#include <string.h>
#include <thread>
Expand All @@ -24,6 +25,15 @@ typedef struct pcap_addr pcap_addr_t;
*/
namespace pcpp
{
/*
* @class SmartPtrApiTag
* Helper tag to disambiguate smart pointer API.
*/
struct SmartPtrApiTag{};
/**
* Helper tag constant for disambuguating smart pointer API.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: disambiguating, btw I rarely see this word, I wonder if it is precise here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed a95c946. I think it should be better now?

*/
extern const SmartPtrApiTag SmartPtrApi;

class PcapLiveDevice;

Expand Down Expand Up @@ -114,9 +124,6 @@ namespace pcpp

// c'tor is not public, there should be only one for every interface (created by PcapLiveDeviceList)
PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway);
// copy c'tor is not public
PcapLiveDevice( const PcapLiveDevice& other );
PcapLiveDevice& operator=(const PcapLiveDevice& other);

void setDeviceMtu();
void setDeviceMacAddress();
Expand All @@ -130,6 +137,8 @@ namespace pcpp
static void onPacketArrivesNoCallback(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
static void onPacketArrivesBlockingMode(uint8_t* user, const struct pcap_pkthdr* pkthdr, const uint8_t* packet);
public:
PcapLiveDevice(const PcapLiveDevice& other) = delete;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also implement moving construction/assignment.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, if the underlying are shared pointers, why do we disallow coping?

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, if the underlying are shared pointers, why do we disallow coping?

IDK. I think the reason a LiveDevice itself is not copiable is something with the underlying Pcap descriptors. Although there is still the clone method so not really sure. The current change is mostly a stylistic from the pre-Cpp11 style of private ctors to post-Cpp11 deleted ctors.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can keep the old clone, but I think we can now embrace the copy constructor/assignment?

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I figured out at least partly why its private / deleted. LiveDevice is a virtual class and usually handled by pointers to base class (i.e. WinPcapLiveDevice device is used through PcapLiveDevice pointer). A copy ctor used from a base pointer will only use the base copy ctor, not the actual copy ctor of the class the base ptr points to.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also implement moving construction/assignment.

Added 4bcb50e

PcapLiveDevice& operator=(const PcapLiveDevice& other) = delete;

/**
* The type of the live device
Expand Down Expand Up @@ -571,13 +580,19 @@ namespace pcpp
* @return Pointer to the copied class
*/
PcapLiveDevice* clone() const;
/**
* Clones the current device class
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return Unique pointer to the copied device.
*/
std::unique_ptr<PcapLiveDevice> clone(SmartPtrApiTag apiTag) const;

void getStatistics(IPcapDevice::PcapStats& stats) const override;

protected:
pcap_t* doOpen(const DeviceConfiguration& config);

virtual PcapLiveDevice* cloneInternal(pcap_if_t& devInterface) const;
virtual std::unique_ptr<PcapLiveDevice> cloneInternal(pcap_if_t& devInterface) const;
};

} // namespace pcpp
111 changes: 90 additions & 21 deletions Pcap++/header/PcapLiveDeviceList.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#pragma once

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


/// @file

#define PCPP_DEPRECATED_RAW_PTR_API PCPP_DEPRECATED("This method is deprecated in favor of the SmartPtrAPI overload.")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doxygen generation really dislikes this macro for some reason, when used on a member function. No idea why... tried several things to fix them including mirroring the way the define is written in TcpLayer.h, but none of them worked. Would like a second opinion on why the generation fails on the macro.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doxygen test fails with the following:

Preprocessing /tmp/cirrus-ci-build/Pcap++/head/tmp/cirrus-ci-build/Pcap++/header/PcapLiveDeviceList.h:62: error: Found ';' while parsing initializer list! (doxygen could be confused by a macro call without semicolon) (warning treated as error, aborting now)
er/MBufRawPacket.h...


/**
* \namespace pcpp
* \brief The main namespace for the PcapPlusPlus lib
Expand All @@ -23,20 +27,24 @@ namespace pcpp
class PcapLiveDeviceList
{
private:
std::vector<PcapLiveDevice*> m_LiveDeviceList;
std::vector<std::shared_ptr<PcapLiveDevice>> m_LiveDeviceList;
// Vector of raw device pointers to keep the signature of getPcapLiveDevicesList, as it returns a reference.
mutable std::vector<PcapLiveDevice*> m_LiveDeviceListView;

std::vector<IPv4Address> m_DnsServers;

// private c'tor
PcapLiveDeviceList();
// private copy c'tor
PcapLiveDeviceList( const PcapLiveDeviceList& other );
PcapLiveDeviceList& operator=(const PcapLiveDeviceList& other);

void init();

void setDnsServers();

void updateLiveDeviceListView() const;
public:
PcapLiveDeviceList(const PcapLiveDeviceList& other) = delete;
PcapLiveDeviceList& operator=(const PcapLiveDeviceList& other) = delete;

/**
* The access method to the singleton
* @return The singleton instance of this class
Expand All @@ -49,50 +57,105 @@ namespace pcpp

/**
* @return A vector containing pointers to all live devices currently installed on the machine
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PCPP_DEPRECATED_RAW_PTR_API const std::vector<PcapLiveDevice*>& getPcapLiveDevicesList() const;
Copy link
Collaborator Author

@Dimi1010 Dimi1010 May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PcapLiveDeviceList provides a getter API to get all registered devices, while PcapRemoteDeviceList provides a container-like API to get all registered devices.

In my opinion, it would be best if both lists provided a standardized API. Which one would be better?
Here are my opinions on the pros and cons for both:

Getter method:
✔️ A getter approach would be consistent with the rest of the getter methods.
✔️ A getter can be made more easily to have overloads via tag dispatching.
❌ Reference getter locks down the internal implementation to always keeping a member of this type.
❌ Value getter will have a cost every time the getter is invoked, constructing the vector and coping the shared pointers.

Iterator API:
✔️ An iterator API would allow direct integration with other iterator algorithms and for-each statements.
✔️ Low overhead iteration over the devices.
❌ Complications with deprecation of existing iterators.[1]

[1] As the current PcapRemoteDeviceList already has an iterator API, that would be difficult to change with respect to the deprecation polity of 1 version notice of deprecation. Ideally, the iterators would refer to const std::shared_ptr<T>& with T being the device class. The current implementation returns iterators to *T. Theoretically it is possible to write a converting iterator, but it is going to be complicated. The current stopgap I ended up on is keeping a duplicate list of raw pointers to the devices, which is not ideal.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to have both a getter and an iterator for a container-like object.

Regarding the iterator type, since std::shared_ptr<T> and T* are pointer types, the users' existing code (such as ->) should not have much impact.

However, if we really don't want to break the API, as you said, it's still possible to have some conversion, but it's just troublesome.

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jun 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to have both a getter and an iterator for a container-like object.

Both is an option.

Regarding the iterator type, since std::shared_ptr<T> and T* are pointer types, the users' existing code (such as ->) should not have much impact.

Yeah, they shouldn't really. unless they decided to save the value from the iterator somewhere. That is a breaking change, but it would only need a get() call from the smart pointer to fix, as old code should still keep the list alive to keep the devices alive.

However, if we really don't want to break the API, as you said, it's still possible to have some conversion, but it's just troublesome.

I tried my hand at converting iterator a bit earlier. It is technically possible but it quickly becomes templated black magic if you try to make a general case converting iterator.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to remove the old API, and have both getter and new iterator. Then we can also remove the hack of SmartPtrApiTag and m_LiveDeviceListView

@seladb what do you say?

/**
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return A reference to a vector containing shared pointers to all live devices currently installed on the machine.
*/
const std::vector<PcapLiveDevice*>& getPcapLiveDevicesList() const { return m_LiveDeviceList; }
const std::vector<std::shared_ptr<PcapLiveDevice>>& getPcapLiveDevicesList(SmartPtrApiTag apiTag) const { return m_LiveDeviceList; };

/**
* 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
* @return A pointer to the live device if this IP address exists, nullptr otherwise
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const;
/**
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
* 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
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return A shared pointer to the live device if this IP address exists, nullptr otherwise
*/
PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const;
std::shared_ptr<PcapLiveDevice> getPcapLiveDeviceByIp(const IPAddress& ipAddr, SmartPtrApiTag apiTag) const;

/**
* 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
* @return A pointer to the live device if this IPv4 address exists, nullptr otherwise
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PcapLiveDevice* getPcapLiveDeviceByIp(const IPv4Address& ipAddr) const;
PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(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
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return A shared pointer to the live device if this IPv4 address exists, nullptr otherwise
*/
std::shared_ptr<PcapLiveDevice> getPcapLiveDeviceByIp(const IPv4Address& ipAddr, SmartPtrApiTag apiTag) const;

/**
* 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
* @return A pointer to the live device if this IPv6 address exists, nullptr otherwise
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(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
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return A shared pointer to the live device if this IPv6 address exists, nullptr otherwise
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PcapLiveDevice* getPcapLiveDeviceByIp(const IPv6Address& ip6Addr) const;
std::shared_ptr<PcapLiveDevice> getPcapLiveDeviceByIp(const IPv6Address& ip6Addr, SmartPtrApiTag apiTag) 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
* @return A pointer to the live device if this IP address is valid and exists. NULL otherwise
* @return A pointer to the live device if this IP address is valid and exists, nullptr otherwise
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PcapLiveDevice* getPcapLiveDeviceByIp(const std::string& ipAddrAsString) const;
PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIp(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
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return A shared pointer to the live device if this IP address is valid and exists, nullptr otherwise
*/
std::shared_ptr<PcapLiveDevice> getPcapLiveDeviceByIp(const std::string& ipAddrAsString, SmartPtrApiTag apiTag) 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
* @return A pointer to the live device if this name exists, nullprt otherwise
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PcapLiveDevice* getPcapLiveDeviceByName(const std::string& name) const;
PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByName(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)
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return A shared pointer to the live device if this name exists, nullptr otherwise
*/
std::shared_ptr<PcapLiveDevice> getPcapLiveDeviceByName(const std::string& name, SmartPtrApiTag apiTag) 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
* @return A pointer to the live device if exists, nullptr otherwise
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const;
PCPP_DEPRECATED_RAW_PTR_API PcapLiveDevice* getPcapLiveDeviceByIpOrName(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
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return A shared pointer to the live device if exists, nullptr otherwise
*/
std::shared_ptr<PcapLiveDevice> getPcapLiveDeviceByIpOrName(const std::string& ipOrName, SmartPtrApiTag apiTag) const;

/**
* @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
Expand All @@ -103,16 +166,22 @@ namespace pcpp
/**
* Copies the current live device list
* @return A pointer to the cloned device list
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload.
*/
PCPP_DEPRECATED_RAW_PTR_API PcapLiveDeviceList* clone() const;
/**
* Copies the current live device list
* @param[in] apiTag Disambiguating tag for SmartPtrAPI.
* @return A unique ptr managing the cloned device list
*/
PcapLiveDeviceList* clone();
std::unique_ptr<PcapLiveDeviceList> clone(SmartPtrApiTag apiTag) const;

/**
* Reset the live device list and DNS server list, meaning clear and refetch them
*/
void reset();

// d'tor
~PcapLiveDeviceList();
};

} // namespace pcpp

#undef PCPP_DEPRECATED_RAW_PTR_API
13 changes: 6 additions & 7 deletions Pcap++/header/PcapRemoteDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if defined(_WIN32)

#include <vector>
#include <memory>
#include "PcapLiveDevice.h"


Expand Down Expand Up @@ -81,22 +82,20 @@ namespace pcpp
private:
IPAddress m_RemoteMachineIpAddress;
uint16_t m_RemoteMachinePort;
PcapRemoteAuthentication* m_RemoteAuthentication;
std::shared_ptr<PcapRemoteAuthentication> m_RemoteAuthentication;

// c'tor is private, as only PcapRemoteDeviceList should create instances of it, and it'll create only one for every remote interface
PcapRemoteDevice(pcap_if_t* iface, PcapRemoteAuthentication* remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort);

// private copy c'tor
PcapRemoteDevice( const PcapRemoteDevice& other );
// private assignment operator
PcapRemoteDevice& operator=(const PcapRemoteDevice& other);
PcapRemoteDevice(pcap_if_t* iface, std::shared_ptr<PcapRemoteAuthentication> remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort);

static void* remoteDeviceCaptureThreadMain(void *ptr);

//overridden methods
ThreadStart getCaptureThreadStart();

public:
PcapRemoteDevice(const PcapRemoteDevice& other) = delete;
PcapRemoteDevice& operator=(const PcapRemoteDevice& other) = delete;

virtual ~PcapRemoteDevice() {}

/**
Expand Down
Loading
Loading