summaryrefslogtreecommitdiffhomepage
path: root/windows
diff options
context:
space:
mode:
authorOdd Stranne <odd@mullvad.net>2018-10-03 09:12:18 +0200
committerOdd Stranne <odd@mullvad.net>2018-10-03 12:51:43 +0200
commit42084afb86d61ef476a8beb5887ec92d43b3f87c (patch)
tree0d74f68d3e5f68b905a815673aeae666fd439f02 /windows
parente61f0cf6fb0f29bda264d13dab44be8ff482c66a (diff)
downloadmullvadvpn-42084afb86d61ef476a8beb5887ec92d43b3f87c.tar.xz
mullvadvpn-42084afb86d61ef476a8beb5887ec92d43b3f87c.zip
Identify network interface based on node
Diffstat (limited to 'windows')
-rw-r--r--windows/nsis-plugins/src/driverlogic/context.cpp37
-rw-r--r--windows/nsis-plugins/src/driverlogic/context.h2
2 files changed, 35 insertions, 4 deletions
diff --git a/windows/nsis-plugins/src/driverlogic/context.cpp b/windows/nsis-plugins/src/driverlogic/context.cpp
index 9c003b440c..178cc337d1 100644
--- a/windows/nsis-plugins/src/driverlogic/context.cpp
+++ b/windows/nsis-plugins/src/driverlogic/context.cpp
@@ -102,6 +102,24 @@ void LogAllAdapters(wmi::Connection &connection)
PluginLogWithDetails(L"Adapters known to WMI", details);
}
+std::wstring DoubleBackslashes(const std::wstring &str)
+{
+ auto result(str);
+
+ size_t offset = 0;
+
+ for (size_t index = 0; index < str.size(); ++index)
+ {
+ if (L'\\' == str[index])
+ {
+ result.insert(index + offset, 1, L'\\');
+ ++offset;
+ }
+ }
+
+ return result;
+}
+
} // anonymous namespace
Context::BaselineStatus Context::establishBaseline(const std::wstring &textBlock)
@@ -177,7 +195,7 @@ std::set<Context::VirtualNic> Context::ParseVirtualNics(const std::wstring &text
nic.node = text.at(line);
nic.name = std::wstring(nameDelimiter + 2);
- nic.alias = GetNicAlias(nic.name);
+ nic.alias = GetNicAlias(nic.node, nic.name);
nics.emplace(std::move(nic));
line += 4;
@@ -187,13 +205,26 @@ std::set<Context::VirtualNic> Context::ParseVirtualNics(const std::wstring &text
}
//static
-std::wstring Context::GetNicAlias(const std::wstring &name)
+std::wstring Context::GetNicAlias(const std::wstring &node, const std::wstring &name)
{
static wmi::Connection connection(wmi::Connection::Namespace::Cimv2);
std::wstringstream ss;
- ss << L"SELECT * from Win32_NetworkAdapter WHERE Name = \"" << name << L"\"";
+ //
+ // The name cannot be used when querying WMI, because WMI sometimes normalizes the
+ // names in its dataset, thereby destroying their uniqueness.
+ //
+ // E.g. if a network interface has a name of "TAP-Windows Adapter V9 #2" it will
+ // sometimes be reported by WMI as "TAP-Windows Adapter V9".
+ //
+ // Also, the node string cannot be used as-is. We have to double the backslashes in it
+ // or the string will be rejected by WMI.
+ //
+
+ const auto formattedNode = DoubleBackslashes(node);
+
+ ss << L"SELECT * FROM Win32_NetworkAdapter WHERE PNPDeviceID = \"" << formattedNode << L"\"";
auto resultset = connection.query(ss.str().c_str());
diff --git a/windows/nsis-plugins/src/driverlogic/context.h b/windows/nsis-plugins/src/driverlogic/context.h
index 53d2ddeedc..b9d34f68d5 100644
--- a/windows/nsis-plugins/src/driverlogic/context.h
+++ b/windows/nsis-plugins/src/driverlogic/context.h
@@ -44,7 +44,7 @@ public:
private:
static std::set<VirtualNic> ParseVirtualNics(const std::wstring &textBlock);
- static std::wstring GetNicAlias(const std::wstring &name);
+ static std::wstring GetNicAlias(const std::wstring &node, const std::wstring &name);
std::set<VirtualNic> m_baseline;
std::set<VirtualNic> m_currentState;