Skip to content

Commit

Permalink
Merge 5be72b9 into 37d6cfc
Browse files Browse the repository at this point in the history
  • Loading branch information
kpango authored Feb 19, 2021
2 parents 37d6cfc + 5be72b9 commit 8687360
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 56 deletions.
90 changes: 54 additions & 36 deletions internal/net/control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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())
}
}
}
})
Expand Down
44 changes: 24 additions & 20 deletions internal/net/control/control_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions internal/net/control/control_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions internal/net/control/control_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/net/control/control_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8687360

Please sign in to comment.