diff options
Diffstat (limited to 'windows')
| -rw-r--r-- | windows/winnet/src/winnet/routing/defaultroutemonitor.cpp | 80 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/routing/defaultroutemonitor.h | 9 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.cpp | 14 | ||||
| -rw-r--r-- | windows/winnet/src/winnet/winnet.h | 6 |
4 files changed, 99 insertions, 10 deletions
diff --git a/windows/winnet/src/winnet/routing/defaultroutemonitor.cpp b/windows/winnet/src/winnet/routing/defaultroutemonitor.cpp index 523c2d7ba0..8e1e2599ad 100644 --- a/windows/winnet/src/winnet/routing/defaultroutemonitor.cpp +++ b/windows/winnet/src/winnet/routing/defaultroutemonitor.cpp @@ -23,6 +23,7 @@ DefaultRouteMonitor::DefaultRouteMonitor : m_family(family) , m_callback(callback) , m_logSink(logSink) + , m_refreshCurrentRoute(false) , m_evaluateRoutesGuard(std::make_unique<common::BurstGuard>( std::bind(&DefaultRouteMonitor::evaluateRoutes, this), POINT_TWO_SECOND_BURST, @@ -31,14 +32,14 @@ DefaultRouteMonitor::DefaultRouteMonitor { std::scoped_lock<std::mutex> lock(m_evaluationLock); - auto status = NotifyRouteChange2(AF_UNSPEC, RouteChangeCallback, this, FALSE, &m_routeNotificationHandle); + auto status = NotifyRouteChange2(family, RouteChangeCallback, this, FALSE, &m_routeNotificationHandle); if (NO_ERROR != status) { THROW_WINDOWS_ERROR(status, "Register for route table change notifications"); } - status = NotifyIpInterfaceChange(AF_UNSPEC, InterfaceChangeCallback, this, + status = NotifyIpInterfaceChange(family, InterfaceChangeCallback, this, FALSE, &m_interfaceNotificationHandle); if (NO_ERROR != status) @@ -47,6 +48,16 @@ DefaultRouteMonitor::DefaultRouteMonitor THROW_WINDOWS_ERROR(status, "Register for network interface change notifications"); } + status = NotifyUnicastIpAddressChange(family, AddressChangeCallback, this, + FALSE, &m_addressNotificationHandle); + + if (NO_ERROR != status) + { + CancelMibChangeNotify2(m_routeNotificationHandle); + CancelMibChangeNotify2(m_interfaceNotificationHandle); + THROW_WINDOWS_ERROR(status, "Register for unicast address change notifications"); + } + try { m_bestRoute = GetBestDefaultRoute(m_family); @@ -62,6 +73,7 @@ DefaultRouteMonitor::~DefaultRouteMonitor() // Cancel notifications to stop triggering the BurstGuard. // + CancelMibChangeNotify2(m_addressNotificationHandle); CancelMibChangeNotify2(m_interfaceNotificationHandle); CancelMibChangeNotify2(m_routeNotificationHandle); @@ -91,18 +103,62 @@ void NETIOAPI_API_ DefaultRouteMonitor::RouteChangeCallback return; } - reinterpret_cast<DefaultRouteMonitor *>(context)->m_evaluateRoutesGuard->trigger(); + const auto monitor = reinterpret_cast<DefaultRouteMonitor*>(context); + monitor->updateRefreshFlag(row->InterfaceLuid, row->InterfaceIndex); + monitor->m_evaluateRoutesGuard->trigger(); } //static void NETIOAPI_API_ DefaultRouteMonitor::InterfaceChangeCallback ( void *context, - MIB_IPINTERFACE_ROW *, + MIB_IPINTERFACE_ROW *row, + MIB_NOTIFICATION_TYPE +) +{ + const auto monitor = reinterpret_cast<DefaultRouteMonitor*>(context); + monitor->updateRefreshFlag(row->InterfaceLuid, row->InterfaceIndex); + monitor->m_evaluateRoutesGuard->trigger(); +} + +//static +void NETIOAPI_API_ DefaultRouteMonitor::AddressChangeCallback +( + void *context, + MIB_UNICASTIPADDRESS_ROW *row, MIB_NOTIFICATION_TYPE ) { - reinterpret_cast<DefaultRouteMonitor *>(context)->m_evaluateRoutesGuard->trigger(); + const auto monitor = reinterpret_cast<DefaultRouteMonitor*>(context); + monitor->updateRefreshFlag(row->InterfaceLuid, row->InterfaceIndex); + monitor->m_evaluateRoutesGuard->trigger(); +} + +void DefaultRouteMonitor::updateRefreshFlag(const NET_LUID &luid, const NET_IFINDEX &index) +{ + std::scoped_lock<std::mutex> lock(m_evaluationLock); + + if (!m_bestRoute.has_value()) + { + return; + } + + if (luid.Value == m_bestRoute->iface.Value) + { + m_refreshCurrentRoute = true; + return; + } + + if (luid.Value != 0) + { + return; + } + + NET_IFINDEX defaultInterfaceIndex = 0; + const auto routeLuid = &m_bestRoute->iface; + ConvertInterfaceLuidToIndex(routeLuid, &defaultInterfaceIndex); + m_refreshCurrentRoute = index == defaultInterfaceIndex || + (defaultInterfaceIndex == NET_IFINDEX_UNSPECIFIED); } void DefaultRouteMonitor::evaluateRoutes() @@ -128,6 +184,9 @@ void DefaultRouteMonitor::evaluateRoutesInner() { std::optional<InterfaceAndGateway> currentBestRoute; + bool refreshCurrent = m_refreshCurrentRoute; + m_refreshCurrentRoute = false; + try { currentBestRoute = GetBestDefaultRoute(m_family); @@ -172,6 +231,17 @@ void DefaultRouteMonitor::evaluateRoutesInner() { m_bestRoute = currentBestRoute; m_callback(EventType::Updated, m_bestRoute); + + return; + } + + // + // Interface details may have changed. + // + + if (refreshCurrent) + { + m_callback(EventType::UpdatedDetails, m_bestRoute); } } diff --git a/windows/winnet/src/winnet/routing/defaultroutemonitor.h b/windows/winnet/src/winnet/routing/defaultroutemonitor.h index 5575685a82..ce2a3ce3f6 100644 --- a/windows/winnet/src/winnet/routing/defaultroutemonitor.h +++ b/windows/winnet/src/winnet/routing/defaultroutemonitor.h @@ -22,6 +22,10 @@ public: // The best default route changed. Updated, + // Interface details changed; the associated interface and + // gateway did not. + UpdatedDetails, + // No default routes exist. Removed, }; @@ -53,14 +57,19 @@ private: std::unique_ptr<common::BurstGuard> m_evaluateRoutesGuard; std::optional<InterfaceAndGateway> m_bestRoute; + bool m_refreshCurrentRoute; HANDLE m_routeNotificationHandle; HANDLE m_interfaceNotificationHandle; + HANDLE m_addressNotificationHandle; std::mutex m_evaluationLock; static void NETIOAPI_API_ RouteChangeCallback(void *context, MIB_IPFORWARD_ROW2 *row, MIB_NOTIFICATION_TYPE notificationType); static void NETIOAPI_API_ InterfaceChangeCallback(void *context, MIB_IPINTERFACE_ROW *row, MIB_NOTIFICATION_TYPE notificationType); + static void NETIOAPI_API_ AddressChangeCallback(void *context, MIB_UNICASTIPADDRESS_ROW *row, MIB_NOTIFICATION_TYPE notificationType); + + void updateRefreshFlag(const NET_LUID &luid, const NET_IFINDEX &index); void evaluateRoutes(); void evaluateRoutesInner(); diff --git a/windows/winnet/src/winnet/winnet.cpp b/windows/winnet/src/winnet/winnet.cpp index c1b864612e..2e35830c96 100644 --- a/windows/winnet/src/winnet/winnet.cpp +++ b/windows/winnet/src/winnet/winnet.cpp @@ -395,6 +395,7 @@ WinNet_RegisterDefaultRouteChangedCallback( static const std::pair<from_t, to_t> eventTypeMap[] =
{
{ from_t::Updated, WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE_UPDATED },
+ { from_t::UpdatedDetails, WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE_UPDATED_DETAILS },
{ from_t::Removed, WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE_REMOVED }
};
@@ -418,11 +419,16 @@ WinNet_RegisterDefaultRouteChangedCallback( // Determine which LUID and gateway to forward.
//
- if (RouteManager::DefaultRouteChangedEventType::Updated == eventType)
+ switch (eventType)
{
- const auto ips = winnet::ConvertNativeAddresses(&route.value().gateway, 1);
- defaultRoute.gateway = ips[0];
- defaultRoute.interfaceLuid = route.value().iface.Value;
+ case RouteManager::DefaultRouteChangedEventType::Updated:
+ case RouteManager::DefaultRouteChangedEventType::UpdatedDetails:
+ {
+ const auto ips = winnet::ConvertNativeAddresses(&route.value().gateway, 1);
+ defaultRoute.gateway = ips[0];
+ defaultRoute.interfaceLuid = route.value().iface.Value;
+ break;
+ }
}
//
diff --git a/windows/winnet/src/winnet/winnet.h b/windows/winnet/src/winnet/winnet.h index 3f2f80a5e5..f6f8d56074 100644 --- a/windows/winnet/src/winnet/winnet.h +++ b/windows/winnet/src/winnet/winnet.h @@ -168,8 +168,12 @@ enum WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE // Best default route changed. WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE_UPDATED = 0, + // The route (gateway or interface) did not change, but + // interface details may have changed. + WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE_UPDATED_DETAILS = 1, + // No default routes exist. - WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE_REMOVED = 1, + WINNET_DEFAULT_ROUTE_CHANGED_EVENT_TYPE_REMOVED = 2, }; typedef void (WINNET_API *WinNetDefaultRouteChangedCallback) |
