1
0

ping.go 4.4 KB

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