Browse Source

wgengine/magicsock, etc: remove mostly unused WriteTo methods

Updates #2331
Updates #5162

Change-Id: I8291884425481eeaedde38a54adfd8ed7292a497
Signed-off-by: Brad Fitzpatrick <[email protected]>
Brad Fitzpatrick 2 years ago
parent
commit
29f7df9d8f
3 changed files with 19 additions and 23 deletions
  1. 9 2
      net/netcheck/netcheck.go
  2. 10 1
      types/nettype/nettype.go
  3. 0 20
      wgengine/magicsock/magicsock.go

+ 9 - 2
net/netcheck/netcheck.go

@@ -517,9 +517,14 @@ func nodeMight4(n *tailcfg.DERPNode) bool {
 	return ip.Is4()
 }
 
+type packetReaderFromCloser interface {
+	ReadFrom([]byte) (int, net.Addr, error)
+	io.Closer
+}
+
 // readPackets reads STUN packets from pc until there's an error or ctx is done.
 // In either case, it closes pc.
-func (c *Client) readPackets(ctx context.Context, pc net.PacketConn) {
+func (c *Client) readPackets(ctx context.Context, pc packetReaderFromCloser) {
 	done := make(chan struct{})
 	defer close(done)
 
@@ -902,7 +907,9 @@ func (c *Client) GetReport(ctx context.Context, dm *tailcfg.DERPMap) (_ *Report,
 	// So do that for now. In the future we might want to classify networks
 	// that do and don't require this separately. But for now help it.
 	const documentationIP = "203.0.113.1"
-	rs.pc4Hair.WriteTo([]byte("tailscale netcheck; see https://github.com/tailscale/tailscale/issues/188"), &net.UDPAddr{IP: net.ParseIP(documentationIP), Port: 12345})
+	rs.pc4Hair.WriteToUDPAddrPort(
+		[]byte("tailscale netcheck; see https://github.com/tailscale/tailscale/issues/188"),
+		netip.AddrPortFrom(netip.MustParseAddr(documentationIP), 12345))
 
 	if f := c.GetSTUNConn4; f != nil {
 		rs.pc4 = f()

+ 10 - 1
types/nettype/nettype.go

@@ -6,8 +6,10 @@ package nettype
 
 import (
 	"context"
+	"io"
 	"net"
 	"net/netip"
+	"time"
 )
 
 // PacketListener defines the ListenPacket method as implemented
@@ -28,9 +30,16 @@ func (Std) ListenPacket(ctx context.Context, network, address string) (net.Packe
 	return conf.ListenPacket(ctx, network, address)
 }
 
+// PacketConn is a net.PacketConn that's about halfway (as of 2023-04-15)
+// converted to use netip.AddrPort.
 type PacketConn interface {
-	net.PacketConn
 	WriteToUDPAddrPort([]byte, netip.AddrPort) (int, error)
+	ReadFrom(p []byte) (int, net.Addr, error)
+	io.Closer
+	LocalAddr() net.Addr
+	SetDeadline(time.Time) error
+	SetReadDeadline(time.Time) error
+	SetWriteDeadline(time.Time) error
 }
 
 func MakePacketListenerWithNetIP(ln PacketListener) PacketListenerWithNetIP {

+ 0 - 20
wgengine/magicsock/magicsock.go

@@ -3433,10 +3433,6 @@ func (c *batchingUDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
 	return c.pc.ReadFrom(p)
 }
 
-func (c *batchingUDPConn) WriteTo(b []byte, addr net.Addr) (n int, err error) {
-	return c.pc.WriteTo(b, addr)
-}
-
 func (c *batchingUDPConn) SetDeadline(t time.Time) error {
 	return c.pc.SetDeadline(t)
 }
@@ -3867,17 +3863,6 @@ func (c *RebindingUDPConn) writeToUDPAddrPortWithInitPconn(pconn nettype.PacketC
 	}
 }
 
-func (c *RebindingUDPConn) WriteTo(b []byte, addr net.Addr) (int, error) {
-	for {
-		pconn := *c.pconnAtomic.Load()
-		n, err := pconn.WriteTo(b, addr)
-		if err != nil && pconn != c.currentConn() {
-			continue
-		}
-		return n, err
-	}
-}
-
 func (c *RebindingUDPConn) WriteToUDPAddrPort(b []byte, addr netip.AddrPort) (int, error) {
 	return c.writeToUDPAddrPortWithInitPconn(*c.pconnAtomic.Load(), b, addr)
 }
@@ -3904,11 +3889,6 @@ func (c *blockForeverConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
 	return 0, nil, net.ErrClosed
 }
 
-func (c *blockForeverConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
-	// Silently drop writes.
-	return len(p), nil
-}
-
 func (c *blockForeverConn) WriteToUDPAddrPort(p []byte, addr netip.AddrPort) (int, error) {
 	// Silently drop writes.
 	return len(p), nil