summaryrefslogtreecommitdiffhomepage
path: root/windows/winnet/src
diff options
context:
space:
mode:
authorDavid Lönnhager <david.l@mullvad.net>2020-02-06 17:38:41 +0100
committerDavid Lönnhager <david.l@mullvad.net>2020-02-07 13:01:26 +0100
commit052d425fb7bfea4ec97bc28e2c1959c86c6220ef (patch)
treeb5bfb95be5cb7e1fa94558725dc5cfcff1da6def /windows/winnet/src
parentd9f11a8f12e5f6843285f98c0edcbf22b8caa451 (diff)
downloadmullvadvpn-052d425fb7bfea4ec97bc28e2c1959c86c6220ef.tar.xz
mullvadvpn-052d425fb7bfea4ec97bc28e2c1959c86c6220ef.zip
Update winnet
Diffstat (limited to 'windows/winnet/src')
-rw-r--r--windows/winnet/src/winnet/InterfacePair.cpp40
-rw-r--r--windows/winnet/src/winnet/InterfacePair.h7
-rw-r--r--windows/winnet/src/winnet/stdafx.h1
-rw-r--r--windows/winnet/src/winnet/winnet.cpp4
4 files changed, 28 insertions, 24 deletions
diff --git a/windows/winnet/src/winnet/InterfacePair.cpp b/windows/winnet/src/winnet/InterfacePair.cpp
index 6046bd32b7..935bc557aa 100644
--- a/windows/winnet/src/winnet/InterfacePair.cpp
+++ b/windows/winnet/src/winnet/InterfacePair.cpp
@@ -2,6 +2,7 @@
#include "InterfacePair.h"
#include <libcommon/error.h>
#include <sstream>
+#include <algorithm>
#ifndef STATUS_NOT_FOUND
#define STATUS_NOT_FOUND 0xC0000225
@@ -11,11 +12,11 @@ InterfacePair::InterfacePair(NET_LUID interface_luid)
{
IPv4Iface.Family = AF_INET;
IPv4Iface.InterfaceLuid = interface_luid;
- InitializeInterface(&IPv4Iface);
+ InitializeInterface(IPv4Iface);
IPv6Iface.Family = AF_INET6;
IPv6Iface.InterfaceLuid = interface_luid;
- InitializeInterface(&IPv6Iface);
+ InitializeInterface(IPv6Iface);
if (!HasIPv4() && !HasIPv6())
{
@@ -30,44 +31,45 @@ InterfacePair::InterfacePair(NET_LUID interface_luid)
int InterfacePair::WorstMetric()
{
- return IPv6Iface.Metric >= IPv4Iface.Metric ? IPv6Iface.Metric : IPv4Iface.Metric;
+ return std::max(IPv6Iface.Metric, IPv4Iface.Metric);
}
int InterfacePair::BestMetric()
{
- return IPv6Iface.Metric < IPv4Iface.Metric ? IPv4Iface.Metric : IPv6Iface.Metric;
+ return std::min(IPv4Iface.Metric, IPv6Iface.Metric);
}
-void InterfacePair::SetMetric(unsigned int 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);
+ SetInterface(IPv4Iface);
}
if (HasIPv6() && (IPv6Iface.UseAutomaticMetric || metric != IPv6Iface.Metric))
{
IPv6Iface.Metric = metric;
IPv6Iface.UseAutomaticMetric = false;
- SetInterface(&IPv6Iface);
+ SetInterface(IPv6Iface);
}
}
-void InterfacePair::SetInterface(PMIB_IPINTERFACE_ROW iface) {
-
- const auto status = SetIpInterfaceEntry(iface);
+void InterfacePair::SetInterface(const MIB_IPINTERFACE_ROW &iface)
+{
+ MIB_IPINTERFACE_ROW row = iface;
+ const auto status = SetIpInterfaceEntry(&row);
- if (status != NO_ERROR)
+ if (NO_ERROR != status)
{
std::stringstream ss;
ss << "Set metric for "
- << (iface->Family == AF_INET ? "IPv4" : "IPv6")
+ << (row.Family == AF_INET ? "IPv4" : "IPv6")
<< " on interface with LUID 0x"
- << std::hex << iface->InterfaceLuid.Value;
+ << std::hex << row.InterfaceLuid.Value;
THROW_WINDOWS_ERROR(status, ss.str().c_str());
}
@@ -75,18 +77,18 @@ void InterfacePair::SetInterface(PMIB_IPINTERFACE_ROW iface) {
bool InterfacePair::HasIPv4()
{
- return IPv4Iface.Family != AF_UNSPEC;
+ return AF_UNSPEC != IPv4Iface.Family;
}
bool InterfacePair::HasIPv6()
{
- return IPv6Iface.Family != AF_UNSPEC;
+ return AF_UNSPEC != IPv6Iface.Family;
}
//static
-void InterfacePair::InitializeInterface(PMIB_IPINTERFACE_ROW iface)
+void InterfacePair::InitializeInterface(MIB_IPINTERFACE_ROW &iface)
{
- const auto status = GetIpInterfaceEntry(iface);
+ const auto status = GetIpInterfaceEntry(&iface);
if (NO_ERROR == status)
{
@@ -95,14 +97,14 @@ void InterfacePair::InitializeInterface(PMIB_IPINTERFACE_ROW iface)
if (STATUS_NOT_FOUND == status || ERROR_NOT_FOUND == status)
{
- iface->Family = AF_UNSPEC;
+ iface.Family = AF_UNSPEC;
}
else
{
std::stringstream ss;
ss << "Retrieve info on network interface with LUID 0x"
- << std::hex << iface->InterfaceLuid.Value;
+ << 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
index 0a5071e4a7..55a3f59e90 100644
--- a/windows/winnet/src/winnet/InterfacePair.h
+++ b/windows/winnet/src/winnet/InterfacePair.h
@@ -4,6 +4,7 @@
#include <ws2ipdef.h>
#include <iphlpapi.h>
#include <netioapi.h>
+#include <cstdint>
class InterfacePair
{
@@ -11,16 +12,16 @@ public:
InterfacePair(NET_LUID interface_luid);
int BestMetric();
int WorstMetric();
- void SetMetric(unsigned int metric);
+ void SetMetric(uint32_t metric);
private:
MIB_IPINTERFACE_ROW IPv4Iface = { 0 };
MIB_IPINTERFACE_ROW IPv6Iface = { 0 };
- static void InitializeInterface(PMIB_IPINTERFACE_ROW iface);
+ static void InitializeInterface(MIB_IPINTERFACE_ROW &iface);
bool HasIPv4();
bool HasIPv6();
- void SetInterface(PMIB_IPINTERFACE_ROW iface);
+ void SetInterface(const MIB_IPINTERFACE_ROW &iface);
};
diff --git a/windows/winnet/src/winnet/stdafx.h b/windows/winnet/src/winnet/stdafx.h
index 254cb49b0d..3115e02cd4 100644
--- a/windows/winnet/src/winnet/stdafx.h
+++ b/windows/winnet/src/winnet/stdafx.h
@@ -10,6 +10,7 @@
#include "targetver.h"
+#define NOMINMAX
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
diff --git a/windows/winnet/src/winnet/winnet.cpp b/windows/winnet/src/winnet/winnet.cpp
index f572b188be..b71484bf7c 100644
--- a/windows/winnet/src/winnet/winnet.cpp
+++ b/windows/winnet/src/winnet/winnet.cpp
@@ -44,8 +44,8 @@ WinNet_EnsureBestMetric(
try
{
NetworkInterfaces interfaces;
- bool metrics_set = interfaces.SetBestMetricForInterfacesByAlias(deviceAlias);
- return metrics_set ? WINNET_EBM_STATUS_METRIC_SET : WINNET_EBM_STATUS_METRIC_NO_CHANGE;
+ return interfaces.SetBestMetricForInterfacesByAlias(deviceAlias) ?
+ WINNET_EBM_STATUS_METRIC_SET : WINNET_EBM_STATUS_METRIC_NO_CHANGE;
}
catch (const std::exception &err)
{