Created
July 14, 2014 21:35
-
-
Save Preetam/54bb7703f21646140a9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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