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

Use smart pointers for RemoteAuthentication in PcapRemoteDeviceList. #1441

Merged
merged 5 commits into from
Jun 13, 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
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
8 changes: 4 additions & 4 deletions Pcap++/src/PcapRemoteDevice.cpp
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
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)
tigercosmos marked this conversation as resolved.
Show resolved Hide resolved
, 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 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
Loading