tuntap

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2023 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Overview

Package tuntap provides a portable interface to create and use TUN/TAP virtual network interfaces.

Note that while this package lets you create the interface and pass packets to/from it, it does not provide an API to configure the interface. Interface configuration is a very large topic and should be dealt with separately.

Index

Constants

View Source
const (
	// various ethernet protocols, using the same names as linux does
	ETH_P_IP   uint16 = 0x0800
	ETH_P_IPV6 uint16 = 0x86dd
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DevKind

type DevKind int
const (
	// Receive/send layer routable 3 packets (IP, IPv6...). Notably,
	// you don't receive link-local multicast with this interface
	// type.
	DevTun DevKind = iota
	// Receive/send Ethernet II frames. You receive all packets that
	// would be visible on an Ethernet link, including broadcast and
	// multicast traffic.
	DevTap
)

type Interface

type Interface struct {
	// contains filtered or unexported fields
}

func Open

func Open(ifPattern string, kind DevKind) (*Interface, error)

Open connects to the specified tun/tap interface.

If the specified device has been configured as persistent, this simply looks like a "cable connected" event to observers of the interface. Otherwise, the interface is created out of thin air.

ifPattern can be an exact interface name, e.g. "tun42", or a pattern containing one %d format specifier, e.g. "tun%d". In the latter case, the kernel will select an available interface name and create it.

Returns a TunTap object with channels to send/receive packets, or nil and an error if connecting to the interface failed.

func (*Interface) AddAddress added in v1.2.0

func (t *Interface) AddAddress(ip net.IP, subnet *net.IPNet) error

AddAddress adds an IP address to the tunnel interface.

func (*Interface) Close

func (t *Interface) Close() error

Disconnect from the tun/tap interface.

If the interface isn't configured to be persistent, it is immediately destroyed by the kernel.

func (*Interface) GetAddrList added in v1.2.0

func (t *Interface) GetAddrList() ([][]byte, error)

GetAddrList returns the IP addresses (as bytes) associated with the interface.

func (*Interface) IPv6 added in v1.2.0

func (t *Interface) IPv6(ctrl bool) error

IPv6 enables/disable ipv6 for the interface.

func (*Interface) IPv6Forwarding added in v1.2.0

func (t *Interface) IPv6Forwarding(ctrl bool) error

IPv6Forwarding enables/disables ipv6 forwarding for the interface.

func (*Interface) IPv6SLAAC added in v1.2.0

func (t *Interface) IPv6SLAAC(ctrl bool) error

IPv6SLAAC enables/disables stateless address auto-configuration (SLAAC) for the interface.

func (*Interface) Name

func (t *Interface) Name() string

The name of the interface. May be different from the name given to Open(), if the latter was a pattern.

func (*Interface) ReadPacket

func (t *Interface) ReadPacket(buffer []byte) (Packet, error)

Read a single packet from the kernel.

func (*Interface) SetMTU added in v1.2.0

func (t *Interface) SetMTU(mtu int) error

SetMTU sets the tunnel interface MTU size.

func (*Interface) Up added in v1.2.0

func (t *Interface) Up() error

Up sets the tunnel interface to the UP state.

func (*Interface) WritePacket

func (t *Interface) WritePacket(pkt Packet) error

Send a single packet to the kernel.

type Packet

type Packet struct {
	// The raw bytes of the Ethernet payload (for DevTun) or the full
	// Ethernet frame (for DevTap).
	Body []byte
	// The Ethernet type of the packet. Commonly seen values are
	// 0x8000 for IPv4 and 0x86dd for IPv6.
	Protocol uint16
	// True if the packet was too large to be read completely.
	Truncated bool
}

func (*Packet) DIP

func (p *Packet) DIP() net.IP

return the destination IP

func (*Packet) DSCP

func (p *Packet) DSCP() int

return the 6-bit DSCP field

func (*Packet) ICMPType

func (p *Packet) ICMPType() (int, int, int)

returns ipproto, icmp type, icmp code, if this is an ICMP packet, or 0,_,_ if it isn't

func (*Packet) IPProto

func (p *Packet) IPProto() (uint8, int, bool)

return the IP protocol, the offset to the IP datagram payload, and true if the payload is from a non-first fragment returns 0,0,false if parsing fails or the IPv6 header 59 (no-next-header) is found

func (*Packet) SIP

func (p *Packet) SIP() net.IP

return the source IP

func (*Packet) String

func (p *Packet) String() string

Directories

Path Synopsis
cmd
print_tuntap
Simple use of the tuntap package that prints packets received by the interface.
Simple use of the tuntap package that prints packets received by the interface.

Jump to

Keyboard shortcuts

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