Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 51 additions & 10 deletions monitor_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ import (
// https://github.com/zerotier/ZeroTierOne/blob/1.8.6/osdep/WindowsEthernetTap.cpp#L994
var zeroTierFakeGatewayIp = netip.MustParseAddr("25.255.255.254")

type defaultRouteCandidate struct {
index int
alias string
metric uint32
}

func selectDefaultRouteCandidate(candidates []defaultRouteCandidate, previousIndex int) (defaultRouteCandidate, bool) {
if len(candidates) == 0 {
return defaultRouteCandidate{}, false
}

best := candidates[0]
bestIsPrevious := best.index == previousIndex

for _, candidate := range candidates[1:] {
switch {
case candidate.metric < best.metric:
best = candidate
bestIsPrevious = candidate.index == previousIndex
case candidate.metric == best.metric && !bestIsPrevious && candidate.index == previousIndex:
best = candidate
bestIsPrevious = true
}
}

return best, true
}

type networkUpdateMonitor struct {
routeListener *winipcfg.RouteChangeCallback
interfaceListener *winipcfg.InterfaceChangeCallback
Expand Down Expand Up @@ -69,9 +97,12 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
return err
}

lowestMetric := ^uint32(0)
alias := ""
var index int
previousIndex := 0
if oldInterface := m.defaultInterface.Load(); oldInterface != nil {
previousIndex = oldInterface.Index
}

candidatesByIndex := map[int]defaultRouteCandidate{}

for _, row := range rows {
if row.DestinationPrefix.PrefixLength != 0 {
Expand Down Expand Up @@ -101,20 +132,30 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
}

metric := row.Metric + iface.Metric
if metric < lowestMetric {
lowestMetric = metric
alias = ifrow.Alias()
index = int(ifrow.InterfaceIndex)

candidate := defaultRouteCandidate{
index: int(ifrow.InterfaceIndex),
alias: ifrow.Alias(),
metric: metric,
}
if existing, ok := candidatesByIndex[candidate.index]; !ok || candidate.metric < existing.metric {
candidatesByIndex[candidate.index] = candidate
}
}

candidates := make([]defaultRouteCandidate, 0, len(candidatesByIndex))
for _, candidate := range candidatesByIndex {
candidates = append(candidates, candidate)
}

if alias == "" {
selected, ok := selectDefaultRouteCandidate(candidates, previousIndex)
if !ok {
return ErrNoRoute
}

newInterface, err := m.interfaceFinder.ByIndex(index)
newInterface, err := m.interfaceFinder.ByIndex(selected.index)
if err != nil {
return E.Cause(err, "find updated interface: ", alias)
return E.Cause(err, "find updated interface: ", selected.alias)
}
oldInterface := m.defaultInterface.Swap(newInterface)
if oldInterface != nil && oldInterface.Equals(*newInterface) {
Expand Down
39 changes: 39 additions & 0 deletions monitor_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tun

import "testing"

func TestSelectDefaultRouteCandidatePrefersPreviousInterfaceOnMetricTie(t *testing.T) {
candidates := []defaultRouteCandidate{
{index: 36, alias: "WLAN", metric: 50},
{index: 37, alias: "WLAN 8", metric: 50},
}

selected, ok := selectDefaultRouteCandidate(candidates, 37)
if !ok {
t.Fatal("expected a selected candidate")
}
if selected.index != 37 {
t.Fatalf("expected previous interface 37 to be kept on metric tie, got %d", selected.index)
}
}

func TestSelectDefaultRouteCandidateSwitchesWhenMetricIsLower(t *testing.T) {
candidates := []defaultRouteCandidate{
{index: 36, alias: "WLAN", metric: 35},
{index: 37, alias: "WLAN 8", metric: 40},
}

selected, ok := selectDefaultRouteCandidate(candidates, 37)
if !ok {
t.Fatal("expected a selected candidate")
}
if selected.index != 36 {
t.Fatalf("expected lower-metric interface 36 to win, got %d", selected.index)
}
}

func TestSelectDefaultRouteCandidateReturnsFalseWhenEmpty(t *testing.T) {
if _, ok := selectDefaultRouteCandidate(nil, 0); ok {
t.Fatal("expected empty candidate list to return false")
}
}