diff --git a/pkg/ping/icmp.go b/pkg/ping/icmp.go index ad1f437..421e6d4 100644 --- a/pkg/ping/icmp.go +++ b/pkg/ping/icmp.go @@ -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 } @@ -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 @@ -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()))) } } } diff --git a/pkg/ping/icmp_windows.go b/pkg/ping/icmp_windows.go index 07e56ee..8aac471 100644 --- a/pkg/ping/icmp_windows.go +++ b/pkg/ping/icmp_windows.go @@ -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() @@ -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, } } }