sockopt_darwin.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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: revive,stylecheck
  12. TCP_KEEPINTVL = 0x101 // nolint: golint,stylecheck
  13. )
  14. func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
  15. if isTCPSocket(network) {
  16. switch config.Tfo {
  17. case SocketConfig_Enable:
  18. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_CLIENT); err != nil {
  19. return err
  20. }
  21. case SocketConfig_Disable:
  22. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
  23. return err
  24. }
  25. }
  26. if config.TcpKeepAliveInterval > 0 {
  27. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
  28. return newError("failed to set TCP_KEEPINTVL", err)
  29. }
  30. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
  31. return newError("failed to set SO_KEEPALIVE", err)
  32. }
  33. }
  34. }
  35. return nil
  36. }
  37. func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
  38. if isTCPSocket(network) {
  39. switch config.Tfo {
  40. case SocketConfig_Enable:
  41. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_SERVER); err != nil {
  42. return err
  43. }
  44. case SocketConfig_Disable:
  45. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
  46. return err
  47. }
  48. }
  49. if config.TcpKeepAliveInterval > 0 {
  50. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_KEEPINTVL, int(config.TcpKeepAliveInterval)); err != nil {
  51. return newError("failed to set TCP_KEEPINTVL", err)
  52. }
  53. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, 1); err != nil {
  54. return newError("failed to set SO_KEEPALIVE", err)
  55. }
  56. }
  57. }
  58. return nil
  59. }
  60. func bindAddr(fd uintptr, address []byte, port uint32) error {
  61. return nil
  62. }
  63. func setReuseAddr(fd uintptr) error {
  64. return nil
  65. }
  66. func setReusePort(fd uintptr) error {
  67. return nil
  68. }