diff --git a/cmd/tap1/main.go b/cmd/tap1/main.go new file mode 100644 index 0000000..be5eebe --- /dev/null +++ b/cmd/tap1/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "log" + "netstack/tcpip/link/rawfile" + "netstack/tcpip/link/tuntap" +) + +func main() { + tapName := "tap0" + c := &tuntap.Config{Name: tapName, Mode: tuntap.TAP} + fd, err := tuntap.NewNetDev(c) + if err != nil { + panic(err) + } + + // 启动tap网卡 + _ = tuntap.SetLinkUp(tapName) + //_ = tuntap.AddIP(tapName, "192.168.1.1/24") + _ = tuntap.SetRoute(tapName, "192.168.1.0/24") // 其实在链路层通信,是可以不需要 ip 地址的 + log.Println("启动tap网卡", tapName, "192.169.1.1/24") + + buf := make([]byte, 1<<16) + for { + rn, err := rawfile.BlockingRead(fd, buf) + if err != nil { + log.Println(err) + continue + } + log.Printf("read %d bytes", rn) + } +} diff --git a/go.mod b/go.mod index 35363a7..30d4eb2 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module tcpip +module netstack go 1.18