2018-09-10 08:39:57 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-10-14 14:18:09 -04:00
|
|
|
// TCP_FASTOPEN is the socket option on darwin for TCP fast open.
|
2021-11-27 04:16:41 -05:00
|
|
|
TCP_FASTOPEN = 0x105 // nolint: revive,stylecheck
|
2018-10-14 14:18:09 -04:00
|
|
|
// TCP_FASTOPEN_SERVER is the value to enable TCP fast open on darwin for server connections.
|
2021-11-27 04:16:41 -05:00
|
|
|
TCP_FASTOPEN_SERVER = 0x01 // nolint: revive,stylecheck
|
2018-10-14 14:18:09 -04:00
|
|
|
// TCP_FASTOPEN_CLIENT is the value to enable TCP fast open on darwin for client connections.
|
2021-11-27 04:16:41 -05:00
|
|
|
TCP_FASTOPEN_CLIENT = 0x02 // nolint: revive,stylecheck
|
2018-09-10 08:39:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
2018-09-11 04:03:26 -04:00
|
|
|
if isTCPSocket(network) {
|
2018-09-10 08:39:57 -04:00
|
|
|
switch config.Tfo {
|
|
|
|
case SocketConfig_Enable:
|
2018-09-10 15:47:24 -04:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_CLIENT); err != nil {
|
2018-09-10 08:39:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
2018-09-10 15:08:59 -04:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
|
2018-09-10 08:39:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 16:46:45 -05:00
|
|
|
|
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPINTVL", err)
|
|
|
|
}
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 08:39:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
2018-09-11 04:03:26 -04:00
|
|
|
if isTCPSocket(network) {
|
2018-09-10 08:39:57 -04:00
|
|
|
switch config.Tfo {
|
|
|
|
case SocketConfig_Enable:
|
2018-09-10 15:47:24 -04:00
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_SERVER); err != nil {
|
2018-09-10 08:39:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 16:46:45 -05:00
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPINTVL", err)
|
|
|
|
}
|
|
|
|
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 08:39:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-09-17 09:12:58 -04:00
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
func bindAddr(fd uintptr, address []byte, port uint32) error {
|
2018-09-17 09:12:58 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-30 13:20:12 -04:00
|
|
|
|
|
|
|
func setReuseAddr(fd uintptr) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReusePort(fd uintptr) error {
|
|
|
|
return nil
|
|
|
|
}
|