From 418746fdb0fa735714dd39f9387906f86bd54efc Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Mon, 10 Sep 2018 14:39:57 +0200 Subject: [PATCH] enable tfo on other platforms --- transport/internet/sockopt_other.go | 2 +- transport/internet/sockopt_unix.go | 49 +++++++++++++++++++++++++++ transport/internet/sockopt_windows.go | 44 ++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 transport/internet/sockopt_unix.go create mode 100644 transport/internet/sockopt_windows.go diff --git a/transport/internet/sockopt_other.go b/transport/internet/sockopt_other.go index bd8ca344..7a126665 100644 --- a/transport/internet/sockopt_other.go +++ b/transport/internet/sockopt_other.go @@ -1,4 +1,4 @@ -// +build !linux +// +build js package internet diff --git a/transport/internet/sockopt_unix.go b/transport/internet/sockopt_unix.go new file mode 100644 index 00000000..c5e768a7 --- /dev/null +++ b/transport/internet/sockopt_unix.go @@ -0,0 +1,49 @@ +// +build darwin dragonfly freebsd netbsd openbsd + +package internet + +import ( + "strings" + "syscall" +) + +const ( + // For incoming connections. + TCP_FASTOPEN = 23 + // For out-going connections. + TCP_FASTOPEN_CONNECT = 30 +) + +func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error { + if strings.HasPrefix(network, "tcp") { + switch config.Tfo { + case SocketConfig_Enable: + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN_CONNECT, 1); err != nil { + return err + } + case SocketConfig_Disable: + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN_CONNECT, 0); err != nil { + return err + } + } + } + + return nil +} + +func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error { + if strings.HasPrefix(network, "tcp") { + switch config.Tfo { + case SocketConfig_Enable: + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil { + return err + } + case SocketConfig_Disable: + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil { + return err + } + } + } + + return nil +} diff --git a/transport/internet/sockopt_windows.go b/transport/internet/sockopt_windows.go new file mode 100644 index 00000000..58a1c572 --- /dev/null +++ b/transport/internet/sockopt_windows.go @@ -0,0 +1,44 @@ +package internet + +import ( + "strings" + "syscall" +) + +const ( + TCP_FASTOPEN = 15 +) + +func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error { + if strings.HasPrefix(network, "tcp") { + switch config.Tfo { + case SocketConfig_Enable: + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil { + return err + } + case SocketConfig_Disable: + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil { + return err + } + } + } + + return nil +} + +func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error { + if strings.HasPrefix(network, "tcp") { + switch config.Tfo { + case SocketConfig_Enable: + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil { + return err + } + case SocketConfig_Disable: + if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil { + return err + } + } + } + + return nil +}