mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-05 16:12:20 -08:00
Provide a shared TCP configuration helper for future DERP, control, and log-upload socket policy. Apply optional keepalive and user-timeout settings before protocol wrapping, and report each result independently so callers can choose whether to fall back or reject a connection. Add a checked user-timeout setter that validates duration bounds and reports unsupported platforms or sockets instead of silently succeeding. Preserve the existing best-effort APIs and all service defaults. Reuse net.TCPConn keepalive configuration; do not introduce RTO tuning or infer hardware offload support. Test actual Linux socket settings on dialed and accepted connections, zero-config behavior, invalid values, partial failures, and timeout reset. Updates tailscale/corp#50948 Change-Id: I8151ab81b8630610ba3ef2e2ec54b62caebadf65 Signed-off-by: James Tucker <[email protected]>
26 lines
675 B
Go
26 lines
675 B
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package ktimeout
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// SetUserTimeout sets the TCP_USER_TIMEOUT option on the given file descriptor.
|
|
func SetUserTimeout(fd uintptr, timeout time.Duration) error {
|
|
return unix.SetsockoptInt(int(fd), unix.SOL_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
|
|
}
|
|
|
|
func setUserTimeoutChecked(fd uintptr, timeout time.Duration) error {
|
|
err := SetUserTimeout(fd, timeout)
|
|
if errors.Is(err, unix.ENOPROTOOPT) || errors.Is(err, unix.EOPNOTSUPP) {
|
|
return fmt.Errorf("%w: %w", errors.ErrUnsupported, err)
|
|
}
|
|
return err
|
|
}
|