-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinglistener.go
283 lines (245 loc) · 5.71 KB
/
pinglistener.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
package pinglistener
import (
"fmt"
"log"
"net"
"time"
"sync"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
const (
// timeSliceLength = 8
protocolICMP = 1
protocolIPv6ICMP = 58
)
var (
ipv4Proto = map[string]string{"ip": "ip4:icmp", "udp": "udp4"}
ipv6Proto = map[string]string{"ip": "ip6:ipv6-icmp", "udp": "udp6"}
)
// type IcmpData struct {
// Bytes []byte
// Tracker int64
// }
type packet struct {
T time.Time
Addr string
Bytes []byte
Nbytes int
}
type PingListener struct {
ipv4 bool
status bool
ipaddr *net.IPAddr
addr string
network string
// OnRecv is called when Pinger receives and filtered a packet
OnRecv func(*packet)
// stop chan bool
done chan bool
}
func bytesToTime(b []byte) time.Time {
var nsec int64
for i := uint8(0); i < 8; i++ {
nsec += int64(b[i]) << ((7 - i) * 8)
}
return time.Unix(nsec/1000000000, nsec%1000000000)
}
func isIPv4(ip net.IP) bool {
return len(ip.To4()) == net.IPv4len
}
func isIPv6(ip net.IP) bool {
return len(ip) == net.IPv6len
}
func (p *PingListener) SetIPAddr(ipaddr *net.IPAddr) {
var ipv4 bool
if isIPv4(ipaddr.IP) {
ipv4 = true
} else if isIPv6(ipaddr.IP) {
ipv4 = false
}
p.ipaddr = ipaddr
p.addr = ipaddr.String()
p.ipv4 = ipv4
}
// network can be "ip" or "udp"
func NewPingListener(addr string, network string)(*PingListener, error){
ipaddr, err := net.ResolveIPAddr("ip", addr)
if err != nil {
return nil, err
}
var ipv4 bool
if isIPv4(ipaddr.IP) {
ipv4 = true
} else if isIPv6(ipaddr.IP) {
ipv4 = false
}
return &PingListener{
ipv4: ipv4,
status: false,
ipaddr: ipaddr,
addr: addr,
network: network,
// stop chan bool
// done: make(chan bool),
}, nil
}
func (p *PingListener) listen(netProto string, source string) *icmp.PacketConn {
conn, err := icmp.ListenPacket(netProto, source)
if err != nil {
log.Printf("Error listening for ICMP packets: %s\n", err.Error())
close(p.done)
return nil
}
return conn
}
func (p *PingListener) Status() bool{
return p.status
}
func (p *PingListener) Start() {
if p.status {
log.Println("PingListener is running")
return
}
p.status = true
p.done = make(chan bool)
var conn *icmp.PacketConn
if p.ipv4 {
if conn = p.listen(ipv4Proto[p.network], p.addr); conn == nil {
return
}
} else {
if conn = p.listen(ipv6Proto[p.network], p.addr); conn == nil {
return
}
}
defer conn.Close()
// defer p.finish()
var wg sync.WaitGroup
recv := make(chan *packet, 5)
defer close(recv)
wg.Add(1)
go p.recvICMP(conn, recv, &wg)
for {
select {
case <-p.done:
p.status = false
wg.Wait()
return
case r := <-recv:
err := p.filterPacket(r)
if err != nil {
log.Println("FATAL: ", err.Error())
}
}
}
}
func (p *PingListener) Stop() {
if p.status {
close(p.done)
p.status = false
}
}
func (p *PingListener) recvICMP(
conn *icmp.PacketConn,
recv chan<- *packet,
wg *sync.WaitGroup,
) {
defer wg.Done()
for {
select {
case <-p.done:
return
default:
bytes := make([]byte, 512)
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
n, addr, err := conn.ReadFrom(bytes)
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
// Read timeout
continue
} else {
log.Printf("Ping listener %v recv exception: %v", p.addr, neterr)
continue
}
}
}
recv <- &packet{T: time.Now() ,Addr: addr.String() ,Bytes: bytes[:n], Nbytes: n}
}
}
}
func (p *PingListener) filterPacket(recv *packet) error {
var bytes []byte
var proto int
if p.ipv4 {
if p.network == "ip" {
bytes = ipv4Payload(recv.Bytes)
} else {
bytes = recv.Bytes
}
proto = protocolICMP
} else {
bytes = recv.Bytes
proto = protocolIPv6ICMP
}
var m *icmp.Message
var err error
if m, err = icmp.ParseMessage(proto, bytes); err != nil {
return fmt.Errorf("Error parsing icmp message")
}
log.Printf("%+v, \n", m)
if m.Type == ipv4.ICMPTypeEchoReply || m.Type == ipv6.ICMPTypeEchoReply {
// Is an echo reply, ignore it
return nil
}
switch pkt := m.Body.(type) {
case *icmp.Echo:
// Don't know how to unmarshal this kind of data.
// data := IcmpData{}
// err := json.Unmarshal(m.Body.(*icmp.Echo).Data, &data)
// if err != nil {
// log.Println(err)
// } else {
// Rtt := time.Since(bytesToTime(data.Bytes))
// }
// Seq := pkt.Seq
// log.Printf("echo: rtt %v seq %v, tracker %v\n", Rtt, Seq, data.Tracker)
case *icmp.DefaultMessageBody:
// Don't know how to unmarshal this kind of data.
// data := IcmpData{}
// log.Printf("default msg: body %v \n", m.Body.(*icmp.DefaultMessageBody))
// err := json.Unmarshal(m.Body.(*icmp.DefaultMessageBody).Data, &data)
// if err != nil {
// log.Println(*(*string)(unsafe.Pointer(&m.Body.(*icmp.DefaultMessageBody).Data)))
// } else {
// Rtt := time.Since(bytesToTime(data.Bytes))
// log.Printf("default msg: rtt %v seq %v, \n", Rtt, data.Tracker)
// }
case *icmp.DstUnreach:
return nil
case *icmp.TimeExceeded:
// log.Printf("time exceed %v", m.Body.(*icmp.TimeExceeded))
return nil
default:
return fmt.Errorf("Error, invalid ICMP echo reply. Body type: %T, %s",
pkt, pkt)
}
handler := p.OnRecv
if handler != nil {
handler(recv)
}
return nil
}
func ipv4Payload(b []byte) []byte {
if len(b) < ipv4.HeaderLen {
return b
}
hdrlen := int(b[0]&0x0f) << 2
if hdrlen < len(b) {
return b[hdrlen:]
} else {
return b
}
}