Skip to content

Commit 2a11b05

Browse files
committed
Convert wgwindows to use netip.Prefix
Signed-off-by: MrMelon54 <[email protected]>
1 parent 6964aab commit 2a11b05

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

internal/wgwindows/client_windows.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package wgwindows
22

33
import (
44
"net"
5+
"net/netip"
56
"os"
67
"time"
78
"unsafe"
@@ -209,19 +210,14 @@ func (c *Client) Device(name string) (*wgtypes.Device, error) {
209210
} else {
210211
a = a.NextAllowedIP()
211212
}
212-
var ip net.IP
213-
var bits int
214-
if a.AddressFamily == windows.AF_INET {
215-
ip = a.Address[:4]
216-
bits = 32
217-
} else if a.AddressFamily == windows.AF_INET6 {
218-
ip = a.Address[:16]
219-
bits = 128
213+
var prefix netip.Prefix
214+
switch a.AddressFamily {
215+
case windows.AF_INET:
216+
prefix = netip.PrefixFrom(netip.AddrFrom4([4]byte(a.Address[:4])), int(a.Cidr))
217+
case windows.AF_INET6:
218+
prefix = netip.PrefixFrom(netip.AddrFrom16(a.Address), int(a.Cidr))
220219
}
221-
peer.AllowedIPs = append(peer.AllowedIPs, net.IPNet{
222-
IP: ip,
223-
Mask: net.CIDRMask(int(a.Cidr), bits),
224-
})
220+
peer.AllowedIPs = append(peer.AllowedIPs, prefix)
225221
}
226222
device.Peers = append(device.Peers, peer)
227223
}
@@ -276,7 +272,7 @@ func (c *Client) ConfigureDevice(name string, cfg wgtypes.Config) error {
276272
}
277273
if cfg.Peers[i].Endpoint != nil {
278274
peer.Flags |= ioctl.PeerHasEndpoint
279-
peer.Endpoint.SetIP(cfg.Peers[i].Endpoint.IP, uint16(cfg.Peers[i].Endpoint.Port))
275+
peer.Endpoint.SetAddrPort(cfg.Peers[i].Endpoint.AddrPort())
280276
}
281277
if cfg.Peers[i].PersistentKeepaliveInterval != nil {
282278
peer.Flags |= ioctl.PeerHasPersistentKeepalive
@@ -285,20 +281,21 @@ func (c *Client) ConfigureDevice(name string, cfg wgtypes.Config) error {
285281
b.AppendPeer(peer)
286282
for j := range cfg.Peers[i].AllowedIPs {
287283
var family ioctl.AddressFamily
288-
var ip net.IP
289-
if ip = cfg.Peers[i].AllowedIPs[j].IP.To4(); ip != nil {
284+
prefix := cfg.Peers[i].AllowedIPs[j]
285+
286+
// Unmap 4in6 addresses to maintain previous compatibility
287+
addr := prefix.Addr().Unmap()
288+
switch {
289+
case addr.Is4():
290290
family = windows.AF_INET
291-
} else if ip = cfg.Peers[i].AllowedIPs[j].IP.To16(); ip != nil {
291+
case addr.Is6():
292292
family = windows.AF_INET6
293-
} else {
294-
ip = cfg.Peers[i].AllowedIPs[j].IP
295293
}
296-
cidr, _ := cfg.Peers[i].AllowedIPs[j].Mask.Size()
297294
a := &ioctl.AllowedIP{
298295
AddressFamily: family,
299-
Cidr: uint8(cidr),
296+
Cidr: uint8(prefix.Bits()),
300297
}
301-
copy(a.Address[:], ip)
298+
copy(a.Address[:], addr.AsSlice())
302299
b.AppendAllowedIP(a)
303300
}
304301
}

internal/wgwindows/internal/ioctl/winipcfg_windows.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package ioctl
88
import (
99
"encoding/binary"
1010
"net"
11+
"net/netip"
1112
"unsafe"
1213

1314
"golang.org/x/sys/windows"
@@ -33,26 +34,26 @@ func htons(i uint16) uint16 {
3334
return *(*uint16)(unsafe.Pointer(&b[0]))
3435
}
3536

36-
// SetIP method sets family, address, and port to the given IPv4 or IPv6 address and port.
37+
// SetAddrPort method sets family, address, and port to the given IPv4 or IPv6 address and port.
3738
// All other members of the structure are set to zero.
38-
func (addr *RawSockaddrInet) SetIP(ip net.IP, port uint16) error {
39-
if v4 := ip.To4(); v4 != nil {
39+
func (addr *RawSockaddrInet) SetAddrPort(addrPort netip.AddrPort) error {
40+
a := addrPort.Addr().Unmap()
41+
switch {
42+
case a.Is4():
4043
addr4 := (*windows.RawSockaddrInet4)(unsafe.Pointer(addr))
4144
addr4.Family = windows.AF_INET
42-
copy(addr4.Addr[:], v4)
43-
addr4.Port = htons(port)
45+
addr4.Addr = a.As4()
46+
addr4.Port = htons(addrPort.Port())
4447
for i := 0; i < 8; i++ {
4548
addr4.Zero[i] = 0
4649
}
4750
return nil
48-
}
49-
50-
if v6 := ip.To16(); v6 != nil {
51+
case a.Is6():
5152
addr6 := (*windows.RawSockaddrInet6)(unsafe.Pointer(addr))
5253
addr6.Family = windows.AF_INET6
53-
addr6.Port = htons(port)
54+
addr6.Port = htons(addrPort.Port())
5455
addr6.Flowinfo = 0
55-
copy(addr6.Addr[:], v6)
56+
addr6.Addr = a.As16()
5657
addr6.Scope_id = 0
5758
return nil
5859
}

0 commit comments

Comments
 (0)