ping.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // TODO(performance): implement ping message channel buffering
  2. //
  3. // Pingo defines a number of high level functions and data structures to
  4. // interact with and represent a Ping via Go. Primarily, it defines the
  5. // Ping() function and Address data structure.
  6. //
  7. // NOTE this project does not provide a Go implementation of the ping protocol.
  8. // Instead, this project merely provides a wrapper to your system's existing
  9. // ping.c binary.
  10. //
  11. // NOTE(os): this project may fail with non-unix compliant system ping binaries,
  12. // predominantly Windows platforms. This relates to the way in which the project
  13. // interacts with the ping binary. (read: the project uses `ping -c1 ADDRESS`
  14. // and will break on systems that implement a different CLI)
  15. //
  16. // The project defines a simple top level binding for the system's ping.c:
  17. // Ping(). It simply reports a delay time as reported by the system ping, and
  18. // any errors encountered.
  19. //
  20. // Additionally, the project defines the "Address" data structure to represent
  21. // a series of ping results relating to the given host. Its fields are simple:
  22. //
  23. // Address (string): the host or IP address
  24. //
  25. // Results ([]float64): a series of ping results
  26. //
  27. // MaxResults (int): The maximum number of results to track, as enforced by
  28. // Address.Truncate()
  29. //
  30. // The Address data structure provides a series of higher level access functions
  31. // to prevent code repetition:
  32. //
  33. // Example:
  34. // ...
  35. // a := Address{Address:"google.ca", MaxResults:20}
  36. // latency, err := a.Ping() // get a single result and return the value
  37. // ... // handle error
  38. // a.Results, err = a.Poll() // append a ping to the Results slice, then return the updated slice
  39. // ... // handle error
  40. // a.Results = a.Truncate() // return a modified slice of only the most n*a.MaxResults long
  41. //
  42. // For more, see each function's GoDoc.
  43. package pingo
  44. import (
  45. "errors"
  46. "os/exec"
  47. "strconv"
  48. "strings"
  49. )
  50. // runPing returns the byte array as returned by [exec.Command]
  51. //
  52. // NOTE(os): this function may fail on non-unix compliant implementations of the Ping spec.
  53. func runPing(address string) (delay *[]byte, err error) {
  54. out, err := exec.Command("ping", address, "-c 1").Output()
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &out, err
  59. }
  60. // splitBytesToLines splits bytes as returned by runPing into an
  61. // array of strings by newline
  62. func splitBytesToLines(bytes *[]byte) (lines []string) {
  63. return strings.Split(strings.ReplaceAll(string(*bytes), "\r\n", "\n"), "\n")
  64. }
  65. // Ping returns the delay of a single Ping as reported by the system Ping binary.
  66. //
  67. // If the function is unable to resolve the system binary output or fails to
  68. // successfully resolve a Ping, it will always return -1.
  69. //
  70. // NOTE(os): this function may fail on non-unix compliant implementations of the Ping spec.
  71. func Ping(address string) (delay float64, err error) {
  72. out, err := runPing(address)
  73. if err != nil {
  74. return -1, err
  75. }
  76. lines := splitBytesToLines(out)
  77. for i := range lines {
  78. if strings.Contains(lines[i], "bytes from") {
  79. position := strings.Index(lines[i], "time=")
  80. before, _, success := strings.Cut(lines[i][position:], " ")
  81. if !success {
  82. return -1, errors.New("line does not match pattern")
  83. }
  84. _, after, success := strings.Cut(before, "=")
  85. if !success {
  86. return -1, errors.New("line does not match pattern")
  87. }
  88. delay, _ := strconv.ParseFloat(after, 64)
  89. return delay, nil
  90. }
  91. }
  92. return -1, errors.New("could not resolve host: " + address)
  93. }
  94. type Address struct {
  95. Address string
  96. Results []float64
  97. MaxResults int
  98. }
  99. // Enforces a Address.Result maximum length by dropping the oldest result,
  100. // then returns the modified slice.
  101. func (a Address) Truncate() []float64 {
  102. if len(a.Results) > a.MaxResults {
  103. a.Results = a.Results[len(a.Results)-a.MaxResults : len(a.Results)] // return modified slice missing first index
  104. }
  105. return a.Results
  106. }
  107. // Wraps Ping, passing Address.Address as its argument.
  108. func (a Address) Ping() (delay float64, err error) {
  109. return Ping(a.Address)
  110. }
  111. // Poll the affiliated Address, append it to a.Results, and report the modified
  112. // slice and any errors.
  113. func (a Address) Poll() (results []float64, err error) {
  114. delay, err := a.Ping()
  115. a.Results = append(a.Results, delay)
  116. a.Results = a.Truncate() // enforce max length
  117. return a.Results, err
  118. }
  119. // Last returns the last result in Address.Results. Returns -1 if no previous result
  120. func (a Address) Last() (delay float64) {
  121. if len(a.Results) > 0 {
  122. return a.Results[len(a.Results)-1]
  123. } else {
  124. return -1
  125. }
  126. }