Solution for Cannot receive custom packets with golang in linux
is Given Below:
I’m a newbie in golang programming and I am trying to write a program that receives packets created by myself using Packeth software. When I run the program, it does not receive packets from Packeth, communication packets in the network are still received normally. When I open Wireshark, my program can receive packets from Packeth. This is my code.
package main
import (
"fmt"
"log"
"net"
"syscall"
)
func main() {
const proto = 0x0300
s, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, proto)
if err != nil {
log.Fatal("socket: ", err)
}
defer syscall.Close(s)
ifi, err := net.InterfaceByName("eth0")
if err != nil {
log.Fatal("interfacebyname: ", err)
}
lla := syscall.SockaddrLinklayer{Protocol: proto, Ifindex: ifi.Index}
if err := syscall.Bind(s, &lla); err != nil {
log.Fatal("bind: ", err)
}
for {
b := make([]byte, 100)
_, _, err := syscall.Recvfrom(s, b, 0)
if err != nil {
log.Fatal("recvfrom ", err)
}
log.Fatal(b[0:20])
}
}