-
Notifications
You must be signed in to change notification settings - Fork 683
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
Changes from all commits
5d10a89
9146330
bbbcd71
19c93f3
9359c2c
226b313
0ca74d9
276fdfb
2c1bfef
074de0a
61fcfa8
0027113
bd1677f
ca0602b
9551dbe
6cedf1b
0a437be
5f683c4
0a16197
14c951f
e8b0086
b5cd2a2
412d5df
be3a169
820d4aa
e0cdeda
74d8be5
e755209
a83d9b1
25cf25f
7b41ece
49d7736
ff165e7
0037b97
8240730
f3ab634
639b85d
786d59e
c7195c2
55a427b
9c26770
bf7516c
a8e97b0
6436b01
89d4b24
9628d3d
2ac28f9
11a4f63
b0b2a83
dd09da0
e6123dd
0daf110
5d4703a
1d2ed71
7930e5b
db3530d
ddf3e21
54143cc
28fba54
e82aad3
775b14a
ec0d8a8
6b64f65
da0a6c4
454b5f6
27caf36
8bcdee9
6dee560
b5be879
9660636
87c4855
fd595c6
f03bb13
541b198
e9c7a9b
9a4003e
65bc6e0
979273f
f6ba4a1
8736f42
6e7f8cb
c9e37ee
2efeaec
39cee9b
0528e92
a95c946
4ae2edb
5d9dcfd
5e25d9f
4bcb50e
905f871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
/// @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 | ||
} | ||
} |
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; | ||
}; | ||
} | ||
} |
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.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doxygen test fails with the following:
|
||
|
||
/** | ||
* \namespace pcpp | ||
* \brief The main namespace for the PcapPlusPlus lib | ||
|
@@ -23,20 +27,26 @@ 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&) = delete; | ||
PcapLiveDeviceList(PcapLiveDeviceList&&) noexcept = delete; | ||
PcapLiveDeviceList& operator=(const PcapLiveDeviceList&) = delete; | ||
PcapLiveDeviceList& operator=(PcapLiveDeviceList&&) noexcept = delete; | ||
|
||
/** | ||
* The access method to the singleton | ||
* @return The singleton instance of this class | ||
|
@@ -49,50 +59,111 @@ 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. | ||
*/ | ||
const std::vector<PcapLiveDevice*>& getPcapLiveDevicesList() const { return m_LiveDeviceList; } | ||
PCPP_DEPRECATED_RAW_PTR_API const std::vector<PcapLiveDevice*>& getPcapLiveDevicesList() const; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In my opinion, it would be best if both lists provided a standardized API. Which one would be better? Getter method: Iterator API: [1] As the current There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Both is an option.
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
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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 @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<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. | ||
*/ | ||
PcapLiveDevice* getPcapLiveDeviceByIp(const IPAddress& ipAddr) const; | ||
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 | ||
*/ | ||
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, 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 | ||
* @return A pointer to the live device if this IPv6 address exists. NULL otherwise | ||
* @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, nullprt otherwise | ||
* @deprecated This method is deprecated in favor of the SmartPtrAPI overload. | ||
*/ | ||
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) | ||
* @return A pointer to the live device if this name exists. NULL otherwise | ||
* @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, nullptr 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* 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 | ||
* @return A pointer to the live device if exists, NULL otherwise | ||
* @param[in] apiTag Disambiguating tag for SmartPtrAPI. | ||
* @return A shared pointer to the live device if exists, nullptr otherwise | ||
*/ | ||
PcapLiveDevice* getPcapLiveDeviceByIpOrName(const std::string& ipOrName) const; | ||
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 | ||
|
@@ -103,16 +174,23 @@ 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
andDeviceUtils
, but it did make sense to be separated.