From 17460683023736da9a9062eed651070e36bc1131 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Thu, 12 Jan 2023 23:21:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BD=93=E6=9C=89=E5=A4=9A=E4=B8=AA?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E8=B7=9F=E8=B8=AA=E5=AE=9E=E4=BE=8B=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=EF=BC=8C=E5=AF=B9=E8=BE=93=E5=87=BA=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E9=80=A0=E6=88=90=E5=B9=B2=E6=89=B0=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20(IPv6,ICMPv6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trace/icmp_ipv6.go | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/trace/icmp_ipv6.go b/trace/icmp_ipv6.go index 31787ea..67901c9 100644 --- a/trace/icmp_ipv6.go +++ b/trace/icmp_ipv6.go @@ -6,7 +6,7 @@ import ( "os" "sync" "time" - + "encoding/binary" "golang.org/x/net/context" "golang.org/x/net/icmp" "golang.org/x/net/ipv6" @@ -74,19 +74,36 @@ func (t *ICMPTracerv6) listenICMP() { if msg.N == nil { continue } - rm, err := icmp.ParseMessage(58, msg.Msg[:*msg.N]) - if err != nil { - log.Println(err) - continue + dstip := net.IP(msg.Msg[32:48]) + if binary.BigEndian.Uint16(msg.Msg[52:54]) != uint16(os.Getpid()&0xffff) { + // // 如果类型为应答消息,且应答消息包的进程ID与主进程相同时不跳过 + if binary.BigEndian.Uint16(msg.Msg[52:54]) != 0 { + continue + } else { + if dstip.String() != "::" { + continue + } + if msg.Peer.String() != t.DestIP.String() { + continue + } + } } - // log.Println(msg.Peer) - switch rm.Type { - case ipv6.ICMPTypeTimeExceeded: - t.handleICMPMessage(msg, 0, rm.Body.(*icmp.TimeExceeded).Data) - case ipv6.ICMPTypeEchoReply: - t.handleICMPMessage(msg, 1, rm.Body.(*icmp.Echo).Data) - default: - // log.Println("received icmp message of unknown type", rm.Type) + + if dstip.Equal(t.DestIP) || dstip.Equal(net.IPv6zero) { + rm, err := icmp.ParseMessage(58, msg.Msg[:*msg.N]) + if err != nil { + log.Println(err) + continue + } + // log.Println(msg.Peer) + switch rm.Type { + case ipv6.ICMPTypeTimeExceeded: + t.handleICMPMessage(msg, 0, rm.Body.(*icmp.TimeExceeded).Data) + case ipv6.ICMPTypeEchoReply: + t.handleICMPMessage(msg, 1, rm.Body.(*icmp.Echo).Data) + default: + // log.Println("received icmp message of unknown type", rm.Type) + } } } }