Skip to content
This repository was archived by the owner on Apr 20, 2019. It is now read-only.

Commit 73ce29e

Browse files
committed
Changed Response, renamed Pingone to Pingonce and a few comments typos.
1 parent 58c2493 commit 73ce29e

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

libping.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ type Response struct {
2020
Delay time.Duration
2121
Error error
2222
Destination string
23+
Seq int
24+
Readsize int
25+
Writesize int
2326
}
2427

2528
func makePingRequest(id, seq, pktlen int, filler []byte) []byte {
@@ -61,8 +64,8 @@ func parsePingReply(p []byte) (id, seq int) {
6164
return
6265
}
6366

64-
// Pingone send one ICMP echo packet to the destination, and return the latency.
65-
func Pingone(destination string) (time.Duration, error) {
67+
// Pingonce send one ICMP echo packet to the destination, and return the latency.
68+
func Pingonce(destination string) (time.Duration, error) {
6669
raddr, err := net.ResolveIPAddr("ip", destination)
6770
if err != nil {
6871
return 0, err
@@ -104,19 +107,19 @@ func Pingone(destination string) (time.Duration, error) {
104107
}
105108
}
106109

107-
// Pinguntil will send ICMP echo packets to the destination until the counter is done, or forever if the counter is set to 0.
108-
// The replies are given in the Response format, with the latency and the error, if one was got.
110+
// Pinguntil will send ICMP echo packets to the destination until the counter is reached, or forever if the counter is set to 0.
111+
// The replies are given in the Response format.
109112
// You can also adjust the delay between two ICMP echo packets with the variable delay.
110113
func Pinguntil(destination string, count int, response chan Response, delay time.Duration) {
111114
raddr, err := net.ResolveIPAddr("ip", destination)
112115
if err != nil {
113-
response <- Response{Delay: 0, Error: err, Destination: destination}
116+
response <- Response{Delay: 0, Error: err, Destination: destination, Seq: 0}
114117
return
115118
}
116119

117120
ipconn, err := net.Dial("ip:icmp", raddr.IP.String())
118121
if err != nil {
119-
response <- Response{Delay: 0, Error: err, Destination: destination}
122+
response <- Response{Delay: 0, Error: err, Destination: destination, Seq: 0}
120123
return
121124
}
122125

@@ -129,9 +132,9 @@ func Pinguntil(destination string, count int, response chan Response, delay time
129132

130133
start := time.Now()
131134

132-
n, err := ipconn.Write(sendpkt)
133-
if err != nil || n != pingpktlen {
134-
response <- Response{Delay: 0, Error: err, Destination: destination}
135+
writesize, err := ipconn.Write(sendpkt)
136+
if err != nil || writesize != pingpktlen {
137+
response <- Response{Delay: 0, Error: err, Destination: destination, Seq: seq, Writesize: writesize, Readsize: 0}
135138
time.Sleep(delay)
136139
continue
137140
}
@@ -140,15 +143,15 @@ func Pinguntil(destination string, count int, response chan Response, delay time
140143

141144
resp := make([]byte, 1024)
142145
for {
143-
_, err := ipconn.Read(resp)
146+
readsize, err := ipconn.Read(resp)
144147

145148
if resp[1] != ICMP_ECHO_REPLY {
146149
continue
147150
} else if err != nil {
148-
response <- Response{Delay: 0, Error: err, Destination: destination}
151+
response <- Response{Delay: 0, Error: err, Destination: destination, Seq: seq, Writesize: writesize, Readsize: readsize}
149152
break
150153
} else {
151-
response <- Response{Delay: time.Now().Sub(start), Error: err, Destination: destination}
154+
response <- Response{Delay: time.Now().Sub(start), Error: err, Destination: destination, Seq: seq, Writesize: writesize, Readsize: readsize}
152155
break
153156
}
154157
}

0 commit comments

Comments
 (0)