searcher_darwin.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //go:build darwin
  2. package process
  3. import (
  4. "context"
  5. "net/netip"
  6. "strconv"
  7. "strings"
  8. "syscall"
  9. "github.com/sagernet/sing-box/adapter"
  10. )
  11. var _ Searcher = (*darwinSearcher)(nil)
  12. type darwinSearcher struct{}
  13. func NewSearcher(_ Config) (Searcher, error) {
  14. return &darwinSearcher{}, nil
  15. }
  16. func (d *darwinSearcher) Close() error {
  17. return nil
  18. }
  19. func (d *darwinSearcher) FindProcessInfo(ctx context.Context, network string, source netip.AddrPort, destination netip.AddrPort) (*adapter.ConnectionOwner, error) {
  20. return FindDarwinConnectionOwner(network, source, destination)
  21. }
  22. var structSize = func() int {
  23. value, _ := syscall.Sysctl("kern.osrelease")
  24. major, _, _ := strings.Cut(value, ".")
  25. n, _ := strconv.ParseInt(major, 10, 64)
  26. switch true {
  27. case n >= 22:
  28. return 408
  29. default:
  30. // from darwin-xnu/bsd/netinet/in_pcblist.c:get_pcblist_n
  31. // size/offset are round up (aligned) to 8 bytes in darwin
  32. // rup8(sizeof(xinpcb_n)) + rup8(sizeof(xsocket_n)) +
  33. // 2 * rup8(sizeof(xsockbuf_n)) + rup8(sizeof(xsockstat_n))
  34. return 384
  35. }
  36. }()