sockopt_darwin.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package internet
  2. import (
  3. "syscall"
  4. )
  5. const (
  6. // TCP_FASTOPEN is the socket option on darwin for TCP fast open.
  7. TCP_FASTOPEN = 0x105 // nolint: golint,stylecheck
  8. // TCP_FASTOPEN_SERVER is the value to enable TCP fast open on darwin for server connections.
  9. TCP_FASTOPEN_SERVER = 0x01 // nolint: golint,stylecheck
  10. // TCP_FASTOPEN_CLIENT is the value to enable TCP fast open on darwin for client connections.
  11. TCP_FASTOPEN_CLIENT = 0x02 // nolint: golint,stylecheck
  12. )
  13. func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
  14. if isTCPSocket(network) {
  15. switch config.Tfo {
  16. case SocketConfig_Enable:
  17. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_CLIENT); err != nil {
  18. return err
  19. }
  20. case SocketConfig_Disable:
  21. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
  22. return err
  23. }
  24. }
  25. if config.TcpKeepAliveInterval > 0 {
  26. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
  27. return newError("failed to set TCP_KEEPINTVL", err)
  28. }
  29. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
  30. return newError("failed to set SO_KEEPALIVE", err)
  31. }
  32. }
  33. }
  34. return nil
  35. }
  36. func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
  37. if isTCPSocket(network) {
  38. switch config.Tfo {
  39. case SocketConfig_Enable:
  40. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_SERVER); err != nil {
  41. return err
  42. }
  43. case SocketConfig_Disable:
  44. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
  45. return err
  46. }
  47. }
  48. if config.TcpKeepAliveInterval > 0 {
  49. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
  50. return newError("failed to set TCP_KEEPINTVL", err)
  51. }
  52. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
  53. return newError("failed to set SO_KEEPALIVE", err)
  54. }
  55. }
  56. }
  57. return nil
  58. }
  59. func bindAddr(fd uintptr, address []byte, port uint32) error {
  60. return nil
  61. }
  62. func setReuseAddr(fd uintptr) error {
  63. return nil
  64. }
  65. func setReusePort(fd uintptr) error {
  66. return nil
  67. }