songgao / water

A simple TUN/TAP library written in native Go.
BSD 3-Clause "New" or "Revised" License
1.89k stars 284 forks source link

tun device in windows can't read data. #39

Open zboya opened 6 years ago

zboya commented 6 years ago
package main

import (
    "log"

    "github.com/songgao/water"
)

func main() {
    ifce, err := water.New(water.Config{
        DeviceType: water.TUN,
        PlatformSpecificParams: water.PlatformSpecificParams{
            ComponentID: "tap0901",
            Network:     "192.168.1.10/24",
        },
    })
    if err != nil {
        log.Fatal(err)
    }

    frame := make([]byte, 1<<12)
    for {
        n, err := ifce.Read(frame)
        if err != nil {
            log.Fatal(err)
        }
        log.Printf("data: %v\n", frame[:n])

    }
}

after run this program, I ping 192.168.1.10, but program print nothing. Please help!

zboya commented 6 years ago

@lixin9311

JavinYang commented 6 years ago

@lixin9311

JavinYang commented 6 years ago
package main

import (
    "log"

    "github.com/songgao/packets/ethernet"
    "github.com/songgao/water"
)

func main() {
    ifce, err := water.New(water.Config{
        DeviceType: water.TAP,
        PlatformSpecificParams: water.PlatformSpecificParams{
            ComponentID: "tap0901",
            Network:     "10.8.0.6/30",
        },
    })
    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 %s\n", frame.Payload())
    }
}

go run main.go netsh interface ip set address name="Ehternet 2" source=static addr=10.8.0.6 mask=255.255.255.252 gateway=none ping 10.8.0.6

no printing. Orz

lixin9311 commented 6 years ago

you should run the program as admin on UAC enabled Windows

lixin9311 commented 6 years ago

And if you ping the address of the virtual nic on the local machine, the NT kernel will intercept and reply the ping, you can try to ping the gateway via the tun device

JavinYang commented 6 years ago

how do i send all data to Virtual Ethernet Adapter? when I send the command “route add 0.0.0.0 mask 0.0.0.0 10.8.0.6" in cmd tools and then visit the website .I can catch data from wireshark ,but can't read data from main program

zboya commented 6 years ago

@JavinYang when I using TAP device, it can read data from main program. using TUN device don't work.

lixin9311 commented 6 years ago

set the address of the adapter to 192.168.1.10/24 and the gateway 192.168.1.1/24. you ping the gateway, then you can read the data.

zboya commented 6 years ago

@lixin9311 I had set the address of the adapter to 192.168.1.10/24 and the gateway 192.168.1.1/24. but using the TUN device still can't read data. using TAP device can read data.

lixin9311 commented 6 years ago

it's working on my pc, I'll look into this later

zboya commented 6 years ago

I run this program in windows 10 in parallels desktop. maybe the vm has some different.

zboya commented 6 years ago

I run this program in real windows 10, and found the Nic can't set IP address to 192.168.1.10, it set the IP address to 169.254.x.x, this IP address is assigned by system, not my program.

lixin9311 commented 6 years ago

there's some issue with netsh, you can try to set the ip by right clicking on the adapter icon.

zboya commented 6 years ago

@lixin9311 how did you set windows gateway?what cmd you used?