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

时间:2017-10-26 18:02:17

标签: sockets go layer gopacket

我正在使用gopacket / layers api从数据包中提取upd数据,然后通过另一个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)

1 个答案:

答案 0 :(得分:1)

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