1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/transport/internet/sockopt_linux.go

133 lines
3.8 KiB
Go
Raw Normal View History

2018-09-06 04:06:57 -04:00
package internet
2018-09-10 07:23:27 -04:00
import (
"net"
2018-09-10 07:23:27 -04:00
"syscall"
"golang.org/x/sys/unix"
2018-09-10 07:23:27 -04:00
)
2018-09-06 04:06:57 -04:00
2018-09-10 07:23:27 -04:00
const (
// For incoming connections.
2020-12-02 08:07:58 -05:00
TCP_FASTOPEN = 23 // nolint: golint,stylecheck
2018-09-10 07:23:27 -04:00
// For out-going connections.
2020-12-02 08:07:58 -05:00
TCP_FASTOPEN_CONNECT = 30 // nolint: golint,stylecheck
2018-09-10 07:23:27 -04:00
)
func bindAddr(fd uintptr, ip []byte, port uint32) error {
setReuseAddr(fd)
setReusePort(fd)
2018-09-17 09:12:58 -04:00
var sockaddr syscall.Sockaddr
switch len(ip) {
case net.IPv4len:
2018-09-17 09:12:58 -04:00
a4 := &syscall.SockaddrInet4{
Port: int(port),
}
copy(a4.Addr[:], ip)
2018-09-17 09:12:58 -04:00
sockaddr = a4
case net.IPv6len:
2018-09-17 09:12:58 -04:00
a6 := &syscall.SockaddrInet6{
Port: int(port),
}
copy(a6.Addr[:], ip)
2018-09-17 09:12:58 -04:00
sockaddr = a6
default:
return newError("unexpected length of ip")
2018-09-17 09:12:58 -04:00
}
return syscall.Bind(int(fd), sockaddr)
}
2018-09-10 07:23:27 -04:00
func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
2018-09-06 04:06:57 -04:00
if config.Mark != 0 {
2018-09-06 04:18:11 -04:00
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil {
2018-09-14 05:05:42 -04:00
return newError("failed to set SO_MARK").Base(err)
2018-09-06 04:06:57 -04:00
}
}
2018-09-10 07:23:27 -04:00
2018-09-11 04:03:26 -04:00
if isTCPSocket(network) {
2018-09-10 07:23:27 -04:00
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 1); err != nil {
2018-09-14 05:05:42 -04:00
return newError("failed to set TCP_FASTOPEN_CONNECT=1").Base(err)
2018-09-10 07:23:27 -04:00
}
case SocketConfig_Disable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 0); err != nil {
2018-09-14 05:05:42 -04:00
return newError("failed to set TCP_FASTOPEN_CONNECT=0").Base(err)
2018-09-10 07:23:27 -04:00
}
}
2021-05-04 17:30:41 -04:00
2021-05-04 17:39:28 -04:00
if config.TcpKeepAliveInterval != 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
2021-05-04 17:30:41 -04:00
return newError("failed to set TCP_KEEPINTVL", err)
}
}
2018-09-10 07:23:27 -04:00
}
2018-09-17 09:12:58 -04:00
if config.Tproxy.IsEnabled() {
2018-09-14 05:05:42 -04:00
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
return newError("failed to set IP_TRANSPARENT").Base(err)
}
}
2018-09-10 07:23:27 -04:00
return nil
}
func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
if config.Mark != 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil {
return newError("failed to set SO_MARK").Base(err)
}
}
2018-09-11 04:03:26 -04:00
if isTCPSocket(network) {
2018-09-10 07:23:27 -04:00
switch config.Tfo {
case SocketConfig_Enable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 1); err != nil {
2018-09-14 05:05:42 -04:00
return newError("failed to set TCP_FASTOPEN=1").Base(err)
2018-09-10 07:23:27 -04:00
}
case SocketConfig_Disable:
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 0); err != nil {
2018-09-14 05:05:42 -04:00
return newError("failed to set TCP_FASTOPEN=0").Base(err)
2018-09-10 07:23:27 -04:00
}
}
2021-05-04 17:39:28 -04:00
if config.TcpKeepAliveInterval != 0 {
if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
return newError("failed to set TCP_KEEPINTVL", err)
}
}
2018-09-10 07:23:27 -04:00
}
2018-09-17 09:12:58 -04:00
if config.Tproxy.IsEnabled() {
2018-09-14 05:05:42 -04:00
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil {
return newError("failed to set IP_TRANSPARENT").Base(err)
}
}
2018-09-17 09:12:58 -04:00
if config.ReceiveOriginalDestAddress && isUDPSocket(network) {
err1 := syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, unix.IPV6_RECVORIGDSTADDR, 1)
err2 := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1)
if err1 != nil && err2 != nil {
return err1
2018-09-17 09:12:58 -04:00
}
}
2018-09-06 04:06:57 -04:00
return nil
}
func setReuseAddr(fd uintptr) error {
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil {
return newError("failed to set SO_REUSEADDR").Base(err).AtWarning()
}
return nil
}
func setReusePort(fd uintptr) error {
if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
return newError("failed to set SO_REUSEPORT").Base(err).AtWarning()
}
return nil
}