can't read data from the tun device

214 views
Skip to first unread message

akalio ja

unread,
Jun 15, 2023, 8:32:14 AM6/15/23
to golang-nuts
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
}

Ian Lance Taylor

unread,
Jun 15, 2023, 11:27:43 AM6/15/23
to akalio ja, golang-nuts
What version of Go are you using?

Ian

--
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.

akalio ja

unread,
Jun 16, 2023, 1:41:14 AM6/16/23
to golang-nuts
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

Ian Lance Taylor

unread,
Jun 16, 2023, 1:56:44 PM6/16/23
to akalio ja, golang-nuts
On Thu, Jun 15, 2023 at 10:41 PM akalio ja <jaak...@gmail.com> wrote:
>
> GOVERSION="go1.20.3"

Thanks. You didn't say what the exact problem was, but I would guess
that you are running into the problem described at
https://go.dev/issue/59545. That problem should be fixed in the
upcoming 1.21 release.

Ian
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5f004bba-963d-4f3e-b5bb-e15ee43c0d2fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages