From 63ca4d0418b4dd2bc2b9071eae14aa1d3d5799b5 Mon Sep 17 00:00:00 2001 From: Leo Date: Sat, 14 Jan 2023 20:22:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=80=E4=BA=9B=E5=8C=85=E5=90=88?= =?UTF-8?q?=E6=B3=95=E6=80=A7=E5=88=A4=E6=96=AD=E4=B8=8A=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- trace/icmp_ipv4.go | 22 ++++++++++++++-------- trace/icmp_ipv6.go | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/trace/icmp_ipv4.go b/trace/icmp_ipv4.go index 1e6c6e1..5d7ddd2 100644 --- a/trace/icmp_ipv4.go +++ b/trace/icmp_ipv4.go @@ -29,16 +29,20 @@ type ICMPTracer struct { } func (t *ICMPTracer) PrintFunc() { - // defer t.wg.Done() + defer t.wg.Done() var ttl = 0 for { + if t.AsyncPrinter != nil { + t.AsyncPrinter(&t.res) + } if t.RealtimePrinter != nil { // 接收的时候检查一下是不是 3 跳都齐了 if len(t.res.Hops)-1 > ttl { if len(t.res.Hops[ttl]) == t.NumMeasurements { t.RealtimePrinter(&t.res, ttl) ttl++ - if ttl == t.final { + + if ttl == t.final-1 || ttl >= t.MaxHops-1 { return } } @@ -50,6 +54,7 @@ func (t *ICMPTracer) PrintFunc() { func (t *ICMPTracer) Execute() (*Result, error) { t.inflightRequest = make(map[int]chan Hop) + if len(t.res.Hops) > 0 { return &t.res, ErrTracerouteExecuted } @@ -68,6 +73,7 @@ func (t *ICMPTracer) Execute() (*Result, error) { t.final = -1 go t.listenICMP() + t.wg.Add(1) go t.PrintFunc() for ttl := t.BeginHop; ttl <= t.MaxHops; ttl++ { t.inflightRequestLock.Lock() @@ -81,15 +87,14 @@ func (t *ICMPTracer) Execute() (*Result, error) { go t.send(ttl) } <-time.After(time.Millisecond * 100) - if t.AsyncPrinter != nil { - t.AsyncPrinter(&t.res) - } } t.wg.Wait() t.res.reduce(t.final) if t.final != -1 { - t.RealtimePrinter(&t.res, t.final-1) + if t.RealtimePrinter != nil { + t.RealtimePrinter(&t.res, t.final-1) + } } else { for i := 0; i < t.NumMeasurements; i++ { t.res.add(Hop{ @@ -100,7 +105,9 @@ func (t *ICMPTracer) Execute() (*Result, error) { Error: ErrHopLimitTimeout, }) } - t.RealtimePrinter(&t.res, t.MaxHops-1) + if t.RealtimePrinter != nil { + t.RealtimePrinter(&t.res, t.MaxHops-1) + } } return &t.res, nil } @@ -293,7 +300,6 @@ func (t *ICMPTracer) send(ttl int) error { h.fetchIPData(t.Config) t.res.add(h) - case <-time.After(t.Timeout): if t.final != -1 && ttl > t.final { return nil diff --git a/trace/icmp_ipv6.go b/trace/icmp_ipv6.go index 3005b90..5b1b851 100644 --- a/trace/icmp_ipv6.go +++ b/trace/icmp_ipv6.go @@ -135,7 +135,7 @@ func (t *ICMPTracerv6) listenICMP() { if msg.N == nil { continue } - if msg.Msg[129] == 0 { + if msg.Msg[0] == 129 { rm, err := icmp.ParseMessage(58, msg.Msg[:*msg.N]) if err != nil { log.Println(err) @@ -161,7 +161,7 @@ func (t *ICMPTracerv6) listenICMP() { if process_id == int64(os.Getpid()&0x7f) { dstip := net.IP(msg.Msg[32:48]) // 无效包本地环回包 - if dstip.String() != "::" { + if dstip.String() == "::" { continue } if dstip.Equal(t.DestIP) || dstip.Equal(net.IPv6zero) {