Skip to content

Commit

Permalink
Use smart pointers for RemoteAuthentication in PcapRemoteDeviceList. (#…
Browse files Browse the repository at this point in the history
…1441)

* Updated copy/move constructors to cpp11 standard.

* Changed shared remote authentication structure to be managed by a shared pointer.

* Changed null to nullptr.
  • Loading branch information
Dimi1010 authored Jun 13, 2024
1 parent 78ef4e6 commit 60032cd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 30 deletions.
15 changes: 8 additions & 7 deletions Pcap++/header/PcapRemoteDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if defined(_WIN32)

#include <vector>
#include <memory>
#include "PcapLiveDevice.h"


Expand Down Expand Up @@ -81,22 +82,22 @@ namespace pcpp
private:
IPAddress m_RemoteMachineIpAddress;
uint16_t m_RemoteMachinePort;
PcapRemoteAuthentication* m_RemoteAuthentication;
std::shared_ptr<PcapRemoteAuthentication> m_RemoteAuthentication;

// c'tor is private, as only PcapRemoteDeviceList should create instances of it, and it'll create only one for every remote interface
PcapRemoteDevice(pcap_if_t* iface, PcapRemoteAuthentication* remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort);

// private copy c'tor
PcapRemoteDevice( const PcapRemoteDevice& other );
// private assignment operator
PcapRemoteDevice& operator=(const PcapRemoteDevice& other);
PcapRemoteDevice(pcap_if_t* iface, std::shared_ptr<PcapRemoteAuthentication> remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort);

static void* remoteDeviceCaptureThreadMain(void *ptr);

//overridden methods
ThreadStart getCaptureThreadStart();

public:
PcapRemoteDevice(const PcapRemoteDevice&) = delete;
PcapRemoteDevice(PcapRemoteDevice&&) noexcept = delete;
PcapRemoteDevice& operator=(const PcapRemoteDevice&) = delete;
PcapRemoteDevice& operator=(PcapRemoteDevice&&) noexcept = delete;

virtual ~PcapRemoteDevice() {}

/**
Expand Down
13 changes: 8 additions & 5 deletions Pcap++/header/PcapRemoteDeviceList.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#if defined(_WIN32)

#include <memory>
#include "IpAddress.h"
#include "PcapRemoteDevice.h"

Expand Down Expand Up @@ -30,13 +31,10 @@ namespace pcpp
std::vector<PcapRemoteDevice*> m_RemoteDeviceList;
IPAddress m_RemoteMachineIpAddress;
uint16_t m_RemoteMachinePort;
PcapRemoteAuthentication* m_RemoteAuthentication;
std::shared_ptr<PcapRemoteAuthentication> m_RemoteAuthentication;

// private c'tor. User should create the list via static methods PcapRemoteDeviceList::getRemoteDeviceList()
PcapRemoteDeviceList() : m_RemoteMachinePort(0), m_RemoteAuthentication(NULL) {}
// private copy c'tor
PcapRemoteDeviceList(const PcapRemoteDeviceList& other);
PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList& other);
PcapRemoteDeviceList() : m_RemoteMachinePort(0), m_RemoteAuthentication(nullptr) {}

void setRemoteMachineIpAddress(const IPAddress& ipAddress);
void setRemoteMachinePort(uint16_t port);
Expand All @@ -53,6 +51,11 @@ namespace pcpp
*/
typedef typename std::vector<PcapRemoteDevice*>::const_iterator ConstRemoteDeviceListIterator;

PcapRemoteDeviceList(const PcapRemoteDeviceList&) = delete;
PcapRemoteDeviceList(PcapRemoteDeviceList&&) noexcept = delete;
PcapRemoteDeviceList& operator=(const PcapRemoteDeviceList&) = delete;
PcapRemoteDeviceList& operator=(PcapRemoteDeviceList&&) noexcept = delete;

~PcapRemoteDeviceList();

/**
Expand Down
16 changes: 8 additions & 8 deletions Pcap++/src/PcapRemoteDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ pcap_rmtauth PcapRemoteAuthentication::getPcapRmAuth() const
return result;
}

PcapRemoteDevice::PcapRemoteDevice(pcap_if_t* iface, PcapRemoteAuthentication* remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort)
PcapRemoteDevice::PcapRemoteDevice(pcap_if_t* iface, std::shared_ptr<PcapRemoteAuthentication> remoteAuthentication, const IPAddress& remoteMachineIP, uint16_t remoteMachinePort)
: PcapLiveDevice(iface, false, false, false)
, m_RemoteMachineIpAddress(remoteMachineIP)
, m_RemoteMachinePort(remoteMachinePort)
, m_RemoteAuthentication(std::move(remoteAuthentication))
{
PCPP_LOG_DEBUG("MTU calculation isn't supported for remote devices. Setting MTU to 1514");
m_DeviceMtu = 1514;
m_RemoteMachineIpAddress = remoteMachineIP;
m_RemoteMachinePort = remoteMachinePort;
m_RemoteAuthentication = remoteAuthentication;
}


Expand All @@ -35,16 +35,16 @@ bool PcapRemoteDevice::open()
char errbuf[PCAP_ERRBUF_SIZE];
int flags = PCAP_OPENFLAG_PROMISCUOUS | PCAP_OPENFLAG_NOCAPTURE_RPCAP; //PCAP_OPENFLAG_DATATX_UDP doesn't always work
PCPP_LOG_DEBUG("Opening device '" << m_Name << "'");
pcap_rmtauth* pRmAuth = NULL;
pcap_rmtauth* pRmAuth = nullptr;
pcap_rmtauth rmAuth;
if (m_RemoteAuthentication != NULL)
if (m_RemoteAuthentication != nullptr)
{
rmAuth = m_RemoteAuthentication->getPcapRmAuth();
pRmAuth = &rmAuth;
}

m_PcapDescriptor = pcap_open(m_Name.c_str(), PCPP_MAX_PACKET_SIZE, flags, 250, pRmAuth, errbuf);
if (m_PcapDescriptor == NULL)
if (m_PcapDescriptor == nullptr)
{
PCPP_LOG_ERROR("Error opening device. Error was: " << errbuf);
m_DeviceOpened = false;
Expand Down Expand Up @@ -73,7 +73,7 @@ bool PcapRemoteDevice::open()
void* PcapRemoteDevice::remoteDeviceCaptureThreadMain(void *ptr)
{
PcapRemoteDevice* pThis = (PcapRemoteDevice*)ptr;
if (pThis == NULL)
if (pThis == nullptr)
{
PCPP_LOG_ERROR("Capture thread: Unable to extract PcapLiveDevice instance");
return 0;
Expand Down
13 changes: 3 additions & 10 deletions Pcap++/src/PcapRemoteDeviceList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,11 @@ void PcapRemoteDeviceList::setRemoteMachinePort(uint16_t port)

void PcapRemoteDeviceList::setRemoteAuthentication(const PcapRemoteAuthentication* remoteAuth)
{
if (remoteAuth != NULL)
m_RemoteAuthentication = new PcapRemoteAuthentication(*remoteAuth);
if (remoteAuth != nullptr)
m_RemoteAuthentication = std::shared_ptr<PcapRemoteAuthentication>(new PcapRemoteAuthentication(*remoteAuth));
else
{
if (m_RemoteAuthentication != NULL)
delete m_RemoteAuthentication;
m_RemoteAuthentication = NULL;
m_RemoteAuthentication = nullptr;
}
}

Expand All @@ -220,11 +218,6 @@ PcapRemoteDeviceList::~PcapRemoteDeviceList()
delete (*devIter);
m_RemoteDeviceList.erase(devIter);
}

if (m_RemoteAuthentication != NULL)
{
delete m_RemoteAuthentication;
}
}

} // namespace pcpp
Expand Down

0 comments on commit 60032cd

Please sign in to comment.