forked from digineo/go-ping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiving.go
105 lines (87 loc) · 2.2 KB
/
receiving.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
package ping
import (
"fmt"
"log"
"net"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
)
// receiver listens on the raw socket and correlates ICMP Echo Replys with
// currently running requests.
func (pinger *Pinger) receiver() {
rb := make([]byte, 1500)
// read incoming packets
for {
if n, _, err := pinger.conn.ReadFrom(rb); err != nil {
if netErr, ok := err.(net.Error); !ok || !netErr.Temporary() {
break // socket gone
}
} else {
pinger.receive(rb[:n], time.Now())
}
}
// close running requests
pinger.mtx.RLock()
for _, req := range pinger.requests {
req.respond(errClosed, nil)
}
pinger.mtx.RUnlock()
// Close() waits for us
pinger.wg.Done()
}
// receive takes the raw message and tries to evaluate an ICMP response.
// If that succeedes, the body will given to process() for further processing.
func (pinger *Pinger) receive(bytes []byte, t time.Time) {
// parse message
rm, err := icmp.ParseMessage(ProtocolICMP, bytes)
if err != nil {
return
}
// evaluate message
switch rm.Type {
case ipv4.ICMPTypeEchoReply:
pinger.process(rm.Body, nil, &t)
case ipv4.ICMPTypeDestinationUnreachable:
body := rm.Body.(*icmp.DstUnreach)
if body == nil {
return
}
// parse header of original IP packet
hdr, err := ipv4.ParseHeader(body.Data)
if err != nil {
return
}
// parse ICMP message after the IP header
msg, err := icmp.ParseMessage(ProtocolICMP, body.Data[hdr.Len:])
if err != nil {
return
}
pinger.process(msg.Body, fmt.Errorf("%s", rm.Type), nil)
case ipv4.ICMPTypeEcho:
// ignore
default:
// other ICMP packet
log.Printf("got: %+v %d", rm, rm.Body.Len(1))
}
}
// process will finish a currently running Echo Request, iff the body is
// an ICMP Echo reply to a request from us.
func (pinger *Pinger) process(body icmp.MessageBody, result error, tRecv *time.Time) {
echo, ok := body.(*icmp.Echo)
if !ok || echo == nil {
log.Printf("expected *icmp.Echo, got %#v", body)
return
}
// check if we sent this
if uint16(echo.ID) != pinger.id {
return
}
// search for existing running echo request
pinger.mtx.RLock()
req := pinger.requests[uint16(echo.Seq)]
pinger.mtx.RUnlock()
if req != nil {
req.respond(result, tRecv)
}
}