-
Notifications
You must be signed in to change notification settings - Fork 673
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralize code for fetching pcap devices. (#1434)
* Added device utilities header for pcap device utility functions. Added memory utilities header for smart pointer utilities. * Replaced usages of pcap_findalldevs(_ex) with internal::getAll(Local/Remote)PcapDevices. * Removed code creating pcap_capture string as it is unused. * Fixed returning nullptr on clone fail. * Renamed MemoryUtils to PcapUtils. * Moved getAllRemotePcapDevices to anonymous namespace in PcapRemoteDeviceList.cpp * Removed duplicate of PcapCloseDeleter. * Remoted internal utilitiy headers from the public header list. * Removed unused forward declare. * Added doxygen conditionals to exclude the internal classes from the public documentation.
- Loading branch information
Showing
9 changed files
with
178 additions
and
65 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,24 @@ | ||
#pragma once | ||
|
||
/// @file | ||
|
||
#include <memory> | ||
#include "IpAddress.h" | ||
#include "PcapUtils.h" | ||
|
||
namespace pcpp | ||
{ | ||
/// @cond PCPP_INTERNAL | ||
|
||
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(); | ||
} | ||
|
||
/// @endcond | ||
} |
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,35 @@ | ||
#pragma once | ||
|
||
// Forward declarations | ||
struct pcap; | ||
typedef pcap pcap_t; | ||
struct pcap_if; | ||
typedef pcap_if pcap_if_t; | ||
|
||
namespace pcpp | ||
{ | ||
/// @cond PCPP_INTERNAL | ||
|
||
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; | ||
}; | ||
} | ||
|
||
/// @endcond | ||
} |
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,27 @@ | ||
#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); | ||
} | ||
} | ||
} |
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
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,13 @@ | ||
#include "PcapUtils.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); } | ||
} | ||
} |