forked from digineo/go-ping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiving.go
118 lines (103 loc) · 2.8 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
106
107
108
109
110
111
112
113
114
115
116
117
118
package ping
import (
"fmt"
"net"
"time"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
// receiver listens on the raw socket and correlates ICMP Echo Replys with
// currently running requests.
func (pinger *Pinger) receiver(proto int, conn *icmp.PacketConn) {
rb := make([]byte, 1500)
// read incoming packets
for {
if n, source, err := conn.ReadFrom(rb); err != nil {
if netErr, ok := err.(net.Error); !ok || !netErr.Temporary() {
break // socket gone
}
} else {
pinger.receive(proto, rb[:n], source.(*net.IPAddr).IP, time.Now())
}
}
// close running requests
pinger.mtx.RLock()
for _, req := range pinger.requests {
req.handleReply(errClosed, nil, 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 succeeds, the body will given to process() for further processing.
func (pinger *Pinger) receive(proto int, bytes []byte, addr net.IP, t time.Time) {
// parse message
m, err := icmp.ParseMessage(proto, bytes)
if err != nil {
return
}
// evaluate message
switch m.Type {
case ipv4.ICMPTypeEchoReply, ipv6.ICMPTypeEchoReply:
pinger.process(m.Body, nil, addr, &t)
case ipv4.ICMPTypeDestinationUnreachable, ipv6.ICMPTypeDestinationUnreachable:
body := m.Body.(*icmp.DstUnreach)
if body == nil {
return
}
var bodyData []byte
switch proto {
case ProtocolICMP:
// parse header of original IPv4 packet
hdr, err := ipv4.ParseHeader(body.Data)
if err != nil {
return
}
bodyData = body.Data[hdr.Len:]
case ProtocolICMPv6:
// parse header of original IPv6 packet (we don't need the actual
// header, but want to detect parsing errors)
_, err := ipv6.ParseHeader(body.Data)
if err != nil {
return
}
bodyData = body.Data[ipv6.HeaderLen:]
default:
return
}
// parse ICMP message after the IP header
msg, err := icmp.ParseMessage(proto, bodyData)
if err != nil {
return
}
pinger.process(msg.Body, fmt.Errorf("%s", m.Type), nil, nil)
}
}
// process will finish a currently running Echo Request, if the body is
// an ICMP Echo reply to a request from us.
func (pinger *Pinger) process(body icmp.MessageBody, result error, addr net.IP, tRecv *time.Time) {
echo, ok := body.(*icmp.Echo)
if !ok || echo == nil {
if pinger.LogUnexpectedPackets {
log.Infof("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.Lock()
req := pinger.requests[uint16(echo.Seq)]
if _, ok := req.(*simpleRequest); ok {
// a simpleRequest is finished on the first reply
delete(pinger.requests, uint16(echo.Seq))
}
pinger.mtx.Unlock()
if req != nil {
req.handleReply(result, addr, tRecv)
}
}