github.com/moqsien/xraycore@v1.8.5/transport/internet/sockopt_darwin.go (about)

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