-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathping.go
100 lines (85 loc) · 1.95 KB
/
ping.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
package libping
import (
"fmt"
"github.com/pkg/errors"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"golang.org/x/sys/unix"
"net"
"os"
"strings"
"time"
)
const payload = "abcdefghijklmnopqrstuvwabcdefghi"
func IcmpPing(address string, timeout int32) (int32, error) {
i := net.ParseIP(address)
if i == nil {
return 0, fmt.Errorf("unable to parse ip %s", address)
}
var err error
v6 := i.To4() == nil
var fd int
if !v6 {
fd, err = unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_ICMP)
} else {
fd, err = unix.Socket(unix.AF_INET6, unix.SOCK_DGRAM, unix.IPPROTO_ICMPV6)
}
f := os.NewFile(uintptr(fd), "dgram")
if err != nil {
return 0, errors.WithMessage(err, "create file from fd")
}
conn, err := net.FilePacketConn(f)
if err != nil {
return 0, errors.WithMessage(err, "create conn")
}
defer func(conn net.PacketConn) {
_ = conn.Close()
}(conn)
start := time.Now()
for seq := 1; timeout > 0; seq++ {
var sockTo int32
if timeout > 1000 {
sockTo = 1000
} else {
sockTo = timeout
}
timeout -= sockTo
err := conn.SetReadDeadline(time.Now().Add(time.Duration(sockTo) * time.Millisecond))
if err != nil {
return 0, errors.WithMessage(err, "set read timeout")
}
msg := icmp.Message{
Body: &icmp.Echo{
ID: 0xDBB,
Seq: seq,
Data: []byte(payload),
},
}
if !v6 {
msg.Type = ipv4.ICMPTypeEcho
} else {
msg.Type = ipv6.ICMPTypeEchoRequest
}
data, err := msg.Marshal(nil)
if err != nil {
return 0, errors.WithMessage(err, "make icmp message")
}
_, err = conn.WriteTo(data, &net.UDPAddr{
IP: i,
Port: 0,
})
if err != nil {
return 0, errors.WithMessage(err, "write icmp message")
}
_, _, err = conn.ReadFrom(data)
if err != nil {
if strings.Contains(err.Error(), "timeout") {
continue
}
return 0, errors.WithMessage(err, "read icmp message")
}
return int32(time.Since(start).Milliseconds()), nil
}
return -1, nil
}