net/{netx,memnet},all: add netx.DialFunc, move memnet Network impl

This adds netx.DialFunc, unifying a type we have a bazillion other
places, giving it now a nice short name that's clickable in
editors, etc.

That highlighted that my earlier move (03b47a55c7) of stuff from
nettest into netx moved too much: it also dragged along the memnet
impl, meaning all users of netx.DialFunc who just wanted netx for the
type definition were instead also pulling in all of memnet.

So move the memnet implementation netx.Network into memnet, a package
we already had.

Then use netx.DialFunc in a bunch of places. I'm sure I missed some.
And plenty remain in other repos, to be updated later.

Updates tailscale/corp#27636

Change-Id: I7296cd4591218e8624e214f8c70dab05fb884e95
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick
2025-04-08 08:32:27 -07:00
committed by Brad Fitzpatrick
parent b95df54b06
commit fb96137d79
23 changed files with 135 additions and 113 deletions

View File

@@ -23,6 +23,7 @@ import (
"tailscale.com/net/netknob"
"tailscale.com/net/netmon"
"tailscale.com/net/netns"
"tailscale.com/net/netx"
"tailscale.com/net/tsaddr"
"tailscale.com/types/logger"
"tailscale.com/types/netmap"
@@ -71,7 +72,7 @@ type Dialer struct {
netnsDialerOnce sync.Once
netnsDialer netns.Dialer
sysDialForTest func(_ context.Context, network, addr string) (net.Conn, error) // or nil
sysDialForTest netx.DialFunc // or nil
routes atomic.Pointer[bart.Table[bool]] // or nil if UserDial should not use routes. `true` indicates routes that point into the Tailscale interface
@@ -364,7 +365,7 @@ func (d *Dialer) logf(format string, args ...any) {
// SetSystemDialerForTest sets an alternate function to use for SystemDial
// instead of netns.Dialer. This is intended for use with nettest.MemoryNetwork.
func (d *Dialer) SetSystemDialerForTest(fn func(ctx context.Context, network, addr string) (net.Conn, error)) {
func (d *Dialer) SetSystemDialerForTest(fn netx.DialFunc) {
testenv.AssertInTest()
d.sysDialForTest = fn
}