aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tunnel/winipcfg
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-06-17 13:08:13 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2019-06-18 12:08:51 +0200
commit6e48647318edad7540c4e99be5aed10ac898893f (patch)
tree13a2d97bcb3ad8e44f288d3220fd9030d9eb968c /tunnel/winipcfg
parentversion: bump (diff)
downloadwireguard-windows-6e48647318edad7540c4e99be5aed10ac898893f.tar.xz
wireguard-windows-6e48647318edad7540c4e99be5aed10ac898893f.zip
tunnel: wait for IP service to attach to wintun
This helps fix startup races without needing to poll, as well as reconfiguring interfaces after wintun destroys and re-adds. It also deals gracefully with IPv6 being disabled. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to '')
-rw-r--r--tunnel/winipcfg/luid.go67
-rw-r--r--tunnel/winipcfg/winipcfg.go4
2 files changed, 69 insertions, 2 deletions
diff --git a/tunnel/winipcfg/luid.go b/tunnel/winipcfg/luid.go
index ff7061d2..396fbbb2 100644
--- a/tunnel/winipcfg/luid.go
+++ b/tunnel/winipcfg/luid.go
@@ -116,6 +116,27 @@ func (luid LUID) SetIPAddresses(addresses []net.IPNet) error {
return luid.AddIPAddresses(addresses)
}
+// SetIPAddressesForFamily method sets new unicast IP addresses for a specific family to the interface.
+func (luid LUID) SetIPAddressesForFamily(family AddressFamily, addresses []net.IPNet) error {
+ err := luid.FlushIPAddresses(family)
+ if err != nil {
+ return err
+ }
+ for i := range addresses {
+ asV4 := addresses[i].IP.To4()
+ if asV4 == nil && family == windows.AF_INET {
+ continue
+ } else if asV4 != nil && family == windows.AF_INET6 {
+ continue
+ }
+ err := luid.AddIPAddress(addresses[i])
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// DeleteIPAddress method deletes interface's unicast IP address. Corresponds to DeleteUnicastIpAddressEntry function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-deleteunicastipaddressentry).
func (luid LUID) DeleteIPAddress(address net.IPNet) error {
@@ -210,6 +231,27 @@ func (luid LUID) SetRoutes(routesData []*RouteData) error {
return luid.AddRoutes(routesData)
}
+// SetRoutesForFamily method sets (flush than add) multiple routes for a specific family to the interface.
+func (luid LUID) SetRoutesForFamily(family AddressFamily, routesData []*RouteData) error {
+ err := luid.FlushRoutes(family)
+ if err != nil {
+ return err
+ }
+ for _, rd := range routesData {
+ asV4 := rd.Destination.IP.To4()
+ if asV4 == nil && family == windows.AF_INET {
+ continue
+ } else if asV4 != nil && family == windows.AF_INET6 {
+ continue
+ }
+ err := luid.AddRoute(rd.Destination, rd.NextHop, rd.Metric)
+ if err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// DeleteRoute method deletes a route that matches the criteria. Corresponds to DeleteIpForwardEntry2 function
// (https://docs.microsoft.com/en-us/windows/desktop/api/netioapi/nf-netioapi-deleteipforwardentry2).
func (luid LUID) DeleteRoute(destination net.IPNet, nextHop net.IP) error {
@@ -359,3 +401,28 @@ func (luid LUID) SetDNS(dnses []net.IP) error {
}
return runNetsh(cmds)
}
+
+// SetDNSForFamily method clears previous and associates new DNS servers with the adapter for a specific family.
+func (luid LUID) SetDNSForFamily(family AddressFamily, dnses []net.IP) error {
+ var templateFlush string
+ if family == windows.AF_INET {
+ templateFlush = netshCmdTemplateFlush4
+ } else if family == windows.AF_INET6 {
+ templateFlush = netshCmdTemplateFlush6
+ }
+
+ cmds := make([]string, 0, 1+len(dnses))
+ ipif, err := luid.IPInterface(family)
+ if err != nil {
+ return err
+ }
+ cmds = append(cmds, fmt.Sprintf(templateFlush, ipif.InterfaceIndex))
+ for i := 0; i < len(dnses); i++ {
+ if v4 := dnses[i].To4(); v4 != nil && family == windows.AF_INET {
+ cmds = append(cmds, fmt.Sprintf(netshCmdTemplateAdd4, ipif.InterfaceIndex, v4.String()))
+ } else if v6 := dnses[i].To16(); v4 == nil && v6 != nil && family == windows.AF_INET6 {
+ cmds = append(cmds, fmt.Sprintf(netshCmdTemplateAdd6, ipif.InterfaceIndex, v6.String()))
+ }
+ }
+ return runNetsh(cmds)
+}
diff --git a/tunnel/winipcfg/winipcfg.go b/tunnel/winipcfg/winipcfg.go
index 5af9f1aa..2fc0c875 100644
--- a/tunnel/winipcfg/winipcfg.go
+++ b/tunnel/winipcfg/winipcfg.go
@@ -32,13 +32,13 @@ import (
// GetAdaptersAddresses function retrieves the addresses associated with the adapters on the local computer.
// https://docs.microsoft.com/en-us/windows/desktop/api/iphlpapi/nf-iphlpapi-getadaptersaddresses
-func GetAdaptersAddresses(family uint32, flags GAAFlags) ([]*IPAdapterAddresses, error) {
+func GetAdaptersAddresses(family AddressFamily, flags GAAFlags) ([]*IPAdapterAddresses, error) {
var b []byte
size := uint32(15000)
for {
b = make([]byte, size)
- err := windows.GetAdaptersAddresses(family, uint32(flags), 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &size)
+ err := windows.GetAdaptersAddresses(uint32(family), uint32(flags), 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &size)
if err == nil {
break
}