diff --git a/internal/net/control/control.go b/internal/net/control/control.go index b469d1d48d0..3afece552cc 100644 --- a/internal/net/control/control.go +++ b/internal/net/control/control.go @@ -53,10 +53,6 @@ const ( TCPDeferAccept IPTransparent IPRecoverDestinationAddr - - // from linux/include/uapi/linux/tcp.h - TCP_FASTOPEN int = 0x17 - TCP_FASTOPEN_CONNECT int = 0x1e ) func New(flag SocketFlag, keepAlive int) SocketController { @@ -99,38 +95,54 @@ func (ctrl *control) GetControl() func(network, addr string, c syscall.RawConn) log.Debugf("controlling socket for %s://%s, config %#v", network, address, ctrl) f := int(fd) var ierr error - ierr = SetsockoptInt(f, SOL_SOCKET, SO_REUSEPORT, boolint(ctrl.reusePort)) - if ierr != nil { - err = errors.Wrap(err, ierr.Error()) - } - ierr = SetsockoptInt(f, SOL_SOCKET, SO_REUSEADDR, boolint(ctrl.reuseAddr)) - if ierr != nil { - err = errors.Wrap(err, ierr.Error()) - } - if isTCP(network) { - ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_FASTOPEN, boolint(ctrl.tcpFastOpen)) + if SO_REUSEPORT != 0 { + ierr = SetsockoptInt(f, SOL_SOCKET, SO_REUSEPORT, boolint(ctrl.reusePort)) if ierr != nil { err = errors.Wrap(err, ierr.Error()) } - ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, boolint(ctrl.tcpFastOpen)) + } + if SO_REUSEADDR != 0 { + ierr = SetsockoptInt(f, SOL_SOCKET, SO_REUSEADDR, boolint(ctrl.reuseAddr)) if ierr != nil { err = errors.Wrap(err, ierr.Error()) } - ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_NODELAY, boolint(ctrl.tcpNoDelay)) - if ierr != nil { - err = errors.Wrap(err, ierr.Error()) + } + if isTCP(network) { + if TCP_FASTOPEN != 0 { + ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_FASTOPEN, boolint(ctrl.tcpFastOpen)) + if ierr != nil { + err = errors.Wrap(err, ierr.Error()) + } } - ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_CORK, boolint(ctrl.tcpCork)) - if ierr != nil { - err = errors.Wrap(err, ierr.Error()) + if TCP_FASTOPEN_CONNECT != 0 { + ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, boolint(ctrl.tcpFastOpen)) + if ierr != nil { + err = errors.Wrap(err, ierr.Error()) + } } - ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_QUICKACK, boolint(ctrl.tcpQuickAck)) - if ierr != nil { - err = errors.Wrap(err, ierr.Error()) + if TCP_NODELAY != 0 { + ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_NODELAY, boolint(ctrl.tcpNoDelay)) + if ierr != nil { + err = errors.Wrap(err, ierr.Error()) + } } - ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_DEFER_ACCEPT, boolint(ctrl.tcpDeferAccept)) - if ierr != nil { - err = errors.Wrap(err, ierr.Error()) + if TCP_CORK != 0 { + ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_CORK, boolint(ctrl.tcpCork)) + if ierr != nil { + err = errors.Wrap(err, ierr.Error()) + } + } + if TCP_QUICKACK != 0 { + ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_QUICKACK, boolint(ctrl.tcpQuickAck)) + if ierr != nil { + err = errors.Wrap(err, ierr.Error()) + } + } + if TCP_DEFER_ACCEPT != 0 { + ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_DEFER_ACCEPT, boolint(ctrl.tcpDeferAccept)) + if ierr != nil { + err = errors.Wrap(err, ierr.Error()) + } } } var sol, trans, rda int @@ -153,18 +165,24 @@ func (ctrl *control) GetControl() func(network, addr string, c syscall.RawConn) } } - ierr = SetsockoptInt(f, SOL_SOCKET, SO_KEEPALIVE, boolint(ctrl.keepAlive > 0)) - if ierr != nil { - err = errors.Wrap(err, ierr.Error()) - } - if ctrl.keepAlive > 0 && isTCP(network) { - ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_KEEPINTVL, ctrl.keepAlive) + if SO_KEEPALIVE != 0 { + ierr = SetsockoptInt(f, SOL_SOCKET, SO_KEEPALIVE, boolint(ctrl.keepAlive > 0)) if ierr != nil { err = errors.Wrap(err, ierr.Error()) } - ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_KEEPIDLE, ctrl.keepAlive) - if ierr != nil { - err = errors.Wrap(err, ierr.Error()) + } + if ctrl.keepAlive > 0 && isTCP(network) { + if TCP_KEEPINTVL != 0 { + ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_KEEPINTVL, ctrl.keepAlive) + if ierr != nil { + err = errors.Wrap(err, ierr.Error()) + } + } + if TCP_KEEPIDLE != 0 { + ierr = SetsockoptInt(f, IPPROTO_TCP, TCP_KEEPIDLE, ctrl.keepAlive) + if ierr != nil { + err = errors.Wrap(err, ierr.Error()) + } } } }) diff --git a/internal/net/control/control_darwin.go b/internal/net/control/control_darwin.go index f4d89a55ad4..8a203435f01 100644 --- a/internal/net/control/control_darwin.go +++ b/internal/net/control/control_darwin.go @@ -21,30 +21,34 @@ package control import ( "syscall" + + "golang.org/x/sys/unix" ) -var SetsockoptInt = syscall.SetsockoptInt +var SetsockoptInt = unix.SetsockoptInt const ( SOL_SOCKET = syscall.SOL_SOCKET IPPROTO_TCP = syscall.IPPROTO_TCP - SOL_IP = syscall.SOL_IP - SOL_IPV6 = syscall.SOL_IPV6 - - SO_REUSEADDR = syscall.SO_REUSEADDR - SO_REUSEPORT = syscall.SO_REUSEPORT - SO_KEEPALIVE = syscall.SO_KEEPALIVE - - TCP_NODELAY = syscall.TCP_NODELAY - TCP_CORK = syscall.TCP_CORK - TCP_QUICKACK = syscall.TCP_QUICKACK - TCP_DEFER_ACCEPT = syscall.TCP_DEFER_ACCEPT - TCP_KEEPINTVL = syscall.TCP_KEEPINTVL - TCP_KEEPIDLE = syscall.TCP_KEEPIDLE - - IP_TRANSPARENT = syscall.IP_TRANSPARENT - IPV6_TRANSPARENT = syscall.IPV6_TRANSPARENT - - IP_RECVORIGDSTADDR = syscall.IP_RECVORIGDSTADDR - IPV6_RECVORIGDSTADDR = syscall.IPV6_RECVORIGDSTADDR + SOL_IP = 0 + SOL_IPV6 = 0 + + SO_REUSEADDR = unix.SO_REUSEADDR + SO_REUSEPORT = unix.SO_REUSEPORT + SO_KEEPALIVE = unix.SO_KEEPALIVE + + TCP_NODELAY = unix.TCP_NODELAY + TCP_CORK = 0 + TCP_QUICKACK = 0 + TCP_DEFER_ACCEPT = 0 + TCP_KEEPINTVL = unix.TCP_KEEPINTVL + TCP_KEEPIDLE = 0 + TCP_FASTOPEN = unix.TCP_FASTOPEN + TCP_FASTOPEN_CONNECT = 0 + + IP_TRANSPARENT = 0 + IPV6_TRANSPARENT = 0 + + IP_RECVORIGDSTADDR = 0 + IPV6_RECVORIGDSTADDR = 0 ) diff --git a/internal/net/control/control_other.go b/internal/net/control/control_other.go index 1a61d107e88..41feee582e7 100644 --- a/internal/net/control/control_other.go +++ b/internal/net/control/control_other.go @@ -37,6 +37,8 @@ const ( TCP_DEFER_ACCEPT = 0 TCP_KEEPINTVL = 0 TCP_KEEPIDLE = 0 + TCP_FASTOPEN = 0 + TCP_FASTOPEN_CONNECT = 0 IP_TRANSPARENT = 0 IPV6_TRANSPARENT = 0 IP_RECVORIGDSTADDR = 0 diff --git a/internal/net/control/control_unix.go b/internal/net/control/control_unix.go index f2595816258..e971d544dc4 100644 --- a/internal/net/control/control_unix.go +++ b/internal/net/control/control_unix.go @@ -41,6 +41,9 @@ const ( TCP_DEFER_ACCEPT = unix.TCP_DEFER_ACCEPT TCP_KEEPINTVL = unix.TCP_KEEPINTVL TCP_KEEPIDLE = unix.TCP_KEEPIDLE + // from linux/include/uapi/linux/tcp.h + TCP_FASTOPEN = unix.TCP_FASTOPEN + TCP_FASTOPEN_CONNECT = unix.TCP_FASTOPEN_CONNECT IP_TRANSPARENT = unix.IP_TRANSPARENT IPV6_TRANSPARENT = unix.IPV6_TRANSPARENT diff --git a/internal/net/control/control_windows.go b/internal/net/control/control_windows.go index 59a3293d99c..d96b36af124 100644 --- a/internal/net/control/control_windows.go +++ b/internal/net/control/control_windows.go @@ -44,6 +44,8 @@ const ( TCP_DEFER_ACCEPT = 0 TCP_KEEPINTVL = 0 TCP_KEEPIDLE = 0 + TCP_FASTOPEN = 0 + TCP_FASTOPEN_CONNECT = 0 IP_TRANSPARENT = 0 IPV6_TRANSPARENT = 0 IP_RECVORIGDSTADDR = 0