Skip to content

Commit 6964aab

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

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed

internal/wgopenbsd/client_openbsd.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/binary"
99
"fmt"
1010
"net"
11+
"net/netip"
1112
"os"
1213
"runtime"
1314
"time"
@@ -201,7 +202,7 @@ func parseDevice(name string, ifio *wgh.WGInterfaceIO) (*wgtypes.Device, error)
201202

202203
// Same idea, we know how many allowed IPs we need to account for, so
203204
// reserve the space and advance the pointer through each WGAIP structure.
204-
p.AllowedIPs = make([]net.IPNet, 0, peer.Aips_count)
205+
p.AllowedIPs = make([]netip.Prefix, 0, peer.Aips_count)
205206
for j := uintptr(0); j < uintptr(peer.Aips_count); j++ {
206207
aip := (*wgh.WGAIPIO)(unsafe.Pointer(
207208
uintptr(unsafe.Pointer(peer)) + wgh.SizeofWGPeerIO + j*wgh.SizeofWGAIPIO,
@@ -283,21 +284,15 @@ func parsePeer(pio *wgh.WGPeerIO) wgtypes.Peer {
283284
}
284285

285286
// parseAllowedIP unpacks a net.IPNet from a WGAIP structure.
286-
func parseAllowedIP(aip *wgh.WGAIPIO) net.IPNet {
287+
func parseAllowedIP(aip *wgh.WGAIPIO) netip.Prefix {
287288
switch aip.Af {
288289
case unix.AF_INET:
289-
return net.IPNet{
290-
IP: net.IP(aip.Addr[:net.IPv4len]),
291-
Mask: net.CIDRMask(int(aip.Cidr), 32),
292-
}
290+
return netip.PrefixFrom(netip.AddrFrom4([4]byte(aip.Addr[:4])), int(aip.Cidr))
293291
case unix.AF_INET6:
294-
return net.IPNet{
295-
IP: net.IP(aip.Addr[:]),
296-
Mask: net.CIDRMask(int(aip.Cidr), 128),
297-
}
292+
return netip.PrefixFrom(netip.AddrFrom16(aip.Addr), int(aip.Cidr))
298293
default:
299294
panicf("wgopenbsd: invalid address family for allowed IP: %+v", aip)
300-
return net.IPNet{}
295+
return netip.Prefix{}
301296
}
302297
}
303298

internal/wgopenbsd/client_openbsd_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package wgopenbsd
55

66
import (
77
"errors"
8-
"net"
8+
"net/netip"
99
"os"
1010
"testing"
1111
"time"
@@ -239,20 +239,20 @@ func TestClientDeviceBasic(t *testing.T) {
239239
ReceiveBytes: 2,
240240
TransmitBytes: 1,
241241
LastHandshakeTime: time.Unix(1, 2),
242-
AllowedIPs: []net.IPNet{
243-
wgtest.MustCIDR("192.168.1.0/24"),
244-
wgtest.MustCIDR("fd00::/64"),
242+
AllowedIPs: []netip.Prefix{
243+
netip.MustParsePrefix("192.168.1.0/24"),
244+
netip.MustParsePrefix("fd00::/64"),
245245
},
246246
ProtocolVersion: 1,
247247
},
248248
{
249249
PublicKey: peerB,
250250
Endpoint: wgtest.MustUDPAddr("[::1]:2048"),
251-
AllowedIPs: []net.IPNet{wgtest.MustCIDR("2001:db8::1/128")},
251+
AllowedIPs: []netip.Prefix{netip.MustParsePrefix("2001:db8::1/128")},
252252
},
253253
{
254254
PublicKey: peerC,
255-
AllowedIPs: []net.IPNet{},
255+
AllowedIPs: []netip.Prefix{},
256256
},
257257
},
258258
}

0 commit comments

Comments
 (0)