]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
Allow determining name
authorJason A. Donenfeld <Jason@zx2c4.com>
Wed, 18 Apr 2018 14:39:14 +0000 (16:39 +0200)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 18 Apr 2018 14:42:30 +0000 (16:42 +0200)
device.go
tun.go
tun_linux.go

index 79ec0b8a7a0e2f144b142f5e97cdc12ffaa90e6f..5adf33a129e5ae89ce64c43eb1de37a45e0f2a06 100644 (file)
--- a/device.go
+++ b/device.go
@@ -255,7 +255,15 @@ func NewDevice(tun TUNDevice, logger *Logger) *Device {
        device.isClosed.Set(false)
 
        device.log = logger
+
        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)
 
        // initialize anti-DoS / anti-scanning features
diff --git a/tun.go b/tun.go
index 7b044ad12c703ee140968a8ec93d58408572203f..318772ab1b723b18496ea29274b605c1b1cdb153 100644 (file)
--- a/tun.go
+++ b/tun.go
@@ -20,7 +20,7 @@ type TUNDevice interface {
        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)
        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
        Close() error                   // stops the device and closes the event channel
 }
index da53907b043f045615ed129c08da8a524f93392c..06b5af4f05c3f2f38ea952e9fe4149afe72d6d6f 100644 (file)
@@ -11,6 +11,7 @@ import (
        "golang.org/x/sys/unix"
        "net"
        "os"
+       "strconv"
        "strings"
        "time"
        "unsafe"
@@ -130,10 +131,6 @@ func (tun *NativeTun) isUp() (bool, error) {
        return inter.Flags&net.FlagUp != 0, err
 }
 
-func (tun *NativeTun) Name() string {
-       return tun.name
-}
-
 func getDummySock() (int, error) {
        return unix.Socket(
                unix.AF_INET,
@@ -229,7 +226,7 @@ func (tun *NativeTun) MTU() (int, error) {
                uintptr(unsafe.Pointer(&ifr[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
@@ -241,6 +238,22 @@ func (tun *NativeTun) MTU() (int, error) {
        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) {
 
        if tun.nopi {
@@ -342,7 +355,7 @@ func CreateTUN(name string) (TUNDevice, error) {
 
        _, _, errno := unix.Syscall(
                unix.SYS_IOCTL,
-               uintptr(fd.Fd()),
+               fd.Fd(),
                uintptr(unix.TUNSETIFF),
                uintptr(unsafe.Pointer(&ifr[0])),
        )