@@ -20,6 +20,9 @@ type Response struct {
20
20
Delay time.Duration
21
21
Error error
22
22
Destination string
23
+ Seq int
24
+ Readsize int
25
+ Writesize int
23
26
}
24
27
25
28
func makePingRequest (id , seq , pktlen int , filler []byte ) []byte {
@@ -61,8 +64,8 @@ func parsePingReply(p []byte) (id, seq int) {
61
64
return
62
65
}
63
66
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 ) {
66
69
raddr , err := net .ResolveIPAddr ("ip" , destination )
67
70
if err != nil {
68
71
return 0 , err
@@ -104,19 +107,19 @@ func Pingone(destination string) (time.Duration, error) {
104
107
}
105
108
}
106
109
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.
109
112
// You can also adjust the delay between two ICMP echo packets with the variable delay.
110
113
func Pinguntil (destination string , count int , response chan Response , delay time.Duration ) {
111
114
raddr , err := net .ResolveIPAddr ("ip" , destination )
112
115
if err != nil {
113
- response <- Response {Delay : 0 , Error : err , Destination : destination }
116
+ response <- Response {Delay : 0 , Error : err , Destination : destination , Seq : 0 }
114
117
return
115
118
}
116
119
117
120
ipconn , err := net .Dial ("ip:icmp" , raddr .IP .String ())
118
121
if err != nil {
119
- response <- Response {Delay : 0 , Error : err , Destination : destination }
122
+ response <- Response {Delay : 0 , Error : err , Destination : destination , Seq : 0 }
120
123
return
121
124
}
122
125
@@ -129,9 +132,9 @@ func Pinguntil(destination string, count int, response chan Response, delay time
129
132
130
133
start := time .Now ()
131
134
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 }
135
138
time .Sleep (delay )
136
139
continue
137
140
}
@@ -140,15 +143,15 @@ func Pinguntil(destination string, count int, response chan Response, delay time
140
143
141
144
resp := make ([]byte , 1024 )
142
145
for {
143
- _ , err := ipconn .Read (resp )
146
+ readsize , err := ipconn .Read (resp )
144
147
145
148
if resp [1 ] != ICMP_ECHO_REPLY {
146
149
continue
147
150
} 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 }
149
152
break
150
153
} 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 }
152
155
break
153
156
}
154
157
}
0 commit comments