fix: 修复IPV6包构建

This commit is contained in:
spiritlhl 2025-04-05 08:52:42 +00:00
parent b93b8fbb4a
commit 5b416aa419

View File

@ -1,6 +1,7 @@
package backtrace package backtrace
import ( import (
"encoding/binary"
"net" "net"
"golang.org/x/net/icmp" "golang.org/x/net/icmp"
@ -8,6 +9,7 @@ import (
) )
func newPacketV6(id uint16, dst net.IP, ttl int) []byte { func newPacketV6(id uint16, dst net.IP, ttl int) []byte {
// 创建ICMP消息回显请求
msg := icmp.Message{ msg := icmp.Message{
Type: ipv6.ICMPTypeEchoRequest, Type: ipv6.ICMPTypeEchoRequest,
Body: &icmp.Echo{ Body: &icmp.Echo{
@ -15,16 +17,20 @@ func newPacketV6(id uint16, dst net.IP, ttl int) []byte {
Seq: int(id), Seq: int(id),
}, },
} }
p, _ := msg.Marshal(nil) // 序列化ICMP消息
ip := &ipv6.Header{ icmpData, _ := msg.Marshal(nil)
Version: ipv6.Version, // 手动创建原始IPv6数据包头部
NextHeader: ProtocolIPv6ICMP, ipHeaderBytes := make([]byte, ipv6.HeaderLen)
HopLimit: ttl, // 设置版本和流量类别(第一个字节)
Dst: dst, ipHeaderBytes[0] = (ipv6.Version << 4)
} // 设置下一个头部(协议)
buf, err := ip.Marshal() //TODO 修复 ipHeaderBytes[6] = ProtocolIPv6ICMP
if err != nil { // 设置跳数限制
return nil ipHeaderBytes[7] = byte(ttl)
} // 设置有效载荷长度2字节字段
return append(buf, p...) binary.BigEndian.PutUint16(ipHeaderBytes[4:6], uint16(len(icmpData)))
// 设置目标地址最后16个字节
copy(ipHeaderBytes[24:40], dst.To16())
// 合并头部和ICMP数据
return append(ipHeaderBytes, icmpData...)
} }