]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: allow special methods in NativeTun
authorJason A. Donenfeld <Jason@zx2c4.com>
Thu, 28 Feb 2019 23:05:57 +0000 (00:05 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Thu, 28 Feb 2019 23:05:57 +0000 (00:05 +0100)
tun/tun.go
tun/tun_darwin.go
tun/tun_freebsd.go
tun/tun_linux.go
tun/tun_openbsd.go
tun/tun_windows.go

index 3493b484c1cbd63646ca0d367cb89f597fd41394..f38ee3156f6b943af0cc32fe04a8bd25797c880d 100644 (file)
@@ -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())
index 8cb29810964679c181a9d6a7d3d58198e1c4d3dc..600b1569b07ab74ccfdc152a0b1ea910f5181d9f 100644 (file)
@@ -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
 
index 9e5c5ad92fa67fd9f2ae1925ee0ed5a609d84bcb..3a607255a1d3845cd0174f6bdc4a8743221dc07f 100644 (file)
@@ -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(
index 17075d9f11716992b5ea76465676a07e7147b42a..f80807917c488bcdcb86c1b7d2ba9560616058b7 100644 (file)
@@ -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),
index 4e740c5f42d16ce6af7edc5dbfa3cb259f05f2b6..57edcb45f36f3e20a3718e978f0b006b97d73e37 100644 (file)
@@ -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(
index e9bf0b77ff2e5374fd23f31ff7b8be8ac29c22d0..ed6d7d2595636bb7cfdabfbc06001e9ed44bb82e 100644 (file)
@@ -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