2018-09-10 08:39:57 -04:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2022-10-07 21:03:51 -04:00
|
|
|
"net"
|
|
|
|
|
2021-11-28 16:20:39 -05:00
|
|
|
"golang.org/x/sys/unix"
|
2018-09-10 08:39:57 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
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:
|
2021-11-28 16:27:28 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, TCP_FASTOPEN_CLIENT); err != nil {
|
2018-09-10 08:39:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
2021-11-28 16:20:39 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, 0); err != nil {
|
2018-09-10 08:39:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-11-13 16:46:45 -05:00
|
|
|
|
2022-05-08 16:31:01 -04:00
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
2022-01-16 15:39:29 -05:00
|
|
|
return newError("failed to set TCP_KEEPINTVL").Base(err)
|
2021-12-23 06:02:47 -05:00
|
|
|
}
|
2022-01-16 08:17:46 -05:00
|
|
|
}
|
2022-05-08 16:31:01 -04:00
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveIdle)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPALIVE (TCP keepalive idle time on Darwin)").Base(err)
|
2021-11-13 16:46:45 -05:00
|
|
|
}
|
2022-01-16 08:17:46 -05:00
|
|
|
}
|
|
|
|
|
2022-05-08 16:31:01 -04:00
|
|
|
if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
|
2021-11-28 16:20:39 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
|
2022-01-16 15:39:29 -05:00
|
|
|
return newError("failed to set SO_KEEPALIVE").Base(err)
|
2021-11-13 16:46:45 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 08:39:57 -04:00
|
|
|
}
|
|
|
|
|
2022-10-07 21:03:51 -04:00
|
|
|
if config.BindToDevice != "" {
|
|
|
|
iface, err := net.InterfaceByName(config.BindToDevice)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to get interface ", config.BindToDevice).Base(err)
|
|
|
|
}
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index); err != nil {
|
|
|
|
return newError("failed to set IP_BOUND_IF", err)
|
|
|
|
}
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index); err != nil {
|
|
|
|
return newError("failed to set IPV6_BOUND_IF", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.TxBufSize != 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_SNDBUF, int(config.TxBufSize)); err != nil {
|
2022-01-16 17:52:31 -05:00
|
|
|
return newError("failed to set SO_SNDBUF").Base(err)
|
2022-01-16 17:49:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.RxBufSize != 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUF, int(config.RxBufSize)); err != nil {
|
2022-01-16 17:52:31 -05:00
|
|
|
return newError("failed to set SO_RCVBUF").Base(err)
|
2022-01-16 17:49:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2021-11-28 16:20:39 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, TCP_FASTOPEN_SERVER); err != nil {
|
2018-09-10 08:39:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
2021-11-28 16:20:39 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, 0); err != nil {
|
2018-09-10 08:39:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 16:31:01 -04:00
|
|
|
if config.TcpKeepAliveInterval > 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
|
2022-01-16 15:39:29 -05:00
|
|
|
return newError("failed to set TCP_KEEPINTVL").Base(err)
|
2021-12-23 06:02:47 -05:00
|
|
|
}
|
2022-01-16 08:17:46 -05:00
|
|
|
}
|
2022-05-08 16:31:01 -04:00
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveIdle)); err != nil {
|
|
|
|
return newError("failed to set TCP_KEEPALIVE (TCP keepalive idle time on Darwin)").Base(err)
|
2021-11-13 16:46:45 -05:00
|
|
|
}
|
2022-01-16 08:17:46 -05:00
|
|
|
}
|
2022-05-08 16:31:01 -04:00
|
|
|
if config.TcpKeepAliveInterval > 0 || config.TcpKeepAliveIdle > 0 {
|
2021-11-28 16:20:39 -05:00
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
|
2022-01-16 15:39:29 -05:00
|
|
|
return newError("failed to set SO_KEEPALIVE").Base(err)
|
2021-11-13 16:46:45 -05:00
|
|
|
}
|
|
|
|
}
|
2018-09-10 08:39:57 -04:00
|
|
|
}
|
|
|
|
|
2022-10-07 21:03:51 -04:00
|
|
|
if config.BindToDevice != "" {
|
|
|
|
iface, err := net.InterfaceByName(config.BindToDevice)
|
|
|
|
if err != nil {
|
|
|
|
return newError("failed to get interface ", config.BindToDevice).Base(err)
|
|
|
|
}
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index); err != nil {
|
|
|
|
return newError("failed to set IP_BOUND_IF", err)
|
|
|
|
}
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index); err != nil {
|
|
|
|
return newError("failed to set IPV6_BOUND_IF", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.TxBufSize != 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_SNDBUF, int(config.TxBufSize)); err != nil {
|
2022-01-16 17:49:58 -05:00
|
|
|
return newError("failed to set SO_SNDBUF/SO_SNDBUFFORCE").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 18:02:24 -05:00
|
|
|
if config.RxBufSize != 0 {
|
|
|
|
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUF, int(config.RxBufSize)); err != nil {
|
2022-01-16 17:49:58 -05:00
|
|
|
return newError("failed to set SO_RCVBUF/SO_RCVBUFFORCE").Base(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
|
|
|
|
}
|