Skip to content

Instantly share code, notes, and snippets.

@Preetam
Last active August 29, 2015 14:04
Show Gist options
  • Save Preetam/705cb591d2bc2d108c97 to your computer and use it in GitHub Desktop.
Save Preetam/705cb591d2bc2d108c97 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"fmt"
"syscall"
"github.com/PreetamJinka/ethernetdecode"
)
func htons(n int) int {
return int(int16(byte(n))<<8 | int16(byte(n>>8)))
}
func main() {
fd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, htons(syscall.ETH_P_ALL))
if err != nil {
log.Fatal(err)
}
buf := make([]byte, 65536)
for {
n, _, err := syscall.Recvfrom(fd, buf, 0)
if err != nil {
log.Fatal(err)
}
a, b, c := ethernetdecode.Decode(buf[:n])
if b != nil {
fmt.Printf("%#v %#v %#v\n\n", a, b, c)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment