diff options
| author | David Lönnhager <david.l@mullvad.net> | 2022-06-22 18:01:23 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2022-07-05 13:31:52 +0200 |
| commit | 055e4c33723590038ef44bb9d5dc8be0e9f79f7d (patch) | |
| tree | ded2abf9601282d0af6b693e12e71a75554ff01b /windows | |
| parent | 6f08ea5d4b222818d7861044eb7559cf7ca185d3 (diff) | |
| download | mullvadvpn-055e4c33723590038ef44bb9d5dc8be0e9f79f7d.tar.xz mullvadvpn-055e4c33723590038ef44bb9d5dc8be0e9f79f7d.zip | |
Move tunnel interface metric update to talpid-core
Diffstat (limited to 'windows')
| -rw-r--r-- | windows/winnet/src/winnet/InterfacePair.cpp | 111 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/InterfacePair.h | 27 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/NetworkInterfaces.cpp | 89 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/NetworkInterfaces.h | 31 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.cpp | 33 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.def | 1 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.h | 17 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.vcxproj | 4 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.vcxproj.filters | 4 |
9 files changed, 0 insertions, 317 deletions
diff --git a/windows/winnet/src/winnet/InterfacePair.cpp b/windows/winnet/src/winnet/InterfacePair.cpp deleted file mode 100644 index 935bc557aa..0000000000 --- a/windows/winnet/src/winnet/InterfacePair.cpp +++ /dev/null @@ -1,111 +0,0 @@ -#include "stdafx.h" -#include "InterfacePair.h" -#include <libcommon/error.h> -#include <sstream> -#include <algorithm> - -#ifndef STATUS_NOT_FOUND -#define STATUS_NOT_FOUND 0xC0000225 -#endif - -InterfacePair::InterfacePair(NET_LUID interface_luid) -{ - IPv4Iface.Family = AF_INET; - IPv4Iface.InterfaceLuid = interface_luid; - InitializeInterface(IPv4Iface); - - IPv6Iface.Family = AF_INET6; - IPv6Iface.InterfaceLuid = interface_luid; - InitializeInterface(IPv6Iface); - - if (!HasIPv4() && !HasIPv6()) - { - std::stringstream ss; - - ss << "LUID 0x" << std::hex << interface_luid.Value - << " does not specify any IPv4 or IPv6 interfaces"; - - THROW_ERROR(ss.str().c_str()); - } -} - -int InterfacePair::WorstMetric() -{ - return std::max(IPv6Iface.Metric, IPv4Iface.Metric); -} - -int InterfacePair::BestMetric() -{ - return std::min(IPv4Iface.Metric, IPv6Iface.Metric); -} - -void InterfacePair::SetMetric(uint32_t metric) -{ - if (HasIPv4() && (IPv4Iface.UseAutomaticMetric || metric != IPv4Iface.Metric)) - { - IPv4Iface.SitePrefixLength = 0; - IPv4Iface.Metric = metric; - IPv4Iface.UseAutomaticMetric = false; - SetInterface(IPv4Iface); - } - - if (HasIPv6() && (IPv6Iface.UseAutomaticMetric || metric != IPv6Iface.Metric)) - { - IPv6Iface.Metric = metric; - IPv6Iface.UseAutomaticMetric = false; - SetInterface(IPv6Iface); - } -} - -void InterfacePair::SetInterface(const MIB_IPINTERFACE_ROW &iface) -{ - MIB_IPINTERFACE_ROW row = iface; - const auto status = SetIpInterfaceEntry(&row); - - if (NO_ERROR != status) - { - std::stringstream ss; - - ss << "Set metric for " - << (row.Family == AF_INET ? "IPv4" : "IPv6") - << " on interface with LUID 0x" - << std::hex << row.InterfaceLuid.Value; - - THROW_WINDOWS_ERROR(status, ss.str().c_str()); - } -} - -bool InterfacePair::HasIPv4() -{ - return AF_UNSPEC != IPv4Iface.Family; -} - -bool InterfacePair::HasIPv6() -{ - return AF_UNSPEC != IPv6Iface.Family; -} - -//static -void InterfacePair::InitializeInterface(MIB_IPINTERFACE_ROW &iface) -{ - const auto status = GetIpInterfaceEntry(&iface); - - if (NO_ERROR == status) - { - return; - } - - if (STATUS_NOT_FOUND == status || ERROR_NOT_FOUND == status) - { - iface.Family = AF_UNSPEC; - } - else - { - std::stringstream ss; - - ss << "Retrieve info on network interface with LUID 0x" - << std::hex << iface.InterfaceLuid.Value; - - THROW_WINDOWS_ERROR(status, ss.str().c_str()); - } -} diff --git a/windows/winnet/src/winnet/InterfacePair.h b/windows/winnet/src/winnet/InterfacePair.h deleted file mode 100644 index 55a3f59e90..0000000000 --- a/windows/winnet/src/winnet/InterfacePair.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include <winsock2.h> -#include <ws2ipdef.h> -#include <iphlpapi.h> -#include <netioapi.h> -#include <cstdint> - -class InterfacePair -{ -public: - InterfacePair(NET_LUID interface_luid); - int BestMetric(); - int WorstMetric(); - void SetMetric(uint32_t metric); - - -private: - MIB_IPINTERFACE_ROW IPv4Iface = { 0 }; - MIB_IPINTERFACE_ROW IPv6Iface = { 0 }; - - static void InitializeInterface(MIB_IPINTERFACE_ROW &iface); - bool HasIPv4(); - bool HasIPv6(); - void SetInterface(const MIB_IPINTERFACE_ROW &iface); - -}; diff --git a/windows/winnet/src/winnet/NetworkInterfaces.cpp b/windows/winnet/src/winnet/NetworkInterfaces.cpp deleted file mode 100644 index dc27e95e92..0000000000 --- a/windows/winnet/src/winnet/NetworkInterfaces.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include "stdafx.h" -#include "NetworkInterfaces.h" -#include "InterfacePair.h" -#include <libcommon/string.h> -#include <libcommon/error.h> -#include <memory> -#include <sstream> -#include <cstdint> - -bool NetworkInterfaces::HasBestMetric(PMIB_IPINTERFACE_ROW targetIface) -{ - for (unsigned int i = 0; i < mInterfaces->NumEntries; ++i) - { - PMIB_IPINTERFACE_ROW iface = &mInterfaces->Table[i]; - - if (iface->InterfaceLuid.Value != targetIface->InterfaceLuid.Value - && targetIface->Metric >= iface->Metric) - return false; - } - return true; -} - -NetworkInterfaces::NetworkInterfaces() -{ - mInterfaces = nullptr; - - const auto status = GetIpInterfaceTable(AF_UNSPEC, &mInterfaces); - - if (NO_ERROR != status) - { - THROW_WINDOWS_ERROR(status, "Failed to enumerate network interfaces"); - } -} - -bool NetworkInterfaces::SetBestMetricForInterfacesByAlias(const wchar_t * deviceAlias) -{ - return SetBestMetricForInterfacesWithLuid(GetInterfaceLuid(deviceAlias)); -} - -bool NetworkInterfaces::SetBestMetricForInterfacesWithLuid(NET_LUID targetIfaceId) -{ - InterfacePair targetInterfaces = InterfacePair(targetIfaceId); - if (BEST_METRIC == targetInterfaces.WorstMetric()) - { - return false; - } - targetInterfaces.SetMetric(BEST_METRIC); - return true; -} - - -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 (NO_ERROR != status) - { - const auto msg = std::wstring(L"Failed to resolve LUID from interface alias \"") - .append(interfaceAlias).append(L"\""); - - THROW_WINDOWS_ERROR(status, common::string::ToAnsi(msg).c_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/winnet/src/winnet/NetworkInterfaces.h b/windows/winnet/src/winnet/NetworkInterfaces.h deleted file mode 100644 index e987939454..0000000000 --- a/windows/winnet/src/winnet/NetworkInterfaces.h +++ /dev/null @@ -1,31 +0,0 @@ -#pragma once - -#include <windows.h> -#include <winsock2.h> -#include <ws2ipdef.h> -#include <iphlpapi.h> -#include <netioapi.h> -#include <cstdint> -#include <string> - -class NetworkInterfaces -{ - -private: - PMIB_IPINTERFACE_TABLE mInterfaces; - bool HasBestMetric(PMIB_IPINTERFACE_ROW targetIface); - -public: - NetworkInterfaces(const NetworkInterfaces &) = delete; - NetworkInterfaces &operator=(const NetworkInterfaces &) = delete; - - NetworkInterfaces(); - bool SetBestMetricForInterfacesByAlias(const wchar_t *deviceAlias); - bool SetBestMetricForInterfacesWithLuid(NET_LUID targetIface); - ~NetworkInterfaces(); - - static NET_LUID GetInterfaceLuid(const std::wstring &interfaceAlias); - const MIB_IPINTERFACE_ROW *GetInterface(NET_LUID interfaceLuid, ADDRESS_FAMILY interfaceFamily); -}; - -constexpr static uint32_t BEST_METRIC = 1; diff --git a/windows/winnet/src/winnet/winnet.cpp b/windows/winnet/src/winnet/winnet.cpp index 2e35830c96..1c0555a10c 100644 --- a/windows/winnet/src/winnet/winnet.cpp +++ b/windows/winnet/src/winnet/winnet.cpp @@ -1,6 +1,5 @@ #include "stdafx.h"
#include "winnet.h"
-#include "NetworkInterfaces.h"
#include "routing/routemanager.h"
#include "converters.h"
#include <libshared/logging/logsinkadapter.h>
@@ -31,38 +30,6 @@ std::shared_ptr<shared::logging::LogSinkAdapter> g_RouteManagerLogSink; extern "C"
WINNET_LINKAGE
-WINNET_EBM_STATUS
-WINNET_API
-WinNet_EnsureBestMetric(
- const wchar_t *deviceAlias,
- MullvadLogSink logSink,
- void *logSinkContext
-)
-{
- try
- {
- if (nullptr == deviceAlias)
- {
- THROW_ERROR("Invalid argument: deviceAlias");
- }
-
- NetworkInterfaces interfaces;
- return interfaces.SetBestMetricForInterfacesByAlias(deviceAlias) ?
- WINNET_EBM_STATUS_METRIC_SET : WINNET_EBM_STATUS_METRIC_NO_CHANGE;
- }
- catch (const std::exception &err)
- {
- shared::logging::UnwindAndLog(logSink, logSinkContext, err);
- return WINNET_EBM_STATUS_FAILURE;
- }
- catch (...)
- {
- return WINNET_EBM_STATUS_FAILURE;
- }
-};
-
-extern "C"
-WINNET_LINKAGE
WINNET_STATUS
WINNET_API
WinNet_GetBestDefaultRoute(
diff --git a/windows/winnet/src/winnet/winnet.def b/windows/winnet/src/winnet/winnet.def index bafbd58aeb..a5f9a63863 100644 --- a/windows/winnet/src/winnet/winnet.def +++ b/windows/winnet/src/winnet/winnet.def @@ -1,6 +1,5 @@ LIBRARY winnet EXPORTS - WinNet_EnsureBestMetric WinNet_ActivateRouteManager WinNet_DeactivateRouteManager WinNet_AddDeviceIpAddresses diff --git a/windows/winnet/src/winnet/winnet.h b/windows/winnet/src/winnet/winnet.h index f6f8d56074..0c72c91eb8 100644 --- a/windows/winnet/src/winnet/winnet.h +++ b/windows/winnet/src/winnet/winnet.h @@ -16,23 +16,6 @@ #define WINNET_API __stdcall -enum WINNET_EBM_STATUS -{ - WINNET_EBM_STATUS_METRIC_NO_CHANGE = 0, - WINNET_EBM_STATUS_METRIC_SET = 1, - WINNET_EBM_STATUS_FAILURE = 2, -}; - -extern "C" -WINNET_LINKAGE -WINNET_EBM_STATUS -WINNET_API -WinNet_EnsureBestMetric( - const wchar_t *deviceAlias, - MullvadLogSink logSink, - void *logSinkContext -); - enum WINNET_ADDR_FAMILY { WINNET_ADDR_FAMILY_IPV4 = 0, diff --git a/windows/winnet/src/winnet/winnet.vcxproj b/windows/winnet/src/winnet/winnet.vcxproj index b6d5ce1d5b..944e2f7bc2 100644 --- a/windows/winnet/src/winnet/winnet.vcxproj +++ b/windows/winnet/src/winnet/winnet.vcxproj @@ -29,8 +29,6 @@ <ItemGroup> <ClCompile Include="converters.cpp" /> <ClCompile Include="dllmain.cpp" /> - <ClCompile Include="InterfacePair.cpp" /> - <ClCompile Include="NetworkInterfaces.cpp" /> <ClCompile Include="routing\defaultroutemonitor.cpp" /> <ClCompile Include="routing\helpers.cpp" /> <ClCompile Include="routing\routemanager.cpp" /> @@ -40,8 +38,6 @@ </ItemGroup> <ItemGroup> <ClInclude Include="converters.h" /> - <ClInclude Include="InterfacePair.h" /> - <ClInclude Include="NetworkInterfaces.h" /> <ClInclude Include="routing\defaultroutemonitor.h" /> <ClInclude Include="routing\helpers.h" /> <ClInclude Include="routing\routemanager.h" /> diff --git a/windows/winnet/src/winnet/winnet.vcxproj.filters b/windows/winnet/src/winnet/winnet.vcxproj.filters index 3576579961..b9a9053165 100644 --- a/windows/winnet/src/winnet/winnet.vcxproj.filters +++ b/windows/winnet/src/winnet/winnet.vcxproj.filters @@ -4,8 +4,6 @@ <ClCompile Include="dllmain.cpp" /> <ClCompile Include="stdafx.cpp" /> <ClCompile Include="winnet.cpp" /> - <ClCompile Include="NetworkInterfaces.cpp" /> - <ClCompile Include="InterfacePair.cpp" /> <ClCompile Include="routing\types.cpp"> <Filter>routing</Filter> </ClCompile> @@ -24,8 +22,6 @@ <ClInclude Include="stdafx.h" /> <ClInclude Include="targetver.h" /> <ClInclude Include="winnet.h" /> - <ClInclude Include="NetworkInterfaces.h" /> - <ClInclude Include="InterfacePair.h" /> <ClInclude Include="routing\types.h"> <Filter>routing</Filter> </ClInclude> |
