-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtracelib.go
389 lines (344 loc) · 8.18 KB
/
tracelib.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
380
381
382
383
384
385
386
387
388
389
package tracelib
import (
"encoding/binary"
"errors"
"math/rand"
"net"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
const (
// ProtocolICMP icmp protocol id
ProtocolICMP = 1
// ProtocolICMP6 icmp protocol id
ProtocolICMP6 = 58
// MaxTimeouts sets number of hops without replay before trace termination
MaxTimeouts = 3
)
// trace struct represents handles connections and info for trace
type trace struct {
conn net.PacketConn
ipv4conn *ipv4.PacketConn
ipv6conn *ipv6.PacketConn
msg icmp.Message
netmsg []byte
id int
maxrtt time.Duration
maxttl int
dest net.Addr
}
// Callback function called after every hop received
type Callback func(info Hop, hopnum int, round int)
// RunTrace preforms traceroute to specified host
func RunTrace(host string, source string, source6 string, maxrtt time.Duration, maxttl int, DNScache *LookupCache, cb Callback) ([]Hop, error) {
hops := make([]Hop, 0, maxttl)
var res trace
var err error
isIPv6 := false
addrList, err := net.LookupIP(host)
if nil != err {
return nil, err
}
for _, addr := range addrList {
if addr.To4() != nil {
res.dest, err = net.ResolveIPAddr("ip4:icmp", addr.String())
break
}
}
if nil == res.dest {
for _, addr := range addrList {
if addr.To16() != nil {
isIPv6 = true
res.dest, err = net.ResolveIPAddr("ip6:58", addr.String())
break
}
}
}
if nil == res.dest {
return nil, errors.New("Unable to resolve destination host")
}
res.maxrtt = maxrtt
res.maxttl = maxttl
res.id = rand.Int() % 0x7fff
if isIPv6 {
res.msg = icmp.Message{Type: ipv6.ICMPTypeEchoRequest, Code: 0, Body: &icmp.Echo{ID: res.id, Seq: 1}}
} else {
res.msg = icmp.Message{Type: ipv4.ICMPTypeEcho, Code: 0, Body: &icmp.Echo{ID: res.id, Seq: 1}}
}
res.netmsg, err = res.msg.Marshal(nil)
if nil != err {
return nil, err
}
if !isIPv6 {
res.conn, err = net.ListenPacket("ip4:icmp", source)
} else {
res.conn, err = net.ListenPacket("ip6:58", source6)
}
if nil != err {
return nil, err
}
defer res.conn.Close()
if isIPv6 {
res.ipv6conn = ipv6.NewPacketConn(res.conn)
defer res.ipv6conn.Close()
if err := res.ipv6conn.SetControlMessage(ipv6.FlagHopLimit|ipv6.FlagSrc|ipv6.FlagDst|ipv6.FlagInterface, true); err != nil {
return nil, err
}
var f ipv6.ICMPFilter
f.SetAll(true)
f.Accept(ipv6.ICMPTypeTimeExceeded)
f.Accept(ipv6.ICMPTypeEchoReply)
f.Accept(ipv6.ICMPTypeDestinationUnreachable)
if err := res.ipv6conn.SetICMPFilter(&f); err != nil {
return nil, err
}
} else {
res.ipv4conn = ipv4.NewPacketConn(res.conn)
defer res.ipv4conn.Close()
}
timeouts := 0
for i := 1; i <= maxttl; i++ {
next := res.Step(i)
if nil != next.Addr {
addrString := next.Addr.String()
if nil != DNScache {
next.Host = DNScache.LookupHost(addrString)
next.AS = DNScache.LookupAS(addrString)
}
}
if nil != cb {
cb(next, i, 1)
}
hops = append(hops, next)
if next.Final {
break
}
if next.Timeout {
timeouts++
} else {
timeouts = 0
}
if timeouts == MaxTimeouts {
break
}
}
return hops, nil
}
// RunMultiTrace preforms traceroute to specified host testing each hop several times
func RunMultiTrace(host string, source string, source6 string, maxrtt time.Duration, maxttl int, DNScache *LookupCache, rounds int, cb Callback) ([][]Hop, error) {
hops := make([][]Hop, 0, maxttl)
var res trace
var err error
isIPv6 := false
addrList, err := net.LookupIP(host)
if nil != err {
return nil, err
}
for _, addr := range addrList {
if addr.To4() != nil {
res.dest, err = net.ResolveIPAddr("ip4:icmp", addr.String())
break
}
}
if nil == res.dest {
for _, addr := range addrList {
if addr.To16() != nil {
isIPv6 = true
res.dest, err = net.ResolveIPAddr("ip6:58", addr.String())
break
}
}
}
if nil == res.dest {
return nil, errors.New("Unable to resolve destination host")
}
res.maxrtt = maxrtt
res.maxttl = maxttl
res.id = rand.Int() % 0x7fff
if isIPv6 {
res.msg = icmp.Message{Type: ipv6.ICMPTypeEchoRequest, Code: 0, Body: &icmp.Echo{ID: res.id, Seq: 1}}
} else {
res.msg = icmp.Message{Type: ipv4.ICMPTypeEcho, Code: 0, Body: &icmp.Echo{ID: res.id, Seq: 1}}
}
res.netmsg, err = res.msg.Marshal(nil)
if nil != err {
return nil, err
}
if !isIPv6 {
res.conn, err = net.ListenPacket("ip4:icmp", source)
} else {
res.conn, err = net.ListenPacket("ip6:58", source6)
}
if nil != err {
return nil, err
}
defer res.conn.Close()
if isIPv6 {
res.ipv6conn = ipv6.NewPacketConn(res.conn)
defer res.ipv6conn.Close()
if err := res.ipv6conn.SetControlMessage(ipv6.FlagHopLimit|ipv6.FlagSrc|ipv6.FlagDst|ipv6.FlagInterface, true); err != nil {
return nil, err
}
// var f ipv6.ICMPFilter
// f.SetAll(true)
// f.Accept(ipv6.ICMPTypeTimeExceeded)
// f.Accept(ipv6.ICMPTypeEchoReply)
// if err := res.ipv6conn.SetICMPFilter(&f); err != nil {
// return nil, err
// }
} else {
res.ipv4conn = ipv4.NewPacketConn(res.conn)
defer res.ipv4conn.Close()
}
timeouts := 0
for i := 1; i <= maxttl; i++ {
thisHops := make([]Hop, 0, rounds)
isFinal := false
notimeout := true
for j := 0; j < rounds; j++ {
next := res.Step(i)
if nil != next.Addr {
addrString := next.Addr.String()
if nil != DNScache {
next.Host = DNScache.LookupHost(addrString)
next.AS = DNScache.LookupAS(addrString)
}
}
if nil != cb {
cb(next, i, j+1)
}
thisHops = append(thisHops, next)
isFinal = next.Final || isFinal
notimeout = notimeout && (!next.Timeout)
}
hops = append(hops, thisHops)
if isFinal {
break
}
if notimeout {
timeouts = 0
} else {
timeouts++
}
if timeouts == MaxTimeouts {
break
}
}
return hops, nil
}
// Hop represents each hop of trace
type Hop struct {
Addr net.Addr
Host string
AS int64
RTT time.Duration
Final bool
Timeout bool
Down bool
Error error
}
// Step sends one echo packet and waits for result
func (t *trace) Step(ttl int) Hop {
var hop Hop
var wcm ipv6.ControlMessage
hop.Error = t.conn.SetReadDeadline(time.Now().Add(t.maxrtt))
if nil != hop.Error {
return hop
}
if nil != t.ipv4conn {
hop.Error = t.ipv4conn.SetTTL(ttl)
}
if nil != t.ipv6conn {
wcm.HopLimit = ttl
}
if nil != hop.Error {
return hop
}
sendOn := time.Now()
if nil != t.ipv4conn {
_, hop.Error = t.conn.WriteTo(t.netmsg, t.dest)
}
if nil != t.ipv6conn {
_, hop.Error = t.ipv6conn.WriteTo(t.netmsg, &wcm, t.dest)
}
if nil != hop.Error {
return hop
}
buf := make([]byte, 1500)
for {
var readLen int
readLen, hop.Addr, hop.Error = t.conn.ReadFrom(buf)
if nerr, ok := hop.Error.(net.Error); ok && nerr.Timeout() {
hop.Timeout = true
return hop
}
if nil != hop.Error {
return hop
}
var result *icmp.Message
if nil != t.ipv4conn {
result, hop.Error = icmp.ParseMessage(ProtocolICMP, buf[:readLen])
}
if nil != t.ipv6conn {
result, hop.Error = icmp.ParseMessage(ProtocolICMP6, buf[:readLen])
}
if nil != hop.Error {
return hop
}
hop.RTT = time.Since(sendOn)
switch result.Type {
case ipv4.ICMPTypeEchoReply:
if rply, ok := result.Body.(*icmp.Echo); ok {
if t.id != rply.ID {
continue
}
hop.Final = true
return hop
}
case ipv4.ICMPTypeTimeExceeded:
if rply, ok := result.Body.(*icmp.TimeExceeded); ok {
if len(rply.Data) > 24 {
if uint16(t.id) != binary.BigEndian.Uint16(rply.Data[24:26]) {
continue
}
return hop
}
}
case ipv6.ICMPTypeTimeExceeded:
if rply, ok := result.Body.(*icmp.TimeExceeded); ok {
if len(rply.Data) > 44 {
if uint16(t.id) != binary.BigEndian.Uint16(rply.Data[44:46]) {
continue
}
return hop
}
}
case ipv6.ICMPTypeEchoReply:
if rply, ok := result.Body.(*icmp.Echo); ok {
if t.id != rply.ID {
continue
}
hop.Final = true
return hop
}
case ipv6.ICMPTypeDestinationUnreachable:
if rply, ok := result.Body.(*icmp.Echo); ok {
if t.id != rply.ID {
continue
}
hop.Down = true
return hop
}
case ipv4.ICMPTypeDestinationUnreachable:
if rply, ok := result.Body.(*icmp.Echo); ok {
if t.id != rply.ID {
continue
}
hop.Down = true
return hop
}
}
}
}