summaryrefslogtreecommitdiffhomepage
path: root/windows/nsis-plugins
diff options
context:
space:
mode:
Diffstat (limited to 'windows/nsis-plugins')
-rw-r--r--windows/nsis-plugins/src/driverlogic/context.cpp17
-rw-r--r--windows/nsis-plugins/src/driverlogic/context.h9
-rw-r--r--windows/nsis-plugins/src/driverlogic/driverlogic.cpp158
-rw-r--r--windows/nsis-plugins/src/driverlogic/driverlogic.def2
4 files changed, 149 insertions, 37 deletions
diff --git a/windows/nsis-plugins/src/driverlogic/context.cpp b/windows/nsis-plugins/src/driverlogic/context.cpp
index 178cc337d1..a01fd29bac 100644
--- a/windows/nsis-plugins/src/driverlogic/context.cpp
+++ b/windows/nsis-plugins/src/driverlogic/context.cpp
@@ -122,6 +122,11 @@ std::wstring DoubleBackslashes(const std::wstring &str)
} // anonymous namespace
+Context::Context()
+ : m_connection(wmi::Connection::Namespace::Cimv2)
+{
+}
+
Context::BaselineStatus Context::establishBaseline(const std::wstring &textBlock)
{
m_baseline = ParseVirtualNics(textBlock);
@@ -167,7 +172,6 @@ Context::VirtualNic Context::getNewAdapter()
return added[0];
}
-//static
std::set<Context::VirtualNic> Context::ParseVirtualNics(const std::wstring &textBlock)
{
// ROOT\NET\0000
@@ -204,13 +208,8 @@ std::set<Context::VirtualNic> Context::ParseVirtualNics(const std::wstring &text
return nics;
}
-//static
std::wstring Context::GetNicAlias(const std::wstring &node, const std::wstring &name)
{
- static wmi::Connection connection(wmi::Connection::Namespace::Cimv2);
-
- std::wstringstream ss;
-
//
// The name cannot be used when querying WMI, because WMI sometimes normalizes the
// names in its dataset, thereby destroying their uniqueness.
@@ -224,14 +223,16 @@ std::wstring Context::GetNicAlias(const std::wstring &node, const std::wstring &
const auto formattedNode = DoubleBackslashes(node);
+ std::wstringstream ss;
+
ss << L"SELECT * FROM Win32_NetworkAdapter WHERE PNPDeviceID = \"" << formattedNode << L"\"";
- auto resultset = connection.query(ss.str().c_str());
+ auto resultset = m_connection.query(ss.str().c_str());
if (false == resultset.advance())
{
PluginLog(std::wstring(L"WMI query failed for adapter: ").append(name));
- LogAllAdapters(connection);
+ LogAllAdapters(m_connection);
throw std::runtime_error("Unable to look up virtual adapter using WMI");
}
diff --git a/windows/nsis-plugins/src/driverlogic/context.h b/windows/nsis-plugins/src/driverlogic/context.h
index b9d34f68d5..1340d561fe 100644
--- a/windows/nsis-plugins/src/driverlogic/context.h
+++ b/windows/nsis-plugins/src/driverlogic/context.h
@@ -1,5 +1,6 @@
#pragma once
+#include <libcommon/wmi/connection.h>
#include <set>
#include <string>
@@ -7,6 +8,8 @@ class Context
{
public:
+ Context();
+
struct VirtualNic
{
std::wstring node;
@@ -43,8 +46,10 @@ public:
private:
- static std::set<VirtualNic> ParseVirtualNics(const std::wstring &textBlock);
- static std::wstring GetNicAlias(const std::wstring &node, const std::wstring &name);
+ common::wmi::Connection m_connection;
+
+ std::set<VirtualNic> ParseVirtualNics(const std::wstring &textBlock);
+ std::wstring GetNicAlias(const std::wstring &node, const std::wstring &name);
std::set<VirtualNic> m_baseline;
std::set<VirtualNic> m_currentState;
diff --git a/windows/nsis-plugins/src/driverlogic/driverlogic.cpp b/windows/nsis-plugins/src/driverlogic/driverlogic.cpp
index 4a8911572c..2aedaf41ee 100644
--- a/windows/nsis-plugins/src/driverlogic/driverlogic.cpp
+++ b/windows/nsis-plugins/src/driverlogic/driverlogic.cpp
@@ -1,12 +1,19 @@
#include <stdafx.h>
#include "context.h"
#include <libcommon/string.h>
+#include <libcommon/valuemapper.h>
#include <windows.h>
+
+// Suppress warnings caused by broken legacy code
+#pragma warning (push)
+#pragma warning (disable: 4005)
#include <nsis/pluginapi.h>
+#pragma warning (pop)
+
#include <string>
#include <vector>
-Context g_context;
+Context *g_context = nullptr;
namespace
{
@@ -66,6 +73,53 @@ void PinDll()
} // anonymous namespace
//
+// Initialize
+//
+// Call this function once during startup.
+//
+enum class InitializeStatus
+{
+ GENERAL_ERROR = 0,
+ SUCCESS,
+};
+
+void __declspec(dllexport) NSISCALL Initialize
+(
+ HWND hwndParent,
+ int string_size,
+ LPTSTR variables,
+ stack_t **stacktop,
+ extra_parameters *extra,
+ ...
+)
+{
+ EXDLL_INIT();
+
+ try
+ {
+ if (nullptr == g_context)
+ {
+ g_context = new Context;
+
+ PinDll();
+ }
+
+ pushstring(L"");
+ pushint(InitializeStatus::SUCCESS);
+ }
+ catch (std::exception &err)
+ {
+ pushstring(common::string::ToWide(err.what()).c_str());
+ pushint(InitializeStatus::GENERAL_ERROR);
+ }
+ catch (...)
+ {
+ pushstring(L"Unspecified error");
+ pushint(InitializeStatus::GENERAL_ERROR);
+ }
+}
+
+//
// EstablishBaseline
//
// Invoke with the output from "tapinstall hwids tap0901"
@@ -91,34 +145,24 @@ void __declspec(dllexport) NSISCALL EstablishBaseline
{
EXDLL_INIT();
- try
+ if (nullptr == g_context)
{
- PinDll();
+ pushstring(L"Initialize() function was not called or was not successful");
+ pushint(EstablishBaselineStatus::GENERAL_ERROR);
+ }
- auto status = EstablishBaselineStatus::GENERAL_ERROR;
+ try
+ {
+ using value_type = common::ValueMapper<Context::BaselineStatus, EstablishBaselineStatus>::value_type;
- switch (g_context.establishBaseline(PopString()))
+ const common::ValueMapper<Context::BaselineStatus, EstablishBaselineStatus> mapper =
{
- case Context::BaselineStatus::NO_INTERFACES_PRESENT:
- {
- status = EstablishBaselineStatus::NO_INTERFACES_PRESENT;
- break;
- }
- case Context::BaselineStatus::SOME_INTERFACES_PRESENT:
- {
- status = EstablishBaselineStatus::SOME_INTERFACES_PRESENT;
- break;
- }
- case Context::BaselineStatus::MULLVAD_INTERFACE_PRESENT:
- {
- status = EstablishBaselineStatus::MULLVAD_INTERFACE_PRESENT;
- break;
- }
- default:
- {
- throw std::runtime_error("Missing case handler in switch clause");
- }
- }
+ value_type(Context::BaselineStatus::NO_INTERFACES_PRESENT, EstablishBaselineStatus::NO_INTERFACES_PRESENT),
+ value_type(Context::BaselineStatus::SOME_INTERFACES_PRESENT, EstablishBaselineStatus::SOME_INTERFACES_PRESENT),
+ value_type(Context::BaselineStatus::MULLVAD_INTERFACE_PRESENT, EstablishBaselineStatus::MULLVAD_INTERFACE_PRESENT)
+ };
+
+ const auto status = mapper.map(g_context->establishBaseline(PopString()));
pushstring(L"");
pushint(status);
@@ -128,6 +172,11 @@ void __declspec(dllexport) NSISCALL EstablishBaseline
pushstring(common::string::ToWide(err.what()).c_str());
pushint(EstablishBaselineStatus::GENERAL_ERROR);
}
+ catch (...)
+ {
+ pushstring(L"Unspecified error");
+ pushint(EstablishBaselineStatus::GENERAL_ERROR);
+ }
}
//
@@ -154,11 +203,17 @@ void __declspec(dllexport) NSISCALL IdentifyNewInterface
{
EXDLL_INIT();
+ if (nullptr == g_context)
+ {
+ pushstring(L"Initialize() function was not called or was not successful");
+ pushint(EstablishBaselineStatus::GENERAL_ERROR);
+ }
+
try
{
- g_context.recordCurrentState(PopString());
+ g_context->recordCurrentState(PopString());
- auto nic = g_context.getNewAdapter();
+ auto nic = g_context->getNewAdapter();
pushstring(nic.alias.c_str());
pushint(IdentifyNewInterfaceStatus::SUCCESS);
@@ -168,4 +223,53 @@ void __declspec(dllexport) NSISCALL IdentifyNewInterface
pushstring(common::string::ToWide(err.what()).c_str());
pushint(IdentifyNewInterfaceStatus::GENERAL_ERROR);
}
+ catch (...)
+ {
+ pushstring(L"Unspecified error");
+ pushint(IdentifyNewInterfaceStatus::GENERAL_ERROR);
+ }
+}
+
+//
+// Deinitialize
+//
+// Call this function once during shutdown.
+//
+enum class DeinitializeStatus
+{
+ GENERAL_ERROR = 0,
+ SUCCESS,
+};
+
+void __declspec(dllexport) NSISCALL Deinitialize
+(
+ HWND hwndParent,
+ int string_size,
+ LPTSTR variables,
+ stack_t **stacktop,
+ extra_parameters *extra,
+ ...
+)
+{
+ EXDLL_INIT();
+
+ try
+ {
+ delete g_context;
+
+ pushstring(L"");
+ pushint(InitializeStatus::SUCCESS);
+ }
+ catch (std::exception &err)
+ {
+ pushstring(common::string::ToWide(err.what()).c_str());
+ pushint(DeinitializeStatus::GENERAL_ERROR);
+ }
+ catch (...)
+ {
+ pushstring(L"Unspecified error");
+ pushint(DeinitializeStatus::GENERAL_ERROR);
+ }
+
+ g_context = nullptr;
}
diff --git a/windows/nsis-plugins/src/driverlogic/driverlogic.def b/windows/nsis-plugins/src/driverlogic/driverlogic.def
index f65de5b6a7..3bd78e1eea 100644
--- a/windows/nsis-plugins/src/driverlogic/driverlogic.def
+++ b/windows/nsis-plugins/src/driverlogic/driverlogic.def
@@ -2,5 +2,7 @@ LIBRARY driverlogic
EXPORTS
+Initialize
EstablishBaseline
IdentifyNewInterface
+Deinitialize