From 967d1a0f3d7aae4a0720895a2f6e35156b34214b Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Fri, 1 Mar 2019 00:05:57 +0100 Subject: tun: allow special methods in NativeTun --- tun/tun.go | 2 +- tun/tun_darwin.go | 24 ++++++++++++------------ tun/tun_freebsd.go | 24 ++++++++++++------------ tun/tun_linux.go | 28 ++++++++++++++-------------- tun/tun_openbsd.go | 24 ++++++++++++------------ tun/tun_windows.go | 28 ++++++++++++++-------------- 6 files changed, 65 insertions(+), 65 deletions(-) diff --git a/tun/tun.go b/tun/tun.go index 3493b48..f38ee31 100644 --- a/tun/tun.go +++ b/tun/tun.go @@ -28,7 +28,7 @@ type TUNDevice interface { 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() if err != nil { tun.errors <- fmt.Errorf("unable to find sysconn for tunfile: %s", err.Error()) diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go index 8cb2981..600b156 100644 --- a/tun/tun_darwin.go +++ b/tun/tun_darwin.go @@ -31,7 +31,7 @@ type sockaddrCtl struct { scReserved [5]uint32 } -type nativeTun struct { +type NativeTun struct { name string tunFile *os.File events chan TUNEvent @@ -41,7 +41,7 @@ type nativeTun struct { var sockaddrCtlSize uintptr = 32 -func (tun *nativeTun) routineRouteListener(tunIfindex int) { +func (tun *NativeTun) routineRouteListener(tunIfindex int) { var ( statusUp bool statusMTU int @@ -160,7 +160,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) { if err == nil && name == "utun" { fname := os.Getenv("WG_TUN_NAME_FILE") 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) { - tun := &nativeTun{ + tun := &NativeTun{ tunFile: file, events: make(chan TUNEvent, 10), errors: make(chan error, 1), @@ -209,7 +209,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) { return tun, nil } -func (tun *nativeTun) Name() (string, error) { +func (tun *NativeTun) Name() (string, error) { var ifName struct { name [16]byte } @@ -234,15 +234,15 @@ func (tun *nativeTun) Name() (string, error) { return tun.name, nil } -func (tun *nativeTun) File() *os.File { +func (tun *NativeTun) File() *os.File { return tun.tunFile } -func (tun *nativeTun) Events() chan TUNEvent { +func (tun *NativeTun) Events() chan TUNEvent { return tun.events } -func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { +func (tun *NativeTun) Read(buff []byte, offset int) (int, error) { select { case err := <-tun.errors: 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 @@ -279,7 +279,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) { return tun.tunFile.Write(buff) } -func (tun *nativeTun) Close() error { +func (tun *NativeTun) Close() error { var err2 error err1 := tun.tunFile.Close() if tun.routeSocket != -1 { @@ -295,7 +295,7 @@ func (tun *nativeTun) Close() error { return err2 } -func (tun *nativeTun) setMTU(n int) error { +func (tun *NativeTun) setMTU(n int) error { // open datagram socket @@ -332,7 +332,7 @@ func (tun *nativeTun) setMTU(n int) error { return nil } -func (tun *nativeTun) MTU() (int, error) { +func (tun *NativeTun) MTU() (int, error) { // open datagram socket diff --git a/tun/tun_freebsd.go b/tun/tun_freebsd.go index 9e5c5ad..3a60725 100644 --- a/tun/tun_freebsd.go +++ b/tun/tun_freebsd.go @@ -48,7 +48,7 @@ type ifstat struct { Ascii [_IFSTATMAX]byte } -type nativeTun struct { +type NativeTun struct { name string tunFile *os.File events chan TUNEvent @@ -56,7 +56,7 @@ type nativeTun struct { routeSocket int } -func (tun *nativeTun) routineRouteListener(tunIfindex int) { +func (tun *NativeTun) routineRouteListener(tunIfindex int) { var ( statusUp bool statusMTU int @@ -240,7 +240,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) { return nil, err } - tun := nativeTun{tunFile: tunFile} + tun := NativeTun{tunFile: tunFile} var assignedName string tun.operateOnFd(func(fd uintptr) { 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) { - tun := &nativeTun{ + tun := &NativeTun{ tunFile: file, events: make(chan TUNEvent, 10), errors: make(chan error, 1), @@ -348,7 +348,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) { return tun, nil } -func (tun *nativeTun) Name() (string, error) { +func (tun *NativeTun) Name() (string, error) { var name string var err error tun.operateOnFd(func(fd uintptr) { @@ -361,15 +361,15 @@ func (tun *nativeTun) Name() (string, error) { return name, nil } -func (tun *nativeTun) File() *os.File { +func (tun *NativeTun) File() *os.File { return tun.tunFile } -func (tun *nativeTun) Events() chan TUNEvent { +func (tun *NativeTun) Events() chan TUNEvent { return tun.events } -func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { +func (tun *NativeTun) Read(buff []byte, offset int) (int, error) { select { case err := <-tun.errors: 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 @@ -406,7 +406,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) { return tun.tunFile.Write(buff) } -func (tun *nativeTun) Close() error { +func (tun *NativeTun) Close() error { var err3 error err1 := tun.tunFile.Close() err2 := tunDestroy(tun.name) @@ -426,7 +426,7 @@ func (tun *nativeTun) Close() error { return err3 } -func (tun *nativeTun) setMTU(n int) error { +func (tun *NativeTun) setMTU(n int) error { // open datagram socket var fd int @@ -463,7 +463,7 @@ func (tun *nativeTun) setMTU(n int) error { return nil } -func (tun *nativeTun) MTU() (int, error) { +func (tun *NativeTun) MTU() (int, error) { // open datagram socket fd, err := unix.Socket( diff --git a/tun/tun_linux.go b/tun/tun_linux.go index 17075d9..f808079 100644 --- a/tun/tun_linux.go +++ b/tun/tun_linux.go @@ -28,7 +28,7 @@ const ( ifReqSize = unix.IFNAMSIZ + 64 ) -type nativeTun struct { +type NativeTun struct { tunFile *os.File fd uintptr fdCancel *rwcancel.RWCancel @@ -43,11 +43,11 @@ type nativeTun struct { statusListenersShutdown chan struct{} } -func (tun *nativeTun) File() *os.File { +func (tun *NativeTun) File() *os.File { return tun.tunFile } -func (tun *nativeTun) routineHackListener() { +func (tun *NativeTun) routineHackListener() { defer tun.hackListenerClosed.Unlock() /* 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. @@ -87,7 +87,7 @@ func createNetlinkSocket() (int, error) { return sock, nil } -func (tun *nativeTun) routineNetlinkListener() { +func (tun *NativeTun) routineNetlinkListener() { defer func() { unix.Close(tun.netlinkSock) 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) 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 } -func (tun *nativeTun) setMTU(n int) error { +func (tun *NativeTun) setMTU(n int) error { // open datagram socket fd, err := unix.Socket( unix.AF_INET, @@ -223,7 +223,7 @@ func (tun *nativeTun) setMTU(n int) error { return nil } -func (tun *nativeTun) MTU() (int, error) { +func (tun *NativeTun) MTU() (int, error) { // open datagram socket fd, err := unix.Socket( unix.AF_INET, @@ -254,7 +254,7 @@ func (tun *nativeTun) MTU() (int, error) { 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 _, _, errno := unix.Syscall( unix.SYS_IOCTL, @@ -274,7 +274,7 @@ func (tun *nativeTun) Name() (string, error) { 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 { buff = buff[offset:] @@ -302,7 +302,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) { 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 { case err := <-tun.errors: 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 { n, err := tun.doRead(buff, offset) 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 } -func (tun *nativeTun) Close() error { +func (tun *NativeTun) Close() error { var err1 error if tun.statusListenersShutdown != nil { close(tun.statusListenersShutdown) @@ -394,7 +394,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) { } func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) { - tun := &nativeTun{ + tun := &NativeTun{ tunFile: file, fd: file.Fd(), events: make(chan TUNEvent, 5), diff --git a/tun/tun_openbsd.go b/tun/tun_openbsd.go index 4e740c5..57edcb4 100644 --- a/tun/tun_openbsd.go +++ b/tun/tun_openbsd.go @@ -25,7 +25,7 @@ type ifreq_mtu struct { const _TUNSIFMODE = 0x8004745d -type nativeTun struct { +type NativeTun struct { name string tunFile *os.File events chan TUNEvent @@ -33,7 +33,7 @@ type nativeTun struct { routeSocket int } -func (tun *nativeTun) routineRouteListener(tunIfindex int) { +func (tun *NativeTun) routineRouteListener(tunIfindex int) { var ( statusUp bool statusMTU int @@ -131,7 +131,7 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) { if err == nil && name == "tun" { fname := os.Getenv("WG_TUN_NAME_FILE") 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) { - tun := &nativeTun{ + tun := &NativeTun{ tunFile: file, events: make(chan TUNEvent, 10), errors: make(chan error, 1), @@ -181,7 +181,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) { return tun, nil } -func (tun *nativeTun) Name() (string, error) { +func (tun *NativeTun) Name() (string, error) { gostat, err := tun.tunFile.Stat() if err != nil { tun.name = "" @@ -192,15 +192,15 @@ func (tun *nativeTun) Name() (string, error) { return tun.name, nil } -func (tun *nativeTun) File() *os.File { +func (tun *NativeTun) File() *os.File { return tun.tunFile } -func (tun *nativeTun) Events() chan TUNEvent { +func (tun *NativeTun) Events() chan TUNEvent { return tun.events } -func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { +func (tun *NativeTun) Read(buff []byte, offset int) (int, error) { select { case err := <-tun.errors: 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 @@ -237,7 +237,7 @@ func (tun *nativeTun) Write(buff []byte, offset int) (int, error) { return tun.tunFile.Write(buff) } -func (tun *nativeTun) Close() error { +func (tun *NativeTun) Close() error { var err2 error err1 := tun.tunFile.Close() if tun.routeSocket != -1 { @@ -253,7 +253,7 @@ func (tun *nativeTun) Close() error { return err2 } -func (tun *nativeTun) setMTU(n int) error { +func (tun *NativeTun) setMTU(n int) error { // open datagram socket var fd int @@ -290,7 +290,7 @@ func (tun *nativeTun) setMTU(n int) error { return nil } -func (tun *nativeTun) MTU() (int, error) { +func (tun *NativeTun) MTU() (int, error) { // open datagram socket fd, err := unix.Socket( diff --git a/tun/tun_windows.go b/tun/tun_windows.go index e9bf0b7..ed6d7d2 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -34,7 +34,7 @@ type exchgBufWrite struct { packetNum uint32 } -type nativeTun struct { +type NativeTun struct { wt *wintun.Wintun tunName string signalName *uint16 @@ -88,7 +88,7 @@ func CreateTUN(ifname string) (TUNDevice, error) { } // Create instance. - tun := &nativeTun{ + tun := &NativeTun{ wt: wt, tunName: wt.DataFileName(), signalName: signalNameUTF16, @@ -108,7 +108,7 @@ func CreateTUN(ifname string) (TUNDevice, error) { return tun, nil } -func (tun *nativeTun) openTUN() error { +func (tun *NativeTun) openTUN() error { for { // 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. @@ -140,7 +140,7 @@ func (tun *nativeTun) openTUN() error { } } -func (tun *nativeTun) closeTUN() (err error) { +func (tun *NativeTun) closeTUN() (err error) { tun.tunLock.Lock() defer tun.tunLock.Unlock() @@ -167,7 +167,7 @@ func (tun *nativeTun) closeTUN() (err error) { return } -func (tun *nativeTun) getTUN() (*os.File, windows.Handle, error) { +func (tun *NativeTun) getTUN() (*os.File, windows.Handle, error) { tun.tunLock.Lock() defer tun.tunLock.Unlock() @@ -182,19 +182,19 @@ func (tun *nativeTun) getTUN() (*os.File, windows.Handle, error) { return tun.tunFile, tun.tunDataAvail, nil } -func (tun *nativeTun) Name() (string, error) { +func (tun *NativeTun) Name() (string, error) { return tun.wt.GetInterfaceName() } -func (tun *nativeTun) File() *os.File { +func (tun *NativeTun) File() *os.File { return nil } -func (tun *nativeTun) Events() chan TUNEvent { +func (tun *NativeTun) Events() chan TUNEvent { return tun.events } -func (tun *nativeTun) Close() error { +func (tun *NativeTun) Close() error { windows.SetEvent(tun.userClose) err := windows.CloseHandle(tun.userClose) @@ -215,11 +215,11 @@ func (tun *nativeTun) Close() error { return err } -func (tun *nativeTun) MTU() (int, error) { +func (tun *NativeTun) MTU() (int, error) { return 1500, nil } -func (tun *nativeTun) Read(buff []byte, offset int) (int, error) { +func (tun *NativeTun) Read(buff []byte, offset int) (int, error) { select { case err := <-tun.errors: 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. -func (tun *nativeTun) flush() error { +func (tun *NativeTun) flush() error { // Get TUN data pipe. file, _, err := tun.getTUN() if err != nil { @@ -312,7 +312,7 @@ func (tun *nativeTun) flush() error { return nil } -func (tun *nativeTun) putTunPacket(buff []byte) error { +func (tun *NativeTun) putTunPacket(buff []byte) error { size := uint32(len(buff)) if size == 0 { return errors.New("Empty packet") @@ -341,7 +341,7 @@ func (tun *nativeTun) putTunPacket(buff []byte) error { 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:]) if err != nil { return 0, err -- cgit v1.2.3-59-g8ed1b