-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinger.go
196 lines (183 loc) · 5.46 KB
/
pinger.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
package main
import (
"errors"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/maltalex/goping/pingsocket"
"math/rand"
"net"
"time"
)
const (
MaxPacketSize = 64 * 1024
MinPacketSize = 20 /*ip*/ + 8 /*icmp*/
)
var (
ErrTimeout = pingsocket.TIMEOUTERR
ErrUnexpectedPacket = errors.New("unexpected packet")
serOptionsIpv6 = gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: false,
}
serOptionsIpv4 = gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
)
type ReceiveResult struct {
recvTime time.Time
source net.IP
ttl uint8
id uint16
seq uint16
len uint16
err error
}
type pinger struct {
socket pingsocket.Pingsocket
ipv4 bool
id uint16
destination net.IP
payload []byte
seqChan chan uint16
resultChan chan ReceiveResult
}
func NewPinger(destination net.IP, payloadLength int) *pinger {
payload := make([]byte, payloadLength)
rand.Seed(time.Now().UnixNano())
_, _ = rand.Read(payload) //always returns nil error according to doc
return &pinger{
ipv4: destination.To4() != nil,
destination: destination,
payload: payload,
id: uint16(rand.Uint32()),
seqChan: make(chan uint16),
resultChan: make(chan ReceiveResult, 1), //buffer of 1 to avoid goroutine leak
}
}
func (p *pinger) Init(ttl uint8, timeout time.Duration) (err error) {
if p.ipv4 {
p.socket, err = pingsocket.NewIPv4()
} else {
p.socket, err = pingsocket.NewIPv6()
}
if err != nil {
return fmt.Errorf("failed to create socket: %v", err)
}
if err := p.socket.SetTTL(uint8(ttl)); err != nil {
return fmt.Errorf("failed to set socket TTL to %v: %v", ttl, err)
}
if err := p.socket.SetReadTimeout(timeout); err != nil {
return fmt.Errorf("failed to set read timeout to %v: %v", timeout, err)
}
return nil
}
func (p *pinger) Send(seq uint16) (err error) {
serBuff := gopacket.NewSerializeBuffer()
if p.ipv4 {
icmp := &layers.ICMPv4{TypeCode: 0x0800 /*echo request*/, Id: p.id, Seq: seq}
if err = gopacket.SerializeLayers(serBuff, serOptionsIpv4, icmp, gopacket.Payload(p.payload)); err != nil {
return fmt.Errorf("error serializing ICMPv4 packet: %v", err)
}
} else {
icmp := &layers.ICMPv6{TypeCode: 0x8000 /*echo request*/}
echo := &layers.ICMPv6Echo{Identifier: p.id, SeqNumber: seq}
if err = gopacket.SerializeLayers(serBuff, serOptionsIpv6, icmp, echo, gopacket.Payload(p.payload)); err != nil {
return fmt.Errorf("error serializing ICMPv6 packet: %v", err)
}
}
return p.socket.SendTo(serBuff.Bytes(), p.destination)
}
func (p *pinger) StartReceiver() {
if p.ipv4 {
go func() {
p.ipv4Receiver()
}()
} else {
go func() {
p.ipv6Receiver()
}()
}
}
func (p *pinger) ipv6Receiver() {
buffer := make([]byte, MaxPacketSize)
for seq := range p.seqChan {
n, source, err := p.socket.Recvfrom(buffer)
receiveTime := time.Now()
if err != nil || n < MinPacketSize || n > MaxPacketSize {
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: err}
continue
}
//recvfrom on an IPv6 raw socket dosen't return the IP header (rfc3542)
parsedPacket := gopacket.NewPacket(buffer[0:n], layers.LayerTypeICMPv6, gopacket.NoCopy)
icmpLayer := parsedPacket.Layer(layers.LayerTypeICMPv6)
if icmpLayer == nil {
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: ErrUnexpectedPacket}
continue
}
echoLayer := parsedPacket.Layer(layers.LayerTypeICMPv6Echo)
if echoLayer == nil {
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: ErrUnexpectedPacket}
continue
}
echo := echoLayer.(*layers.ICMPv6Echo)
if echo.SeqNumber != seq || echo.Identifier != p.id { //Check seq and id
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: ErrUnexpectedPacket}
continue
}
p.resultChan <- ReceiveResult{
recvTime: receiveTime,
source: source,
len: uint16(n),
id: echo.Identifier,
seq: echo.SeqNumber,
//ttl: ip.TTL, //FIXME use recvMsg to get ttl
}
}
close(p.resultChan)
_ = p.socket.Close()
}
func (p *pinger) ipv4Receiver() {
buffer := make([]byte, MaxPacketSize)
for seq := range p.seqChan {
n, source, err := p.socket.Recvfrom(buffer)
receiveTime := time.Now()
if err != nil || n < MinPacketSize || n > MaxPacketSize {
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: err}
continue
}
parsedPacket := gopacket.NewPacket(buffer[0:n], layers.LayerTypeIPv4, gopacket.NoCopy)
ipLayer := parsedPacket.Layer(layers.LayerTypeIPv4)
if ipLayer == nil {
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: ErrUnexpectedPacket}
continue
}
ip := ipLayer.(*layers.IPv4)
if !ip.SrcIP.Equal(p.destination) { //Check source IP
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: ErrUnexpectedPacket}
continue
}
icmpLayer := parsedPacket.Layer(layers.LayerTypeICMPv4)
if icmpLayer == nil {
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: ErrUnexpectedPacket}
continue
}
icmp := icmpLayer.(*layers.ICMPv4)
if icmp.TypeCode.Type() != 0 || icmp.TypeCode.Code() != 0 || //Echo Reply
icmp.Seq != seq || icmp.Id != p.id { //Check seq and id
p.resultChan <- ReceiveResult{recvTime: receiveTime, err: ErrUnexpectedPacket}
continue
}
p.resultChan <- ReceiveResult{
recvTime: receiveTime,
source: source,
len: ip.Length - uint16(ip.IHL)*4,
id: icmp.Id,
seq: icmp.Seq,
ttl: ip.TTL,
}
}
close(p.resultChan)
_ = p.socket.Close()
}