tun

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

The tun package creates, reads and writes to/from tun devices.

Index

Constants

View Source
const (
	SizeofPtr  = 0x8
	SizeofLong = 0x8
)
View Source
const (
	DEV_NET_TUN string = "/dev/net/tun"
)

Variables

View Source
var (
	ErrInvalidAddress error = errors.New("invalid address")
)

Functions

func ByteSliceToString

func ByteSliceToString(s []byte) string

ByteSliceToString returns a string form of the text represented by the slice s, with a terminating NUL and any bytes after the NUL removed.

func IoctlIfreq

func IoctlIfreq(fd int, req uint, value *Ifreq) error

IoctlIfreq performs an ioctl using an Ifreq structure for input and/or output. See the netdevice(7) man page for details.

Types

type Ifreq

type Ifreq struct {
	Ifrn [syscall.IFNAMSIZ]byte
	Ifru [24]byte
}

An Ifreq is a type-safe wrapper around the raw ifreq struct. An Ifreq contains an interface name and a union of arbitrary data which can be accessed using the Ifreq's methods. To create an Ifreq, use the NewIfreq function.

Use the Name method to access the stored interface name. The union data fields can be get and set using the following methods:

  • Uint16/SetUint16: flags
  • Uint32/SetUint32: ifindex, metric, mtu

func NewIfreq

func NewIfreq(name string) (*Ifreq, error)

NewIfreq creates an Ifreq with the input network interface name after validating the name does not exceed IFNAMSIZ-1 (trailing NULL required) bytes.

func (*Ifreq) Clear

func (ifr *Ifreq) Clear()

clear zeroes the ifreq's union field to prevent trailing garbage data from being sent to the kernel if an ifreq is reused.

func (*Ifreq) Inet4Addr

func (ifr *Ifreq) Inet4Addr() ([]byte, error)

Inet4Addr returns the Ifreq union data from an embedded sockaddr as a C in_addr/Go []byte (4-byte IPv4 address) value. If the sockaddr family is not AF_INET, an error is returned.

func (*Ifreq) Name

func (ifr *Ifreq) Name() string

Name returns the interface name associated with the Ifreq.

func (*Ifreq) SetInet4Addr

func (ifr *Ifreq) SetInet4Addr(v []byte) error

SetInet4Addr sets a C in_addr/Go []byte (4-byte IPv4 address) value in an embedded sockaddr within the Ifreq's union data. v must be 4 bytes in length or an error will be returned.

func (*Ifreq) SetUint16

func (ifr *Ifreq) SetUint16(v uint16)

SetUint16 sets a C short/Go uint16 value as the Ifreq's union data.

func (*Ifreq) SetUint32

func (ifr *Ifreq) SetUint32(v uint32)

SetUint32 sets a C int/Go uint32 value as the Ifreq's union data.

func (*Ifreq) Uint16

func (ifr *Ifreq) Uint16() uint16

Uint16 returns the Ifreq union data as a C short/Go uint16 value.

func (*Ifreq) Uint32

func (ifr *Ifreq) Uint32() uint32

Uint32 returns the Ifreq union data as a C int/Go uint32 value.

type TUN

type TUN struct {
	Name  string
	File  *os.File
	Fd    int
	Ifreq *Ifreq
}

func CreateTUN

func CreateTUN(name string, mtu, uid, gid int) (*TUN, error)

CreateTUN creates a new tun device with name. If mtu is above 0 it attempts to set the MTU. If uid is above 0 it attempts to set the owner, same with gid. Returns a TUN which should be closed with receiver function Close() when you want to terminate the tunnel.

func (*TUN) Close

func (t *TUN) Close() error

func (*TUN) ConfigureInterface

func (t *TUN) ConfigureInterface(ipv4_address_with_cidr string) error

func (*TUN) LinkUp

func (t *TUN) LinkUp() error

func (*TUN) SetMTU

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

Jump to

Keyboard shortcuts

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