aboutsummaryrefslogtreecommitdiffstats
path: root/tun_linux.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-04-18 16:39:14 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-04-18 16:42:30 +0200
commit26a56a652eeeece7677ba4f1896da34c83930652 (patch)
tree27f4a4a2925287dd3087916a867b81882c806c93 /tun_linux.go
parentUse socketcall on x86 (diff)
downloadwireguard-go-26a56a652eeeece7677ba4f1896da34c83930652.tar.xz
wireguard-go-26a56a652eeeece7677ba4f1896da34c83930652.zip
Allow determining name
Diffstat (limited to '')
-rw-r--r--tun_linux.go25
1 files changed, 19 insertions, 6 deletions
diff --git a/tun_linux.go b/tun_linux.go
index da53907..06b5af4 100644
--- a/tun_linux.go
+++ b/tun_linux.go
@@ -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])),
)