tun: allow special methods in NativeTun
This commit is contained in:
parent
88ff67fb6f
commit
967d1a0f3d
@ -28,7 +28,7 @@ type TUNDevice interface {
|
|||||||
Close() error // stops the device and closes the event channel
|
Close() error // stops the device and closes the event channel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) operateOnFd(fn func(fd uintptr)) {
|
func (tun *NativeTun) operateOnFd(fn func(fd uintptr)) {
|
||||||
sysconn, err := tun.tunFile.SyscallConn()
|
sysconn, err := tun.tunFile.SyscallConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.errors <- fmt.Errorf("unable to find sysconn for tunfile: %s", err.Error())
|
tun.errors <- fmt.Errorf("unable to find sysconn for tunfile: %s", err.Error())
|
||||||
|
@ -31,7 +31,7 @@ type sockaddrCtl struct {
|
|||||||
scReserved [5]uint32
|
scReserved [5]uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
type nativeTun struct {
|
type NativeTun struct {
|
||||||
name string
|
name string
|
||||||
tunFile *os.File
|
tunFile *os.File
|
||||||
events chan TUNEvent
|
events chan TUNEvent
|
||||||
@ -41,7 +41,7 @@ type nativeTun struct {
|
|||||||
|
|
||||||
var sockaddrCtlSize uintptr = 32
|
var sockaddrCtlSize uintptr = 32
|
||||||
|
|
||||||
func (tun *nativeTun) routineRouteListener(tunIfindex int) {
|
func (tun *NativeTun) routineRouteListener(tunIfindex int) {
|
||||||
var (
|
var (
|
||||||
statusUp bool
|
statusUp bool
|
||||||
statusMTU int
|
statusMTU int
|
||||||
@ -160,7 +160,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
if err == nil && name == "utun" {
|
if err == nil && name == "utun" {
|
||||||
fname := os.Getenv("WG_TUN_NAME_FILE")
|
fname := os.Getenv("WG_TUN_NAME_FILE")
|
||||||
if fname != "" {
|
if fname != "" {
|
||||||
ioutil.WriteFile(fname, []byte(tun.(*nativeTun).name+"\n"), 0400)
|
ioutil.WriteFile(fname, []byte(tun.(*NativeTun).name+"\n"), 0400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +168,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
||||||
tun := &nativeTun{
|
tun := &NativeTun{
|
||||||
tunFile: file,
|
tunFile: file,
|
||||||
events: make(chan TUNEvent, 10),
|
events: make(chan TUNEvent, 10),
|
||||||
errors: make(chan error, 1),
|
errors: make(chan error, 1),
|
||||||
@ -209,7 +209,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
return tun, nil
|
return tun, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Name() (string, error) {
|
func (tun *NativeTun) Name() (string, error) {
|
||||||
var ifName struct {
|
var ifName struct {
|
||||||
name [16]byte
|
name [16]byte
|
||||||
}
|
}
|
||||||
@ -234,15 +234,15 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
return tun.name, nil
|
return tun.name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *NativeTun) File() *os.File {
|
||||||
return tun.tunFile
|
return tun.tunFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Events() chan TUNEvent {
|
func (tun *NativeTun) Events() chan TUNEvent {
|
||||||
return tun.events
|
return tun.events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
select {
|
select {
|
||||||
case err := <-tun.errors:
|
case err := <-tun.errors:
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -256,7 +256,7 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
@ -279,7 +279,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
return tun.tunFile.Write(buff)
|
return tun.tunFile.Write(buff)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Close() error {
|
func (tun *NativeTun) Close() error {
|
||||||
var err2 error
|
var err2 error
|
||||||
err1 := tun.tunFile.Close()
|
err1 := tun.tunFile.Close()
|
||||||
if tun.routeSocket != -1 {
|
if tun.routeSocket != -1 {
|
||||||
@ -295,7 +295,7 @@ func (tun *nativeTun) Close() error {
|
|||||||
return err2
|
return err2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) setMTU(n int) error {
|
func (tun *NativeTun) setMTU(n int) error {
|
||||||
|
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
|
|
||||||
@ -332,7 +332,7 @@ func (tun *nativeTun) setMTU(n int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) MTU() (int, error) {
|
func (tun *NativeTun) MTU() (int, error) {
|
||||||
|
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ type ifstat struct {
|
|||||||
Ascii [_IFSTATMAX]byte
|
Ascii [_IFSTATMAX]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
type nativeTun struct {
|
type NativeTun struct {
|
||||||
name string
|
name string
|
||||||
tunFile *os.File
|
tunFile *os.File
|
||||||
events chan TUNEvent
|
events chan TUNEvent
|
||||||
@ -56,7 +56,7 @@ type nativeTun struct {
|
|||||||
routeSocket int
|
routeSocket int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) routineRouteListener(tunIfindex int) {
|
func (tun *NativeTun) routineRouteListener(tunIfindex int) {
|
||||||
var (
|
var (
|
||||||
statusUp bool
|
statusUp bool
|
||||||
statusMTU int
|
statusMTU int
|
||||||
@ -240,7 +240,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tun := nativeTun{tunFile: tunFile}
|
tun := NativeTun{tunFile: tunFile}
|
||||||
var assignedName string
|
var assignedName string
|
||||||
tun.operateOnFd(func(fd uintptr) {
|
tun.operateOnFd(func(fd uintptr) {
|
||||||
assignedName, err = tunName(fd)
|
assignedName, err = tunName(fd)
|
||||||
@ -307,7 +307,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
|
|
||||||
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
||||||
|
|
||||||
tun := &nativeTun{
|
tun := &NativeTun{
|
||||||
tunFile: file,
|
tunFile: file,
|
||||||
events: make(chan TUNEvent, 10),
|
events: make(chan TUNEvent, 10),
|
||||||
errors: make(chan error, 1),
|
errors: make(chan error, 1),
|
||||||
@ -348,7 +348,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
return tun, nil
|
return tun, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Name() (string, error) {
|
func (tun *NativeTun) Name() (string, error) {
|
||||||
var name string
|
var name string
|
||||||
var err error
|
var err error
|
||||||
tun.operateOnFd(func(fd uintptr) {
|
tun.operateOnFd(func(fd uintptr) {
|
||||||
@ -361,15 +361,15 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
return name, nil
|
return name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *NativeTun) File() *os.File {
|
||||||
return tun.tunFile
|
return tun.tunFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Events() chan TUNEvent {
|
func (tun *NativeTun) Events() chan TUNEvent {
|
||||||
return tun.events
|
return tun.events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
select {
|
select {
|
||||||
case err := <-tun.errors:
|
case err := <-tun.errors:
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -383,7 +383,7 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
@ -406,7 +406,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
return tun.tunFile.Write(buff)
|
return tun.tunFile.Write(buff)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Close() error {
|
func (tun *NativeTun) Close() error {
|
||||||
var err3 error
|
var err3 error
|
||||||
err1 := tun.tunFile.Close()
|
err1 := tun.tunFile.Close()
|
||||||
err2 := tunDestroy(tun.name)
|
err2 := tunDestroy(tun.name)
|
||||||
@ -426,7 +426,7 @@ func (tun *nativeTun) Close() error {
|
|||||||
return err3
|
return err3
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) setMTU(n int) error {
|
func (tun *NativeTun) setMTU(n int) error {
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
|
|
||||||
var fd int
|
var fd int
|
||||||
@ -463,7 +463,7 @@ func (tun *nativeTun) setMTU(n int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) MTU() (int, error) {
|
func (tun *NativeTun) MTU() (int, error) {
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
|
|
||||||
fd, err := unix.Socket(
|
fd, err := unix.Socket(
|
||||||
|
@ -28,7 +28,7 @@ const (
|
|||||||
ifReqSize = unix.IFNAMSIZ + 64
|
ifReqSize = unix.IFNAMSIZ + 64
|
||||||
)
|
)
|
||||||
|
|
||||||
type nativeTun struct {
|
type NativeTun struct {
|
||||||
tunFile *os.File
|
tunFile *os.File
|
||||||
fd uintptr
|
fd uintptr
|
||||||
fdCancel *rwcancel.RWCancel
|
fdCancel *rwcancel.RWCancel
|
||||||
@ -43,11 +43,11 @@ type nativeTun struct {
|
|||||||
statusListenersShutdown chan struct{}
|
statusListenersShutdown chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *NativeTun) File() *os.File {
|
||||||
return tun.tunFile
|
return tun.tunFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) routineHackListener() {
|
func (tun *NativeTun) routineHackListener() {
|
||||||
defer tun.hackListenerClosed.Unlock()
|
defer tun.hackListenerClosed.Unlock()
|
||||||
/* This is needed for the detection to work across network namespaces
|
/* This is needed for the detection to work across network namespaces
|
||||||
* If you are reading this and know a better method, please get in touch.
|
* If you are reading this and know a better method, please get in touch.
|
||||||
@ -87,7 +87,7 @@ func createNetlinkSocket() (int, error) {
|
|||||||
return sock, nil
|
return sock, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) routineNetlinkListener() {
|
func (tun *NativeTun) routineNetlinkListener() {
|
||||||
defer func() {
|
defer func() {
|
||||||
unix.Close(tun.netlinkSock)
|
unix.Close(tun.netlinkSock)
|
||||||
tun.hackListenerClosed.Lock()
|
tun.hackListenerClosed.Lock()
|
||||||
@ -157,7 +157,7 @@ func (tun *nativeTun) routineNetlinkListener() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) isUp() (bool, error) {
|
func (tun *NativeTun) isUp() (bool, error) {
|
||||||
inter, err := net.InterfaceByName(tun.name)
|
inter, err := net.InterfaceByName(tun.name)
|
||||||
return inter.Flags&net.FlagUp != 0, err
|
return inter.Flags&net.FlagUp != 0, err
|
||||||
}
|
}
|
||||||
@ -190,7 +190,7 @@ func getIFIndex(name string) (int32, error) {
|
|||||||
return *(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])), nil
|
return *(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ])), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) setMTU(n int) error {
|
func (tun *NativeTun) setMTU(n int) error {
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
fd, err := unix.Socket(
|
fd, err := unix.Socket(
|
||||||
unix.AF_INET,
|
unix.AF_INET,
|
||||||
@ -223,7 +223,7 @@ func (tun *nativeTun) setMTU(n int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) MTU() (int, error) {
|
func (tun *NativeTun) MTU() (int, error) {
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
fd, err := unix.Socket(
|
fd, err := unix.Socket(
|
||||||
unix.AF_INET,
|
unix.AF_INET,
|
||||||
@ -254,7 +254,7 @@ func (tun *nativeTun) MTU() (int, error) {
|
|||||||
return int(*(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ]))), nil
|
return int(*(*int32)(unsafe.Pointer(&ifr[unix.IFNAMSIZ]))), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Name() (string, error) {
|
func (tun *NativeTun) Name() (string, error) {
|
||||||
var ifr [ifReqSize]byte
|
var ifr [ifReqSize]byte
|
||||||
_, _, errno := unix.Syscall(
|
_, _, errno := unix.Syscall(
|
||||||
unix.SYS_IOCTL,
|
unix.SYS_IOCTL,
|
||||||
@ -274,7 +274,7 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
return tun.name, nil
|
return tun.name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
||||||
|
|
||||||
if tun.nopi {
|
if tun.nopi {
|
||||||
buff = buff[offset:]
|
buff = buff[offset:]
|
||||||
@ -302,7 +302,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
return tun.tunFile.Write(buff)
|
return tun.tunFile.Write(buff)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) doRead(buff []byte, offset int) (int, error) {
|
||||||
select {
|
select {
|
||||||
case err := <-tun.errors:
|
case err := <-tun.errors:
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -320,7 +320,7 @@ func (tun *nativeTun) doRead(buff []byte, offset int) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
for {
|
for {
|
||||||
n, err := tun.doRead(buff, offset)
|
n, err := tun.doRead(buff, offset)
|
||||||
if err == nil || !rwcancel.RetryAfterError(err) {
|
if err == nil || !rwcancel.RetryAfterError(err) {
|
||||||
@ -332,11 +332,11 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Events() chan TUNEvent {
|
func (tun *NativeTun) Events() chan TUNEvent {
|
||||||
return tun.events
|
return tun.events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Close() error {
|
func (tun *NativeTun) Close() error {
|
||||||
var err1 error
|
var err1 error
|
||||||
if tun.statusListenersShutdown != nil {
|
if tun.statusListenersShutdown != nil {
|
||||||
close(tun.statusListenersShutdown)
|
close(tun.statusListenersShutdown)
|
||||||
@ -394,7 +394,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
||||||
tun := &nativeTun{
|
tun := &NativeTun{
|
||||||
tunFile: file,
|
tunFile: file,
|
||||||
fd: file.Fd(),
|
fd: file.Fd(),
|
||||||
events: make(chan TUNEvent, 5),
|
events: make(chan TUNEvent, 5),
|
||||||
|
@ -25,7 +25,7 @@ type ifreq_mtu struct {
|
|||||||
|
|
||||||
const _TUNSIFMODE = 0x8004745d
|
const _TUNSIFMODE = 0x8004745d
|
||||||
|
|
||||||
type nativeTun struct {
|
type NativeTun struct {
|
||||||
name string
|
name string
|
||||||
tunFile *os.File
|
tunFile *os.File
|
||||||
events chan TUNEvent
|
events chan TUNEvent
|
||||||
@ -33,7 +33,7 @@ type nativeTun struct {
|
|||||||
routeSocket int
|
routeSocket int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) routineRouteListener(tunIfindex int) {
|
func (tun *NativeTun) routineRouteListener(tunIfindex int) {
|
||||||
var (
|
var (
|
||||||
statusUp bool
|
statusUp bool
|
||||||
statusMTU int
|
statusMTU int
|
||||||
@ -131,7 +131,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
if err == nil && name == "tun" {
|
if err == nil && name == "tun" {
|
||||||
fname := os.Getenv("WG_TUN_NAME_FILE")
|
fname := os.Getenv("WG_TUN_NAME_FILE")
|
||||||
if fname != "" {
|
if fname != "" {
|
||||||
ioutil.WriteFile(fname, []byte(tun.(*nativeTun).name+"\n"), 0400)
|
ioutil.WriteFile(fname, []byte(tun.(*NativeTun).name+"\n"), 0400)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,7 +140,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
|
|||||||
|
|
||||||
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
||||||
|
|
||||||
tun := &nativeTun{
|
tun := &NativeTun{
|
||||||
tunFile: file,
|
tunFile: file,
|
||||||
events: make(chan TUNEvent, 10),
|
events: make(chan TUNEvent, 10),
|
||||||
errors: make(chan error, 1),
|
errors: make(chan error, 1),
|
||||||
@ -181,7 +181,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
|
|||||||
return tun, nil
|
return tun, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Name() (string, error) {
|
func (tun *NativeTun) Name() (string, error) {
|
||||||
gostat, err := tun.tunFile.Stat()
|
gostat, err := tun.tunFile.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tun.name = ""
|
tun.name = ""
|
||||||
@ -192,15 +192,15 @@ func (tun *nativeTun) Name() (string, error) {
|
|||||||
return tun.name, nil
|
return tun.name, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *NativeTun) File() *os.File {
|
||||||
return tun.tunFile
|
return tun.tunFile
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Events() chan TUNEvent {
|
func (tun *NativeTun) Events() chan TUNEvent {
|
||||||
return tun.events
|
return tun.events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
select {
|
select {
|
||||||
case err := <-tun.errors:
|
case err := <-tun.errors:
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -214,7 +214,7 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
@ -237,7 +237,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
|||||||
return tun.tunFile.Write(buff)
|
return tun.tunFile.Write(buff)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Close() error {
|
func (tun *NativeTun) Close() error {
|
||||||
var err2 error
|
var err2 error
|
||||||
err1 := tun.tunFile.Close()
|
err1 := tun.tunFile.Close()
|
||||||
if tun.routeSocket != -1 {
|
if tun.routeSocket != -1 {
|
||||||
@ -253,7 +253,7 @@ func (tun *nativeTun) Close() error {
|
|||||||
return err2
|
return err2
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) setMTU(n int) error {
|
func (tun *NativeTun) setMTU(n int) error {
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
|
|
||||||
var fd int
|
var fd int
|
||||||
@ -290,7 +290,7 @@ func (tun *nativeTun) setMTU(n int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) MTU() (int, error) {
|
func (tun *NativeTun) MTU() (int, error) {
|
||||||
// open datagram socket
|
// open datagram socket
|
||||||
|
|
||||||
fd, err := unix.Socket(
|
fd, err := unix.Socket(
|
||||||
|
@ -34,7 +34,7 @@ type exchgBufWrite struct {
|
|||||||
packetNum uint32
|
packetNum uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
type nativeTun struct {
|
type NativeTun struct {
|
||||||
wt *wintun.Wintun
|
wt *wintun.Wintun
|
||||||
tunName string
|
tunName string
|
||||||
signalName *uint16
|
signalName *uint16
|
||||||
@ -88,7 +88,7 @@ func CreateTUN(ifname string) (TUNDevice, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create instance.
|
// Create instance.
|
||||||
tun := &nativeTun{
|
tun := &NativeTun{
|
||||||
wt: wt,
|
wt: wt,
|
||||||
tunName: wt.DataFileName(),
|
tunName: wt.DataFileName(),
|
||||||
signalName: signalNameUTF16,
|
signalName: signalNameUTF16,
|
||||||
@ -108,7 +108,7 @@ func CreateTUN(ifname string) (TUNDevice, error) {
|
|||||||
return tun, nil
|
return tun, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) openTUN() error {
|
func (tun *NativeTun) openTUN() error {
|
||||||
for {
|
for {
|
||||||
// Open interface data pipe.
|
// Open interface data pipe.
|
||||||
// Data pipe must be opened first, as the interface data available event is created when somebody actually connects to the data pipe.
|
// Data pipe must be opened first, as the interface data available event is created when somebody actually connects to the data pipe.
|
||||||
@ -140,7 +140,7 @@ func (tun *nativeTun) openTUN() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) closeTUN() (err error) {
|
func (tun *NativeTun) closeTUN() (err error) {
|
||||||
tun.tunLock.Lock()
|
tun.tunLock.Lock()
|
||||||
defer tun.tunLock.Unlock()
|
defer tun.tunLock.Unlock()
|
||||||
|
|
||||||
@ -167,7 +167,7 @@ func (tun *nativeTun) closeTUN() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) getTUN() (*os.File, windows.Handle, error) {
|
func (tun *NativeTun) getTUN() (*os.File, windows.Handle, error) {
|
||||||
tun.tunLock.Lock()
|
tun.tunLock.Lock()
|
||||||
defer tun.tunLock.Unlock()
|
defer tun.tunLock.Unlock()
|
||||||
|
|
||||||
@ -182,19 +182,19 @@ func (tun *nativeTun) getTUN() (*os.File, windows.Handle, error) {
|
|||||||
return tun.tunFile, tun.tunDataAvail, nil
|
return tun.tunFile, tun.tunDataAvail, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Name() (string, error) {
|
func (tun *NativeTun) Name() (string, error) {
|
||||||
return tun.wt.GetInterfaceName()
|
return tun.wt.GetInterfaceName()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) File() *os.File {
|
func (tun *NativeTun) File() *os.File {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Events() chan TUNEvent {
|
func (tun *NativeTun) Events() chan TUNEvent {
|
||||||
return tun.events
|
return tun.events
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Close() error {
|
func (tun *NativeTun) Close() error {
|
||||||
windows.SetEvent(tun.userClose)
|
windows.SetEvent(tun.userClose)
|
||||||
err := windows.CloseHandle(tun.userClose)
|
err := windows.CloseHandle(tun.userClose)
|
||||||
|
|
||||||
@ -215,11 +215,11 @@ func (tun *nativeTun) Close() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) MTU() (int, error) {
|
func (tun *NativeTun) MTU() (int, error) {
|
||||||
return 1500, nil
|
return 1500, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
|
||||||
select {
|
select {
|
||||||
case err := <-tun.errors:
|
case err := <-tun.errors:
|
||||||
return 0, err
|
return 0, err
|
||||||
@ -292,7 +292,7 @@ func (tun *nativeTun) Read(buff []byte, offset int) (int, error) {
|
|||||||
|
|
||||||
// Note: flush() and putTunPacket() assume the caller comes only from a single thread; there's no locking.
|
// Note: flush() and putTunPacket() assume the caller comes only from a single thread; there's no locking.
|
||||||
|
|
||||||
func (tun *nativeTun) flush() error {
|
func (tun *NativeTun) flush() error {
|
||||||
// Get TUN data pipe.
|
// Get TUN data pipe.
|
||||||
file, _, err := tun.getTUN()
|
file, _, err := tun.getTUN()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -312,7 +312,7 @@ func (tun *nativeTun) flush() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) putTunPacket(buff []byte) error {
|
func (tun *NativeTun) putTunPacket(buff []byte) error {
|
||||||
size := uint32(len(buff))
|
size := uint32(len(buff))
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
return errors.New("Empty packet")
|
return errors.New("Empty packet")
|
||||||
@ -341,7 +341,7 @@ func (tun *nativeTun) putTunPacket(buff []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tun *nativeTun) Write(buff []byte, offset int) (int, error) {
|
func (tun *NativeTun) Write(buff []byte, offset int) (int, error) {
|
||||||
err := tun.putTunPacket(buff[offset:])
|
err := tun.putTunPacket(buff[offset:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
Loading…
Reference in New Issue
Block a user