github.com/imannamdari/v2ray-core/v5@v5.0.5/transport/internet/sockopt_windows.go (about)

     1  package internet
     2  
     3  import (
     4  	"net"
     5  	"syscall"
     6  
     7  	"golang.org/x/sys/windows"
     8  )
     9  
    10  const (
    11  	TCP_FASTOPEN    = 15 // nolint: revive,stylecheck
    12  	IP_UNICAST_IF   = 31 // nolint: revive,stylecheck
    13  	IPV6_UNICAST_IF = 31 // nolint: revive,stylecheck
    14  )
    15  
    16  func setTFO(fd syscall.Handle, settings SocketConfig_TCPFastOpenState) error {
    17  	switch settings {
    18  	case SocketConfig_Enable:
    19  		if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, 1); err != nil {
    20  			return err
    21  		}
    22  	case SocketConfig_Disable:
    23  		if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
    24  			return err
    25  		}
    26  	}
    27  	return nil
    28  }
    29  
    30  func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
    31  	if isTCPSocket(network) {
    32  		if err := setTFO(syscall.Handle(fd), config.Tfo); err != nil {
    33  			return err
    34  		}
    35  		if config.TcpKeepAliveIdle > 0 {
    36  			if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
    37  				return newError("failed to set SO_KEEPALIVE", err)
    38  			}
    39  		}
    40  	}
    41  
    42  	if config.BindToDevice != "" {
    43  		iface, err := net.InterfaceByName(config.BindToDevice)
    44  		if err != nil {
    45  			return newError("failed to get interface ", config.BindToDevice).Base(err)
    46  		}
    47  		if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, IP_UNICAST_IF, iface.Index); err != nil {
    48  			return newError("failed to set IP_UNICAST_IF", err)
    49  		}
    50  		if err := windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IPV6, IPV6_UNICAST_IF, iface.Index); err != nil {
    51  			return newError("failed to set IPV6_UNICAST_IF", err)
    52  		}
    53  	}
    54  
    55  	if config.TxBufSize != 0 {
    56  		if err := windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_SNDBUF, int(config.TxBufSize)); err != nil {
    57  			return newError("failed to set SO_SNDBUF").Base(err)
    58  		}
    59  	}
    60  
    61  	if config.RxBufSize != 0 {
    62  		if err := windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_RCVBUF, int(config.TxBufSize)); err != nil {
    63  			return newError("failed to set SO_RCVBUF").Base(err)
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
    71  	if isTCPSocket(network) {
    72  		if err := setTFO(syscall.Handle(fd), config.Tfo); err != nil {
    73  			return err
    74  		}
    75  		if config.TcpKeepAliveIdle > 0 {
    76  			if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
    77  				return newError("failed to set SO_KEEPALIVE", err)
    78  			}
    79  		}
    80  	}
    81  
    82  	if config.TxBufSize != 0 {
    83  		if err := windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_SNDBUF, int(config.TxBufSize)); err != nil {
    84  			return newError("failed to set SO_SNDBUF").Base(err)
    85  		}
    86  	}
    87  
    88  	if config.RxBufSize != 0 {
    89  		if err := windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_RCVBUF, int(config.TxBufSize)); err != nil {
    90  			return newError("failed to set SO_RCVBUF").Base(err)
    91  		}
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func bindAddr(fd uintptr, ip []byte, port uint32) error {
    98  	return nil
    99  }
   100  
   101  func setReuseAddr(fd uintptr) error {
   102  	return nil
   103  }
   104  
   105  func setReusePort(fd uintptr) error {
   106  	return nil
   107  }