1 回答

TA贡献1809条经验 获得超8个赞
Usingos.Write将在后台执行write系统调用,这意味着实际文件。要将数据“写入”到网络套接字,您需要使用sendto系统调用。
以下示例使用自定义以太类型发送数据。所以只是一个带有一些数据的以太网数据包。
package main
import (
"log"
"net"
"os"
"syscall"
)
func main() {
ifname := os.Args[1]
iface, err := net.InterfaceByName(ifname)
if err != nil {
log.Fatal("get link by name:", err)
}
srcMac := iface.HardwareAddr
if len(srcMac) == 0 {
srcMac = []byte{0, 0, 0, 0, 0, 0}
}
dstMac := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}
fd, _ := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, int(htons(syscall.ETH_P_ALL)))
addr := syscall.SockaddrLinklayer{
Ifindex: iface.Index,
Halen: 6, // Ethernet address length is 6 bytes
Addr: [8]uint8{
dstMac[0],
dstMac[1],
dstMac[2],
dstMac[3],
dstMac[4],
dstMac[5],
},
}
ethHeader := []byte{
dstMac[0], dstMac[1], dstMac[2], dstMac[3], dstMac[4], dstMac[5],
srcMac[0], srcMac[1], srcMac[2], srcMac[3], srcMac[4], srcMac[5],
0x12, 0x34, // your custom ethertype
}
// Your custom data
p := append(ethHeader, []byte("Hello World")...)
err = syscall.Sendto(fd, p, 0, &addr)
if err != nil {
log.Fatal("Sendto:", err)
}
}
// htons converts a short (uint16) from host-to-network byte order.
func htons(i uint16) uint16 {
return (i<<8)&0xff00 | i>>8
}
- 1 回答
- 0 关注
- 118 浏览
添加回答
举报