all: use various net/netip parse funcs directly

Mechanical change with perl+goimports.

Changed {Must,}Parse{IP,IPPrefix,IPPort} to their netip variants, then
goimports -d .

Finally, removed the net/netaddr wrappers, to prevent future use.

Updates #5162

Change-Id: I59c0e38b5fbca5a935d701645789cddf3d7863ad
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2022-07-25 20:55:44 -07:00
committed by Brad Fitzpatrick
parent 730ca4203c
commit 6a396731eb
84 changed files with 401 additions and 355 deletions
+3 -3
View File
@@ -13,19 +13,19 @@ import (
"net"
"net/http"
"net/http/pprof"
"net/netip"
"os"
"strconv"
"time"
"tailscale.com/net/netaddr"
"tailscale.com/types/logger"
)
const PayloadSize = 1000
const ICMPMinSize = 24
var Addr1 = netaddr.MustParseIPPrefix("100.64.1.1/32")
var Addr2 = netaddr.MustParseIPPrefix("100.64.1.2/32")
var Addr1 = netip.MustParsePrefix("100.64.1.1/32")
var Addr2 = netip.MustParsePrefix("100.64.1.2/32")
func main() {
var logf logger.Logf = log.Printf
+20 -19
View File
@@ -7,6 +7,7 @@ package filter
import (
"encoding/hex"
"fmt"
"net/netip"
"reflect"
"strconv"
"strings"
@@ -439,16 +440,16 @@ func TestLoggingPrivacy(t *testing.T) {
}
var logB netipx.IPSetBuilder
logB.AddPrefix(netaddr.MustParseIPPrefix("100.64.0.0/10"))
logB.AddPrefix(netip.MustParsePrefix("100.64.0.0/10"))
logB.AddPrefix(tsaddr.TailscaleULARange())
f := newFilter(logf)
f.logIPs, _ = logB.IPSet()
var (
ts4 = netaddr.IPPortFrom(tsaddr.CGNATRange().Addr().Next(), 1234)
internet4 = netaddr.IPPortFrom(netaddr.MustParseIP("8.8.8.8"), 1234)
internet4 = netaddr.IPPortFrom(netip.MustParseAddr("8.8.8.8"), 1234)
ts6 = netaddr.IPPortFrom(tsaddr.TailscaleULARange().Addr().Next(), 1234)
internet6 = netaddr.IPPortFrom(netaddr.MustParseIP("2001::1"), 1234)
internet6 = netaddr.IPPortFrom(netip.MustParseAddr("2001::1"), 1234)
)
tests := []struct {
@@ -551,7 +552,7 @@ func TestLoggingPrivacy(t *testing.T) {
}
}
var mustIP = netaddr.MustParseIP
var mustIP = netip.MustParseAddr
func parsed(proto ipproto.Proto, src, dst string, sport, dport uint16) packet.Parsed {
sip, dip := mustIP(src), mustIP(dst)
@@ -657,7 +658,7 @@ func parseHexPkt(t *testing.T, h string) *packet.Parsed {
}
func mustIPPort(s string) netaddr.IPPort {
ipp, err := netaddr.ParseIPPort(s)
ipp, err := netip.ParseAddrPort(s)
if err != nil {
panic(err)
}
@@ -666,7 +667,7 @@ func mustIPPort(s string) netaddr.IPPort {
func pfx(strs ...string) (ret []netaddr.IPPrefix) {
for _, s := range strs {
pfx, err := netaddr.ParseIPPrefix(s)
pfx, err := netip.ParsePrefix(s)
if err != nil {
panic(err)
}
@@ -678,7 +679,7 @@ func pfx(strs ...string) (ret []netaddr.IPPrefix) {
func nets(nets ...string) (ret []netaddr.IPPrefix) {
for _, s := range nets {
if !strings.Contains(s, "/") {
ip, err := netaddr.ParseIP(s)
ip, err := netip.ParseAddr(s)
if err != nil {
panic(err)
}
@@ -688,7 +689,7 @@ func nets(nets ...string) (ret []netaddr.IPPrefix) {
}
ret = append(ret, netaddr.IPPrefixFrom(ip, bits))
} else {
pfx, err := netaddr.ParseIPPrefix(s)
pfx, err := netip.ParsePrefix(s)
if err != nil {
panic(err)
}
@@ -770,16 +771,16 @@ func TestMatchesFromFilterRules(t *testing.T) {
},
Dsts: []NetPortRange{
{
Net: netaddr.MustParseIPPrefix("0.0.0.0/0"),
Net: netip.MustParsePrefix("0.0.0.0/0"),
Ports: PortRange{22, 22},
},
{
Net: netaddr.MustParseIPPrefix("::0/0"),
Net: netip.MustParsePrefix("::0/0"),
Ports: PortRange{22, 22},
},
},
Srcs: []netaddr.IPPrefix{
netaddr.MustParseIPPrefix("100.64.1.1/32"),
netip.MustParsePrefix("100.64.1.1/32"),
},
Caps: []CapMatch{},
},
@@ -804,12 +805,12 @@ func TestMatchesFromFilterRules(t *testing.T) {
},
Dsts: []NetPortRange{
{
Net: netaddr.MustParseIPPrefix("1.2.0.0/16"),
Net: netip.MustParsePrefix("1.2.0.0/16"),
Ports: PortRange{22, 22},
},
},
Srcs: []netaddr.IPPrefix{
netaddr.MustParseIPPrefix("100.64.1.1/32"),
netip.MustParsePrefix("100.64.1.1/32"),
},
Caps: []CapMatch{},
},
@@ -834,8 +835,8 @@ func TestMatchesFromFilterRules(t *testing.T) {
func TestNewAllowAllForTest(t *testing.T) {
f := NewAllowAllForTest(logger.Discard)
src := netaddr.MustParseIP("100.100.2.3")
dst := netaddr.MustParseIP("100.100.1.2")
src := netip.MustParseAddr("100.100.2.3")
dst := netip.MustParseAddr("100.100.1.2")
res := f.CheckTCP(src, dst, 80)
if res.IsDrop() {
t.Fatalf("unexpected drop verdict: %v", res)
@@ -885,7 +886,7 @@ func TestCaps(t *testing.T) {
SrcIPs: []string{"*"},
CapGrant: []tailcfg.CapGrant{{
Dsts: []netaddr.IPPrefix{
netaddr.MustParseIPPrefix("0.0.0.0/0"),
netip.MustParsePrefix("0.0.0.0/0"),
},
Caps: []string{"is_ipv4"},
}},
@@ -894,7 +895,7 @@ func TestCaps(t *testing.T) {
SrcIPs: []string{"*"},
CapGrant: []tailcfg.CapGrant{{
Dsts: []netaddr.IPPrefix{
netaddr.MustParseIPPrefix("::/0"),
netip.MustParsePrefix("::/0"),
},
Caps: []string{"is_ipv6"},
}},
@@ -903,7 +904,7 @@ func TestCaps(t *testing.T) {
SrcIPs: []string{"100.199.0.0/16"},
CapGrant: []tailcfg.CapGrant{{
Dsts: []netaddr.IPPrefix{
netaddr.MustParseIPPrefix("100.200.0.0/16"),
netip.MustParsePrefix("100.200.0.0/16"),
},
Caps: []string{"some_super_admin"},
}},
@@ -951,7 +952,7 @@ func TestCaps(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := filt.AppendCaps(nil, netaddr.MustParseIP(tt.src), netaddr.MustParseIP(tt.dst))
got := filt.AppendCaps(nil, netip.MustParseAddr(tt.src), netip.MustParseAddr(tt.dst))
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %q; want %q", got, tt.want)
}
+5 -4
View File
@@ -6,6 +6,7 @@ package filter
import (
"fmt"
"net/netip"
"strings"
"go4.org/netipx"
@@ -122,7 +123,7 @@ func parseIPSet(arg string, bits *int) ([]netaddr.IPPrefix, error) {
}, nil
}
if strings.Contains(arg, "/") {
pfx, err := netaddr.ParseIPPrefix(arg)
pfx, err := netip.ParsePrefix(arg)
if err != nil {
return nil, err
}
@@ -133,11 +134,11 @@ func parseIPSet(arg string, bits *int) ([]netaddr.IPPrefix, error) {
}
if strings.Count(arg, "-") == 1 {
ip1s, ip2s, _ := strings.Cut(arg, "-")
ip1, err := netaddr.ParseIP(ip1s)
ip1, err := netip.ParseAddr(ip1s)
if err != nil {
return nil, err
}
ip2, err := netaddr.ParseIP(ip2s)
ip2, err := netip.ParseAddr(ip2s)
if err != nil {
return nil, err
}
@@ -147,7 +148,7 @@ func parseIPSet(arg string, bits *int) ([]netaddr.IPPrefix, error) {
}
return r.Prefixes(), nil
}
ip, err := netaddr.ParseIP(arg)
ip, err := netip.ParseAddr(arg)
if err != nil {
return nil, fmt.Errorf("invalid IP address %q", arg)
}
+6 -6
View File
@@ -441,7 +441,7 @@ func (c *Conn) addDerpPeerRoute(peer key.NodePublic, derpID int, dc *derphttp.Cl
mak.Set(&c.derpRoute, peer, derpRoute{derpID, dc})
}
var derpMagicIPAddr = netaddr.MustParseIP(tailcfg.DerpMagicIP)
var derpMagicIPAddr = netip.MustParseAddr(tailcfg.DerpMagicIP)
// activeDerp contains fields for an active DERP connection.
type activeDerp struct {
@@ -1034,7 +1034,7 @@ func (c *Conn) determineEndpoints(ctx context.Context) ([]tailcfg.Endpoint, erro
// back.
return []tailcfg.Endpoint{
{
Addr: netaddr.MustParseIPPort("[fe80:123:456:789::1]:12345"),
Addr: netip.MustParseAddrPort("[fe80:123:456:789::1]:12345"),
Type: tailcfg.EndpointLocal,
},
}, nil
@@ -1044,7 +1044,7 @@ func (c *Conn) determineEndpoints(ctx context.Context) ([]tailcfg.Endpoint, erro
var eps []tailcfg.Endpoint // unique endpoints
ipp := func(s string) (ipp netaddr.IPPort) {
ipp, _ = netaddr.ParseIPPort(s)
ipp, _ = netip.ParseAddrPort(s)
return
}
addAddr := func(ipp netaddr.IPPort, et tailcfg.EndpointType) {
@@ -2359,7 +2359,7 @@ func (c *Conn) SetNetworkMap(nm *netmap.NetworkMap) {
c.logf("magicsock: created endpoint key=%s: disco=%s; %v", n.Key.ShortString(), n.DiscoKey.ShortString(), logger.ArgWriter(func(w *bufio.Writer) {
const derpPrefix = "127.3.3.40:"
if strings.HasPrefix(n.DERP, derpPrefix) {
ipp, _ := netaddr.ParseIPPort(n.DERP)
ipp, _ := netip.ParseAddrPort(n.DERP)
regionID := int(ipp.Port())
code := c.derpRegionCodeLocked(regionID)
if code != "" {
@@ -3751,7 +3751,7 @@ func (de *endpoint) updateFromNode(n *tailcfg.Node) {
if n.DERP == "" {
de.derpAddr = netaddr.IPPort{}
} else {
de.derpAddr, _ = netaddr.ParseIPPort(n.DERP)
de.derpAddr, _ = netip.ParseAddrPort(n.DERP)
}
for _, st := range de.endpointState {
@@ -3762,7 +3762,7 @@ func (de *endpoint) updateFromNode(n *tailcfg.Node) {
// Seems unlikely.
continue
}
ipp, err := netaddr.ParseIPPort(epStr)
ipp, err := netip.ParseAddrPort(epStr)
if err != nil {
de.c.logf("magicsock: bogus netmap endpoint %q", epStr)
continue
+8 -8
View File
@@ -787,11 +787,11 @@ func TestActiveDiscovery(t *testing.T) {
inet := natlab.NewInternet()
lan1 := &natlab.Network{
Name: "lan1",
Prefix4: netaddr.MustParseIPPrefix("192.168.0.0/24"),
Prefix4: netip.MustParsePrefix("192.168.0.0/24"),
}
lan2 := &natlab.Network{
Name: "lan2",
Prefix4: netaddr.MustParseIPPrefix("192.168.1.0/24"),
Prefix4: netip.MustParsePrefix("192.168.1.0/24"),
}
sif := mstun.Attach("eth0", inet)
@@ -1010,24 +1010,24 @@ func testTwoDevicePing(t *testing.T, d *devices) {
m1cfg := &wgcfg.Config{
Name: "peer1",
PrivateKey: m1.privateKey,
Addresses: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.0.0.1/32")},
Addresses: []netaddr.IPPrefix{netip.MustParsePrefix("1.0.0.1/32")},
Peers: []wgcfg.Peer{
{
PublicKey: m2.privateKey.Public(),
DiscoKey: m2.conn.DiscoPublicKey(),
AllowedIPs: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.0.0.2/32")},
AllowedIPs: []netaddr.IPPrefix{netip.MustParsePrefix("1.0.0.2/32")},
},
},
}
m2cfg := &wgcfg.Config{
Name: "peer2",
PrivateKey: m2.privateKey,
Addresses: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.0.0.2/32")},
Addresses: []netaddr.IPPrefix{netip.MustParsePrefix("1.0.0.2/32")},
Peers: []wgcfg.Peer{
{
PublicKey: m1.privateKey.Public(),
DiscoKey: m1.conn.DiscoPublicKey(),
AllowedIPs: []netaddr.IPPrefix{netaddr.MustParseIPPrefix("1.0.0.1/32")},
AllowedIPs: []netaddr.IPPrefix{netip.MustParsePrefix("1.0.0.1/32")},
},
},
}
@@ -1247,7 +1247,7 @@ func addTestEndpoint(tb testing.TB, conn *Conn, sendConn net.PacketConn) (key.No
if err != nil {
tb.Fatal(err)
}
conn.addValidDiscoPathForTest(nodeKey, netaddr.MustParseIPPort(sendConn.LocalAddr().String()))
conn.addValidDiscoPathForTest(nodeKey, netip.MustParseAddrPort(sendConn.LocalAddr().String()))
return nodeKey, discoKey
}
@@ -1597,7 +1597,7 @@ func TestEndpointSetsEqual(t *testing.T) {
func TestBetterAddr(t *testing.T) {
const ms = time.Millisecond
al := func(ipps string, d time.Duration) addrLatency {
return addrLatency{netaddr.MustParseIPPort(ipps), d}
return addrLatency{netip.MustParseAddrPort(ipps), d}
}
zero := addrLatency{}
tests := []struct {
+1 -1
View File
@@ -29,7 +29,7 @@ import (
"tailscale.com/wgengine/winnet"
)
var wintunLinkLocal = netaddr.MustParseIP("fe80::99d0:ec2d:b2e7:536b")
var wintunLinkLocal = netip.MustParseAddr("fe80::99d0:ec2d:b2e7:536b")
// monitorDefaultRoutes subscribes to route change events and updates
// the Tailscale tunnel interface's MTU to match that of the
+2 -2
View File
@@ -8,12 +8,12 @@ import (
"fmt"
"math/rand"
"net"
"net/netip"
"strings"
"testing"
"go4.org/netipx"
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
"tailscale.com/net/netaddr"
)
func randIP() net.IP {
@@ -35,7 +35,7 @@ func randRouteData() *winipcfg.RouteData {
func TestRouteLess(t *testing.T) {
type D = winipcfg.RouteData
ipnet := func(s string) net.IPNet {
ipp, err := netaddr.ParseIPPrefix(s)
ipp, err := netip.ParsePrefix(s)
if err != nil {
t.Fatalf("error parsing test data %q: %v", s, err)
}
+2 -2
View File
@@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"math/rand"
"net/netip"
"os"
"sort"
"strings"
@@ -17,7 +18,6 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/vishvananda/netlink"
"golang.zx2c4.com/wireguard/tun"
"tailscale.com/net/netaddr"
"tailscale.com/tstest"
"tailscale.com/types/logger"
"tailscale.com/wgengine/monitor"
@@ -711,7 +711,7 @@ func TestDelRouteIdempotent(t *testing.T) {
"192.0.2.0/24", // RFC 5737
"2001:DB8::/32", // RFC 3849
} {
cidr := netaddr.MustParseIPPrefix(s)
cidr := netip.MustParsePrefix(s)
if err := lt.r.addRoute(cidr); err != nil {
t.Error(err)
continue
+3 -2
View File
@@ -5,6 +5,7 @@
package router
import (
"net/netip"
"reflect"
"testing"
@@ -15,7 +16,7 @@ import (
func mustCIDRs(ss ...string) []netaddr.IPPrefix {
var ret []netaddr.IPPrefix
for _, s := range ss {
ret = append(ret, netaddr.MustParseIPPrefix(s))
ret = append(ret, netip.MustParsePrefix(s))
}
return ret
}
@@ -37,7 +38,7 @@ func TestConfigEqual(t *testing.T) {
nets := func(strs ...string) (ns []netaddr.IPPrefix) {
for _, s := range strs {
n, err := netaddr.ParseIPPrefix(s)
n, err := netip.ParsePrefix(s)
if err != nil {
panic(err)
}
+2 -2
View File
@@ -1589,9 +1589,9 @@ func dnsIPsOverTailscale(dnsCfg *dns.Config, routerCfg *router.Config) (ret []ne
add := func(resolvers []*dnstype.Resolver) {
for _, r := range resolvers {
ip, err := netaddr.ParseIP(r.Addr)
ip, err := netip.ParseAddr(r.Addr)
if err != nil {
if ipp, err := netaddr.ParseIPPort(r.Addr); err == nil {
if ipp, err := netip.ParseAddrPort(r.Addr); err == nil {
ip = ipp.Addr()
} else {
continue
+4 -3
View File
@@ -6,6 +6,7 @@ package wgengine
import (
"fmt"
"net/netip"
"reflect"
"testing"
@@ -223,9 +224,9 @@ func nkFromHex(hex string) key.NodePublic {
// an experiment to see if genLocalAddrFunc was worth it. As of Go
// 1.16, it still very much is. (30-40x faster)
func BenchmarkGenLocalAddrFunc(b *testing.B) {
la1 := netaddr.MustParseIP("1.2.3.4")
la2 := netaddr.MustParseIP("::4")
lanot := netaddr.MustParseIP("5.5.5.5")
la1 := netip.MustParseAddr("1.2.3.4")
la2 := netip.MustParseAddr("::4")
lanot := netip.MustParseAddr("5.5.5.5")
var x bool
b.Run("map1", func(b *testing.B) {
b.ReportAllocs()
+3 -3
View File
@@ -30,13 +30,13 @@ func TestDeviceConfig(t *testing.T) {
return k.Public(), k
}
k1, pk1 := newK()
ip1 := netaddr.MustParseIPPrefix("10.0.0.1/32")
ip1 := netip.MustParsePrefix("10.0.0.1/32")
k2, pk2 := newK()
ip2 := netaddr.MustParseIPPrefix("10.0.0.2/32")
ip2 := netip.MustParsePrefix("10.0.0.2/32")
k3, _ := newK()
ip3 := netaddr.MustParseIPPrefix("10.0.0.3/32")
ip3 := netip.MustParsePrefix("10.0.0.3/32")
cfg1 := &Config{
PrivateKey: pk1,
+2 -1
View File
@@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"io"
"net/netip"
"reflect"
"runtime"
"testing"
@@ -67,7 +68,7 @@ func BenchmarkFromUAPI(b *testing.B) {
return k.Public(), k
}
k1, pk1 := newK()
ip1 := netaddr.MustParseIPPrefix("10.0.0.1/32")
ip1 := netip.MustParsePrefix("10.0.0.1/32")
peer := Peer{
PublicKey: k1,