Compare commits

...

2 commits

Author SHA1 Message Date
snow flurry 470f2f0248 Cleanup error handling 2022-03-21 19:40:24 -07:00
snow flurry 5181a06304 fix whitespace mismatch 2022-03-21 19:37:56 -07:00

View file

@ -6,12 +6,12 @@
package tun
import (
"errors"
"errors"
"fmt"
"io/ioutil"
"net"
"os"
"sync"
"sync"
"syscall"
"unsafe"
@ -27,18 +27,18 @@ type ifreq_mtu struct {
}
const (
_TUNGIFHEAD = 0x40047441
_TUNSIFMODE = 0x80047458
_TUNSIFHEAD = 0x80047442
_TUNGIFHEAD = 0x40047441
_TUNSIFMODE = 0x80047458
_TUNSIFHEAD = 0x80047442
)
type NativeTun struct {
name string
tunFile *os.File
events chan Event
errors chan error
name string
tunFile *os.File
events chan Event
errors chan error
routeSocket int
closeOnce sync.Once
closeOnce sync.Once
}
func (tun *NativeTun) routineRouteListener(tunIfindex int) {
@ -148,29 +148,29 @@ func CreateTUN(name string, mtu int) (Device, error) {
// Enable ifhead mode, otherwise tun will complain if it gets a non-AF_INET packet
ifheadmode := 1
var errno syscall.Errno
_, _, errno = unix.Syscall(
unix.SYS_IOCTL,
tunfile.Fd(),
uintptr(_TUNSIFHEAD),
uintptr(unsafe.Pointer(&ifheadmode)),
)
_, _, errno = unix.Syscall(
unix.SYS_IOCTL,
tunfile.Fd(),
uintptr(_TUNSIFHEAD),
uintptr(unsafe.Pointer(&ifheadmode)),
)
if errno != 0 {
tunfile.Close()
return nil, fmt.Errorf("Unable to put into IFHEAD mode: %v", errno)
}
if errno != 0 {
tunfile.Close()
return nil, fmt.Errorf("Unable to put into multi-af mode: %v", errno)
}
_, _, errno = unix.Syscall(
unix.SYS_IOCTL,
tunfile.Fd(),
uintptr(_TUNGIFHEAD),
uintptr(unsafe.Pointer(&ifheadmode)),
)
_, _, errno = unix.Syscall(
unix.SYS_IOCTL,
tunfile.Fd(),
uintptr(_TUNGIFHEAD),
uintptr(unsafe.Pointer(&ifheadmode)),
)
if errno != 0 || ifheadmode == 0 {
tunfile.Close()
return nil, fmt.Errorf("Unable to validate IFHEAD mode: %v (ifheadmode = %d)", errno, ifheadmode)
}
if errno != 0 || ifheadmode == 0 {
tunfile.Close()
return nil, fmt.Errorf("Unable to validate multi-af mode: %v", errno)
}
if err == nil && name == "tun" {
fname := os.Getenv("WG_TUN_NAME_FILE")
@ -289,16 +289,16 @@ func (tun *NativeTun) Flush() error {
func (tun *NativeTun) Close() error {
var err1, err2 error
tun.closeOnce.Do(func () {
err1 = tun.tunFile.Close()
if tun.routeSocket != -1 {
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
err2 = unix.Close(tun.routeSocket)
tun.routeSocket = -1
} else if tun.events != nil {
close(tun.events)
}
})
tun.closeOnce.Do(func () {
err1 = tun.tunFile.Close()
if tun.routeSocket != -1 {
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
err2 = unix.Close(tun.routeSocket)
tun.routeSocket = -1
} else if tun.events != nil {
close(tun.events)
}
})
if err1 != nil {
return err1
}