Skip to content

Commit 91e55d9

Browse files
committed
Remove deprecated netaddr package
1 parent aeacae4 commit 91e55d9

10 files changed

+47
-67
lines changed

config.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/netip"
67
"os"
78
"strconv"
89
"strings"
910
"time"
10-
11-
"inet.af/netaddr"
1211
)
1312

1413
const (
@@ -146,7 +145,7 @@ type serverConfig struct {
146145
KeyFilename string
147146
LeaserSyncInterval time.Duration
148147
LeasesFilename string
149-
WireguardIPPrefix netaddr.IPPrefix
148+
WireguardIPPrefix netip.Prefix
150149
WireguardListenPort int
151150
OauthIntrospectURL string
152151
OauthClientID string
@@ -194,17 +193,13 @@ func verifyServerConfig(conf *serverConfig) error {
194193
if conf.Address == "" {
195194
return fmt.Errorf("config missing `address`")
196195
}
197-
ipPrefix, err := netaddr.ParseIPPrefix(conf.Address)
198-
if err != nil {
199-
return fmt.Errorf("could not parse address as a CIDR: %w", err)
200-
}
201-
conf.WireguardIPPrefix = ipPrefix
196+
conf.WireguardIPPrefix = netip.MustParsePrefix(conf.Address)
202197
if len(conf.AllowedIPs) == 0 {
203198
logger.Verbosef("config missing `allowedIPs`, this server is not exposing any networks")
204199
}
205200
// Append the server wg /32 ip to the allowed ips in case the agent
206201
// wants to ping it for health checking
207-
conf.AllowedIPs = append(conf.AllowedIPs, fmt.Sprintf("%s/%s", conf.WireguardIPPrefix.IP().String(), "32"))
202+
conf.AllowedIPs = append(conf.AllowedIPs, conf.WireguardIPPrefix.String())
208203

209204
if conf.DeviceName == "" {
210205
conf.DeviceName = defaultWireguardDeviceName

config_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package main
22

33
import (
44
"encoding/json"
5+
"net/netip"
56
"testing"
67
"time"
78

89
"github.com/stretchr/testify/assert"
9-
"inet.af/netaddr"
1010
)
1111

1212
func TestAgentConfigFmt(t *testing.T) {
@@ -137,7 +137,7 @@ func TestAgentConfigFmt(t *testing.T) {
137137
func TestServerConfig(t *testing.T) {
138138
setLogLevel("error")
139139
logger = newLogger("wiresteward-test")
140-
ipPrefix := netaddr.MustParseIPPrefix("10.0.0.1/24")
140+
ipPrefix := netip.MustParsePrefix("10.0.0.1/24")
141141
testCases := []struct {
142142
input []byte
143143
cfg *serverConfig

device.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/coreos/go-iptables/iptables"
1313
"github.com/vishvananda/netlink"
14+
"go4.org/netipx"
1415
"golang.org/x/sys/unix"
1516
"golang.zx2c4.com/wireguard/conn"
1617
"golang.zx2c4.com/wireguard/device"
@@ -224,7 +225,7 @@ func newServerDevice(cfg *serverConfig) *ServerDevice {
224225
}
225226
return &ServerDevice{
226227
deviceAddress: netlink.Addr{
227-
IPNet: cfg.WireguardIPPrefix.IPNet(),
228+
IPNet: netipx.PrefixIPNet(cfg.WireguardIPPrefix),
228229
},
229230
deviceMTU: cfg.DeviceMTU,
230231
iptablesRule: []string{

go.mod

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ require (
2020
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b
2121
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
2222
gopkg.in/square/go-jose.v2 v2.6.0
23-
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a
2423
)
2524

25+
require go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
26+
2627
require (
2728
github.com/beorn7/perks v1.0.1 // indirect
2829
github.com/cespare/xxhash/v2 v2.3.0 // indirect
@@ -35,8 +36,6 @@ require (
3536
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
3637
github.com/pmezard/go-difflib v1.0.0 // indirect
3738
github.com/prometheus/client_model v0.6.1 // indirect
38-
go4.org/intern v0.0.0-20230525184215-6c62f75575cb // indirect
39-
go4.org/unsafe/assume-no-moving-gc v0.0.0-20230525183740-e7c30c78aeb2 // indirect
4039
golang.org/x/sync v0.7.0 // indirect
4140
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2 // indirect
4241
google.golang.org/protobuf v1.34.2 // indirect

go.sum

+2-17
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ github.com/coreos/go-iptables v0.8.0/go.mod h1:Qe8Bv2Xik5FyTXwgIbLAnv2sWSBmvWdFE
1414
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1515
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1616
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
17-
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
1817
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
1918
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
2019
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
@@ -108,27 +107,19 @@ github.com/vishvananda/netlink v1.3.0 h1:X7l42GfcV4S6E4vHTsw48qbrV+9PVojNfIhZcwQ
108107
github.com/vishvananda/netlink v1.3.0/go.mod h1:i6NetklAujEcC6fK0JPjT8qSwWyO0HLn4UKG+hGqeJs=
109108
github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8=
110109
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
111-
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
112110
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
113-
go4.org/intern v0.0.0-20211027215823-ae77deb06f29/go.mod h1:cS2ma+47FKrLPdXFpr7CuxiTW3eyJbWew4qx0qtQWDA=
114-
go4.org/intern v0.0.0-20230525184215-6c62f75575cb h1:ae7kzL5Cfdmcecbh22ll7lYP3iuUdnfnhiPcSaDgH/8=
115-
go4.org/intern v0.0.0-20230525184215-6c62f75575cb/go.mod h1:Ycrt6raEcnF5FTsLiLKkhBTO6DPX3RCUCUVnks3gFJU=
116-
go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
117-
go4.org/unsafe/assume-no-moving-gc v0.0.0-20230525183740-e7c30c78aeb2 h1:WJhcL4p+YeDxmZWg141nRm7XC8IDmhz7lk5GpadO1Sg=
118-
go4.org/unsafe/assume-no-moving-gc v0.0.0-20230525183740-e7c30c78aeb2/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
111+
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBseWJUpBw5I82+2U4M=
112+
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y=
119113
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
120114
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
121115
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
122-
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
123116
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
124117
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
125-
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
126118
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
127119
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
128120
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
129121
golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
130122
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
131-
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
132123
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
133124
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
134125
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
@@ -138,7 +129,6 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ
138129
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
139130
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
140131
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
141-
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
142132
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
143133
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
144134
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
@@ -150,9 +140,7 @@ golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7w
150140
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
151141
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
152142
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
153-
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
154143
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
155-
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
156144
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
157145
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
158146
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -165,7 +153,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
165153
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
166154
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
167155
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
168-
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
169156
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
170157
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
171158
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -198,5 +185,3 @@ gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
198185
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
199186
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
200187
gvisor.dev/gvisor v0.0.0-20221203005347-703fd9b7fbc0 h1:Wobr37noukisGxpKo5jAsLREcpj61RxrWYzD8uwveOY=
201-
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a h1:1XCVEdxrvL6c0TGOhecLuB7U9zYNdxZEjvOqJreKZiM=
202-
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a/go.mod h1:e83i32mAQOW1LAqEIweALsuK2Uw4mhQadA5r7b0Wobo=

lease.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ package main
55
import (
66
"bufio"
77
"fmt"
8+
"net/netip"
89
"os"
910
"path/filepath"
1011
"strings"
1112
"sync"
1213
"time"
1314

15+
"go4.org/netipx"
1416
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
15-
"inet.af/netaddr"
1617
)
1718

1819
// WGRecord describes a lease entry for a peer.
1920
type WGRecord struct {
2021
PubKey string
21-
IP netaddr.IP
22+
IP netip.Addr
2223
expires time.Time
2324
}
2425

@@ -31,7 +32,7 @@ func (wgr WGRecord) String() string {
3132
type fileLeaseManager struct {
3233
deviceName string
3334
filename string
34-
ipPrefix netaddr.IPPrefix
35+
ipPrefix netip.Prefix
3536
wgRecords map[string]WGRecord
3637
wgRecordsMutex sync.Mutex
3738
}
@@ -92,10 +93,7 @@ func (lm *fileLeaseManager) loadWgRecords() error {
9293

9394
username := tokens[0]
9495
pubKey := tokens[1]
95-
ipaddr, err := netaddr.ParseIP(tokens[2])
96-
if err != nil {
97-
return err
98-
}
96+
ipaddr := netip.MustParseAddr(tokens[2])
9997
expires, err := time.Parse(time.RFC3339, tokens[3])
10098
if err != nil {
10199
return fmt.Errorf("expected time of exipry in RFC3339 format, got: %v", tokens[2])
@@ -208,16 +206,17 @@ func (lm *fileLeaseManager) addNewPeer(username, pubKey string, expiry time.Time
208206
// - remove the *first* and *last* address (reserved)
209207
// https://en.wikipedia.org/wiki/IPv4#First_and_last_subnet_addresses
210208
// - remove all already leased addresses
209+
//
211210
// Remaining IPs are "available", get the first one
212-
func (lm *fileLeaseManager) nextAvailableAddress() netaddr.IP {
213-
var b netaddr.IPSetBuilder
211+
func (lm *fileLeaseManager) nextAvailableAddress() netip.Addr {
212+
var b netipx.IPSetBuilder
214213
b.AddPrefix(lm.ipPrefix)
215-
b.Remove(lm.ipPrefix.IP())
216-
b.Remove(lm.ipPrefix.Range().From())
217-
b.Remove(lm.ipPrefix.Range().To())
214+
b.Remove(lm.ipPrefix.Addr())
215+
b.Remove(lm.ipPrefix.Masked().Addr())
216+
b.Remove(netipx.PrefixLastIP(lm.ipPrefix))
218217
for _, r := range lm.wgRecords {
219218
b.Remove(r.IP)
220219
}
221220
a, _ := b.IPSet()
222-
return a.Prefixes()[0].IP()
221+
return a.Prefixes()[0].Addr()
223222
}

lease_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package main
22

33
import (
44
"fmt"
5+
"net/netip"
56
"testing"
67
"time"
78

89
"github.com/stretchr/testify/assert"
9-
"inet.af/netaddr"
1010
)
1111

1212
func TestFileLeaseManager_createOrUpdatePeer(t *testing.T) {
13-
ipPrefix := netaddr.MustParseIPPrefix("10.90.0.1/20")
13+
ipPrefix := netip.MustParsePrefix("10.90.0.1/20")
1414
lm := &fileLeaseManager{
1515
wgRecords: map[string]WGRecord{},
1616
ipPrefix: ipPrefix,
@@ -25,7 +25,7 @@ func TestFileLeaseManager_createOrUpdatePeer(t *testing.T) {
2525
if err != nil {
2626
t.Fatal(err)
2727
}
28-
if record.IP.Compare(netaddr.MustParseIP("10.90.0.2")) != 0 {
28+
if record.IP != netip.MustParseAddr("10.90.0.2") {
2929
t.Fatalf("Unexpected IP returned %s", record.IP.String())
3030
}
3131
assert.Equal(t, 1, len(lm.wgRecords))
@@ -47,15 +47,15 @@ func TestFileLeaseManager_createOrUpdatePeer(t *testing.T) {
4747
}
4848

4949
func TestGetAvailableIPAddresses(t *testing.T) {
50-
ipPrefix := netaddr.MustParseIPPrefix("10.90.0.1/20")
50+
ipPrefix := netip.MustParsePrefix("10.90.0.1/20")
5151
r1 := WGRecord{
5252
PubKey: "k1a1fEw+lqB/JR1pKjI597R54xzfP9Kxv4M7hufyNAY=",
53-
IP: netaddr.MustParseIP("10.90.0.2"),
53+
IP: netip.MustParseAddr("10.90.0.2"),
5454
expires: time.Unix(0, 0)}
5555

5656
r2 := WGRecord{
5757
PubKey: "E1gSkv2jS/P+p8YYmvm7ByEvwpLPqQBdx70SPtNSwCo=",
58-
IP: netaddr.MustParseIP("10.90.0.4"),
58+
IP: netip.MustParseAddr("10.90.0.4"),
5959
expires: time.Unix(0, 0)}
6060

6161
lm := &fileLeaseManager{
@@ -65,11 +65,11 @@ func TestGetAvailableIPAddresses(t *testing.T) {
6565

6666
testCases := []struct {
6767
t fileLeaseManager
68-
e netaddr.IP
68+
e netip.Addr
6969
}{
7070
{
7171
t: *lm,
72-
e: netaddr.IPv4(10, 90, 0, 3),
72+
e: netip.MustParseAddr("10.90.0.3"),
7373
},
7474
}
7575
for _, test := range testCases {

metrics_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package main
33
import (
44
"fmt"
55
"net"
6+
"net/netip"
67
"testing"
78
"time"
89

910
"github.com/mdlayher/promtest"
1011
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
11-
"inet.af/netaddr"
1212
)
1313

1414
func TestCollector(t *testing.T) {
@@ -87,12 +87,12 @@ func TestCollector(t *testing.T) {
8787
wgRecords: map[string]WGRecord{
8888
userA: WGRecord{
8989
PubKey: pubPeerA.String(),
90-
IP: netaddr.MustParseIP("10.0.0.1"),
90+
IP: netip.MustParseAddr("10.0.0.1"),
9191
expires: time.Unix(100, 0),
9292
},
9393
userB: WGRecord{
9494
PubKey: pubPeerB.String(),
95-
IP: netaddr.MustParseIP("10.0.0.3"),
95+
IP: netip.MustParseAddr("10.0.0.3"),
9696
},
9797
},
9898
},

ping.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ package main
55
import (
66
"fmt"
77
"net"
8+
"net/netip"
89
"os"
910
"time"
1011

1112
"golang.org/x/net/icmp"
1213
"golang.org/x/net/ipv4"
13-
"inet.af/netaddr"
1414
)
1515

1616
var nextPingCheckerID = os.Getpid() & 0xffff
1717

1818
type pingChecker struct {
19-
IP netaddr.IP
19+
IP netip.Addr
2020
ID int
2121
Seqnum int
2222
Timeout Duration
@@ -28,10 +28,7 @@ type checker interface {
2828
}
2929

3030
func newPingChecker(device, address string, timeout Duration) (*pingChecker, error) {
31-
ip, err := netaddr.ParseIP(address)
32-
if err != nil {
33-
return nil, fmt.Errorf("No valid ip for %s", address)
34-
}
31+
ip := netip.MustParseAddr(address)
3532
id := nextPingCheckerID
3633
nextPingCheckerID++
3734
return &pingChecker{
@@ -69,14 +66,18 @@ func newICMPv4EchoRequest(id, seqnum int, data []byte) ([]byte, error) {
6966
return wm.Marshal(nil)
7067
}
7168

72-
func exchangeICMPEcho(ip netaddr.IP, timeout time.Duration, echo []byte) error {
69+
func exchangeICMPEcho(ip netip.Addr, timeout time.Duration, echo []byte) error {
7370
c, err := net.ListenPacket("ip4:icmp", "")
7471
if err != nil {
7572
return err
7673
}
7774
defer c.Close()
7875

79-
_, err = c.WriteTo(echo, ip.IPAddr())
76+
ipAddr := &net.IPAddr{
77+
IP: ip.AsSlice(),
78+
}
79+
80+
_, err = c.WriteTo(echo, ipAddr)
8081
if err != nil {
8182
return err
8283
}
@@ -88,8 +89,8 @@ func exchangeICMPEcho(ip netaddr.IP, timeout time.Duration, echo []byte) error {
8889
if err != nil {
8990
return err
9091
}
91-
rip := netaddr.MustParseIP(addr.String())
92-
if ip.Compare(rip) != 0 {
92+
rip := netip.MustParseAddr(addr.String())
93+
if ip != rip {
9394
continue
9495
}
9596
// 1 == ipv4 ICMP proto number

0 commit comments

Comments
 (0)