2020-11-25 14:01:53 +03:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2022-07-31 16:55:40 +03:00
|
|
|
"golang.org/x/sys/unix"
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// TCP_FASTOPEN_SERVER is the value to enable TCP fast open on darwin for server connections.
|
|
|
|
TCP_FASTOPEN_SERVER = 0x01
|
|
|
|
// TCP_FASTOPEN_CLIENT is the value to enable TCP fast open on darwin for client connections.
|
2022-07-31 16:55:40 +03:00
|
|
|
TCP_FASTOPEN_CLIENT = 0x02 // nolint: revive,stylecheck
|
|
|
|
// syscall.TCP_KEEPINTVL is missing on some darwin architectures.
|
|
|
|
sysTCP_KEEPINTVL = 0x101 // nolint: revive,stylecheck
|
2020-11-25 14:01:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if isTCPSocket(network) {
|
2021-03-30 19:42:02 +03:00
|
|
|
tfo := config.ParseTFOValue()
|
2021-03-06 17:45:12 +03:00
|
|
|
if tfo > 0 {
|
|
|
|
tfo = TCP_FASTOPEN_CLIENT
|
|
|
|
}
|
|
|
|
if tfo >= 0 {
|
2022-07-31 16:55:40 +03:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
|
2020-11-25 14:01:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 16:55:40 +03:00
|
|
|
|
|
|
|
if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
|
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPINTVL", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, sysTCP_KEEPINTVL, int(config.TcpKeepAliveIdle)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPIDLE", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
} else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 0); err != nil {
|
|
|
|
return newError("failed to unset SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if isTCPSocket(network) {
|
2021-03-30 19:42:02 +03:00
|
|
|
tfo := config.ParseTFOValue()
|
2021-03-06 17:45:12 +03:00
|
|
|
if tfo > 0 {
|
|
|
|
tfo = TCP_FASTOPEN_SERVER
|
|
|
|
}
|
|
|
|
if tfo >= 0 {
|
2022-07-31 16:55:40 +03:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
|
2020-11-25 14:01:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-07-31 16:55:40 +03:00
|
|
|
if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
|
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPINTVL", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, sysTCP_KEEPINTVL, int(config.TcpKeepAliveIdle)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPIDLE", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
} else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 0); err != nil {
|
|
|
|
return newError("failed to unset SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bindAddr(fd uintptr, address []byte, port uint32) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReuseAddr(fd uintptr) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReusePort(fd uintptr) error {
|
|
|
|
return nil
|
|
|
|
}
|