tun_netbsd: update to follow tun_openbsd changes
Most of tun_netbsd is from tun_openbsd with some small changes, so we should follow the changes made in tun_openbsd.
This commit is contained in:
parent
ff49dc80a3
commit
5657c3c1e8
|
@ -6,6 +6,7 @@
|
||||||
package tun
|
package tun
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
|
@ -130,7 +131,7 @@ func CreateTUN(name string, mtu int) (Device, error) {
|
||||||
} else {
|
} else {
|
||||||
for ifIndex = 0; ifIndex < 256; ifIndex += 1 {
|
for ifIndex = 0; ifIndex < 256; ifIndex += 1 {
|
||||||
tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0)
|
tunfile, err = os.OpenFile(fmt.Sprintf("/dev/tun%d", ifIndex), unix.O_RDWR, 0)
|
||||||
if err == nil || !errorIsEBUSY(err) {
|
if err == nil || !errors.Is(err, syscall.EBUSY) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,19 +250,15 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
default:
|
default:
|
||||||
buff := buff[offset-4:]
|
buff := buff[offset-4:]
|
||||||
// buff := buff[offset:]
|
|
||||||
n, err := tun.tunFile.Read(buff[:])
|
n, err := tun.tunFile.Read(buff[:])
|
||||||
if n < 4 {
|
if n < 4 {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
return n - 4, err
|
return n - 4, err
|
||||||
// return n, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
||||||
|
|
||||||
|
|
||||||
// reserve space for header
|
// reserve space for header
|
||||||
|
|
||||||
buff = buff[offset-4:]
|
buff = buff[offset-4:]
|
||||||
|
@ -289,15 +286,17 @@ func (tun *NativeTun) Flush() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *NativeTun) Close() error {
|
func (tun *NativeTun) Close() error {
|
||||||
var err2 error
|
var err1, err2 error
|
||||||
err1 := tun.tunFile.Close()
|
tun.closeOnce.Do(func () {
|
||||||
if tun.routeSocket != -1 {
|
err1 = tun.tunFile.Close()
|
||||||
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
|
if tun.routeSocket != -1 {
|
||||||
err2 = unix.Close(tun.routeSocket)
|
unix.Shutdown(tun.routeSocket, unix.SHUT_RDWR)
|
||||||
tun.routeSocket = -1
|
err2 = unix.Close(tun.routeSocket)
|
||||||
} else if tun.events != nil {
|
tun.routeSocket = -1
|
||||||
close(tun.events)
|
} else if tun.events != nil {
|
||||||
}
|
close(tun.events)
|
||||||
|
}
|
||||||
|
})
|
||||||
if err1 != nil {
|
if err1 != nil {
|
||||||
return err1
|
return err1
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue