netstack_userping_apple.go 1014 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build darwin || ios
  4. package netstack
  5. import (
  6. "net/netip"
  7. "time"
  8. probing "github.com/prometheus-community/pro-bing"
  9. )
  10. // sendOutboundUserPing sends a non-privileged ICMP (or ICMPv6) ping to dstIP with the given timeout.
  11. func (ns *Impl) sendOutboundUserPing(dstIP netip.Addr, timeout time.Duration) error {
  12. p, err := probing.NewPinger(dstIP.String())
  13. if err != nil {
  14. ns.logf("sendICMPPingToIP failed to create pinger: %v", err)
  15. return err
  16. }
  17. p.Timeout = timeout
  18. p.Count = 1
  19. p.SetPrivileged(false)
  20. p.OnSend = func(pkt *probing.Packet) {
  21. ns.logf("sendICMPPingToIP: forwarding ping to %s:", p.Addr())
  22. }
  23. p.OnRecv = func(pkt *probing.Packet) {
  24. ns.logf("sendICMPPingToIP: %d bytes pong from %s: icmp_seq=%d time=%v", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt)
  25. }
  26. p.OnFinish = func(stats *probing.Statistics) {
  27. ns.logf("sendICMPPingToIP: done, %d replies received", stats.PacketsRecv)
  28. }
  29. return p.Run()
  30. }