Skip to content

Commit

Permalink
[tailscale] net: add TS_PANIC_ON_TEST_LISTEN_UNSPEC env to panic on u…
Browse files Browse the repository at this point in the history
…nspecified addr listens

For debugging Mac firewall dialog boxes blocking+failing tests.

Change-Id: Ic1a0cd51de7fe553de1c1c9333fa1cc75b8490f2
(cherry picked from commit cd7323c)
(cherry picked from commit 8c9eff4)
  • Loading branch information
josharian authored and bradfitz committed Aug 21, 2024
1 parent c994ff5 commit 179980e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/net/tcpsock_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,34 @@ func (ln *TCPListener) file() (*os.File, error) {
return f, nil
}

// Tailscale addition: if TS_PANIC_ON_TEST_LISTEN_UNSPEC is set, panic
// if a listen tries to listen on all interfaces (for debugging Mac
// firewall dialogs in tests).
func panicOnUnspecListen(ip IP) bool {
if ip != nil && !ip.IsUnspecified() {
return false
}
v := os.Getenv("TS_PANIC_ON_TEST_LISTEN_UNSPEC")
if v == "" {
return false
}
switch v[0] {
case 't', 'T', '1':
return true
}
return false
}

func (sl *sysListener) listenTCP(ctx context.Context, laddr *TCPAddr) (*TCPListener, error) {
return sl.listenTCPProto(ctx, laddr, 0)
}

func (sl *sysListener) listenTCPProto(ctx context.Context, laddr *TCPAddr, proto int) (*TCPListener, error) {
var ctrlCtxFn func(ctx context.Context, network, address string, c syscall.RawConn) error
if panicOnUnspecListen(laddr.IP) {
panic("tailscale: can't listen on unspecified address in test")
}

var ctrlCtxFn func(cxt context.Context, network, address string, c syscall.RawConn) error
if sl.ListenConfig.Control != nil {
ctrlCtxFn = func(ctx context.Context, network, address string, c syscall.RawConn) error {
return sl.ListenConfig.Control(network, address, c)
Expand Down
6 changes: 5 additions & 1 deletion src/net/udpsock_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ func (sd *sysDialer) dialUDP(ctx context.Context, laddr, raddr *UDPAddr) (*UDPCo
}

func (sl *sysListener) listenUDP(ctx context.Context, laddr *UDPAddr) (*UDPConn, error) {
var ctrlCtxFn func(ctx context.Context, network, address string, c syscall.RawConn) error
if panicOnUnspecListen(laddr.IP) {
panic("tailscale: can't listen on unspecified address in test")
}

var ctrlCtxFn func(cxt context.Context, network, address string, c syscall.RawConn) error
if sl.ListenConfig.Control != nil {
ctrlCtxFn = func(ctx context.Context, network, address string, c syscall.RawConn) error {
return sl.ListenConfig.Control(network, address, c)
Expand Down

0 comments on commit 179980e

Please sign in to comment.