This example creates a TAP interface named O_O and continuously reads Ethernet frames, printing their source, destination, and payload.
Note: You must run the application with sudo and manually bring the interface up and assign an IP address using ip commands.
package main
import (
"log"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
)
func main() {
config := water.Config{
DeviceType: water.TAP,
}
config.Name = "O_O"
ifce, err := water.New(config);
if err != nil {
log.Fatal(err)
}
var frame ethernet.Frame
for {
frame.Resize(1500)
n, err := ifce.Read([]byte(frame))
if err != nil {
log.Fatal(err)
}
frame = frame[:n]
log.Printf("Dst: %s\n", frame.Destination())
log.Printf("Src: %s\n", frame.Source())
log.Printf("Ethertype: % x\n", frame.Ethertype())
log.Printf("Payload: % x\n", frame.Payload())
}
}