sockopt_darwin.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package internet
  2. import (
  3. "os"
  4. "syscall"
  5. "unsafe"
  6. "github.com/xtls/xray-core/common/net"
  7. "golang.org/x/sys/unix"
  8. )
  9. const (
  10. // TCP_FASTOPEN_SERVER is the value to enable TCP fast open on darwin for server connections.
  11. TCP_FASTOPEN_SERVER = 0x01
  12. // TCP_FASTOPEN_CLIENT is the value to enable TCP fast open on darwin for client connections.
  13. TCP_FASTOPEN_CLIENT = 0x02 // nolint: revive,stylecheck
  14. // syscall.TCP_KEEPINTVL is missing on some darwin architectures.
  15. sysTCP_KEEPINTVL = 0x101 // nolint: revive,stylecheck
  16. )
  17. const (
  18. PfOut = 2
  19. IOCOut = 0x40000000
  20. IOCIn = 0x80000000
  21. IOCInOut = IOCIn | IOCOut
  22. IOCPARMMask = 0x1FFF
  23. LEN = 4*16 + 4*4 + 4*1
  24. // #define _IOC(inout,group,num,len) (inout | ((len & IOCPARMMask) << 16) | ((group) << 8) | (num))
  25. // #define _IOWR(g,n,t) _IOC(IOCInOut, (g), (n), sizeof(t))
  26. // #define DIOCNATLOOK _IOWR('D', 23, struct pfioc_natlook)
  27. DIOCNATLOOK = IOCInOut | ((LEN & IOCPARMMask) << 16) | ('D' << 8) | 23
  28. )
  29. // OriginalDst uses ioctl to read original destination from /dev/pf
  30. func OriginalDst(la, ra net.Addr) (net.IP, int, error) {
  31. f, err := os.Open("/dev/pf")
  32. if err != nil {
  33. return net.IP{}, -1, newError("failed to open device /dev/pf").Base(err)
  34. }
  35. defer f.Close()
  36. fd := f.Fd()
  37. nl := struct { // struct pfioc_natlook
  38. saddr, daddr, rsaddr, rdaddr [16]byte
  39. sxport, dxport, rsxport, rdxport [4]byte
  40. af, proto, protoVariant, direction uint8
  41. }{
  42. af: syscall.AF_INET,
  43. proto: syscall.IPPROTO_TCP,
  44. direction: PfOut,
  45. }
  46. var raIP, laIP net.IP
  47. var raPort, laPort int
  48. switch la.(type) {
  49. case *net.TCPAddr:
  50. raIP = ra.(*net.TCPAddr).IP
  51. laIP = la.(*net.TCPAddr).IP
  52. raPort = ra.(*net.TCPAddr).Port
  53. laPort = la.(*net.TCPAddr).Port
  54. case *net.UDPAddr:
  55. raIP = ra.(*net.UDPAddr).IP
  56. laIP = la.(*net.UDPAddr).IP
  57. raPort = ra.(*net.UDPAddr).Port
  58. laPort = la.(*net.UDPAddr).Port
  59. }
  60. if raIP.To4() != nil {
  61. if laIP.IsUnspecified() {
  62. laIP = net.ParseIP("127.0.0.1")
  63. }
  64. copy(nl.saddr[:net.IPv4len], raIP.To4())
  65. copy(nl.daddr[:net.IPv4len], laIP.To4())
  66. }
  67. if raIP.To16() != nil && raIP.To4() == nil {
  68. if laIP.IsUnspecified() {
  69. laIP = net.ParseIP("::1")
  70. }
  71. copy(nl.saddr[:], raIP)
  72. copy(nl.daddr[:], laIP)
  73. }
  74. nl.sxport[0], nl.sxport[1] = byte(raPort>>8), byte(raPort)
  75. nl.dxport[0], nl.dxport[1] = byte(laPort>>8), byte(laPort)
  76. if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, DIOCNATLOOK, uintptr(unsafe.Pointer(&nl))); errno != 0 {
  77. return net.IP{}, -1, os.NewSyscallError("ioctl", err)
  78. }
  79. odPort := nl.rdxport
  80. var odIP net.IP
  81. switch nl.af {
  82. case syscall.AF_INET:
  83. odIP = make(net.IP, net.IPv4len)
  84. copy(odIP, nl.rdaddr[:net.IPv4len])
  85. case syscall.AF_INET6:
  86. odIP = make(net.IP, net.IPv6len)
  87. copy(odIP, nl.rdaddr[:])
  88. }
  89. return odIP, int(net.PortFromBytes(odPort[:2])), nil
  90. }
  91. func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
  92. if isTCPSocket(network) {
  93. tfo := config.ParseTFOValue()
  94. if tfo > 0 {
  95. tfo = TCP_FASTOPEN_CLIENT
  96. }
  97. if tfo >= 0 {
  98. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
  99. return err
  100. }
  101. }
  102. if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
  103. if config.TcpKeepAliveIdle > 0 {
  104. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
  105. return newError("failed to set TCP_KEEPINTVL", err)
  106. }
  107. }
  108. if config.TcpKeepAliveInterval > 0 {
  109. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, sysTCP_KEEPINTVL, int(config.TcpKeepAliveIdle)); err != nil {
  110. return newError("failed to set TCP_KEEPIDLE", err)
  111. }
  112. }
  113. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
  114. return newError("failed to set SO_KEEPALIVE", err)
  115. }
  116. } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
  117. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 0); err != nil {
  118. return newError("failed to unset SO_KEEPALIVE", err)
  119. }
  120. }
  121. }
  122. return nil
  123. }
  124. func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
  125. if isTCPSocket(network) {
  126. tfo := config.ParseTFOValue()
  127. if tfo > 0 {
  128. tfo = TCP_FASTOPEN_SERVER
  129. }
  130. if tfo >= 0 {
  131. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_FASTOPEN, tfo); err != nil {
  132. return err
  133. }
  134. }
  135. if config.TcpKeepAliveIdle > 0 || config.TcpKeepAliveInterval > 0 {
  136. if config.TcpKeepAliveIdle > 0 {
  137. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPALIVE, int(config.TcpKeepAliveInterval)); err != nil {
  138. return newError("failed to set TCP_KEEPINTVL", err)
  139. }
  140. }
  141. if config.TcpKeepAliveInterval > 0 {
  142. if err := unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, sysTCP_KEEPINTVL, int(config.TcpKeepAliveIdle)); err != nil {
  143. return newError("failed to set TCP_KEEPIDLE", err)
  144. }
  145. }
  146. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 1); err != nil {
  147. return newError("failed to set SO_KEEPALIVE", err)
  148. }
  149. } else if config.TcpKeepAliveInterval < 0 || config.TcpKeepAliveIdle < 0 {
  150. if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_KEEPALIVE, 0); err != nil {
  151. return newError("failed to unset SO_KEEPALIVE", err)
  152. }
  153. }
  154. }
  155. return nil
  156. }
  157. func bindAddr(fd uintptr, address []byte, port uint32) error {
  158. return nil
  159. }
  160. func setReuseAddr(fd uintptr) error {
  161. return nil
  162. }
  163. func setReusePort(fd uintptr) error {
  164. return nil
  165. }