-
Notifications
You must be signed in to change notification settings - Fork 15
/
packet_reader.go
213 lines (195 loc) · 5.37 KB
/
packet_reader.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
package pinger
import (
"fmt"
"log"
"net"
"runtime"
"syscall"
"time"
"unsafe"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
// from "golang.org/x/net/internal/iana"
const ProtocolIPv6ICMP = 58 // ICMP for IPv6
const ProtocolICMP = 1 // Internet Control Message
type EchoResponse struct {
Peer string
Id int
Seq int
Received time.Time
}
func (e *EchoResponse) String() string {
return fmt.Sprintf("Peer %s, Id: %d, Seq: %d, Recv: %s", e.Peer, e.Id, e.Seq, e.Received)
}
func (p *Pinger) v6PacketReader() {
runtime.LockOSThread()
if p.Debug {
log.Printf("ipv6 listen loop starting.")
}
var f ipv6.ICMPFilter
f.SetAll(true)
f.Accept(ipv6.ICMPTypeDestinationUnreachable)
f.Accept(ipv6.ICMPTypePacketTooBig)
f.Accept(ipv6.ICMPTypeTimeExceeded)
f.Accept(ipv6.ICMPTypeParameterProblem)
f.Accept(ipv6.ICMPTypeEchoReply)
c := ipv6.NewPacketConn(p.v6Conn)
if err := c.SetICMPFilter(&f); err != nil {
panic(err)
}
rb := make([]byte, 1500)
var data []byte
ipconn, ok := p.v6Conn.(*net.IPConn)
if !ok {
panic("connection is not IPConn")
}
file, err := ipconn.File()
if err != nil {
panic(err.Error())
}
defer file.Close()
fd := file.Fd()
var pktTime time.Time
recvTime := syscall.Timeval{}
for {
n, peer, err := p.v6Conn.ReadFrom(rb)
if err != nil {
p.RLock()
if !p.shutdown {
log.Printf("go-pinger: failed to read from ipv6 socket. %s", err)
}
p.RUnlock()
break
}
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.SIOCGSTAMP), uintptr(unsafe.Pointer(&recvTime)))
err = nil
if errno != 0 {
err = errno
}
if err == nil {
pktTime = time.Unix(0, recvTime.Nano())
} else {
pktTime = time.Now()
}
rm, err := icmp.ParseMessage(ProtocolIPv6ICMP, rb[:n])
if err != nil {
fmt.Println(err.Error())
continue
}
if rm.Type == ipv6.ICMPTypeEchoReply {
data = rm.Body.(*icmp.Echo).Data
if len(data) < 9 {
log.Printf("go-pinger: invalid data payload from %s. Expected at least 9bytes got %d", peer.String(), len(data))
continue
}
if p.Debug {
log.Printf("go-pinger: recieved pkt. Peer %s, Id: %d, Seq: %d, Recv: %s\n", peer.String(), rm.Body.(*icmp.Echo).ID, rm.Body.(*icmp.Echo).Seq, pktTime.String())
}
// this goroutine needs to read packets from the network as fast as possible so we can get accurate timing information.
// if the packetChan blocks, latency measurements could start to grow. If we changed this to a non-blocking write on the
// channel, then packets that could not be written would appear as packet loss, which is probably worse then higher latency.
p.packetChan <- &EchoResponse{
Peer: peer.String(),
Seq: rm.Body.(*icmp.Echo).Seq,
Id: rm.Body.(*icmp.Echo).ID,
Received: pktTime,
}
}
}
if p.Debug {
log.Printf("ipv6 listen loop ended.")
}
}
func (p *Pinger) v4PacketReader() {
runtime.LockOSThread()
if p.Debug {
log.Printf("ipv4 listen loop starting.")
}
rb := make([]byte, 1500)
var data []byte
ipconn, ok := p.v4Conn.(*net.IPConn)
if !ok {
panic("connection is not IPConn")
}
file, err := ipconn.File()
if err != nil {
panic(err.Error())
}
defer file.Close()
fd := file.Fd()
var pktTime time.Time
recvTime := syscall.Timeval{}
for {
n, peer, err := p.v4Conn.ReadFrom(rb)
if err != nil {
p.RLock()
if !p.shutdown {
log.Printf("go-pinger: failed to read from ipv4 socket. %s", err)
}
p.RUnlock()
break
}
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.SIOCGSTAMP), uintptr(unsafe.Pointer(&recvTime)))
err = nil
if errno != 0 {
err = errno
}
if err == nil {
pktTime = time.Unix(0, recvTime.Nano())
} else {
pktTime = time.Now()
}
rm, err := icmp.ParseMessage(ProtocolICMP, rb[:n])
if err != nil {
fmt.Println(err.Error())
continue
}
if rm.Type == ipv4.ICMPTypeEchoReply {
data = rm.Body.(*icmp.Echo).Data
if len(data) < 9 {
log.Printf("go-pinger: invalid data payload from %s. Expected at least 9bytes got %d", peer.String(), len(data))
continue
}
if p.Debug {
log.Printf("go-pinger: recieved pkt. Peer %s, Id: %d, Seq: %d, Recv: %s\n", peer.String(), rm.Body.(*icmp.Echo).ID, rm.Body.(*icmp.Echo).Seq, pktTime.String())
}
// this goroutine needs to read packets from the network as fast as possible so we can get accurate timing information.
// if the packetChan blocks, latency measurements could start to grow. If we changed this to a non-blocking write on the
// channel, then packets that could not be written would appear as packet loss, which is probably worse then higher latency.
p.packetChan <- &EchoResponse{
Peer: peer.String(),
Seq: rm.Body.(*icmp.Echo).Seq,
Id: rm.Body.(*icmp.Echo).ID,
Received: pktTime,
}
}
}
if p.Debug {
log.Printf("go-pinger: ipv6 listen loop ended.")
}
}
func (p *Pinger) processPkt() {
defer p.processWg.Done()
for pkt := range p.packetChan {
key := packetKey(pkt.Peer, pkt.Id, pkt.Seq)
p.Lock()
req, ok := p.inFlight[key]
if ok {
delete(p.inFlight, key)
}
if ok {
if p.Debug {
log.Printf("go-pinger: reply packet matches request packet. %s\n", pkt.String())
}
req.Received = pkt.Received
req.WaitGroup.Done()
} else {
if p.Debug {
log.Printf("go-pinger: go-pinger: unexpected echo response. %s\n", pkt.String())
}
}
p.Unlock()
}
}