ping.go 704 B

123456789101112131415161718192021222324252627
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package osutil
  7. import (
  8. "net"
  9. "time"
  10. )
  11. // TCPPing returns the duration required to establish a TCP connection
  12. // to the given host. ICMP packets require root priviledges, hence why we use
  13. // tcp.
  14. func TCPPing(address string) (time.Duration, error) {
  15. dialer := net.Dialer{
  16. Deadline: time.Now().Add(time.Second),
  17. }
  18. start := time.Now()
  19. conn, err := dialer.Dial("tcp", address)
  20. if conn != nil {
  21. conn.Close()
  22. }
  23. return time.Since(start), err
  24. }