-
Notifications
You must be signed in to change notification settings - Fork 4
/
traceroute.go
187 lines (164 loc) · 4.22 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
package traceroute
import (
"errors"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"math/rand"
"net"
"strconv"
"time"
)
// Exec returns TraceData with initialized Hops and inserts the IP version into the protocol
func Exec(dest net.IP, timeout time.Duration, tries int, maxTTL int, proto string, port int) (data TraceData) {
data = TraceData{
Hops: make([][]Hop, tries),
Dest: dest,
Timeout: timeout,
Tries: tries,
MaxTTL: maxTTL,
Port: port,
Proto: proto,
}
if dest.To4() == nil {
data.IPv = "6"
} else {
data.IPv = "4"
}
return
}
// Next executes the doHop method for every try.
func (data *TraceData) Next() (err error) {
ttl := len(data.Hops[0]) + 1
if ttl > data.MaxTTL {
return errors.New("Maximum TTL reached")
}
for try := 0; try < data.Tries; try++ {
currentHop, err := doHop(ttl, data.Dest, data.Timeout, data.Proto, data.Port, data.IPv)
if err != nil {
return err
}
if currentHop.Err == nil {
currentHop.AddrDNS, _ = net.LookupAddr(currentHop.AddrIP.String()) // maybe use memoization
}
currentHop.TryNumber = try
data.Hops[try] = append(data.Hops[try], currentHop)
}
return
}
func doHop(ttl int, dest net.IP, timeout time.Duration, proto string, port int, ipv string) (currentHop Hop, err error) {
req := []byte{}
conn, err := createConnection(dest, port, ttl, ipv, proto)
if err != nil {
}
listenAddress := "0.0.0.0"
if ipv == "4" {
if proto == "icmp" {
req, err = createICMPEcho(ipv4.ICMPTypeEcho)
if err != nil {
return
}
}
} else if ipv == "6" {
listenAddress = "::0"
if proto == "icmp" {
req, err = createICMPEcho(ipv6.ICMPTypeEchoRequest)
if err != nil {
return
}
}
}
packetConn, err := icmp.ListenPacket("ip"+ipv+":"+"icmp", listenAddress)
if err != nil {
return
}
defer packetConn.Close()
start := time.Now()
_, err = conn.Write(req)
if err != nil {
return
}
if err = packetConn.SetDeadline(time.Now().Add(timeout)); err != nil {
return
}
readBytes := make([]byte, 1500) // 1500 Bytes ethernet MTU
_, sAddr, connErr := packetConn.ReadFrom(readBytes) // first return value (Code) might be useful
latency := time.Since(start)
currentHop = Hop{
TTL: ttl,
Latency: latency,
Err: connErr,
}
if connErr == nil {
currentHop.AddrIP = net.ParseIP(sAddr.String())
if currentHop.AddrIP == nil {
currentHop.Err = errors.New("timeout reached")
}
}
return currentHop, err
}
// All executes all doHops for all tries.
func (data *TraceData) All() (err error) {
for try := 0; try < data.Tries; try++ {
for ttl := 1; ttl <= data.MaxTTL; ttl++ {
currentHop, err := doHop(ttl, data.Dest, data.Timeout, data.Proto, data.Port, data.IPv)
if err != nil {
return err
}
if currentHop.Err == nil {
currentHop.AddrDNS, _ = net.LookupAddr(currentHop.AddrIP.String()) // maybe use memoization
}
currentHop.TryNumber = try
data.Hops[try] = append(data.Hops[try], currentHop)
if currentHop.Err == nil && data.Dest.Equal(currentHop.AddrIP) {
break
}
}
}
return
}
func createConnection(dest net.IP, port, ttl int, ipv, proto string) (net.Conn, error) {
var destString string
if port == 0 {
destString = dest.String()
} else {
destString = dest.String() + ":" + strconv.Itoa(port)
}
dialProto := proto
if proto == "udp" {
dialProto += ipv
} else if proto == "icmp" {
dialProto = "ip" + ipv + ":" + proto
} else {
return nil, errors.New("protocol not implemented")
}
conn, err := net.Dial(dialProto, destString)
if err != nil {
return nil, err
}
defer conn.Close()
if ipv == "4" {
newConn := ipv4.NewConn(conn)
err = newConn.SetTTL(ttl)
return conn, err
} else if ipv == "6" {
newConn := ipv6.NewConn(conn)
err = newConn.SetHopLimit(ttl)
return conn, err
}
return nil, errors.New("no valid ip version given")
}
func createICMPEcho(ICMPTypeEcho icmp.Type) (req []byte, err error) {
echo := icmp.Message{
Type: ICMPTypeEcho, Code: 0,
Body: &icmp.Echo{
ID: rand.Int(),
Seq: 1, // TODO Sequence should be incremented every Hop & the id should be changed on every try(not random but different)
Data: []byte("TABS"),
}}
req, err = echo.Marshal(nil)
return
}
type IPConn interface {
Write([]byte)
}