forked from aeden/traceroute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraceroute.go
376 lines (323 loc) · 10 KB
/
traceroute.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
// Package traceroute provides functions for executing a tracroute to a remote
// host.
package traceroute
import (
"errors"
"fmt"
"net"
"syscall"
"time"
"context"
"sync"
)
const DEFAULT_PORT = 33434
const DEFAULT_MAX_HOPS = 64
const DEFAULT_FIRST_HOP = 1
const DEFAULT_TIMEOUT_MS = 500
const DEFAULT_RETRIES = 3
const DEFAULT_PACKET_SIZE = 52
const DEFAULT_RESOLVE_TIMEOUT_MS = 1000
// Return the first non-loopback address as a 4 byte IP address. This address
// is used for sending packets out.
func socketAddr() (addr [4]byte, err error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if len(ipnet.IP.To4()) == net.IPv4len {
copy(addr[:], ipnet.IP.To4())
return
}
}
}
err = errors.New("you do not appear to be connected to the Internet")
return
}
// Given a host name convert it to a 4 byte IP address.
func destAddr(dest string) (destAddr [4]byte, err error) {
addrs, err := net.LookupHost(dest)
if err != nil {
return
}
addr := addrs[0]
ipAddr, err := net.ResolveIPAddr("ip", addr)
if err != nil {
return
}
copy(destAddr[:], ipAddr.IP.To4())
return
}
// TracrouteOptions type
type TracerouteOptions struct {
Port int
MaxHops int
FirstHop int
TimeoutMs int
Retries int
PacketSize int
ResolveTimeoutMs int
ResolveHosts bool
}
// Create a default options structure for a traceroute
func NewTracerouteOptions() *TracerouteOptions {
return &TracerouteOptions{
Port: DEFAULT_PORT,
MaxHops: DEFAULT_MAX_HOPS,
FirstHop: DEFAULT_FIRST_HOP,
TimeoutMs: DEFAULT_TIMEOUT_MS,
Retries: DEFAULT_RETRIES,
PacketSize: DEFAULT_PACKET_SIZE,
ResolveTimeoutMs: DEFAULT_RESOLVE_TIMEOUT_MS,
ResolveHosts: true,
}
}
// TracerouteHop type
type TracerouteHop struct {
Success bool
Address [4]byte
Host string
N int
ElapsedTime time.Duration
TTL int
}
func (hop *TracerouteHop) AddressString() string {
return fmt.Sprintf("%v.%v.%v.%v", hop.Address[0], hop.Address[1], hop.Address[2], hop.Address[3])
}
func (hop *TracerouteHop) HostOrAddressString() string {
hostOrAddr := hop.AddressString()
if hop.Host != "" {
hostOrAddr = hop.Host
}
return hostOrAddr
}
// TracerouteResult type
type TracerouteResult struct {
DestinationAddress [4]byte
Hops []TracerouteHop
}
func notify(hop TracerouteHop, channels []chan TracerouteHop) {
for _, c := range channels {
c <- hop
}
}
func closeNotify(channels []chan TracerouteHop) {
for _, c := range channels {
close(c)
}
}
// Structure that wraps a file descriptor and only allows it to be closed once
type fdCloser struct {
fd int
once sync.Once
}
// Closes the file descriptor if it's open, otherwise does nothing
func (c *fdCloser) Close() {
c.once.Do(func() {
syscall.Close(c.fd)
})
}
// Resolves an IP address to a hostname and returns the first result; returns an empty string if no PTR record exists,
// or ctx is cancelled before resolution completes
func resolveHost(ctx context.Context, address string) string {
currHost, err := net.DefaultResolver.LookupAddr(ctx, address)
if err == nil {
return currHost[0]
} else {
return ""
}
}
// Waits for hops on resolverChan, then asynchronously resolves their IP addresses to hostname and then writes
// them to resolvedChan
func resolver(ctx context.Context, options *TracerouteOptions, resolverChan chan TracerouteHop, resolvedChan chan TracerouteHop) {
resolveTimeout := time.Duration(options.ResolveTimeoutMs) * time.Millisecond
wg := sync.WaitGroup{}
for hop := range resolverChan {
if hop.Success && options.ResolveHosts { // only need resolution if successful and ResolveHosts was requested
wg.Add(1)
go func(hopToResolve TracerouteHop) { // resolve asynchronously so that a slow DNS resolver doesn't slow down the traceroute
defer wg.Done()
rctx, _ := context.WithDeadline(ctx, time.Now().Add(resolveTimeout))
hopToResolve.Host = resolveHost(rctx, hopToResolve.AddressString())
resolvedChan <- hopToResolve
}(hop)
} else {
resolvedChan <- hop
}
}
wg.Wait()
close(resolvedChan)
}
// Waits on recordChan for hops (which may arrive out of sequence), then reorders them sequentially, writes them to
// notifyChan, and records them to result
func recorder(result *TracerouteResult, recordChan chan TracerouteHop, notifyChan ...chan TracerouteHop) {
expectedTTL := 1
results := make(map[int]TracerouteHop)
record := func(hop TracerouteHop) {
result.Hops = append(result.Hops, hop)
notify(hop, notifyChan)
}
maxTTL := 0
for hop := range recordChan {
if hop.TTL > maxTTL {
maxTTL = hop.TTL
}
// if there is a gap between our last displayed hop and the current hop's TTL, see if we have queued the
// hop(s) in the gap, and if so, record them
for ttl := expectedTTL; ttl < hop.TTL; ttl++ {
priorHop, ok := results[ttl]
if ok {
record(priorHop)
delete(results, ttl)
expectedTTL = ttl + 1
} else {
// if we're missing any hop, then stop and wait
break
}
}
// if this hop is now in sequence, record it
if hop.TTL == expectedTTL {
record(hop)
expectedTTL++
} else {
// otherwise queue it to be recorded once we receive the hops before it
results[hop.TTL] = hop
}
}
// if the final hop we received is not the actual last hop, we need to iterate over the queued last hop(s) and
// display them in order
for ttl := expectedTTL; ttl <= maxTTL; ttl++ {
priorHop, ok := results[ttl]
if ok {
record(priorHop)
}
}
}
// Traceroute uses the given dest (hostname) and options to execute a traceroute
// from your machine to the remote host.
//
// Outbound packets are UDP packets and inbound packets are ICMP.
//
// Terminates with an error if ctx is cancelled before completion.
//
// Returns a TracerouteResult which contains an array of hops. Each hop includes
// the elapsed time and its IP address.
func Traceroute(ctx context.Context, dest string, options *TracerouteOptions, c ...chan TracerouteHop) (result TracerouteResult, err error) {
defer closeNotify(c)
if options == nil {
options = NewTracerouteOptions()
}
result.Hops = []TracerouteHop{}
destAddr, err := destAddr(dest)
result.DestinationAddress = destAddr
socketAddr, err := socketAddr()
if err != nil {
return
}
timeoutMs := (int64)(options.TimeoutMs)
tv := syscall.NsecToTimeval(1000 * 1000 * timeoutMs)
// Set up the socket to receive inbound packets
recvSocket, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP)
if err != nil {
return result, err
}
recvCloser := &fdCloser{fd: recvSocket}
defer recvCloser.Close()
// Set up the socket to send packets out.
sendSocket, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
return result, err
}
sendCloser := &fdCloser{fd: sendSocket}
defer sendCloser.Close()
// wrap the provided context so that we can signal our goroutines to terminate when we're done
doneCtx, done := context.WithCancel(ctx)
aborted := false
wg := sync.WaitGroup{}
// launch a goroutine to close the send/recv sockets when the context is cancelled; this will cause any
// sendto/recvfrom calls in progress to exit with an error, and avoid waiting for them to block until they time out
wg.Add(1)
go func(aborted *bool) {
defer wg.Done()
select {
case <-doneCtx.Done():
sendCloser.Close()
recvCloser.Close()
}
*aborted = true
return
}(&aborted)
// allocate queues large enough for the worst-case scenario of MaxHops hops; we don't want these channels to block
// as that would kill performance
resolverChan := make(chan TracerouteHop, options.MaxHops)
resolvedChan := make(chan TracerouteHop, options.MaxHops)
// launch our asynchronous DNS resolver goroutine
wg.Add(1)
go func() {
defer wg.Done()
resolver(ctx, options, resolverChan, resolvedChan)
}()
// launch our asynchronous hop recorder goroutine
wg.Add(1)
go func() {
defer wg.Done()
recorder(&result, resolvedChan, c...)
}()
// Bind to the local socket to listen for ICMP packets
syscall.Bind(recvSocket, &syscall.SockaddrInet4{Port: options.Port, Addr: socketAddr})
// main traceroute loop
retry := 0
for ttl := options.FirstHop; ttl <= options.MaxHops; ttl++ {
//log.Println("TTL: ", ttl)
start := time.Now()
// This sets the current hop TTL
syscall.SetsockoptInt(sendSocket, 0x0, syscall.IP_TTL, ttl)
// This sets the timeout to wait for a response from the remote host
syscall.SetsockoptTimeval(recvSocket, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &tv)
// Send a single null byte UDP packet
syscall.Sendto(sendSocket, []byte{0x0}, 0, &syscall.SockaddrInet4{Port: options.Port, Addr: destAddr})
var p = make([]byte, options.PacketSize)
n, from, err := syscall.Recvfrom(recvSocket, p, 0)
elapsed := time.Since(start)
if err == nil {
currAddr := from.(*syscall.SockaddrInet4).Addr
resolverChan <- TracerouteHop{Success: true, Address: currAddr, N: n, ElapsedTime: elapsed, TTL: ttl}
retry = 0
// if this reply is from our destination host, we are done
if currAddr == destAddr {
break
}
} else {
// if Recvfrom returned an error, it's possible the context was cancelled and recvSocket was closed; if so,
// aborted will be true
if aborted {
break
}
// optionally retry if requested, otherwise record an unsuccessful hop
retry++
if retry > options.Retries {
resolverChan <- TracerouteHop{Success: false, TTL: ttl}
retry = 0
} else {
ttl--
}
}
}
if (aborted) {
err = errors.New("cancelled")
} else {
err = nil
}
// close our resolver channel to signal the resolver to exit
close(resolverChan)
// cancel our context to signal goroutines to exit
done()
// wait for any pending DNS queries
wg.Wait()
return result, err
}
// Simplified traceroute wrapper
func TracerouteSimple(dest string, c ...chan TracerouteHop) (result TracerouteResult, err error) {
return Traceroute(context.Background(), dest, NewTracerouteOptions(), c...)
}