Go:如何从数据包中删除数据链路层?

发布于 2025-01-19 12:59:53 字数 252 浏览 4 评论 0原文

我正在使用 gopacket 库,我从电线中读取数据包。目前,我阅读的所有数据包都包含四个层:链接,网络,传输和应用程序数据。

我需要从所有数据包中删除链接层,然后将其余的保存到文件中。尚未找到任何有关使数据包剥离部分正确的信息或文档。

有人知道该怎么做吗?

I am using the gopacket library, and I read packets from the wire. Right now, all packets I read contain four layers: Link, Network, Transport, and Application Data.

I need to remove the Link layer from all packets and save the rest to a file. Haven't found any information or docs about making the packet stripping part right.

Does anyone know how to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月竹挽风 2025-01-26 12:59:53

我找到了一种可能的方法 - 从必要的数据包层中加入字节:

// `packet` variable contains four layers including the Link layer 
packet := <-packetSource.Packets()

var packetData []byte
packetData = append(packetData, packet.NetworkLayer().LayerContents()...)
packetData = append(packetData, packet.TransportLayer().LayerContents()...)
packetData = append(packetData, packet.ApplicationLayer().LayerContents()...)

// The `packetData` variable is a []bytes representation of all layers 
// except the Link layer, and it might be written to a *.pcap file.

I found one possible way - to concatenate bytes from necessary packet layers:

// `packet` variable contains four layers including the Link layer 
packet := <-packetSource.Packets()

var packetData []byte
packetData = append(packetData, packet.NetworkLayer().LayerContents()...)
packetData = append(packetData, packet.TransportLayer().LayerContents()...)
packetData = append(packetData, packet.ApplicationLayer().LayerContents()...)

// The `packetData` variable is a []bytes representation of all layers 
// except the Link layer, and it might be written to a *.pcap file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文