-
Notifications
You must be signed in to change notification settings - Fork 652
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
Centralize code for fetching pcap devices. #1434
Changes from 4 commits
6a2fefa
e98e6b3
1167bd1
535bc76
c09f664
0d91ddf
9a5e638
68b8632
fb685bf
9b0c9c7
f343771
617273e
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; | ||
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Dimi1010 marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "DeviceUtils.h" | ||
|
||
#include <array> | ||
#include <string> | ||
|
||
#include "pcap.h" | ||
#include "Logger.h" | ||
#include "IpAddress.h" | ||
|
||
namespace pcpp | ||
{ | ||
namespace internal | ||
{ | ||
std::unique_ptr<pcap_if_t, PcapFreeAllDevsDeleter> getAllLocalPcapDevices() | ||
{ | ||
pcap_if_t* interfaceListRaw; | ||
std::array<char, PCAP_ERRBUF_SIZE> errbuf; | ||
int err = pcap_findalldevs(&interfaceListRaw, errbuf.data()); | ||
if (err < 0) | ||
{ | ||
throw std::runtime_error("Error searching for devices: " + std::string(errbuf.begin(), errbuf.end())); | ||
} | ||
// Assigns the raw pointer to the smart pointer with specialized deleter. | ||
return std::unique_ptr<pcap_if_t, internal::PcapFreeAllDevsDeleter>(interfaceListRaw); | ||
} | ||
|
||
#ifdef _WIN32 | ||
std::unique_ptr<pcap_if_t, PcapFreeAllDevsDeleter> getAllRemotePcapDevices(const IPAddress& ipAddress, uint16_t port, pcap_rmtauth* pRmAuth) | ||
{ | ||
PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); | ||
std::array<char, PCAP_BUF_SIZE> remoteCaptureString; | ||
std::array<char, PCAP_ERRBUF_SIZE> errorBuf; | ||
if (pcap_createsrcstr(remoteCaptureString.data(), PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), | ||
std::to_string(port).c_str(), nullptr, errorBuf.data()) != 0) | ||
{ | ||
throw std::runtime_error("Error creating the remote connection string. Error: " + std::string(errorBuf.begin(), errorBuf.end())); | ||
} | ||
|
||
PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString.data()); | ||
|
||
pcap_if_t* interfaceListRaw; | ||
if (pcap_findalldevs_ex(remoteCaptureString.data(), pRmAuth, &interfaceListRaw, errorBuf.data()) < 0) | ||
{ | ||
throw std::runtime_error("Error retrieving device on remote machine. Error: " + std::string(errorBuf.begin(), errorBuf.end())); | ||
} | ||
return std::unique_ptr<pcap_if_t, internal::PcapFreeAllDevsDeleter>(interfaceListRaw); | ||
} | ||
#endif // _WIN32 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include "MemoryUtils.h" | ||
|
||
#include "pcap.h" | ||
|
||
namespace pcpp | ||
{ | ||
namespace internal | ||
{ | ||
void PcapCloseDeleter::operator()(pcap_t* ptr) const { pcap_close(ptr); } | ||
|
||
void PcapFreeAllDevsDeleter::operator()(pcap_if_t* ptr) const { pcap_freealldevs(ptr); } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
#include "IpAddressUtils.h" | ||
#include "PcapLiveDeviceList.h" | ||
#include "Logger.h" | ||
#include "MemoryUtils.h" | ||
#include "DeviceUtils.h" | ||
#include "SystemUtils.h" | ||
#include "pcap.h" | ||
#include <string.h> | ||
|
@@ -39,32 +41,30 @@ PcapLiveDeviceList::~PcapLiveDeviceList() | |
|
||
void PcapLiveDeviceList::init() | ||
{ | ||
pcap_if_t* interfaceList; | ||
char errbuf[PCAP_ERRBUF_SIZE]; | ||
int err = pcap_findalldevs(&interfaceList, errbuf); | ||
if (err < 0) | ||
std::unique_ptr<pcap_if_t, internal::PcapFreeAllDevsDeleter> interfaceList; | ||
try | ||
{ | ||
interfaceList = internal::getAllLocalPcapDevices(); | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
PCPP_LOG_ERROR("Error searching for devices: " << errbuf); | ||
PCPP_LOG_ERROR(e.what()); | ||
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. recommend to have a meaningful error message here. 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. The error messages are actually the same as before. It is just that the DeviceUtil functions have the error messages thrown in a |
||
} | ||
|
||
PCPP_LOG_DEBUG("Pcap lib version info: " << IPcapDevice::getPcapLibVersionInfo()); | ||
|
||
pcap_if_t* currInterface = interfaceList; | ||
while (currInterface != nullptr) | ||
|
||
for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next) | ||
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. nice catch |
||
{ | ||
#if defined(_WIN32) | ||
PcapLiveDevice* dev = new WinPcapLiveDevice(currInterface, true, true, true); | ||
#else //__linux__, __APPLE__, __FreeBSD__ | ||
PcapLiveDevice* dev = new PcapLiveDevice(currInterface, true, true, true); | ||
#endif | ||
currInterface = currInterface->next; | ||
m_LiveDeviceList.insert(m_LiveDeviceList.end(), dev); | ||
} | ||
|
||
setDnsServers(); | ||
|
||
PCPP_LOG_DEBUG("Freeing live device data"); | ||
pcap_freealldevs(interfaceList); | ||
} | ||
|
||
void PcapLiveDeviceList::setDnsServers() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ | |
#include "PcapRemoteDeviceList.h" | ||
#include "Logger.h" | ||
#include "IpUtils.h" | ||
#include "DeviceUtils.h" | ||
#include "MemoryUtils.h" | ||
#include "IpAddressUtils.h" | ||
#include "pcap.h" | ||
#include <array> | ||
|
@@ -20,51 +22,39 @@ PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& | |
|
||
PcapRemoteDeviceList* PcapRemoteDeviceList::getRemoteDeviceList(const IPAddress& ipAddress, uint16_t port, PcapRemoteAuthentication* remoteAuth) | ||
{ | ||
PCPP_LOG_DEBUG("Searching remote devices on IP: " << ipAddress << " and port: " << port); | ||
char remoteCaptureString[PCAP_BUF_SIZE]; | ||
char errbuf[PCAP_ERRBUF_SIZE]; | ||
std::ostringstream portAsString; | ||
portAsString << port; | ||
if (pcap_createsrcstr(remoteCaptureString, PCAP_SRC_IFREMOTE, ipAddress.toString().c_str(), portAsString.str().c_str(), NULL, errbuf) != 0) | ||
{ | ||
PCPP_LOG_ERROR("Error in creating the remote connection string. Error was: " << errbuf); | ||
return NULL; | ||
} | ||
|
||
PCPP_LOG_DEBUG("Remote capture string: " << remoteCaptureString); | ||
|
||
pcap_rmtauth* pRmAuth = NULL; | ||
pcap_rmtauth* pRmAuth = nullptr; | ||
pcap_rmtauth rmAuth; | ||
if (remoteAuth != NULL) | ||
if (remoteAuth != nullptr) | ||
{ | ||
PCPP_LOG_DEBUG("Authentication requested. Username: " << remoteAuth->userName << ", Password: " << remoteAuth->password); | ||
rmAuth = remoteAuth->getPcapRmAuth(); | ||
pRmAuth = &rmAuth; | ||
} | ||
|
||
pcap_if_t* interfaceList; | ||
char errorBuf[PCAP_ERRBUF_SIZE]; | ||
if (pcap_findalldevs_ex(remoteCaptureString, pRmAuth, &interfaceList, errorBuf) < 0) | ||
std::unique_ptr<pcap_if_t, internal::PcapFreeAllDevsDeleter> interfaceList; | ||
try | ||
{ | ||
PCPP_LOG_ERROR("Error retrieving device on remote machine. Error string is: " << errorBuf); | ||
return NULL; | ||
interfaceList = internal::getAllRemotePcapDevices(ipAddress, port, pRmAuth); | ||
} | ||
catch (const std::exception& e) | ||
{ | ||
PCPP_LOG_ERROR(e.what()); | ||
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. same here |
||
return nullptr; | ||
} | ||
|
||
PcapRemoteDeviceList* resultList = new PcapRemoteDeviceList(); | ||
resultList->setRemoteMachineIpAddress(ipAddress); | ||
resultList->setRemoteMachinePort(port); | ||
resultList->setRemoteAuthentication(remoteAuth); | ||
|
||
pcap_if_t* currInterface = interfaceList; | ||
while (currInterface != NULL) | ||
|
||
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); | ||
currInterface = currInterface->next; | ||
} | ||
|
||
pcap_freealldevs(interfaceList); | ||
return resultList; | ||
} | ||
|
||
|
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.
These 2 new files contain internal utils, so they shouldn't be exposed as part of the 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.
Where do you want me to put them then?
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'm not a CMake expert, but I think there is a way to decide which header files are "public" (meaning exposed as part of PcapPlusPlus API) and which are "private". This may also affect our doxygen API documentation: https://pcapplusplus.github.io/api-docs/next/ (which currently looks at all the header files in
Pcap++/header
😕 )Maybe @clementperon can help here?
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.
Well, removed the headers from the public header list. (fb685bf)
Things appear to be building fine, and the only difference is that the headers are missing from the include folder of the install command output.
Don't know if anything has changed in the docs generation. The CI action still shows that the headers are being parsed.
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 think that's ok. For doxygen we probably need to exclude these files in
Doxyfile
andDoxyfile-ci
which are in the web-site repo. Here is how to exclude files:https://stackoverflow.com/questions/34776315/doxygen-special-command-to-ignore-a-file
Do you want to open a PR for it or prefer I do it?
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.
Hmm, maybe we can mark the documentation as internal for doxygen in the code?
Something like this: https://stackoverflow.com/a/18443973/11922936
That way the docs won't be generated for the public documentation but it will retain the option for generating internal documentation if we want one in the future.
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.
Enclosed the internal namespace in a conditional doxygen section (617273e). I think this might remove the namespace's contents from the public documentation.
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.
Nice!! You can test it locally or merge this PR and I'll test it later. It's not a blocker for merging because the documentation isn't final for a non-releases and we can fix it later if needed
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.
let's make it in another PR if necessary.