尝试使用 gopacket 创建一个以太网数据包(ARP、BGP、UDP、TCP)并获取它的头字节和长度。
尝试使用下面的示例,尝试列出所有层并找到有效负载位置,然后计算总标头长度和字节:
package main
import (
"fmt"
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func main() {
var (
buffer gopacket.SerializeBuffer
options gopacket.SerializeOptions
)
// Create the layers
ethernetLayer := &layers.Ethernet{
SrcMAC: net.HardwareAddr{0xff, 0xaa, 0xfa, 0xaa, 0xff, 0xaa},
DstMAC: net.HardwareAddr{0xbd, 0xbd, 0xbd, 0xbd, 0xbd, 0xbd},
EthernetType: layers.EthernetTypeIPv4,
}
ipLayer := &layers.IPv4{
SrcIP: net.IP{192, 168, 1, 3},
DstIP: net.IP{8, 8, 8, 8},
// Version: 4,
// IHL: 5,
Length: 24,
}
tcpLayer := &layers.TCP{
SrcPort: layers.TCPPort(4321),
DstPort: layers.TCPPort(80),
}
fmt.Println(tcpLayer)
payload := []byte{10, 20, 30, 40, 50, 60}
buffer = gopacket.NewSerializeBuffer()
gopacket.SerializeLayers(buffer, options, ethernetLayer, ipLayer, tcpLayer, gopacket.Payload(payload))
outgoingPacket := buffer.Bytes()
packet := gopacket.NewPacket(outgoingPacket, layers.LayerTypeEthernet, gopacket.Default)
for _, layer := range packet.Layers() {
fmt.Println("Packet layer:", layer.LayerType())
}
}
我得到如下错误:
Packet layer: Ethernet
Packet layer: IPv4
Packet layer: DecodeFailure
我在那里做错了吗?
谁能给我任何想法如何获取标头字节?