tuntap

package module
v0.0.0-...-cb1fb27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2019 License: BSD-2-Clause-Views Imports: 5 Imported by: 1

README

This repository is unmaintained: You should use the more comprehensive and well maintained water library.

TUN/TAP

GoDoc License

NOTE: This package is new and should be considered unstable, in terms of both API and function.

tuntap is a native wrapper for interfacing with TUN/TAP network devices in an idiomatic fashion.

Currently supported are Linux and Mac OS X.

go get github.com/jamescun/tuntap

Configuration

The configuration required to open a TUN/TAP device varies by platform. The differences are noted below.

Linux

When creating a TUN/TAP device, Linux expects to be given a name for the new interface, and a new interface will be allocated for it by the kernel module. If left blank, one will be generated (tun|tap)([0-9]+).

tap, err := tuntap.Tap("tap1") // created tap1 device if available
tun, err := tuntap.Tun("")     // created tun device at first available id (tun0)
Mac OS X

On startup, the Mac OS X TUN/TAP kernel extension will allocate multiple TUN/TAP devices, up to the maximum number of each. When creating a TUN/TAP device, Mac OS X expects to be given a path to an unused device. If left blank, this package will attempt to find the first unused one.

tap, err := tuntap.Tap("/dev/tap1") // open tap1 device if unused
tun, err := tuntap.Tun("")          // open first available tun device (tun0)

Additionally, unlike Linux, a TUN/TAP device is not "ready" on OS X until it has an address assigned to it. Any attempt to read from/write to the interface will fail with ErrBusy. It is safe to backoff and try again until a successful operation.

Examples

See the examples directory.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBusy        = errors.New("device is already in use")
	ErrNotReady    = errors.New("device is not ready")
	ErrExhausted   = errors.New("no devices are available")
	ErrUnsupported = errors.New("device is unsupported on this platform")
)

Functions

This section is empty.

Types

type Interface

type Interface interface {
	// return name of TUN/TAP interface
	Name() string

	// implement io.Reader interface, read bytes into p from TUN/TAP interface
	Read(p []byte) (n int, err error)

	// implement io.Writer interface, write bytes from p to TUN/TAP interface
	Write(p []byte) (n int, err error)

	// implement io.Closer interface, must be called when done with TUN/TAP interface
	Close() error

	// return string representation of TUN/TAP interface
	String() string
}

Interface represents a TUN/TAP network interface

func Tap

func Tap(name string) (Interface, error)

return a TAP interface. depending on platform, the device may not be ready for use yet; a caller must poll the Ready() method before use. additionally the caller is responsible for calling Close() to terminate the device.

func Tun

func Tun(name string) (Interface, error)

return a TUN interface. depending on platform, the device may not be ready for use yet; a caller must poll the Ready() method before use. additionally the caller is responsible for calling Close() to terminate the device.

Directories

Path Synopsis
dump packets sent to TUN device to stdout example: -- Packet Received -- IP Version: 4 Length: 84 Protocol: 1 (1=ICMP, 6=TCP, 17=UDP) Source IP: 10.12.0.2 Destination IP: 10.12.0.1 Payload: [08001467cc79000156bcc24d0004130d08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637]
dump packets sent to TUN device to stdout example: -- Packet Received -- IP Version: 4 Length: 84 Protocol: 1 (1=ICMP, 6=TCP, 17=UDP) Source IP: 10.12.0.2 Destination IP: 10.12.0.1 Payload: [08001467cc79000156bcc24d0004130d08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637]

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL