Skip to content

Instantly share code, notes, and snippets.

@obonyojimmy
Forked from Preetam/sniffer.go
Created February 3, 2017 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obonyojimmy/7723e5c1c2cd99a405774b4ceeff1af5 to your computer and use it in GitHub Desktop.
Save obonyojimmy/7723e5c1c2cd99a405774b4ceeff1af5 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"syscall"
)
func htonsInt16(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, htonsInt16(syscall.ETH_P_ALL))
if err != nil {
log.Fatal(err)
}
buf := make([]byte, 65536)
for {
n, from, err := syscall.Recvfrom(fd, buf, 0)
if err != nil {
log.Fatal(err)
}
log.Printf("Read %d bytes from %v", n, from)
if n > 20 {
n = 20
}
log.Print(buf[:n])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment