-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.go
219 lines (159 loc) · 3.3 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
package ping
import (
"context"
"errors"
"log"
"net"
"os"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"golang.org/x/time/rate"
)
var (
ErrUnknownReply = errors.New("unknown reply")
ErrCorruptedReply = errors.New("corrupted reply")
)
const (
ProtocolICMP = 1
ProtocolICMPv6 = 58
)
type Request struct {
Target net.IP
Size int
Count int
Delay time.Duration
}
type Reply struct {
TTL int
RTT time.Duration
Seq int
Src net.IP
Err error
}
func (r *Request) Send(ctx context.Context) (<-chan Reply, error) {
var c *icmp.PacketConn
var err error
var proto int
var pktType icmp.Type
rc := make(chan Reply)
if r.Size < 15 {
r.Size = 15
}
if len(r.Target) == 4 {
c, err = icmp.ListenPacket("udp6", "::")
proto = ProtocolICMPv6
pktType = ipv6.ICMPTypeEchoRequest
} else {
c, err = icmp.ListenPacket("udp4", "0.0.0.0")
proto = ProtocolICMP
pktType = ipv4.ICMPTypeEcho
}
if err != nil {
return nil, err
}
go func() {
defer c.Close()
received := 0
var im *icmp.Message
for {
sent := time.Now()
var reply Reply
var err error
var n int
rb := make([]byte, 1500)
if proto == ProtocolICMPv6 {
var rcm *ipv6.ControlMessage
pc := c.IPv6PacketConn()
if err := pc.SetControlMessage(0xFF, true); err != nil {
panic("couldn't set ipv6 cm flags: " + err.Error())
}
n, rcm, _, err = pc.ReadFrom(rb)
if err != nil {
reply.Err = err
goto send
}
reply.Src = rcm.Src
reply.TTL = rcm.HopLimit
} else {
var rcm *ipv4.ControlMessage
pc := c.IPv4PacketConn()
if err := pc.SetControlMessage(0xFF, true); err != nil {
panic("couldn't set ipv4 cm flags: " + err.Error())
}
n, rcm, _, err = pc.ReadFrom(rb)
if err != nil {
log.Printf("err: %v", err)
reply.Err = err
goto send
}
reply.Src = rcm.Src
reply.TTL = rcm.TTL
}
im, err = icmp.ParseMessage(proto, rb[:n])
if err != nil {
reply.Err = err
goto send
}
switch im.Type {
case ipv4.ICMPTypeEchoReply, ipv6.ICMPTypeEchoReply:
if ep, ok := im.Body.(*icmp.Echo); ok {
err := sent.UnmarshalBinary(ep.Data[0:15])
if err != nil {
reply.Err = ErrCorruptedReply
break
}
reply.RTT = time.Since(sent)
reply.Seq = ep.Seq
break
}
default:
reply.Err = ErrUnknownReply
}
send:
rc <- reply
received += 1
if received >= r.Count {
close(rc)
return
}
}
}()
go func() {
id := os.Getpid() & 0xffff
data := make([]byte, r.Size)
lim := rate.NewLimiter(rate.Every(r.Delay), 1)
for seq := 0; seq < r.Count; seq++ {
lim.Wait(ctx)
t := time.Now()
tsb, err := t.MarshalBinary()
if err != nil {
log.Printf("time marshal error: %v", err)
continue
}
copy(data, tsb)
msg := icmp.Message{
Type: pktType,
Code: 0,
Body: &icmp.Echo{
ID: id,
Seq: seq,
Data: data,
},
}
mmsg, err := msg.Marshal(nil)
if err != nil {
log.Printf("icmp marshal error: %v", err)
continue
}
target := &net.UDPAddr{IP: r.Target}
if n, err := c.WriteTo(mmsg, target); err != nil {
log.Printf("write error: %v", err)
} else if n != len(mmsg) {
log.Printf("incomplete write: %v", err)
}
}
}()
return rc, nil
}