Golang 将 gopacket udpLayer 转换为字节并发送

Golang Converting gopacket udpLayer to bytes and sending

我正在使用 gopacket/layers api 从数据包中提取更新数据,然后通过另一个 udp 流再次发送,我不确定这样做是否正确,还有遇到了一些错误,如果有人能指出我正确的方向,那就太好了 我的代码

conn, err := net.Dial("udp", 1.1.1.1)

udp, _ := updpLayer.(*layers.UDP)

/*now if i send it like this*/

conn.Write(udp)
/*i get the errors: cannot use udp (type *layers.UDP) as type []byte in argument to conn.Write*/

/*I tried to convert to bytes using unsafe*/

con := *(*[unsafe.Sizeof(udp)]byte)(unsafe.Pointer(&udp))

/* I get cannot use conv (type [8]byte) as type []byte in argument to conn.Write */

/* I used ecoding/gob, but is it sending it as a byte stream?*/

encoder := gob.NewEncoder(conn)
encoder.Encode(udp) //or Encode(udp)

updpLayer 不直接表示数据,因此尝试将其转换为字节是没有意义的。请改为阅读 the Contents or Payload field