github.com/moqsien/xraycore@v1.8.5/transport/internet/sockopt_windows.go (about) 1 package internet 2 3 import ( 4 "syscall" 5 ) 6 7 const ( 8 TCP_FASTOPEN = 15 9 ) 10 11 func setTFO(fd syscall.Handle, tfo int) error { 12 if tfo > 0 { 13 tfo = 1 14 } 15 if tfo >= 0 { 16 if err := syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, TCP_FASTOPEN, tfo); err != nil { 17 return err 18 } 19 } 20 return nil 21 } 22 23 func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error { 24 if isTCPSocket(network) { 25 if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil { 26 return err 27 } 28 if config.TcpKeepAliveIdle > 0 { 29 if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { 30 return newError("failed to set SO_KEEPALIVE", err) 31 } 32 } else if config.TcpKeepAliveIdle < 0 { 33 if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { 34 return newError("failed to unset SO_KEEPALIVE", err) 35 } 36 } 37 if config.TcpNoDelay { 38 if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1); err != nil { 39 return newError("failed to set TCP_NODELAY", err) 40 } 41 } 42 } 43 44 return nil 45 } 46 47 func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error { 48 if isTCPSocket(network) { 49 if err := setTFO(syscall.Handle(fd), config.ParseTFOValue()); err != nil { 50 return err 51 } 52 if config.TcpKeepAliveIdle > 0 { 53 if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil { 54 return newError("failed to set SO_KEEPALIVE", err) 55 } 56 } else if config.TcpKeepAliveIdle < 0 { 57 if err := syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 0); err != nil { 58 return newError("failed to unset SO_KEEPALIVE", err) 59 } 60 } 61 } 62 63 return nil 64 } 65 66 func bindAddr(fd uintptr, ip []byte, port uint32) error { 67 return nil 68 } 69 70 func setReuseAddr(fd uintptr) error { 71 return nil 72 } 73 74 func setReusePort(fd uintptr) error { 75 return nil 76 }