Skip to content

Commit

Permalink
优化 icmp ping
Browse files Browse the repository at this point in the history
1. 从响应中获取真实 IP
2. 如果收到错误响应,在错误信息中添加响应 IP
  • Loading branch information
wzv5 committed Sep 24, 2020
1 parent 6b02892 commit f8099b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 12 additions & 4 deletions pkg/ping/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,16 @@ func (this *IcmpPing) rawping(network string) IPingResult {

for {
ttl := -1
var peer net.Addr
if isipv6 {
var cm *ipv6.ControlMessage
recvSize, cm, _, err = conn.IPv6PacketConn().ReadFrom(recvBytes)
recvSize, cm, peer, err = conn.IPv6PacketConn().ReadFrom(recvBytes)
if cm != nil {
ttl = cm.HopLimit
}
} else {
var cm *ipv4.ControlMessage
recvSize, cm, _, err = conn.IPv4PacketConn().ReadFrom(recvBytes)
recvSize, cm, peer, err = conn.IPv4PacketConn().ReadFrom(recvBytes)
if cm != nil {
ttl = cm.TTL
}
Expand Down Expand Up @@ -170,6 +171,13 @@ func (this *IcmpPing) rawping(network string) IPingResult {
if recvType == 1 && network == "ip" && recvID != id {
continue
}

if peer != nil {
if _ip := net.ParseIP(peer.String()); _ip != nil {
ip = _ip
}
}

switch recvType {
case 1:
// echo
Expand All @@ -180,10 +188,10 @@ func (this *IcmpPing) rawping(network string) IPingResult {
}
case 2:
// destination unreachable
return this.errorResult(errors.New("destination unreachable"))
return this.errorResult(errors.New(fmt.Sprintf("%s: destination unreachable", ip.String())))
case 3:
// time exceeded
return this.errorResult(errors.New("time exceeded"))
return this.errorResult(errors.New(fmt.Sprintf("%s: time exceeded", ip.String())))
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/ping/icmp_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ func (this *IcmpPing) ping_rootless(ctx context.Context) IPingResult {
return this.errorResult(errors.New("IcmpSendEcho failed"))
}
recvmsg := (*icmpv6_echo_reply)(unsafe.Pointer(&recv[0]))
var ip net.IP = recvmsg.address.sin6_addr[:]
if recvmsg.status != 0 {
return this.errorResult(errors.New(icmpStatusToString(recvmsg.status)))
return this.errorResult(errors.New(fmt.Sprintf("%s: %s", ip.String(), icmpStatusToString(recvmsg.status))))
}
return &IcmpPingResult{
Time: int(recvmsg.roundtriptime),
TTL: -1,
IP: recvmsg.address.sin6_addr[:],
IP: ip,
}
} else {
handle = IcmpCreateFile()
Expand All @@ -97,13 +98,14 @@ func (this *IcmpPing) ping_rootless(ctx context.Context) IPingResult {
return this.errorResult(errors.New("IcmpSendEcho failed"))
}
recvmsg := (*icmp_echo_reply)(unsafe.Pointer(&recv[0]))
var ip net.IP = recvmsg.address[:]
if recvmsg.status != 0 {
return this.errorResult(errors.New(icmpStatusToString(recvmsg.status)))
return this.errorResult(errors.New(fmt.Sprintf("%s: %s", ip.String(), icmpStatusToString(recvmsg.status))))
}
return &IcmpPingResult{
Time: int(recvmsg.roundtriptime),
TTL: int(recvmsg.option.ttl),
IP: recvmsg.address[:],
IP: ip,
}
}
}
Expand Down

0 comments on commit f8099b4

Please sign in to comment.