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

Centralize code for fetching pcap devices. #1434

Merged
merged 12 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
Copy link
Owner

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...

Copy link
Collaborator Author

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?

Copy link
Owner

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?

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jun 6, 2024

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.

Copy link
Owner

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 and Doxyfile-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?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Jun 7, 2024

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.

Copy link
Owner

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

Copy link
Collaborator

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.

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

/// @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
}
}
31 changes: 31 additions & 0 deletions Pcap++/header/MemoryUtils.h
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;
};
}
}
50 changes: 50 additions & 0 deletions Pcap++/src/DeviceUtils.cpp
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
}
}
13 changes: 13 additions & 0 deletions Pcap++/src/MemoryUtils.cpp
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); }
}
}
37 changes: 17 additions & 20 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#define LOG_MODULE PcapLogModuleLiveDevice

#include "IpUtils.h"
#include "DeviceUtils.h"
#include "MemoryUtils.h"
#include "PcapLiveDevice.h"
#include "PcapLiveDeviceList.h"
#include "Packet.h"
Expand All @@ -11,7 +13,7 @@
#include <thread>
#include "Logger.h"
#include "SystemUtils.h"
#include <string.h>
#include <cstring>
#include <iostream>
#include <fstream>
#include <chrono>
Expand Down Expand Up @@ -405,32 +407,27 @@ void PcapLiveDevice::close()

PcapLiveDevice* PcapLiveDevice::clone() const
{
PcapLiveDevice* retval = nullptr;

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());
return nullptr;
}

pcap_if_t* currInterface = interfaceList;
while (currInterface != nullptr)
for (pcap_if_t* currInterface = interfaceList.get(); currInterface != nullptr; currInterface = currInterface->next)
{
if(!strcmp(currInterface->name, getName().c_str()))
break;
currInterface = currInterface->next;
if (!std::strcmp(currInterface->name, getName().c_str()))
{
return cloneInternal(*currInterface);
}
}

if(currInterface)
retval = cloneInternal(*currInterface);
else
PCPP_LOG_ERROR("Can't find interface " << getName().c_str());

pcap_freealldevs(interfaceList);
return retval;
PCPP_LOG_ERROR("Can't find interface " << getName().c_str());
return nullptr;
}

PcapLiveDevice* PcapLiveDevice::cloneInternal(pcap_if_t& devInterface) const
Expand Down
22 changes: 11 additions & 11 deletions Pcap++/src/PcapLiveDeviceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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());
Copy link
Collaborator

Choose a reason for hiding this comment

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

recommend to have a meaningful error message 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.

The error messages are actually the same as before. It is just that the DeviceUtil functions have the error messages thrown in a runtime_error as the exception reason and this code catches that and logs the message in the exception.

}

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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()
Expand Down
38 changes: 14 additions & 24 deletions Pcap++/src/PcapRemoteDeviceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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());
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}

Expand Down
Loading