diff --git a/net/netcheck/netcheck.go b/net/netcheck/netcheck.go index 5c4a6391d..8b83ff830 100644 --- a/net/netcheck/netcheck.go +++ b/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() diff --git a/types/nettype/nettype.go b/types/nettype/nettype.go index 3f0edd6a2..bd5dafb4c 100644 --- a/types/nettype/nettype.go +++ b/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 { diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index 6fe0e8881..f92e4996a 100644 --- a/wgengine/magicsock/magicsock.go +++ b/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