-
Notifications
You must be signed in to change notification settings - Fork 686
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralized duplicated code for fetching device interfaces from libpcap.
- Loading branch information
Showing
6 changed files
with
105 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#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(); | ||
/** | ||
* Fetches a list of all network devices on a remote machine that LibPcap/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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#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); | ||
} | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters