turn PCAPDLL into a singleton

pull/25/head
Jeffrey Quesnelle 2020-03-24 13:15:26 -04:00
parent 38e24d7641
commit 99879c9021
4 changed files with 32 additions and 23 deletions

View File

@ -12,8 +12,7 @@ namespace icsneo {
class PCAPDLL { class PCAPDLL {
public: public:
// The first time we use the DLL we keep it in here and it won't get freed until the user unloads us (for speed reasons) // The first time we use the DLL we keep it in here and it won't get freed until the user unloads us (for speed reasons)
static std::shared_ptr<PCAPDLL> lazyLoadHolder; static const PCAPDLL& getInstance();
static bool lazyLoaded;
// Functions // Functions
typedef int(__cdecl* PCAPFINDDEVICE)(char* source, struct pcap_rmtauth* auth, pcap_if_t** alldevs, char* errbuf); typedef int(__cdecl* PCAPFINDDEVICE)(char* source, struct pcap_rmtauth* auth, pcap_if_t** alldevs, char* errbuf);
@ -45,14 +44,14 @@ public:
PCAPCREATESRCSTR createsrcstr; PCAPCREATESRCSTR createsrcstr;
PCAPSETBUFF setbuff; PCAPSETBUFF setbuff;
PCAPDLL();
~PCAPDLL() { closeDLL(); } ~PCAPDLL();
bool ok() const { return dll != nullptr; } bool ok() const;
private: private:
PCAPDLL();
HINSTANCE dll; HINSTANCE dll;
void closeDLL(); void closeDLL();
}; };
} }
#endif #endif

View File

@ -26,7 +26,7 @@ public:
bool isOpen(); bool isOpen();
bool close(); bool close();
private: private:
PCAPDLL pcap; const PCAPDLL& pcap;
char errbuf[PCAP_ERRBUF_SIZE] = { 0 }; char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
neodevice_t& device; neodevice_t& device;
uint8_t deviceMAC[6]; uint8_t deviceMAC[6];

View File

@ -2,15 +2,31 @@
using namespace icsneo; using namespace icsneo;
std::shared_ptr<PCAPDLL> PCAPDLL::lazyLoadHolder; const PCAPDLL& PCAPDLL::getInstance()
bool PCAPDLL::lazyLoaded = false; {
static PCAPDLL instance;
PCAPDLL::PCAPDLL() { return instance;
if(!lazyLoaded) {
lazyLoaded = true;
lazyLoadHolder = std::make_shared<PCAPDLL>();
} }
PCAPDLL::~PCAPDLL()
{
closeDLL();
}
void PCAPDLL::closeDLL()
{
if (dll)
FreeLibrary(dll);
dll = nullptr;
}
bool PCAPDLL::ok() const
{
return dll != nullptr;
}
PCAPDLL::PCAPDLL()
{
dll = LoadLibrary("wpcap.dll"); dll = LoadLibrary("wpcap.dll");
if(dll == NULL) { if(dll == NULL) {
@ -43,9 +59,3 @@ PCAPDLL::PCAPDLL() {
} }
} }
} }
void PCAPDLL::closeDLL() {
if(dll)
FreeLibrary(dll);
dll = nullptr;
}

View File

@ -18,7 +18,7 @@ std::vector<PCAP::NetworkInterface> PCAP::knownInterfaces;
std::vector<PCAP::PCAPFoundDevice> PCAP::FindAll() { std::vector<PCAP::PCAPFoundDevice> PCAP::FindAll() {
std::vector<PCAPFoundDevice> foundDevices; std::vector<PCAPFoundDevice> foundDevices;
PCAPDLL pcap; const PCAPDLL& pcap = PCAPDLL::getInstance();
if(!pcap.ok()) { if(!pcap.ok()) {
EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotStart, APIEvent::Severity::Error); EventManager::GetInstance().add(APIEvent::Type::PCAPCouldNotStart, APIEvent::Severity::Error);
return std::vector<PCAPFoundDevice>(); return std::vector<PCAPFoundDevice>();
@ -177,7 +177,7 @@ bool PCAP::IsHandleValid(neodevice_handle_t handle) {
return (netifIndex < knownInterfaces.size()); return (netifIndex < knownInterfaces.size());
} }
PCAP::PCAP(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice) { PCAP::PCAP(const device_eventhandler_t& err, neodevice_t& forDevice) : Driver(err), device(forDevice), pcap(PCAPDLL::getInstance()) {
if(IsHandleValid(device.handle)) { if(IsHandleValid(device.handle)) {
interface = knownInterfaces[(device.handle >> 24) & 0xFF]; interface = knownInterfaces[(device.handle >> 24) & 0xFF];
interface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case. interface.fp = nullptr; // We're going to open our own connection to the interface. This should already be nullptr but just in case.