func TestSetupTunDevice(t *testing.T) { var err error if err := exec.Command("sudo", "ip", "tuntap", "del", "tun87", "mode", "tun").Run(); err != nil { // t.Fatal(err) } if err = exec.Command("sudo", "ip", "tuntap", "add", deviceName, "mode", "tun").Run(); err != nil { t.Fatal(err) } if err = exec.Command("sudo", "ip", "link", "set", deviceName, "up").Run(); err != nil { t.Fatal(err) } if err = exec.Command("sudo", "ip", "addr", "add", deviceMask, "dev", deviceName).Run(); err != nil { t.Fatal(err) } if iface, err = tuntap.Open(deviceName, tuntap.DevTun); err != nil { t.Fatal(err) } tunio = NewTunIO(&mockDialer{}) go runPacketCaptureLoop(iface, tunio) }
func startDevPump() error { var err error tunDev, err = tuntap.Open("tunTCP%d", tuntap.DevTun) if err != nil { return err } if err := setupTun(tunDev, tunAddr); err != nil { tunDev.Close() return err } return nil }
func main() { if len(os.Args) != 3 { fmt.Println("syntax:", os.Args[0], "tun|tap", "<device name>") return } var typ tuntap.DevKind switch os.Args[1] { case "tun": typ = tuntap.DevTun case "tap": typ = tuntap.DevTap default: fmt.Println("Unknown device type", os.Args[1]) return } tun, err := tuntap.Open(os.Args[2], typ) if err != nil { fmt.Println("Error opening tun/tap device:", err) return } fmt.Println("Listening on", tun.Name()) for { pkt, err := tun.ReadPacket() if err != nil { fmt.Println("Read error:", err) } else { if pkt.Truncated { fmt.Printf("!") } else { fmt.Printf(" ") } fmt.Printf("%x %x\n", pkt.Protocol, pkt.Packet) } } }