-
Notifications
You must be signed in to change notification settings - Fork 15
/
packet_writer.go
59 lines (51 loc) · 1.22 KB
/
packet_writer.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
package pinger
import (
"fmt"
"net"
"sync"
"time"
"golang.org/x/net/icmp"
)
// An EchoRequest is a single ICMP echo request packet that will be sent.
// When the echoResponse is recieved, the "Received" field will be set and
// the Done() will be called on the WaitGroup.
//
type EchoRequest struct {
icmp.Message
Destination net.IP
Sent time.Time
Received time.Time
ID string
WaitGroup *sync.WaitGroup
}
// create a new EchoRequest instance.
func NewEchoRequest(msg icmp.Message, dest net.IP, wg *sync.WaitGroup) *EchoRequest {
return &EchoRequest{
Message: msg,
Destination: dest,
WaitGroup: wg,
ID: packetKey(dest.String(), msg.Body.(*icmp.Echo).ID, msg.Body.(*icmp.Echo).Seq),
}
}
func packetKey(addr string, id, seq int) string {
return fmt.Sprintf("%s-%d-%d", addr, id, seq)
}
func (p *Pinger) Send(req *EchoRequest) error {
b, err := req.Marshal(nil)
if err != nil {
return err
}
req.Sent = time.Now()
if req.Destination.To4() == nil {
_, err = p.v6Conn.WriteTo(b, &net.IPAddr{IP: req.Destination})
if err != nil {
return err
}
} else {
_, err = p.v4Conn.WriteTo(b, &net.IPAddr{IP: req.Destination})
if err != nil {
return err
}
}
return nil
}