This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
/
ping.go
369 lines (328 loc) · 10.1 KB
/
ping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributors: Sushant Dinesh sushant.dinesh94@gmail.com [:sushant94]
// Julien Vehent jvehent@mozilla.com [:ulfr]
// ping module is used to check the connection between an endpoint
// and a destination host.
package ping /* import "github.com/mozilla/mig/modules/ping" */
import (
"bytes"
"encoding/json"
"fmt"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"github.com/mozilla/mig/modules"
"net"
"os"
"strings"
"time"
)
type module struct {
}
func (m *module) NewRun() modules.Runner {
return new(run)
}
func init() {
modules.Register("ping", new(module))
}
type run struct {
Parameters params
Results modules.Result
}
type params struct {
Destination string `json:"destination"` // ipv4, ipv6 or fqdn.
DestinationPort float64 `json:"destinationport,omitempty"` // 16 bits integer. Throws an error when used with icmp. Defaults to 80 otherwise.
Protocol string `json:"protocol"` // icmp, tcp, udp
Count float64 `json:"count,omitempty"` // Number of tests
Timeout float64 `json:"timeout,omitempty"` // Timeout for individual test. defaults to 5s.
ipDest string
}
type elements struct {
Latencies []float64 `json:"latencies"` // response latency in milliseconds: 9999999 indicates timeout, -1 indicates unreachable, 0 general error.
Protocol string `json:"protocol"` // icmp, tcp, udp
ResolvedHost string `json:"resolvedhost"` // Information about the ip:port being pinged
Failures []string `json:"failures"` // ping failures, soft errors
}
const (
E_ConnRefused = "connection refused"
E_Timeout = "connection timed out"
)
func (r *run) Run(in modules.ModuleReader) (out string) {
var (
err error
el elements
)
defer func() {
if e := recover(); e != nil {
r.Results.Errors = append(r.Results.Errors, fmt.Sprintf("%v", e))
r.Results.Success = false
buf, _ := json.Marshal(r.Results)
out = string(buf[:])
}
}()
err = modules.ReadInputParameters(in, &r.Parameters)
if err != nil {
panic(err)
}
err = r.ValidateParameters()
if err != nil {
panic(err)
}
el.ResolvedHost = r.Parameters.Destination
if r.Parameters.Protocol == "udp" || r.Parameters.Protocol == "tcp" {
el.ResolvedHost += fmt.Sprintf(":%.0f", r.Parameters.DestinationPort)
}
el.ResolvedHost += " (" + r.Parameters.ipDest + ")"
el.Protocol = r.Parameters.Protocol
for i := 0; i < int(r.Parameters.Count); i += 1 {
var err error
// startTime for calculating the latency/RTT
startTime := time.Now()
switch r.Parameters.Protocol {
case "icmp":
err = r.pingIcmp()
case "tcp":
err = r.pingTcp()
case "udp":
err = r.pingUdp()
}
// store the time elapsed before processing potential errors
latency := time.Since(startTime).Seconds() * 1000
// evaluate potential ping failures
if err != nil {
switch err.Error() {
case E_Timeout:
latency = 9999999
case E_ConnRefused:
latency = -1
default:
el.Failures = append(el.Failures, fmt.Sprintf("ping #%d failed with error: %v", i+1, err))
latency = 0
}
}
switch latency {
case -1, 0:
// do nothing
case 9999999:
// For udp, a timeout indicates that the port *maybe* open.
if r.Parameters.Protocol == "udp" {
r.Results.FoundAnything = true
}
default:
r.Results.FoundAnything = true
}
el.Latencies = append(el.Latencies, latency)
// sleep 100 milliseconds between pings to prevent floods
time.Sleep(100 * time.Millisecond)
}
return r.buildResults(el)
}
func (r *run) ValidateParameters() (err error) {
// check if Protocol is a valid one that we support with this module
switch r.Parameters.Protocol {
case "icmp", "udp", "tcp":
break
default:
return fmt.Errorf("%s is not a supported ping protocol", r.Parameters.Protocol)
}
// tcp and udp pings must have a destination port
if r.Parameters.Protocol != "icmp" && (r.Parameters.DestinationPort < 0 || r.Parameters.DestinationPort > 65535) {
return fmt.Errorf("%s ping requires a valid destination port between 0 and 65535, got %.0f",
r.Parameters.Protocol, r.Parameters.DestinationPort)
}
// if the destination is a FQDN, resolve it and take the first IP returned as the dest
ips, err := net.LookupHost(r.Parameters.Destination)
ip := ""
// Get ip based on destination.
// if ip == nil, destination may not be a hostname.
if err != nil {
ip = r.Parameters.Destination
} else {
if len(ips) == 0 {
return fmt.Errorf("FQDN does not resolve to any known ip")
}
ip = ips[0]
}
// check the format of the destination IP
ip_parsed := net.ParseIP(ip)
if ip_parsed == nil {
return fmt.Errorf("destination IP is invalid: %v", ip)
}
r.Parameters.ipDest = ip
// if timeout is not set, default to 5 seconds
if r.Parameters.Timeout == 0.0 {
r.Parameters.Timeout = 5.0
}
// if count of pings is not set, default to 3
if r.Parameters.Count == 0.0 {
r.Parameters.Count = 3
}
return
}
// pingIcmp performs a ping to a destination. It select between ipv4 or ipv6 ping based
// on the format of the destination ip.
func (r *run) pingIcmp() (err error) {
var (
icmpType icmp.Type
network string
)
if strings.Contains(r.Parameters.Destination, ":") {
network = "ip6:ipv6-icmp"
icmpType = ipv6.ICMPTypeEchoRequest
} else {
network = "ip4:icmp"
icmpType = ipv4.ICMPTypeEcho
}
c, err := net.Dial(network, r.Parameters.Destination)
if err != nil {
return fmt.Errorf("Dial failed: %v", err)
}
c.SetDeadline(time.Now().Add(time.Duration(r.Parameters.Timeout) * time.Second))
defer c.Close()
// xid is the process ID.
// Get process ID and make sure it fits in 16bits.
xid := os.Getpid() & 0xffff
// Sequence number of the packet.
xseq := 0
packet := icmp.Message{
Type: icmpType, // Type of icmp message
Code: 0, // icmp query messages use code 0
Body: &icmp.Echo{
ID: xid, // Packet id
Seq: xseq, // Sequence number of the packet
Data: bytes.Repeat([]byte("Ping!Ping!Ping!"), 3),
},
}
wb, err := packet.Marshal(nil)
if err != nil {
return fmt.Errorf("Connection failed: %v", err)
}
if _, err := c.Write(wb); err != nil {
return fmt.Errorf("Conn.Write Error: %v", err)
}
rb := make([]byte, 1500)
if _, err := c.Read(rb); err != nil {
// If connection timed out, we return E_Timeout
if e := err.(*net.OpError).Timeout(); e {
return fmt.Errorf(E_Timeout)
}
if strings.Contains(err.Error(), "connection refused") {
return fmt.Errorf(E_ConnRefused)
}
return fmt.Errorf("Conn.Read failed: %v", err)
}
_, err = icmp.ParseMessage(icmpType.Protocol(), rb)
if err != nil {
return fmt.Errorf("ParseICMPMessage failed: %v", err)
}
return
}
// pingTcp performs a straighfoward connection attempt on a destination ip:port and returns
// an error if the attempt failed
func (r *run) pingTcp() (err error) {
conn, err := net.DialTimeout("tcp",
fmt.Sprintf("%s:%d", r.Parameters.Destination, int(r.Parameters.DestinationPort)),
time.Duration(r.Parameters.Timeout)*time.Second)
defer conn.Close()
if err != nil {
// If connection timed out, we return E_Timeout
if e := err.(*net.OpError).Timeout(); e {
return fmt.Errorf(E_Timeout)
}
if strings.Contains(err.Error(), "connection refused") {
return fmt.Errorf(E_ConnRefused)
}
return fmt.Errorf("Dial Error: %v", err)
}
return
}
// pingUdp sends a UDP packet to a destination ip:port to determine if it is open or closed.
// Because UDP does not reply to connection requests, a lack of response may indicate that the
// port is open, or that the packet got dropped. We chose to be optimistic and treat lack of
// response (connection timeout) as an open port.
func (r *run) pingUdp() (err error) {
// Make it ip:port format
destination := r.Parameters.Destination + ":" + fmt.Sprintf("%d", int(r.Parameters.DestinationPort))
c, err := net.Dial("udp", destination)
if err != nil {
return fmt.Errorf("Dial Error: %v", err)
}
c.Write([]byte("Ping!Ping!Ping!"))
c.SetReadDeadline(time.Now().Add(time.Duration(r.Parameters.Timeout) * time.Second))
defer c.Close()
rb := make([]byte, 1500)
if _, err := c.Read(rb); err != nil {
// If connection timed out, we return E_Timeout
if e := err.(*net.OpError).Timeout(); e {
return fmt.Errorf(E_Timeout)
}
if strings.Contains(err.Error(), "connection refused") {
return fmt.Errorf(E_ConnRefused)
}
return fmt.Errorf("Read Error: %v", err.Error())
}
return nil
}
func (r *run) buildResults(el elements) string {
r.Results.Elements = el
if len(r.Results.Errors) == 0 {
r.Results.Success = true
}
jsonOutput, err := json.Marshal(r.Results)
if err != nil {
panic(err)
}
return string(jsonOutput[:])
}
func (r *run) PrintResults(result modules.Result, foundOnly bool) (prints []string, err error) {
var el elements
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("Print Error: %v", e)
}
}()
err = result.GetElements(&el)
if err != nil {
panic(err)
}
if result.FoundAnything {
prints = append(prints,
fmt.Sprintf("%s ping of %s succeeded. Target is reachable.",
el.Protocol,
el.ResolvedHost,
),
)
}
// if we don't care about results where the target was not reachable, stop here
if foundOnly {
return
}
if !result.FoundAnything {
prints = append(prints,
fmt.Sprintf("%s ping of %s failed. Target is no reachable.",
el.Protocol,
el.ResolvedHost,
),
)
}
for i, lat := range el.Latencies {
switch lat {
case -1:
prints = append(prints, fmt.Sprintf("ping #%d failed, target was unreachable", i+1))
case 0:
prints = append(prints, fmt.Sprintf("ping #%d failed, reason unknown", i+1))
case 9999999:
if el.Protocol == "udp" {
prints = append(prints, fmt.Sprintf("ping #%d may have succeeded (no udp response)", i+1))
} else {
prints = append(prints, fmt.Sprintf("ping #%d failed, connection timed out", i+1))
}
default:
prints = append(prints, fmt.Sprintf("ping #%d succeeded in %.0fms", i+1, lat))
}
}
return
}