2018-09-10 12:39:57 +00:00
|
|
|
package internet
|
|
|
|
|
2022-01-16 22:44:09 +00:00
|
|
|
import (
|
|
|
|
"syscall"
|
2022-04-06 16:06:08 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2022-01-16 22:44:09 +00:00
|
|
|
)
|
2018-09-10 12:39:57 +00:00
|
|
|
|
|
|
|
const (
|
2021-11-27 09:16:41 +00:00
|
|
|
TCP_FASTOPEN = 15 // nolint: revive,stylecheck
|
2018-09-10 12:39:57 +00:00
|
|
|
)
|
|
|
|
|
2018-09-11 08:03:26 +00:00
|
|
|
func setTFO(fd syscall.Handle, settings SocketConfig_TCPFastOpenState) error {
|
|
|
|
switch settings {
|
|
|
|
case SocketConfig_Enable:
|
|
|
|
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case SocketConfig_Disable:
|
|
|
|
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:39:57 +00:00
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
2018-09-11 08:03:26 +00:00
|
|
|
if isTCPSocket(network) {
|
2018-09-11 08:18:26 +00:00
|
|
|
if err := setTFO(syscall.Handle(fd), config.Tfo); err != nil {
|
2018-09-11 08:03:26 +00:00
|
|
|
return err
|
2018-09-10 12:39:57 +00:00
|
|
|
}
|
2021-12-23 11:02:47 +00:00
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
2021-11-13 22:13:37 +00:00
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 12:39:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 23:02:24 +00:00
|
|
|
if config.TxBufSize != 0 {
|
|
|
|
if err := windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_SNDBUF, int(config.TxBufSize)); err != nil {
|
2022-01-16 22:44:09 +00:00
|
|
|
return newError("failed to set SO_SNDBUF").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 23:02:24 +00:00
|
|
|
if config.RxBufSize != 0 {
|
|
|
|
if err := windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_RCVBUF, int(config.TxBufSize)); err != nil {
|
2022-01-16 22:44:09 +00:00
|
|
|
return newError("failed to set SO_RCVBUF").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:39:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
2018-09-11 08:03:26 +00:00
|
|
|
if isTCPSocket(network) {
|
2018-09-11 08:18:26 +00:00
|
|
|
if err := setTFO(syscall.Handle(fd), config.Tfo); err != nil {
|
2018-09-11 08:03:26 +00:00
|
|
|
return err
|
2018-09-10 12:39:57 +00:00
|
|
|
}
|
2021-12-23 11:02:47 +00:00
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
2021-11-13 22:13:37 +00:00
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 12:39:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-16 23:02:24 +00:00
|
|
|
if config.TxBufSize != 0 {
|
|
|
|
if err := windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_SNDBUF, int(config.TxBufSize)); err != nil {
|
2022-01-16 22:44:09 +00:00
|
|
|
return newError("failed to set SO_SNDBUF").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 23:02:24 +00:00
|
|
|
if config.RxBufSize != 0 {
|
|
|
|
if err := windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_RCVBUF, int(config.TxBufSize)); err != nil {
|
2022-01-16 22:44:09 +00:00
|
|
|
return newError("failed to set SO_RCVBUF").Base(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 12:39:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-09-17 13:12:58 +00:00
|
|
|
|
2018-11-21 13:54:40 +00:00
|
|
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
2018-09-17 13:12:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-30 17:20:12 +00:00
|
|
|
|
|
|
|
func setReuseAddr(fd uintptr) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReusePort(fd uintptr) error {
|
|
|
|
return nil
|
|
|
|
}
|