diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2018-05-05 04:42:44 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2018-05-05 04:42:44 +0200 |
commit | 34891d92cd36289294d38cd9f26f1fc2456ef26a (patch) | |
tree | ed1b0e95b702c83c23c08fd97b125d2c5499a5a8 | |
parent | 8ba28be732a3ca9819595dc4cea24d45d0d676c3 (diff) | |
parent | beab52258ae22c6901b769123ae1c8d64fea294c (diff) |
Merge branch 'master' of ssh://git.zx2c4.com/wireguard-go
-rw-r--r-- | tun_linux.go | 63 |
1 files changed, 17 insertions, 46 deletions
diff --git a/tun_linux.go b/tun_linux.go index e3e1512..18994cc 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -20,7 +20,6 @@ import ( "net" "os" "strconv" - "strings" "syscall" "time" "unsafe" @@ -361,6 +360,7 @@ func (tun *NativeTun) doRead(buff []byte, offset int) (int, error) { } } +/* https://golang.org/src/crypto/rand/eagain.go */ func unixIsEAGAIN(err error) bool { if pe, ok := err.(*os.PathError); ok { if errno, ok := pe.Err.(syscall.Errno); ok && errno == syscall.EAGAIN { @@ -395,57 +395,18 @@ func (tun *NativeTun) Close() error { return nil } -func CreateTUNFromFile(fd *os.File) (TUNDevice, error) { - device := &NativeTun{ - fd: fd, - events: make(chan TUNEvent, 5), - errors: make(chan error, 5), - nopi: false, - } - var err error - - err = syscall.SetNonblock(int(fd.Fd()), true) - if err != nil { - return nil, err - } - - _, err = device.Name() - if err != nil { - return nil, err - } - - device.closingReader, device.closingWriter, err = os.Pipe() - if err != nil { - return nil, err - } - - // start event listener - - device.index, err = getIFIndex(device.name) - if err != nil { - return nil, err - } - - go device.RoutineNetlinkListener() - go device.RoutineHackListener() // cross namespace - - // set default MTU - - return device, device.setMTU(DefaultMTU) -} - func CreateTUN(name string) (TUNDevice, error) { // open clone device // HACK: we open it as a raw Fd first, so that f.nonblock=false // when we make it into a file object. - nfd, err := syscall.Open(cloneDevicePath, os.O_RDWR, 0) + nfd, err := unix.Open(cloneDevicePath, os.O_RDWR, 0) if err != nil { return nil, err } - err = syscall.SetNonblock(nfd, true) + err = unix.SetNonblock(nfd, true) if err != nil { return nil, err } @@ -476,17 +437,27 @@ func CreateTUN(name string) (TUNDevice, error) { return nil, errno } - // read (new) name of interface + return CreateTUNFromFile(fd) +} - newName := string(ifr[:]) - newName = newName[:strings.Index(newName, "\000")] +func CreateTUNFromFile(fd *os.File) (TUNDevice, error) { device := &NativeTun{ fd: fd, - name: newName, events: make(chan TUNEvent, 5), errors: make(chan error, 5), nopi: false, } + var err error + + err = unix.SetNonblock(int(fd.Fd()), true) + if err != nil { + return nil, err + } + + _, err = device.Name() + if err != nil { + return nil, err + } device.closingReader, device.closingWriter, err = os.Pipe() if err != nil { |