gpt4 book ai didi

Golang ICMP 数据包发送

转载 作者:行者123 更新时间:2023-12-01 22:25:51 25 4
gpt4 key购买 nike

我目前正在开发基于 Go 的路由器以用于教育目的。

当尝试序列化我从主机转发的 ICMP 数据包时,gopacket.ICMPv4 结构的 .SerializeTo 函数似乎正在剥离 ICMP 有效负载部分,并且仅包括字节数组中的类型、代码、校验和、ID 和序列它返回到缓冲区。

下面是我用来将我的 eth、ip 和 icmp 层发送到我的 WAN 接口(interface)的代码。从 LAN 接口(interface)收到这些层后,我对这些层所做的唯一更改是 ICMP ID、以太网 src/dst 和 IP src。

opts := gopacket.SerializeOptions{FixLengths: true, ComputeChecksums: true}
buff := gopacket.NewSerializeBuffer()
_ = gopacket.SerializeLayers(buff, opts, ethLayer, ipLayer, icmpLayer)

我发送之前的 ICMP 层显示它有一个有效负载部分:

icmpLayer = &{BaseLayer:{内容:[8 0 65 238 7 109 0 1] 有效载荷:[122 238 50 94 15 84 7 0 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55]} TypeCode:EchoRequest Checksum:53486 Id:10000 Seq:1}

客户端的 PCAP LAN 端向服务器发送 ICMP 请求。
PCAP host to server

服务器将请求转发到目的地的 PCAP WAN 端。
PCAP server to WAN

如果需要任何其他信息,请告诉我,我试图提供尽可能多的背景信息。

最佳答案

我最终确定 ICMP 类 SerializeTo 函数在将其字节数组表示放入缓冲区时正在剥离有效负载。为了测试,如果数据包有有效负载部分,我可以更改函数以使用以下代码动态分配更多空间,然后将有效负载添加到缓冲区中。将把它作为一个错误发布到 git 上,以便得到解决或指出为什么这个用例是一个边缘案例。

// SerializeTo writes the serialized form of this layer into the
// SerializationBuffer, implementing gopacket.SerializableLayer.
// See the docs for gopacket.SerializableLayer for more info.
func (i *ICMPv4) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
bytes, err := b.PrependBytes(len(i.Payload)+8)//8)
if err != nil {
return err
}
i.TypeCode.SerializeTo(bytes)
binary.BigEndian.PutUint16(bytes[4:], i.Id)
binary.BigEndian.PutUint16(bytes[6:], i.Seq)

startIndex := 8
for _, element := range i.Payload {
bytes[startIndex] = byte(uint16(element))
startIndex+= 1
}

if opts.ComputeChecksums {
bytes[2] = 0
bytes[3] = 0
i.Checksum = tcpipChecksum(b.Bytes(), 0)
}
binary.BigEndian.PutUint16(bytes[2:], i.Checksum)

return nil
}

关于Golang ICMP 数据包发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59989003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com