sockopt_darwin.go 2.8 KB

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