sockopt_darwin.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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: revive,stylecheck
  8. // TCP_FASTOPEN_SERVER is the value to enable TCP fast open on darwin for server connections.
  9. TCP_FASTOPEN_SERVER = 0x01 // nolint: revive,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. )
  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. }
  26. return nil
  27. }
  28. func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
  29. if isTCPSocket(network) {
  30. switch config.Tfo {
  31. case SocketConfig_Enable:
  32. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, TCP_FASTOPEN_SERVER); err != nil {
  33. return err
  34. }
  35. case SocketConfig_Disable:
  36. if err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, TCP_FASTOPEN, 0); err != nil {
  37. return err
  38. }
  39. }
  40. }
  41. return nil
  42. }
  43. func bindAddr(fd uintptr, address []byte, port uint32) error {
  44. return nil
  45. }
  46. func setReuseAddr(fd uintptr) error {
  47. return nil
  48. }
  49. func setReusePort(fd uintptr) error {
  50. return nil
  51. }