Skip to content

Commit

Permalink
Fix windows tap search and if issue
Browse files Browse the repository at this point in the history
  • Loading branch information
daniely committed Jan 16, 2018
0 parents commit b22c42a
Show file tree
Hide file tree
Showing 24 changed files with 1,605 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Song Gao <[email protected]>
Harshal Sheth <[email protected]>
KOJIMA Takanori <[email protected]>
Sean Purser-Haskell <[email protected]>
daregod <[email protected]>
Lucus Lee <[email protected]>
Arroyo Networks, LLC <[email protected]>
Tony Lu <[email protected]>
ajee cai <[email protected]>
yinheli <[email protected]>
27 changes: 27 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2016, Song Gao <[email protected]>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of water nor the names of its contributors may be used to
endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
227 changes: 227 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
# water

`water` is a native Go library for [TUN/TAP](http://en.wikipedia.org/wiki/TUN/TAP) interfaces.

`water` is designed to be simple and efficient. It

* wraps almost only syscalls and uses only Go standard types;
* exposes standard interfaces; plays well with standard packages like `io`, `bufio`, etc..
* does not handle memory management (allocating/destructing slice). It's up to user to decide whether/how to reuse buffers.

~~`water/waterutil` has some useful functions to interpret MAC frame headers and IP packet headers. It also contains some constants such as protocol numbers and ethernet frame types.~~

See https://github.com/songgao/packets for functions for parsing various packets.

## Supported Platforms

* Linux
* Windows (experimental; APIs might change)
* macOS (point-to-point TUN only)

## Installation
```
go get -u github.com/songgao/water
go get -u github.com/songgao/water/waterutil
```

## Documentation
[http://godoc.org/github.com/songgao/water](http://godoc.org/github.com/songgao/water)

## Example

### TAP on Linux:

```go
package main

import (
"log"

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

func main() {
config := water.Config{
DeviceType: water.TAP,
}
config.Name = "O_O"

ifce, err := water.New(config)
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: % x\n", frame.Payload())
}
}
```

This piece of code creates a `TAP` interface, and prints some header information for every frame. After pull up the `main.go`, you'll need to bring up the interface and assign an IP address. All of these need root permission.

```bash
sudo go run main.go
```

In a new terminal:

```bash
sudo ip addr add 10.1.0.10/24 dev O_O
sudo ip link set dev O_O up
```

Wait until the output `main.go` terminal, try sending some ICMP broadcast message:
```bash
ping -c1 -b 10.1.0.255
```

You'll see output containing the IPv4 ICMP frame:
```
2016/10/24 03:18:16 Dst: ff:ff:ff:ff:ff:ff
2016/10/24 03:18:16 Src: 72:3c:fc:29:1c:6f
2016/10/24 03:18:16 Ethertype: 08 00
2016/10/24 03:18:16 Payload: 45 00 00 54 00 00 40 00 40 01 25 9f 0a 01 00 0a 0a 01 00 ff 08 00 01 c1 08 49 00 01 78 7d 0d 58 00 00 00 00 a2 4c 07 00 00 00 00 00 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37
```

### TUN on macOS

```go
package main

import (
"log"

"github.com/songgao/water"
)

func main() {
ifce, err := water.New(water.Config{
DeviceType: water.TUN,
})
if err != nil {
log.Fatal(err)
}

log.Printf("Interface Name: %s\n", ifce.Name())

packet := make([]byte, 2000)
for {
n, err := ifce.Read(packet)
if err != nil {
log.Fatal(err)
}
log.Printf("Packet Received: % x\n", packet[:n])
}
}
```

Run it!

```bash
$ sudo go run main.go
```

This is a point-to-point only interface. Use `ifconfig` to see its attributes. You need to bring it up and assign IP addresses (apparently replace `utun2` if needed):

```bash
$ sudo ifconfig utun2 10.1.0.10 10.1.0.20 up
```

Now send some ICMP packets to the interface:

```bash
$ ping 10.1.0.20
```

You'd see the ICMP packets printed out:

```
2017/03/20 21:17:30 Interface Name: utun2
2017/03/20 21:17:40 Packet Received: 45 00 00 54 e9 1d 00 00 40 01 7d 6c 0a 01 00 0a 0a 01 00 14 08 00 ee 04 21 15 00 00 58 d0 a9 64 00 08 fb a5 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37
```

#### Caveats

1. Only Point-to-Point user TUN devices are supported. TAP devices are *not* supported natively by macOS.
2. Custom interface names are not supported by macOS. Interface names are automatically generated serially, using the `utun<#>` naming convention.

### TAP on Windows:

To use it with windows, you will need to install a [tap driver](https://github.com/OpenVPN/tap-windows6), or [OpenVPN client](https://github.com/OpenVPN/openvpn) for windows.

It's compatible with the Linux code.

```go
package main

import (
"log"

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

func main() {
ifce, err := water.New(water.Config{
DeviceType: water.TAP,
})
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: % x\n", frame.Payload())
}
}
```

Same as Linux version, but you don't need to bring up the device by hand, the only thing you need is to assign an IP address to it.

```dos
go run main.go
```

It will output a lot of lines because of some windows services and dhcp.
You will need admin right to assign IP.

In a new cmd (admin right):

```dos
# Replace with your device name, it can be achieved by ifce.Name().
netsh interface ip set address name="Ehternet 2" source=static addr=10.1.0.10 mask=255.255.255.0 gateway=none
```

The `main.go` terminal should be silenced after IP assignment, try sending some ICMP broadcast message:

```dos
ping 10.1.0.255
```

You'll see output containing the IPv4 ICMP frame same as the Linux version.

## TODO
* tuntaposx for TAP on Darwin

## Alternatives
`tuntap`: [https://code.google.com/p/tuntap/](https://code.google.com/p/tuntap/)
4 changes: 4 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Package water is a simple TUN/TAP interface library that efficiently works
// with standard packages like io, bufio, etc.. Use waterutil with it to work
// with TUN/TAP packets/frames.
package water
80 changes: 80 additions & 0 deletions if.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package water

import (
"errors"
"io"
)

// Interface is a TUN/TAP interface.
//
// MultiQueue(Linux kernel > 3.8): With MultiQueue enabled, user should hold multiple
// interfaces to send/receive packet in parallel.
// Kernel document about MultiQueue: https://www.kernel.org/doc/Documentation/networking/tuntap.txt
type Interface struct {
isTAP bool
io.ReadWriteCloser
name string
}

// DeviceType is the type for specifying device types.
type DeviceType int

// TUN and TAP device types.
const (
_ = iota
TUN
TAP
)

// Config defines parameters required to create a TUN/TAP interface. It's only
// used when the device is initialized. A zero-value Config is a valid
// configuration.
type Config struct {
// DeviceType specifies whether the device is a TUN or TAP interface. A
// zero-value is treated as TUN.
DeviceType DeviceType

// PlatformSpecificParams defines parameters that differ on different
// platforms. See comments for the type for more details.
PlatformSpecificParams
}

func defaultConfig() Config {
return Config{
DeviceType: TUN,
PlatformSpecificParams: defaultPlatformSpecificParams(),
}
}

var zeroConfig Config

// New creates a new TUN/TAP interface using config.
func New(config Config) (ifce *Interface, err error) {
if zeroConfig == config {
config = defaultConfig()
}
config.PlatformSpecificParams = defaultPlatformSpecificParams()
switch config.DeviceType {
case TUN:
return newTUN(config)
case TAP:
return newTAP(config)
default:
return nil, errors.New("unknown device type")
}
}

// IsTUN returns true if ifce is a TUN interface.
func (ifce *Interface) IsTUN() bool {
return !ifce.isTAP
}

// IsTAP returns true if ifce is a TAP interface.
func (ifce *Interface) IsTAP() bool {
return ifce.isTAP
}

// Name returns the interface name of ifce, e.g. tun0, tap1, tun0, etc..
func (ifce *Interface) Name() string {
return ifce.name
}
32 changes: 32 additions & 0 deletions if_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// +build linux

package water

import (
"fmt"
)

// NewTAP creates a new TAP interface whose name is ifName. If ifName is empty, a
// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed
// 16 bytes. TAP interfaces are not supported on darwin.
// ifName cannot be specified on windows, you will need ifce.Name() to use some cmds.
//
// Deprecated: This function may be removed in the future. Please use New() instead.
func NewTAP(ifName string) (ifce *Interface, err error) {
fmt.Println("Deprecated: NewTAP(..) may be removed in the future. Please use New() instead.")
config := Config{DeviceType: TAP}
config.Name = ifName
return newTAP(config)
}

// NewTUN creates a new TUN interface whose name is ifName. If ifName is empty, a
// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed
// ifName cannot be specified on windows, you will need ifce.Name() to use some cmds.
//
// Deprecated: This function will be removed in the future. Please use New() instead.
func NewTUN(ifName string) (ifce *Interface, err error) {
fmt.Println("Deprecated: NewTUN(..) may be removed in the future. Please use New() instead.")
config := Config{DeviceType: TUN}
config.Name = ifName
return newTUN(config)
}
Loading

0 comments on commit b22c42a

Please sign in to comment.