2020-11-25 14:01:53 +03:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
TCP_FASTOPEN = 15
|
|
|
|
)
|
|
|
|
|
2021-03-30 19:42:02 +03:00
|
|
|
func setTFO(fd syscall.Handle, tfo int) error {
|
2021-03-06 17:45:12 +03:00
|
|
|
if tfo > 0 {
|
|
|
|
tfo = 1
|
|
|
|
}
|
|
|
|
if tfo >= 0 {
|
2021-03-30 19:42:02 +03:00
|
|
|
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, tfo); err != nil {
|
2020-11-25 14:01:53 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if isTCPSocket(network) {
|
2021-03-30 19:42:02 +03:00
|
|
|
if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil {
|
2020-11-25 14:01:53 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-07-31 16:55:40 +03:00
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
} else if config.TcpKeepAliveIdle < 0 {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
|
|
|
|
return newError("failed to unset SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if isTCPSocket(network) {
|
2021-03-30 19:42:02 +03:00
|
|
|
if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil {
|
2020-11-25 14:01:53 +03:00
|
|
|
return err
|
|
|
|
}
|
2022-07-31 16:55:40 +03:00
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
|
|
|
return newError("failed to set SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
} else if config.TcpKeepAliveIdle < 0 {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
|
|
|
|
return newError("failed to unset SO_KEEPALIVE", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bindAddr(fd uintptr, ip []byte, port uint32) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReuseAddr(fd uintptr) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setReusePort(fd uintptr) error {
|
|
|
|
return nil
|
|
|
|
}
|