Allow determining name

This commit is contained in:
Jason A. Donenfeld 2018-04-18 16:39:14 +02:00
parent 0b940a7568
commit 26a56a652e
3 changed files with 28 additions and 7 deletions

View file

@ -255,7 +255,15 @@ func NewDevice(tun TUNDevice, logger *Logger) *Device {
device.isClosed.Set(false) device.isClosed.Set(false)
device.log = logger device.log = logger
device.tun.device = tun device.tun.device = tun
mtu, err := device.tun.device.MTU()
if err != nil {
logger.Error.Println("Trouble determining MTU, assuming 1420:", err)
mtu = 1420
}
device.tun.mtu = int32(mtu)
device.peers.keyMap = make(map[NoisePublicKey]*Peer) device.peers.keyMap = make(map[NoisePublicKey]*Peer)
// initialize anti-DoS / anti-scanning features // initialize anti-DoS / anti-scanning features

2
tun.go
View file

@ -20,7 +20,7 @@ type TUNDevice interface {
Read([]byte, int) (int, error) // read a packet from the device (without any additional headers) Read([]byte, int) (int, error) // read a packet from the device (without any additional headers)
Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers) Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
MTU() (int, error) // returns the MTU of the device MTU() (int, error) // returns the MTU of the device
Name() string // returns the current name Name() (string, error) // fetches and returns the current name
Events() chan TUNEvent // returns a constant channel of events related to the device Events() chan TUNEvent // returns a constant channel of events related to the device
Close() error // stops the device and closes the event channel Close() error // stops the device and closes the event channel
} }

View file

@ -11,6 +11,7 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"net" "net"
"os" "os"
"strconv"
"strings" "strings"
"time" "time"
"unsafe" "unsafe"
@ -130,10 +131,6 @@ func (tun *NativeTun) isUp() (bool, error) {
return inter.Flags&net.FlagUp != 0, err return inter.Flags&net.FlagUp != 0, err
} }
func (tun *NativeTun) Name() string {
return tun.name
}
func getDummySock() (int, error) { func getDummySock() (int, error) {
return unix.Socket( return unix.Socket(
unix.AF_INET, unix.AF_INET,
@ -229,7 +226,7 @@ func (tun *NativeTun) MTU() (int, error) {
uintptr(unsafe.Pointer(&ifr[0])), uintptr(unsafe.Pointer(&ifr[0])),
) )
if errno != 0 { if errno != 0 {
return 0, errors.New("Failed to get MTU of TUN device") return 0, errors.New("Failed to get MTU of TUN device: " + strconv.FormatInt(int64(errno), 10))
} }
// convert result to signed 32-bit int // convert result to signed 32-bit int
@ -241,6 +238,22 @@ func (tun *NativeTun) MTU() (int, error) {
return int(val), nil return int(val), nil
} }
func (tun *NativeTun) Name() (string, error) {
var ifr [ifReqSize]byte
_, _, errno := unix.Syscall(
unix.SYS_IOCTL,
tun.fd.Fd(),
uintptr(unix.TUNGETIFF),
uintptr(unsafe.Pointer(&ifr[0])),
)
if errno != 0 {
return "", errors.New("Failed to get name of TUN device: " + strconv.FormatInt(int64(errno), 10))
}
tun.name = string(ifr[:])
return tun.name, nil
}
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) { func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
if tun.nopi { if tun.nopi {
@ -342,7 +355,7 @@ func CreateTUN(name string) (TUNDevice, error) {
_, _, errno := unix.Syscall( _, _, errno := unix.Syscall(
unix.SYS_IOCTL, unix.SYS_IOCTL,
uintptr(fd.Fd()), fd.Fd(),
uintptr(unix.TUNSETIFF), uintptr(unix.TUNSETIFF),
uintptr(unsafe.Pointer(&ifr[0])), uintptr(unsafe.Pointer(&ifr[0])),
) )