1 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個贊
Usingos.Write將在后臺執(zhí)行write系統(tǒng)調(diào)用,這意味著實(shí)際文件。要將數(shù)據(jù)“寫入”到網(wǎng)絡(luò)套接字,您需要使用sendto系統(tǒng)調(diào)用。
以下示例使用自定義以太類型發(fā)送數(shù)據(jù)。所以只是一個帶有一些數(shù)據(jù)的以太網(wǎng)數(shù)據(jù)包。
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 關(guān)注
- 143 瀏覽
添加回答
舉報(bào)