aboutsummaryrefslogtreecommitdiffstats
path: root/tun/tun_darwin.go
diff options
context:
space:
mode:
Diffstat (limited to 'tun/tun_darwin.go')
-rw-r--r--tun/tun_darwin.go30
1 files changed, 16 insertions, 14 deletions
diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go
index 24d2319..a4e2e9a 100644
--- a/tun/tun_darwin.go
+++ b/tun/tun_darwin.go
@@ -37,7 +37,8 @@ type sockaddrCtl struct {
type nativeTun struct {
name string
- fd *os.File
+ tunFile *os.File
+ fd uintptr
rwcancel *rwcancel.RWCancel
events chan TUNEvent
errors chan error
@@ -171,14 +172,15 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
tun := &nativeTun{
- fd: file,
- events: make(chan TUNEvent, 10),
- errors: make(chan error, 1),
+ tunFile: file,
+ fd: file.Fd(),
+ events: make(chan TUNEvent, 10),
+ errors: make(chan error, 1),
}
name, err := tun.Name()
if err != nil {
- tun.fd.Close()
+ tun.tunFile.Close()
return nil, err
}
@@ -190,19 +192,19 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
return iface.Index, nil
}()
if err != nil {
- tun.fd.Close()
+ tun.tunFile.Close()
return nil, err
}
- tun.rwcancel, err = rwcancel.NewRWCancel(int(file.Fd()))
+ tun.rwcancel, err = rwcancel.NewRWCancel(int(tun.fd))
if err != nil {
- tun.fd.Close()
+ tun.tunFile.Close()
return nil, err
}
tun.routeSocket, err = unix.Socket(unix.AF_ROUTE, unix.SOCK_RAW, unix.AF_UNSPEC)
if err != nil {
- tun.fd.Close()
+ tun.tunFile.Close()
return nil, err
}
@@ -226,7 +228,7 @@ func (tun *nativeTun) Name() (string, error) {
_, _, errno := unix.Syscall6(
unix.SYS_GETSOCKOPT,
- uintptr(tun.fd.Fd()),
+ uintptr(tun.fd),
2, /* #define SYSPROTO_CONTROL 2 */
2, /* #define UTUN_OPT_IFNAME 2 */
uintptr(unsafe.Pointer(&ifName)),
@@ -241,7 +243,7 @@ func (tun *nativeTun) Name() (string, error) {
}
func (tun *nativeTun) File() *os.File {
- return tun.fd
+ return tun.tunFile
}
func (tun *nativeTun) Events() chan TUNEvent {
@@ -254,7 +256,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
return 0, err
default:
buff := buff[offset-4:]
- n, err := tun.fd.Read(buff[:])
+ n, err := tun.tunFile.Read(buff[:])
if n < 4 {
return 0, err
}
@@ -294,13 +296,13 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
// write
- return tun.fd.Write(buff)
+ return tun.tunFile.Write(buff)
}
func (tun *nativeTun) Close() error {
var err3 error
err1 := tun.rwcancel.Cancel()
- err2 := tun.fd.Close()
+ err2 := tun.tunFile.Close()
if tun.routeSocket != -1 {
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
err3 = unix.Close(tun.routeSocket)