summaryrefslogtreecommitdiffhomepage
path: root/windows
diff options
context:
space:
mode:
authorOdd Stranne <odd@mullvad.net>2018-07-25 11:42:39 +0200
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2018-09-11 12:52:20 -0300
commit5c01cc7095364c12fa305589ea8c4f81c8458bc2 (patch)
tree4fdd309f926833e27f7de881d7c60f754d052d61 /windows
parentb3f21d87471d589b3ce8b1de08373b98ffe5cb09 (diff)
downloadmullvadvpn-5c01cc7095364c12fa305589ea8c4f81c8458bc2.tar.xz
mullvadvpn-5c01cc7095364c12fa305589ea8c4f81c8458bc2.zip
Add GetTapInterfaceIpv6Status function
Diffstat (limited to 'windows')
-rw-r--r--windows/winroute/src/winroute/NetworkInterfaces.cpp52
-rw-r--r--windows/winroute/src/winroute/NetworkInterfaces.h4
-rw-r--r--windows/winroute/src/winroute/winroute.cpp45
-rw-r--r--windows/winroute/src/winroute/winroute.def3
-rw-r--r--windows/winroute/src/winroute/winroute.h20
5 files changed, 110 insertions, 14 deletions
diff --git a/windows/winroute/src/winroute/NetworkInterfaces.cpp b/windows/winroute/src/winroute/NetworkInterfaces.cpp
index a0a4d53db8..2aa352de27 100644
--- a/windows/winroute/src/winroute/NetworkInterfaces.cpp
+++ b/windows/winroute/src/winroute/NetworkInterfaces.cpp
@@ -74,19 +74,7 @@ NetworkInterfaces::NetworkInterfaces()
bool NetworkInterfaces::SetTopMetricForInterfacesByAlias(const wchar_t * deviceAlias)
{
- NET_LUID targetIfaceLuid;
- DWORD success = 0;
- success = ConvertInterfaceAliasToLuid(deviceAlias, &targetIfaceLuid);
- if (success != NO_ERROR)
- {
- std::wstringstream ss;
- ss << L"Failed to convert interface alias '"
- << deviceAlias
- << "' into LUID: "
- << success;
- throw std::runtime_error(common::string::ToAnsi(ss.str()));
- }
- return SetTopMetricForInterfacesWithLuid(targetIfaceLuid);
+ return SetTopMetricForInterfacesWithLuid(GetInterfaceLuid(deviceAlias));
}
bool NetworkInterfaces::SetTopMetricForInterfacesWithLuid(NET_LUID targetIfaceId)
@@ -107,3 +95,41 @@ NetworkInterfaces::~NetworkInterfaces()
{
FreeMibTable(mInterfaces);
}
+
+//static
+NET_LUID NetworkInterfaces::GetInterfaceLuid(const std::wstring &interfaceAlias)
+{
+ NET_LUID interfaceLuid;
+
+ const auto status = ConvertInterfaceAliasToLuid(interfaceAlias.c_str(), &interfaceLuid);
+
+ if (status != NO_ERROR)
+ {
+ std::wstringstream ss;
+
+ ss << L"Failed to convert interface alias '"
+ << interfaceAlias
+ << "' into LUID. Error: "
+ << status;
+
+ throw std::runtime_error(common::string::ToAnsi(ss.str()));
+ }
+
+ return interfaceLuid;
+}
+
+const MIB_IPINTERFACE_ROW *NetworkInterfaces::GetInterface(NET_LUID interfaceLuid, ADDRESS_FAMILY interfaceFamily)
+{
+ for (unsigned int i = 0; i < mInterfaces->NumEntries; ++i)
+ {
+ MIB_IPINTERFACE_ROW &candidateInterface = mInterfaces->Table[i];
+
+ if (candidateInterface.InterfaceLuid.Value == interfaceLuid.Value
+ && candidateInterface.Family == interfaceFamily)
+ {
+ return &candidateInterface;
+ }
+ }
+
+ return nullptr;
+}
diff --git a/windows/winroute/src/winroute/NetworkInterfaces.h b/windows/winroute/src/winroute/NetworkInterfaces.h
index b29ce0fa44..22ff4b79d0 100644
--- a/windows/winroute/src/winroute/NetworkInterfaces.h
+++ b/windows/winroute/src/winroute/NetworkInterfaces.h
@@ -6,6 +6,7 @@
#include <iphlpapi.h>
#include <netioapi.h>
#include <cstdint>
+#include <string>
class NetworkInterfaces
{
@@ -20,6 +21,9 @@ public:
bool SetTopMetricForInterfacesByAlias(const wchar_t *deviceAlias);
bool SetTopMetricForInterfacesWithLuid(NET_LUID targetIface);
~NetworkInterfaces();
+
+ static NET_LUID GetInterfaceLuid(const std::wstring &interfaceAlias);
+ const MIB_IPINTERFACE_ROW *GetInterface(NET_LUID interfaceLuid, ADDRESS_FAMILY interfaceFamily);
};
const static uint32_t MAX_METRIC = 1;
diff --git a/windows/winroute/src/winroute/winroute.cpp b/windows/winroute/src/winroute/winroute.cpp
index c417bd44ea..b9f10e3786 100644
--- a/windows/winroute/src/winroute/winroute.cpp
+++ b/windows/winroute/src/winroute/winroute.cpp
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "winroute.h"
#include "NetworkInterfaces.h"
+#include "libcommon/error.h"
#include <cstdint>
#include <stdexcept>
@@ -35,3 +36,47 @@ WinRoute_EnsureTopMetric(
}
};
+extern "C"
+WINROUTE_LINKAGE
+TAP_IPV6_STATUS
+WINROUTE_API
+GetTapInterfaceIpv6Status(
+ WinRouteErrorSink errorSink,
+ void* errorSinkContext
+)
+{
+ try
+ {
+ MIB_IPINTERFACE_ROW interface = { 0 };
+
+ interface.InterfaceLuid = NetworkInterfaces::GetInterfaceLuid(L"Mullvad");
+ interface.Family = AF_INET6;
+
+ const auto status = GetIpInterfaceEntry(&interface);
+
+ if (NO_ERROR == status)
+ {
+ return TAP_IPV6_STATUS::ENABLED;
+ }
+
+ if (ERROR_NOT_FOUND == status)
+ {
+ return TAP_IPV6_STATUS::DISABLED;
+ }
+
+ common::error::Throw("Resolve TAP IPv6 interface", status);
+ }
+ catch (std::exception &err)
+ {
+ if (nullptr != errorSink)
+ {
+ errorSink(err.what(), errorSinkContext);
+ }
+
+ return TAP_IPV6_STATUS::FAILURE;
+ }
+ catch (...)
+ {
+ return TAP_IPV6_STATUS::FAILURE;
+ }
+}
diff --git a/windows/winroute/src/winroute/winroute.def b/windows/winroute/src/winroute/winroute.def
index d409889c50..2ea9222482 100644
--- a/windows/winroute/src/winroute/winroute.def
+++ b/windows/winroute/src/winroute/winroute.def
@@ -1,3 +1,4 @@
LIBRARY winroute
EXPORTS
- WinRoute_EnsureTopMetric \ No newline at end of file
+ WinRoute_EnsureTopMetric
+ GetTapInterfaceIpv6Status
diff --git a/windows/winroute/src/winroute/winroute.h b/windows/winroute/src/winroute/winroute.h
index 6bf0c3b882..6b500cee7e 100644
--- a/windows/winroute/src/winroute/winroute.h
+++ b/windows/winroute/src/winroute/winroute.h
@@ -28,3 +28,23 @@ WinRoute_EnsureTopMetric(
WinRouteErrorSink errorSink,
void* errorSinkContext
);
+
+enum class TAP_IPV6_STATUS : uint32_t
+{
+ ENABLED = 0,
+ DISABLED = 1,
+ FAILURE = 2,
+};
+
+//
+// This has nothing to do with routing.
+// We should probably rename this module and use it to gather one-off network functions.
+//
+extern "C"
+WINROUTE_LINKAGE
+TAP_IPV6_STATUS
+WINROUTE_API
+GetTapInterfaceIpv6Status(
+ WinRouteErrorSink errorSink,
+ void* errorSinkContext
+);