我正在尝试使用 gopacket 向 127.0.0.1 发送一个 UDP 数据包。这是我的代码:
package main
import (
"fmt"
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
func main() {
handle, err := pcap.OpenLive("lo", 1500, false, pcap.BlockForever)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
eth := layers.Ethernet{
EthernetType: layers.EthernetTypeIPv4,
SrcMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
DstMAC: net.HardwareAddr{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
}
ip := layers.IPv4{
Version: 4,
TTL: 64,
SrcIP: net.IP{127, 0, 0, 1},
DstIP: net.IP{127, 0, 0, 1},
Protocol: layers.IPProtocolUDP,
}
udp := layers.UDP{
SrcPort: 62003,
DstPort: 8080,
}
udp.SetNetworkLayerForChecksum(&ip)
payload := []byte{'a', 'b', 'c', '\n'}
options := gopacket.SerializeOptions{
ComputeChecksums: true,
FixLengths: true,
}
buffer := gopacket.NewSerializeBuffer()
err = gopacket.SerializeLayers(buffer, options,
ð,
&ip,
&udp,
gopacket.Payload(payload),
)
if err != nil {
fmt.Printf("[-] Serialize error: %s\n", err.Error())
return
}
outgoingPacket := buffer.Bytes()
err = handle.WritePacketData(outgoingPacket)
if err != nil {
fmt.Printf("[-] Error while sending: %s\n", err.Error())
return
}
}
在终端中,我使用 netcat 监听传入的数据包:
nc -ulp 8080 -s 127.0.0.1
当我运行我的代码时,我可以在环回接口上的wireshark中看到生成的数据包具有正确的校验和,但数据包永远不会到达netcat。可能是什么问题?