5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-23 12:15:39 +00:00

Fix Windows TUN build

This commit is contained in:
Neil Alexander 2023-10-12 00:08:16 +01:00
parent 854cd75f04
commit 4b48fd0b5f
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -4,11 +4,10 @@
package tun package tun
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"log" "log"
"net" "net/netip"
"github.com/yggdrasil-network/yggdrasil-go/src/config" "github.com/yggdrasil-network/yggdrasil-go/src/config"
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
@ -89,13 +88,9 @@ func (tun *TunAdapter) setupAddress(addr string) error {
return errors.New("Can't configure IPv6 address as TUN adapter is not present") return errors.New("Can't configure IPv6 address as TUN adapter is not present")
} }
if intf, ok := tun.iface.(*wgtun.NativeTun); ok { if intf, ok := tun.iface.(*wgtun.NativeTun); ok {
if ipaddr, ipnet, err := net.ParseCIDR(addr); err == nil { if ipnet, err := netip.ParsePrefix(addr); err == nil {
luid := winipcfg.LUID(intf.LUID()) luid := winipcfg.LUID(intf.LUID())
addresses := append([]net.IPNet{}, net.IPNet{ addresses := []netip.Prefix{ipnet}
IP: ipaddr,
Mask: ipnet.Mask,
})
err := luid.SetIPAddressesForFamily(windows.AF_INET6, addresses) err := luid.SetIPAddressesForFamily(windows.AF_INET6, addresses)
if err == windows.ERROR_OBJECT_ALREADY_EXISTS { if err == windows.ERROR_OBJECT_ALREADY_EXISTS {
cleanupAddressesOnDisconnectedInterfaces(windows.AF_INET6, addresses) cleanupAddressesOnDisconnectedInterfaces(windows.AF_INET6, addresses)
@ -118,24 +113,13 @@ func (tun *TunAdapter) setupAddress(addr string) error {
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
* Copyright (C) 2019 WireGuard LLC. All Rights Reserved. * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
*/ */
func cleanupAddressesOnDisconnectedInterfaces(family winipcfg.AddressFamily, addresses []net.IPNet) { func cleanupAddressesOnDisconnectedInterfaces(family winipcfg.AddressFamily, addresses []netip.Prefix) {
if len(addresses) == 0 { if len(addresses) == 0 {
return return
} }
includedInAddresses := func(a net.IPNet) bool { addrHash := make(map[netip.Addr]bool, len(addresses))
// TODO: this makes the whole algorithm O(n^2). But we can't stick net.IPNet in a Go hashmap. Bummer! for i := range addresses {
for _, addr := range addresses { addrHash[addresses[i].Addr()] = true
ip := addr.IP
if ip4 := ip.To4(); ip4 != nil {
ip = ip4
}
mA, _ := addr.Mask.Size()
mB, _ := a.Mask.Size()
if bytes.Equal(ip, a.IP) && mA == mB {
return true
}
}
return false
} }
interfaces, err := winipcfg.GetAdaptersAddresses(family, winipcfg.GAAFlagDefault) interfaces, err := winipcfg.GetAdaptersAddresses(family, winipcfg.GAAFlagDefault)
if err != nil { if err != nil {
@ -146,11 +130,10 @@ func cleanupAddressesOnDisconnectedInterfaces(family winipcfg.AddressFamily, add
continue continue
} }
for address := iface.FirstUnicastAddress; address != nil; address = address.Next { for address := iface.FirstUnicastAddress; address != nil; address = address.Next {
ip := address.Address.IP() if ip, _ := netip.AddrFromSlice(address.Address.IP()); addrHash[ip] {
ipnet := net.IPNet{IP: ip, Mask: net.CIDRMask(int(address.OnLinkPrefixLength), 8*len(ip))} prefix := netip.PrefixFrom(ip, int(address.OnLinkPrefixLength))
if includedInAddresses(ipnet) { log.Printf("Cleaning up stale address %s from interface %s", prefix.String(), iface.FriendlyName())
log.Printf("Cleaning up stale address %s from interface %s", ipnet.String(), iface.FriendlyName()) iface.LUID.DeleteIPAddress(prefix)
iface.LUID.DeleteIPAddress(ipnet)
} }
} }
} }