2020-11-25 11:01:53 +00:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
2023-12-17 22:56:15 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"net"
|
2020-11-25 11:01:53 +00:00
|
|
|
"syscall"
|
2023-12-17 22:56:15 +00:00
|
|
|
"unsafe"
|
2024-06-29 18:32:57 +00:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-12-17 22:56:15 +00:00
|
|
|
TCP_FASTOPEN = 15
|
|
|
|
IP_UNICAST_IF = 31
|
|
|
|
IPV6_UNICAST_IF = 31
|
2020-11-25 11:01:53 +00:00
|
|
|
)
|
|
|
|
|
2021-03-30 16:42:02 +00:00
|
|
|
func setTFO(fd syscall.Handle, tfo int) error {
|
2021-03-06 14:45:12 +00:00
|
|
|
if tfo > 0 {
|
|
|
|
tfo = 1
|
|
|
|
}
|
|
|
|
if tfo >= 0 {
|
2021-03-30 16:42:02 +00:00
|
|
|
if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, tfo); err != nil {
|
2020-11-25 11:01:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
|
2023-12-17 22:56:15 +00:00
|
|
|
if config.Interface != "" {
|
|
|
|
inf, err := net.InterfaceByName(config.Interface)
|
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to find the interface").Base(err)
|
2023-12-17 22:56:15 +00:00
|
|
|
}
|
|
|
|
isV4 := (network == "tcp4")
|
|
|
|
if isV4 {
|
|
|
|
var bytes [4]byte
|
|
|
|
binary.BigEndian.PutUint32(bytes[:], uint32(inf.Index))
|
|
|
|
idx := *(*uint32)(unsafe.Pointer(&bytes[0]))
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_IP, IP_UNICAST_IF, int(idx)); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to set IP_UNICAST_IF").Base(err)
|
2023-12-17 22:56:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_IPV6, IPV6_UNICAST_IF, inf.Index); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to set IPV6_UNICAST_IF").Base(err)
|
2023-12-17 22:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:01:53 +00:00
|
|
|
if isTCPSocket(network) {
|
2021-03-30 16:42:02 +00:00
|
|
|
if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil {
|
2020-11-25 11:01:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-31 13:55:40 +00:00
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to set SO_KEEPALIVE", err)
|
2022-07-31 13:55:40 +00:00
|
|
|
}
|
|
|
|
} else if config.TcpKeepAliveIdle < 0 {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to unset SO_KEEPALIVE", err)
|
2022-07-31 13:55:40 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-22 02:59:58 +00:00
|
|
|
if config.TcpNoDelay {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to set TCP_NODELAY", err)
|
2023-05-22 02:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
|
|
|
|
if isTCPSocket(network) {
|
2021-03-30 16:42:02 +00:00
|
|
|
if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil {
|
2020-11-25 11:01:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-07-31 13:55:40 +00:00
|
|
|
if config.TcpKeepAliveIdle > 0 {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to set SO_KEEPALIVE", err)
|
2022-07-31 13:55:40 +00:00
|
|
|
}
|
|
|
|
} else if config.TcpKeepAliveIdle < 0 {
|
|
|
|
if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return errors.New("failed to unset SO_KEEPALIVE", err)
|
2022-07-31 13:55:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-25 11:01:53 +00: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
|
|
|
|
}
|