-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathping.go
379 lines (324 loc) · 9.76 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
370
371
372
373
374
375
376
377
378
379
package ping
import (
"bytes"
"context"
"errors"
"fmt"
"net"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
const (
// protocolIPv4ICMP defines ICMP for IPv4.
protocolIPv4ICMP = 1
// protocolIPv6ICMP defines ICMP for IPv6.
protocolIPv6ICMP = 58
)
// A Request represents an icmp echo request to be sent by a client.
type Request struct {
sentAt time.Time // time at which echo request was sent
dst net.Addr // useable destination address
ID int // ID is the ICMP ID. It is an identifier to aid in matching echos and replies when using privileged datagrams, may be zero.
Seq int // Seq is the ICMP sequence number.
Data []byte // Data is generally an arbitrary byte string of size 56. It is used to set icmp.Echo.Data.
Dst net.IP // The address of the host to which the message should be sent.
Src net.IP // The address of the host that composes the ICMP message.
}
// Response represents an icmp echo response received by a client.
type Response struct {
rcvdAt time.Time // time at which echo response was received
TotalLength int // Length of internet header and data of the echo response in octets.
RTT time.Duration // RTT is the round-trip time it took to ping.
TTL int // Time to live in seconds; as this field is decremented at each machine in which the datagram is processed, the value in this field should be at least as great as the number of gateways which this datagram will traverse. Maximum possible value of this field is 255.
ID int // ID is the ICMP ID. It is an identifier to aid in matching echos and replies when using privileged datagrams, may be zero.
Seq uint // Seq is the ICMP sequence number.
Data []byte // Data is the body of the ICMP response.
Dst net.IP // The local address of the host that composed the echo request.
Src net.IP // The address of the host to which the message was received from.
Req *Request // Req is the request that elicited this response.
}
// Client is a ping client.
type Client struct{}
// DefaultClient is the default client used by Do.
var DefaultClient = &Client{}
// NewRequest resolves dst as an IPv4 address and returns a pointer to a request
// using that as the destination.
func NewRequest(dst string) (*Request, error) {
host, err := net.ResolveIPAddr("ip4", dst)
if err != nil {
return nil, fmt.Errorf("can't resolve host: %s", err.Error())
}
return &Request{Dst: net.ParseIP(host.String())}, nil
}
// NewRequest6 resolves dst as an IPv6 address and returns a pointer to a request
// using that as the destination.
func NewRequest6(dst string) (*Request, error) {
host, err := net.ResolveIPAddr("ip6", dst)
if err != nil {
return nil, fmt.Errorf("can't resolve host: %s", err.Error())
}
return &Request{Dst: net.ParseIP(host.String())}, nil
}
// Do sends a ping request using the default client and returns a ping response.
func Do(ctx context.Context, req *Request) (*Response, error) {
return DefaultClient.Do(ctx, req)
}
// Do sends a ping request and returns a ping response.
func (c *Client) Do(ctx context.Context, req *Request) (*Response, error) {
conn, network, err := c.listen(req)
if err != nil {
return nil, err
}
defer conn.Close()
var addr net.Addr
if req.isIPv6() {
addr, err = net.ResolveIPAddr("ip6", req.Dst.String())
} else {
addr, err = net.ResolveIPAddr("ip4", req.Dst.String())
}
if err != nil {
return nil, err
}
req.dst = addr
switch network {
case "udp4", "udp6":
if a, ok := addr.(*net.IPAddr); ok {
req.dst = &net.UDPAddr{IP: a.IP, Zone: a.Zone}
}
}
sentAt, err := send(ctx, conn, req)
if err != nil {
return nil, err
}
resp, err := read(ctx, conn, req)
if err != nil {
return nil, err
}
resp.RTT = resp.rcvdAt.Sub(sentAt)
req.sentAt = sentAt
resp.Req = req
return resp, nil
}
// IPv4 resolves dst as an IPv4 address and pings using DefaultClient, returning
// the response and error.
func IPv4(ctx context.Context, dst string) (*Response, error) {
req, err := NewRequest(dst)
if err != nil {
return nil, err
}
return Do(ctx, req)
}
// IPv6 resolves dst as an IPv6 address and pings using DefaultClient, returning
// the response and error.
func IPv6(ctx context.Context, dst string) (*Response, error) {
req, err := NewRequest6(dst)
if err != nil {
return nil, err
}
return Do(ctx, req)
}
// listen tries first to create a privileged datagram-oriented ICMP endpoint then
// attempts to create a non-privileged one. If both fail, it returns an error.
func (c *Client) listen(req *Request) (*icmp.PacketConn, string, error) {
network := "ip4:icmp"
if req.isIPv6() {
network = "ip6:ipv6-icmp"
}
srcIP := req.Src.String()
if srcIP == "<nil>" {
srcIP = ""
}
conn, err := icmp.ListenPacket(network, srcIP)
if err != nil {
network = "udp4"
if req.isIPv6() {
network = "udp6"
}
var err2 error
conn, err2 = icmp.ListenPacket(network, srcIP)
if err2 != nil {
return nil, "", fmt.Errorf("error listening for ICMP packets: %s: %s", err.Error(), err2.Error())
}
}
return conn, network, nil
}
func (req *Request) isIPv6() bool {
if p4 := req.Dst.To4(); len(p4) == net.IPv4len {
return false
}
return true
}
func (req *Request) proto() int {
if req.isIPv6() {
return protocolIPv6ICMP
}
return protocolIPv4ICMP
}
func read(ctx context.Context, conn *icmp.PacketConn, req *Request) (*Response, error) {
if c4 := conn.IPv4PacketConn(); c4 != nil {
return read4(ctx, c4, req)
}
if c6 := conn.IPv6PacketConn(); c6 != nil {
return read6(ctx, c6, req)
}
return nil, errors.New("bad icmp connection type")
}
func read4(ctx context.Context, conn *ipv4.PacketConn, req *Request) (*Response, error) {
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
bytesReceived := make([]byte, 1500)
n, cm, src, err := conn.ReadFrom(bytesReceived)
rcv := time.Now()
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
continue
} else {
return nil, err
}
}
return nil, err
}
// If there was no data read or the packet didn't originate from the host
// assumed, skip processing.
if n <= 0 || (cm != nil && cm.Src.String() != req.Dst.String()) {
continue
}
// Process the data as an icmp message.
m, err := icmp.ParseMessage(protocolIPv4ICMP, bytesReceived[:n])
if err != nil {
return nil, err
}
// Likely an `ICMPTypeDestinationUnreachable`, ignore it.
if m.Type != ipv4.ICMPTypeEchoReply {
continue
}
// Verify the sequence numbers match our expectations for correct rtt.
// If using `ip4:icmp`, the ID can be verified as well (preferred).
b, ok := m.Body.(*icmp.Echo)
if !ok || b.Seq != req.Seq {
continue
}
srcHost, _, _ := net.SplitHostPort(src.String())
dstHost, _, _ := net.SplitHostPort(conn.LocalAddr().String())
resp := &Response{
ID: b.ID,
Seq: uint(b.Seq),
Data: b.Data,
TotalLength: n,
Src: net.ParseIP(srcHost),
Dst: net.ParseIP(dstHost),
rcvdAt: rcv,
}
if cm != nil {
resp.TTL = cm.TTL
}
return resp, nil
}
}
}
func read6(ctx context.Context, conn *ipv6.PacketConn, req *Request) (*Response, error) {
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
bytesReceived := make([]byte, 1500)
n, cm, src, err := conn.ReadFrom(bytesReceived)
rcv := time.Now()
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
continue
} else {
return nil, err
}
}
return nil, err
}
// If there was no data read or the packet didn't originate from the host
// assumed, skip processing.
if n <= 0 || cm.Src.String() != req.Dst.String() {
continue
}
// Process the data as an icmp message.
m, err := icmp.ParseMessage(protocolIPv6ICMP, bytesReceived[:n])
if err != nil {
return nil, err
}
// Likely an `ICMPTypeDestinationUnreachable`, ignore it.
if m.Type != ipv6.ICMPTypeEchoReply {
continue
}
// Verify the sequence numbers match our expectations for correct rtt.
// If using `ip4:icmp`, the ID can be verified as well (preferred).
b, ok := m.Body.(*icmp.Echo)
if !ok || b.Seq != req.Seq {
continue
}
srcHost, _, _ := net.SplitHostPort(src.String())
dstHost, _, _ := net.SplitHostPort(conn.LocalAddr().String())
return &Response{
ID: b.ID,
Seq: uint(b.Seq),
Data: bytesReceived[:n],
TotalLength: n,
Src: net.ParseIP(srcHost),
Dst: net.ParseIP(dstHost),
TTL: cm.HopLimit,
rcvdAt: rcv,
}, nil
}
}
}
func (req *Request) data() []byte {
if len(req.Data) == 0 {
return bytes.Repeat([]byte{1}, 56)
}
return req.Data
}
func send(ctx context.Context, conn *icmp.PacketConn, req *Request) (time.Time, error) {
sentAt := time.Time{}
select {
case <-ctx.Done():
return sentAt, nil
default:
body := &icmp.Echo{
ID: req.ID,
Seq: req.Seq,
Data: req.data(),
}
msg := &icmp.Message{
Type: ipv6.ICMPTypeEchoRequest,
Code: 0,
Body: body,
}
if req.proto() == protocolIPv4ICMP {
msg.Type = ipv4.ICMPTypeEcho
conn.IPv4PacketConn().SetControlMessage(ipv4.FlagTTL|ipv4.FlagSrc|ipv4.FlagDst, true)
} else {
conn.IPv6PacketConn().SetControlMessage(ipv6.FlagHopLimit|ipv6.FlagSrc|ipv6.FlagDst, true)
}
msgBytes, err := msg.Marshal(nil)
if err != nil {
return sentAt, err
}
if timeout, ok := ctx.Deadline(); ok {
if err := conn.SetWriteDeadline(timeout); err != nil {
return sentAt, err
}
}
if _, err := conn.WriteTo(msgBytes, req.dst); err != nil {
return sentAt, err
}
sentAt = time.Now()
}
return sentAt, nil
}