github.com/EagleQL/Xray-core@v1.4.3/transport/internet/sockopt_linux.go (about) 1 package internet 2 3 import ( 4 "net" 5 "syscall" 6 7 "golang.org/x/sys/unix" 8 ) 9 10 const ( 11 // For incoming connections. 12 TCP_FASTOPEN = 23 13 // For out-going connections. 14 TCP_FASTOPEN_CONNECT = 30 15 ) 16 17 func bindAddr(fd uintptr, ip []byte, port uint32) error { 18 setReuseAddr(fd) 19 setReusePort(fd) 20 21 var sockaddr syscall.Sockaddr 22 23 switch len(ip) { 24 case net.IPv4len: 25 a4 := &syscall.SockaddrInet4{ 26 Port: int(port), 27 } 28 copy(a4.Addr[:], ip) 29 sockaddr = a4 30 case net.IPv6len: 31 a6 := &syscall.SockaddrInet6{ 32 Port: int(port), 33 } 34 copy(a6.Addr[:], ip) 35 sockaddr = a6 36 default: 37 return newError("unexpected length of ip") 38 } 39 40 return syscall.Bind(int(fd), sockaddr) 41 } 42 43 func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error { 44 if config.Mark != 0 { 45 if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil { 46 return newError("failed to set SO_MARK").Base(err) 47 } 48 } 49 50 if isTCPSocket(network) { 51 tfo := config.ParseTFOValue() 52 if tfo > 0 { 53 tfo = 1 54 } 55 if tfo >= 0 { 56 if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, tfo); err != nil { 57 return newError("failed to set TCP_FASTOPEN_CONNECT=", tfo).Base(err) 58 } 59 } 60 } 61 62 if config.Tproxy.IsEnabled() { 63 if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil { 64 return newError("failed to set IP_TRANSPARENT").Base(err) 65 } 66 } 67 68 return nil 69 } 70 71 func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error { 72 if config.Mark != 0 { 73 if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil { 74 return newError("failed to set SO_MARK").Base(err) 75 } 76 } 77 if isTCPSocket(network) { 78 tfo := config.ParseTFOValue() 79 if tfo >= 0 { 80 if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, tfo); err != nil { 81 return newError("failed to set TCP_FASTOPEN=", tfo).Base(err) 82 } 83 } 84 } 85 86 if config.Tproxy.IsEnabled() { 87 if err := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1); err != nil { 88 return newError("failed to set IP_TRANSPARENT").Base(err) 89 } 90 } 91 92 if config.ReceiveOriginalDestAddress && isUDPSocket(network) { 93 err1 := syscall.SetsockoptInt(int(fd), syscall.SOL_IPV6, unix.IPV6_RECVORIGDSTADDR, 1) 94 err2 := syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_RECVORIGDSTADDR, 1) 95 if err1 != nil && err2 != nil { 96 return err1 97 } 98 } 99 100 return nil 101 } 102 103 func setReuseAddr(fd uintptr) error { 104 if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1); err != nil { 105 return newError("failed to set SO_REUSEADDR").Base(err).AtWarning() 106 } 107 return nil 108 } 109 110 func setReusePort(fd uintptr) error { 111 if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil { 112 return newError("failed to set SO_REUSEPORT").Base(err).AtWarning() 113 } 114 return nil 115 }