GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/root/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/root/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.20.3" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/root/projs/tmp/go.mod" GOWORK="" CGO_CFLAGS="-O2 -g" CGO_CPPFLAGS="" CGO_CXXFLAGS="-O2 -g" CGO_FFLAGS="-O2 -g" CGO_LDFLAGS="-O2 -g" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3940382547=/tmp/go-build -gno-record-gcc-switches"
this my env 在2023年6月15日星期四 UTC+8 23:27:43<Ian Lance Taylor> 写道: > What version of Go are you using? > > Ian > > On Thu, Jun 15, 2023, 5:32 AM akalio ja <jaak...@gmail.com> wrote: > >> func openTun(ch chan int, timer *time.Timer) { >> >> fd, err := unix.Open("/dev/net/tun", unix.O_RDWR|unix.O_NONBLOCK, 0) >> if err != nil { >> panic(err) >> } >> defer unix.Close(fd) >> >> var ifr struct { >> name [16]byte >> flags uint16 >> _ [22]byte >> } >> copy(ifr.name[:], "tun0") >> ifr.flags = unix.IFF_TUN | unix.IFF_NO_PI >> _, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(fd), >> unix.TUNSETIFF, uintptr(unsafe.Pointer(&ifr))) >> if errno != 0 { >> log.Println("err to syscall:", errno) >> return >> } >> >> ch <- fd >> bytes := []byte("hell can you recv?") >> fmt.Println(unix.Write(fd, bytes)) >> >> ticker := time.NewTicker(time.Second * 10) >> for { >> select { >> case <-ticker.C: >> fmt.Println(unix.Write(fd, bytes)) >> case <-timer.C: >> return >> } >> } >> } >> >> func readFd(fd int) { >> >> ticker := time.NewTicker(time.Second * 10) >> buffer := make([]byte, 1024) >> for { >> <-ticker.C >> n, err := unix.Read(fd, buffer) >> if err != nil { >> if err == unix.EAGAIN { >> log.Println("No data available at the moment") >> } else { >> log.Println("Error reading from file descriptor:", err) >> return >> } >> } else { >> log.Println("reading from file descriptor:", string >> (buffer[:n])) >> } >> } >> } >> >> func main() { >> fdchannel, done := make(chan int, 1), time.NewTimer(time.Minute) >> go openTun(fdchannel, done) >> fd := <-fdchannel >> log.Println("opened fd", fd) >> go readFd(fd) >> >> <- done.C >> } >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/golang-nuts/4267190c-4024-48d0-b8e3-9749db9a87f9n%40googlegroups.com >> >> <https://groups.google.com/d/msgid/golang-nuts/4267190c-4024-48d0-b8e3-9749db9a87f9n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5f004bba-963d-4f3e-b5bb-e15ee43c0d2fn%40googlegroups.com.