sockopt_linux.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package internet
  2. import (
  3. "syscall"
  4. )
  5. const (
  6. // For incoming connections.
  7. TCP_FASTOPEN = 23
  8. // For out-going connections.
  9. TCP_FASTOPEN_CONNECT = 30
  10. )
  11. func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error {
  12. if config.Mark != 0 {
  13. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil {
  14. return err
  15. }
  16. }
  17. if isTCPSocket(network) {
  18. switch config.Tfo {
  19. case SocketConfig_Enable:
  20. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 1); err != nil {
  21. return err
  22. }
  23. case SocketConfig_Disable:
  24. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN_CONNECT, 0); err != nil {
  25. return err
  26. }
  27. }
  28. }
  29. return nil
  30. }
  31. func applyInboundSocketOptions(network string, fd uintptr, config *SocketConfig) error {
  32. if isTCPSocket(network) {
  33. switch config.Tfo {
  34. case SocketConfig_Enable:
  35. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 1); err != nil {
  36. return err
  37. }
  38. case SocketConfig_Disable:
  39. if err := syscall.SetsockoptInt(int(fd), syscall.SOL_TCP, TCP_FASTOPEN, 0); err != nil {
  40. return err
  41. }
  42. }
  43. }
  44. return nil
  45. }