From 0727e02fa22bda3c2c27626ba7f2876e1a5a5230 Mon Sep 17 00:00:00 2001 From: ValdikSS Date: Sat, 13 Nov 2021 21:46:45 +0000 Subject: [PATCH] Add TCP keep alive support in MacOS, FreeBSD --- transport/internet/sockopt_darwin.go | 17 +++++++++++++++++ transport/internet/sockopt_freebsd.go | 16 ++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/transport/internet/sockopt_darwin.go b/transport/internet/sockopt_darwin.go index c4b2e8deb..a90a9815c 100644 --- a/transport/internet/sockopt_darwin.go +++ b/transport/internet/sockopt_darwin.go @@ -25,6 +25,15 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf return err } } + + 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) + } + } } return nil @@ -42,6 +51,14 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return err } } + 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) + } + } } return nil diff --git a/transport/internet/sockopt_freebsd.go b/transport/internet/sockopt_freebsd.go index 50834cdd2..98e8d11ee 100644 --- a/transport/internet/sockopt_freebsd.go +++ b/transport/internet/sockopt_freebsd.go @@ -141,6 +141,14 @@ func applyOutboundSocketOptions(network string, address string, fd uintptr, conf return newError("failed to set TCP_FASTOPEN_CONNECT=0").Base(err) } } + 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) + } + } } if config.Tproxy.IsEnabled() { @@ -175,6 +183,14 @@ func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) return newError("failed to set TCP_FASTOPEN=0").Base(err) } } + 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) + } + } } if config.Tproxy.IsEnabled() {