From 5b416aa419f45b5e28c6bf853f6a4628bab3a268 Mon Sep 17 00:00:00 2001 From: spiritlhl <103393591+spiritLHLS@users.noreply.github.com> Date: Sat, 5 Apr 2025 08:52:42 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DIPV6=E5=8C=85=E6=9E=84?= =?UTF-8?q?=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bk/trace_ipv6.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/bk/trace_ipv6.go b/bk/trace_ipv6.go index 22d1d48..3dff9cc 100644 --- a/bk/trace_ipv6.go +++ b/bk/trace_ipv6.go @@ -1,6 +1,7 @@ package backtrace import ( + "encoding/binary" "net" "golang.org/x/net/icmp" @@ -8,6 +9,7 @@ import ( ) func newPacketV6(id uint16, dst net.IP, ttl int) []byte { + // 创建ICMP消息(回显请求) msg := icmp.Message{ Type: ipv6.ICMPTypeEchoRequest, Body: &icmp.Echo{ @@ -15,16 +17,20 @@ func newPacketV6(id uint16, dst net.IP, ttl int) []byte { Seq: int(id), }, } - p, _ := msg.Marshal(nil) - ip := &ipv6.Header{ - Version: ipv6.Version, - NextHeader: ProtocolIPv6ICMP, - HopLimit: ttl, - Dst: dst, - } - buf, err := ip.Marshal() //TODO 修复 - if err != nil { - return nil - } - return append(buf, p...) + // 序列化ICMP消息 + icmpData, _ := msg.Marshal(nil) + // 手动创建原始IPv6数据包头部 + ipHeaderBytes := make([]byte, ipv6.HeaderLen) + // 设置版本和流量类别(第一个字节) + ipHeaderBytes[0] = (ipv6.Version << 4) + // 设置下一个头部(协议) + ipHeaderBytes[6] = ProtocolIPv6ICMP + // 设置跳数限制 + ipHeaderBytes[7] = byte(ttl) + // 设置有效载荷长度(2字节字段) + binary.BigEndian.PutUint16(ipHeaderBytes[4:6], uint16(len(icmpData))) + // 设置目标地址(最后16个字节) + copy(ipHeaderBytes[24:40], dst.To16()) + // 合并头部和ICMP数据 + return append(ipHeaderBytes, icmpData...) }