forked from mehrdadrad/mylg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icmp.go
293 lines (254 loc) · 5.78 KB
/
icmp.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
// Copyright 2016 Mehrdad Arshad Rad <arshad.rad@gmail.com>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
package icmp
import (
"fmt"
"math/rand"
"net"
"os"
"strings"
"syscall"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
const (
// DefaultTXTimeout is socket send timeout
DefaultTXTimeout int64 = 2000
// ProtocolIPv4ICMP is IANA ICMP IPv4
ProtocolIPv4ICMP = 1
// ProtocolIPv6ICMP is IANA ICMP IPv6
ProtocolIPv6ICMP = 58
// IPv4ICMPTypeEchoReply is ICMPv4 Echo Reply
IPv4ICMPTypeEchoReply = 0
// IPv4ICMPTypeDestinationUnreachable is ICMPv4 Destination Unreachable
IPv4ICMPTypeDestinationUnreachable = 3
// IPv4ICMPTypeTimeExceeded is ICMPv4 Time Exceeded
IPv4ICMPTypeTimeExceeded = 11
// IPv6ICMPTypeEchoReply is ICMPv6 Echo Reply
IPv6ICMPTypeEchoReply = 129
// IPv6ICMPTypeDestinationUnreachable is ICMPv6 Destination Unreachable
IPv6ICMPTypeDestinationUnreachable = 1
//IPv6ICMPTypeTimeExceeded is ICMPv6 Time Exceeded
IPv6ICMPTypeTimeExceeded = 3
)
// Trace represents trace properties
type Trace struct {
host string
src net.IP
ip net.IP
ips []net.IP
ttl int
fd int
seq int
family int
proto int
wait string
icmp bool
udp bool
tcp bool
resolve bool
ripe bool
realTime bool
pSize int
maxTTL int
uiTheme string
report bool
km bool
count int
}
// Ping represents ping request
type Ping struct {
m icmp.Message
id int
seq int
pSize int
count int
addr *net.IPAddr
addrs []net.IP
target string
isV4Avail bool
isV6Avail bool
isCIDR bool
forceV4 bool
forceV6 bool
network string
source string
timeout time.Duration
interval time.Duration
MaxRTT time.Duration
}
// HopResp represents hop's response
type HopResp struct {
num int
hop string
ip string
elapsed float64
last bool
err error
whois Whois
}
// ICMPResp represents ICMP response msg
type ICMPResp struct {
typ int
code int
id int
seq int
src net.IP
udp struct{ dstPort int }
ip struct {
dst net.IP
id int
}
}
// Whois represents prefix info from RIPE
type Whois struct {
holder string
asn float64
}
// Stats represents statistic's fields
type Stats struct {
count int64 // sent packet
min float64 // minimum/best rtt
avg float64 // average rtt
max float64 // maximum/worst rtt
pkl int64 // packet loss
}
func bytesToIPv6(b []byte) net.IP {
ip := make(net.IP, net.IPv6len)
copy(ip, b)
return ip
}
func icmpV4RespParser(b []byte) ICMPResp {
var resp ICMPResp
resp.typ = int(b[20])
resp.code = int(b[21])
resp.src = net.IPv4(b[12], b[13], b[14], b[15])
switch resp.typ {
case IPv4ICMPTypeEchoReply:
resp.id = int(b[24])<<8 | int(b[25])
resp.seq = int(b[26])<<8 | int(b[27])
case IPv4ICMPTypeDestinationUnreachable:
h, err := ipv4.ParseHeader(b[28:48])
if err == nil {
resp.ip.id = h.ID
resp.ip.dst = h.Dst
}
case IPv4ICMPTypeTimeExceeded:
h, err := ipv4.ParseHeader(b[28:48])
resp.id = int(b[52])<<8 | int(b[53])
resp.seq = int(b[54])<<8 | int(b[55])
if err == nil {
resp.ip.id = h.ID
resp.ip.dst = h.Dst
}
}
return resp
}
func icmpV6RespParser(b []byte) ICMPResp {
var resp ICMPResp
resp.typ = int(b[0])
resp.code = int(b[1])
//getting time exceeded w/ less than 32 bytes
if len(b) < 44 && resp.typ == IPv6ICMPTypeTimeExceeded {
return resp
}
switch resp.typ {
case IPv6ICMPTypeEchoReply:
resp.id = int(b[4])<<8 | int(b[5])
resp.seq = int(b[6])<<8 | int(b[7])
case IPv6ICMPTypeDestinationUnreachable:
resp.ip.dst = bytesToIPv6(b[32:48])
case IPv6ICMPTypeTimeExceeded:
resp.id = int(b[52])<<8 | int(b[53])
resp.seq = int(b[54])<<8 | int(b[55])
resp.ip.dst = bytesToIPv6(b[32:48])
}
return resp
}
func icmpV6Message(id, seq, pSize int) ([]byte, error) {
m, err := (&icmp.Message{
Type: ipv6.ICMPTypeEchoRequest, Code: 0,
Body: &icmp.Echo{
ID: id, Seq: seq,
Data: icmpPayload(pSize, false),
},
}).Marshal(nil)
if err != nil {
return m, os.NewSyscallError("icmpmsg", err)
}
return m, nil
}
func icmpV4Message(id, seq, pSize int) ([]byte, error) {
m, err := (&icmp.Message{
Type: ipv4.ICMPTypeEcho, Code: 0,
Body: &icmp.Echo{
ID: id, Seq: seq,
Data: icmpPayload(pSize, true),
},
}).Marshal(nil)
if err != nil {
return m, os.NewSyscallError("icmpmsg", err)
}
return m, nil
}
func icmpPayload(pSize int, isIPv4 bool) []byte {
var gData int
if isIPv4 {
gData = absInt(pSize) - (20 + 8)
} else {
gData = absInt(pSize) - (40 + 8)
}
// in case small packet requested
if gData < 0 {
gData = 0
}
if pSize >= 0 {
return make([]byte, gData)
}
return make([]byte, rand.Intn(gData))
}
func setIPv4TOS(fd int, v int) error {
err := syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_TOS, v)
if err != nil {
return os.NewSyscallError("setsockopt", err)
}
return nil
}
func setIPv4TTL(fd int, v int) error {
err := syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_TTL, v)
if err != nil {
return os.NewSyscallError("setsockopt", err)
}
return nil
}
func setIPv6HopLimit(fd int, v int) error {
err := syscall.SetsockoptInt(fd, syscall.IPPROTO_IPV6, syscall.IPV6_UNICAST_HOPS, v)
if err != nil {
return os.NewSyscallError("setsockopt", err)
}
return nil
}
func absInt(i int) int {
if i < 0 {
return i * -1
}
return i
}
func getLocalAddr(rAddr string) (net.IP, error) {
var ip []string
conn, err := net.Dial("udp", net.JoinHostPort(rAddr, "80"))
if err != nil {
return nil, err
}
defer conn.Close()
localAddr := conn.LocalAddr().String()
ip = strings.Split(localAddr, ":")
if len(ip) < 1 {
return nil, fmt.Errorf("local ip addr not found")
}
lAddr := net.ParseIP(ip[0])
return lAddr, nil
}