@@ -2,6 +2,7 @@ package wgwindows
22
33import (
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 }
0 commit comments