Skip to content

Commit d50c0ea

Browse files
authored
Merge pull request #65 from gaopeiliang/master
fix parse mtr icmp package
2 parents 6f4d521 + 67c7aed commit d50c0ea

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

pkg/icmp/icmp.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package icmp
22

33
import (
4-
"bytes"
54
"encoding/binary"
65
"fmt"
76
"net"
@@ -174,19 +173,22 @@ func listenForSpecific4(conn *icmp.PacketConn, neededBody []byte, needID int, ne
174173

175174
if x.Type.(ipv4.ICMPType) == ipv4.ICMPTypeTimeExceeded {
176175
body := x.Body.(*icmp.TimeExceeded).Data
177-
index := bytes.Index(body, sent[:4])
178-
if index > 0 {
179-
x, _ := icmp.ParseMessage(protocolICMP, body[index:])
180-
switch x.Body.(type) {
181-
case *icmp.Echo:
182-
// Verification
183-
msg := x.Body.(*icmp.Echo)
184-
if msg.ID == needID && msg.Seq == needSeq {
185-
return peer.String(), []byte{}, nil
186-
}
187-
default:
188-
// ignore
176+
oh, err := ipv4.ParseHeader(body)
177+
if err != nil {
178+
continue
179+
}
180+
x, err := icmp.ParseMessage(protocolICMP, body[oh.Len:])
181+
if err != nil {
182+
continue
183+
}
184+
185+
switch x.Body.(type) {
186+
case *icmp.Echo:
187+
msg := x.Body.(*icmp.Echo)
188+
if msg.ID == needID && msg.Seq == needSeq {
189+
return peer.String(), []byte{}, nil
189190
}
191+
default:
190192
}
191193
}
192194

0 commit comments

Comments
 (0)