|
|
|
@ -33,6 +33,7 @@ func getICMP(seq uint16) ICMP {
|
|
|
|
|
|
|
|
|
|
func sendICMPRequest(icmp ICMP, destAddr *net.IPAddr, timeout time.Duration) (PingResult, error) {
|
|
|
|
|
var res PingResult
|
|
|
|
|
res.RemoteIP = destAddr.String()
|
|
|
|
|
conn, err := net.DialIP("ip:icmp", nil, destAddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return res, err
|
|
|
|
@ -84,6 +85,7 @@ func checkSum(data []byte) uint16 {
|
|
|
|
|
type PingResult struct {
|
|
|
|
|
Duration time.Duration
|
|
|
|
|
RecvCount int
|
|
|
|
|
RemoteIP string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Ping(ip string, seq int, timeout time.Duration) (PingResult, error) {
|
|
|
|
@ -95,3 +97,14 @@ func Ping(ip string, seq int, timeout time.Duration) (PingResult, error) {
|
|
|
|
|
icmp := getICMP(uint16(seq))
|
|
|
|
|
return sendICMPRequest(icmp, ipAddr, timeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func IsIpPingable(ip string, timeout time.Duration, retryLimit int) bool {
|
|
|
|
|
for i := 0; i < retryLimit; i++ {
|
|
|
|
|
_, err := Ping(ip, 29, timeout)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|