Files
KKRainbow 3d0c9c3ca5 chore(go): use lowercase module import path (#2560)
* chore(go): use lowercase module import path
* ci: scope checks for Go module changes
2026-09-10 15:13:55 +08:00

37 lines
962 B
Go

//go:build aix || android || darwin || dragonfly || freebsd || linux || netbsd || openbsd
package netstd
import (
"fmt"
"github.com/easytier/easytier/easytier-go/platform"
"golang.org/x/sys/unix"
)
func applyTCPSocketOptions(
descriptor uintptr,
network string,
options platform.TCPBindOptions,
) error {
fd := int(descriptor)
reuseAddr := true
if options.ReuseAddr != nil {
reuseAddr = *options.ReuseAddr
}
if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEADDR, boolInt(reuseAddr)); err != nil {
return fmt.Errorf("set TCP SO_REUSEADDR: %w", err)
}
if options.ReusePort {
if err := unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
return fmt.Errorf("set TCP SO_REUSEPORT: %w", err)
}
}
if options.OnlyV6 && network == "tcp6" {
if err := unix.SetsockoptInt(fd, unix.IPPROTO_IPV6, unix.IPV6_V6ONLY, 1); err != nil {
return fmt.Errorf("set TCP IPV6_V6ONLY: %w", err)
}
}
return nil
}