]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
tun: remove TUN prefix from types to reduce stutter elsewhere
authorMatt Layher <mdlayher@gmail.com>
Mon, 10 Jun 2019 21:33:40 +0000 (17:33 -0400)
committerJason A. Donenfeld <Jason@zx2c4.com>
Fri, 14 Jun 2019 16:35:57 +0000 (18:35 +0200)
Signed-off-by: Matt Layher <mdlayher@gmail.com>
device/device.go
device/tun.go
device/tun_test.go
main.go
tun/tun.go
tun/tun_darwin.go
tun/tun_freebsd.go
tun/tun_linux.go
tun/tun_openbsd.go
tun/tun_windows.go

index 77758447c366c756d1a2d8298e083c4e2cf74fb2..a583fa9008058addd1f0ce4746e7f76c2d9decf1 100644 (file)
@@ -86,7 +86,7 @@ type Device struct {
        }
 
        tun struct {
-               device tun.TUNDevice
+               device tun.Device
                mtu    int32
        }
 }
@@ -252,7 +252,7 @@ func (device *Device) SetPrivateKey(sk NoisePrivateKey) error {
        return nil
 }
 
-func NewDevice(tunDevice tun.TUNDevice, logger *Logger) *Device {
+func NewDevice(tunDevice tun.Device, logger *Logger) *Device {
        device := new(Device)
 
        device.isUp.Set(false)
@@ -324,7 +324,6 @@ func (device *Device) LookupPeer(pk NoisePublicKey) *Peer {
 func (device *Device) RemovePeer(key NoisePublicKey) {
        device.peers.Lock()
        defer device.peers.Unlock()
-
        // stop peer and remove from routing
 
        peer, ok := device.peers.keyMap[key]
index fe1158c1b87ebd63a1bdffd6c0a52a013a39ce9a..0a3fc79fa6f393f33964a8e37224917be4693aaa 100644 (file)
@@ -23,7 +23,7 @@ func (device *Device) RoutineTUNEventReader() {
        device.state.starting.Done()
 
        for event := range device.tun.device.Events() {
-               if event&tun.TUNEventMTUUpdate != 0 {
+               if event&tun.EventMTUUpdate != 0 {
                        mtu, err := device.tun.device.MTU()
                        old := atomic.LoadInt32(&device.tun.mtu)
                        if err != nil {
@@ -38,13 +38,13 @@ func (device *Device) RoutineTUNEventReader() {
                        }
                }
 
-               if event&tun.TUNEventUp != 0 && !setUp {
+               if event&tun.EventUp != 0 && !setUp {
                        logInfo.Println("Interface set up")
                        setUp = true
                        device.Up()
                }
 
-               if event&tun.TUNEventDown != 0 && setUp {
+               if event&tun.EventDown != 0 && setUp {
                        logInfo.Println("Interface set down")
                        setUp = false
                        device.Down()
index fbe4c1dff552a00b623a5ffa46c67f20d3a727be..5614771c42272aab5b82da548fdf2ce3d5743ee5 100644 (file)
@@ -13,27 +13,27 @@ import (
 )
 
 // newDummyTUN creates a dummy TUN device with the specified name.
-func newDummyTUN(name string) tun.TUNDevice {
+func newDummyTUN(name string) tun.Device {
        return &dummyTUN{
                name:    name,
                packets: make(chan []byte, 100),
-               events:  make(chan tun.TUNEvent, 10),
+               events:  make(chan tun.Event, 10),
        }
 }
 
-// A dummyTUN is a tun.TUNDevice which is used in unit tests.
+// A dummyTUN is a tun.Device which is used in unit tests.
 type dummyTUN struct {
        name    string
        mtu     int
        packets chan []byte
-       events  chan tun.TUNEvent
+       events  chan tun.Event
 }
 
-func (d *dummyTUN) Events() chan tun.TUNEvent { return d.events }
-func (*dummyTUN) File() *os.File              { return nil }
-func (*dummyTUN) Flush() error                { return nil }
-func (d *dummyTUN) MTU() (int, error)         { return d.mtu, nil }
-func (d *dummyTUN) Name() (string, error)     { return d.name, nil }
+func (d *dummyTUN) Events() chan tun.Event { return d.events }
+func (*dummyTUN) File() *os.File           { return nil }
+func (*dummyTUN) Flush() error             { return nil }
+func (d *dummyTUN) MTU() (int, error)      { return d.mtu, nil }
+func (d *dummyTUN) Name() (string, error)  { return d.name, nil }
 
 func (d *dummyTUN) Close() error {
        close(d.events)
diff --git a/main.go b/main.go
index 77a660f23318b9d0a075695e566e3d3f69f32d80..12753d6fb1247955ab03255f26a62e70e058e199 100644 (file)
--- a/main.go
+++ b/main.go
@@ -125,7 +125,7 @@ func main() {
 
        // open TUN device (or use supplied fd)
 
-       tun, err := func() (tun.TUNDevice, error) {
+       tun, err := func() (tun.Device, error) {
                tunFdStr := os.Getenv(ENV_WG_TUN_FD)
                if tunFdStr == "" {
                        return tun.CreateTUN(interfaceName, device.DefaultMTU)
index 12febb8890184dc052789ec4bdc6616fde6eb7c8..5395bdbcd7ebab7a45b0b0523fe343101379fd24 100644 (file)
@@ -9,21 +9,21 @@ import (
        "os"
 )
 
-type TUNEvent int
+type Event int
 
 const (
-       TUNEventUp = 1 << iota
-       TUNEventDown
-       TUNEventMTUUpdate
+       EventUp = 1 << iota
+       EventDown
+       EventMTUUpdate
 )
 
-type TUNDevice interface {
+type Device interface {
        File() *os.File                 // returns the file descriptor of the device
        Read([]byte, int) (int, error)  // read a packet from the device (without any additional headers)
        Write([]byte, int) (int, error) // writes a packet to the device (without any additional headers)
        Flush() error                   // flush all previous writes to the device
        MTU() (int, error)              // returns the MTU of the device
        Name() (string, error)          // fetches and returns the current name
-       Events() chan TUNEvent          // returns a constant channel of events related to the device
+       Events() chan Event             // returns a constant channel of events related to the device
        Close() error                   // stops the device and closes the event channel
 }
index 8cd22a88be47e01dfb73d943f82cc7994645124d..0815495d513cc21a1eed72f39905aa4af911e512 100644 (file)
@@ -35,7 +35,7 @@ type sockaddrCtl struct {
 type NativeTun struct {
        name        string
        tunFile     *os.File
-       events      chan TUNEvent
+       events      chan Event
        errors      chan error
        routeSocket int
 }
@@ -86,22 +86,22 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
                // Up / Down event
                up := (iface.Flags & net.FlagUp) != 0
                if up != statusUp && up {
-                       tun.events <- TUNEventUp
+                       tun.events <- EventUp
                }
                if up != statusUp && !up {
-                       tun.events <- TUNEventDown
+                       tun.events <- EventDown
                }
                statusUp = up
 
                // MTU changes
                if iface.MTU != statusMTU {
-                       tun.events <- TUNEventMTUUpdate
+                       tun.events <- EventMTUUpdate
                }
                statusMTU = iface.MTU
        }
 }
 
-func CreateTUN(name string, mtu int) (TUNDevice, error) {
+func CreateTUN(name string, mtu int) (Device, error) {
        ifIndex := -1
        if name != "utun" {
                _, err := fmt.Sscanf(name, "utun%d", &ifIndex)
@@ -171,10 +171,10 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
        return tun, err
 }
 
-func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
+func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {
        tun := &NativeTun{
                tunFile: file,
-               events:  make(chan TUNEvent, 10),
+               events:  make(chan Event, 10),
                errors:  make(chan error, 5),
        }
 
@@ -244,7 +244,7 @@ func (tun *NativeTun) File() *os.File {
        return tun.tunFile
 }
 
-func (tun *NativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan Event {
        return tun.events
 }
 
index df43ab7fc6f8f18119b4b556c2003ea7a1e180df..6cf93136f8f9abcbe2bf1766b2e0c14ff0da420a 100644 (file)
@@ -79,7 +79,7 @@ type in6_ndireq struct {
 type NativeTun struct {
        name        string
        tunFile     *os.File
-       events      chan TUNEvent
+       events      chan Event
        errors      chan error
        routeSocket int
 }
@@ -125,16 +125,16 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
                // Up / Down event
                up := (iface.Flags & net.FlagUp) != 0
                if up != statusUp && up {
-                       tun.events <- TUNEventUp
+                       tun.events <- EventUp
                }
                if up != statusUp && !up {
-                       tun.events <- TUNEventDown
+                       tun.events <- EventDown
                }
                statusUp = up
 
                // MTU changes
                if iface.MTU != statusMTU {
-                       tun.events <- TUNEventMTUUpdate
+                       tun.events <- EventMTUUpdate
                }
                statusMTU = iface.MTU
        }
@@ -246,7 +246,7 @@ func tunDestroy(name string) error {
        return nil
 }
 
-func CreateTUN(name string, mtu int) (TUNDevice, error) {
+func CreateTUN(name string, mtu int) (Device, error) {
        if len(name) > unix.IFNAMSIZ-1 {
                return nil, errors.New("interface name too long")
        }
@@ -365,11 +365,11 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
        return CreateTUNFromFile(tunFile, mtu)
 }
 
-func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
+func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {
 
        tun := &NativeTun{
                tunFile: file,
-               events:  make(chan TUNEvent, 10),
+               events:  make(chan Event, 10),
                errors:  make(chan error, 1),
        }
 
@@ -425,7 +425,7 @@ func (tun *NativeTun) File() *os.File {
        return tun.tunFile
 }
 
-func (tun *NativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan Event {
        return tun.events
 }
 
index 2168f5bd223f400ee24e3aa663c005721f3b5749..61902e9cee40b8907cf5bdf0b712195d95da35e8 100644 (file)
@@ -31,11 +31,11 @@ const (
 
 type NativeTun struct {
        tunFile                 *os.File
-       index                   int32         // if index
-       name                    string        // name of interface
-       errors                  chan error    // async error handling
-       events                  chan TUNEvent // device related events
-       nopi                    bool          // the device was pased IFF_NO_PI
+       index                   int32      // if index
+       name                    string     // name of interface
+       errors                  chan error // async error handling
+       events                  chan Event // device related events
+       nopi                    bool       // the device was pased IFF_NO_PI
        netlinkSock             int
        netlinkCancel           *rwcancel.RWCancel
        hackListenerClosed      sync.Mutex
@@ -64,9 +64,9 @@ func (tun *NativeTun) routineHackListener() {
                }
                switch err {
                case unix.EINVAL:
-                       tun.events <- TUNEventUp
+                       tun.events <- EventUp
                case unix.EIO:
-                       tun.events <- TUNEventDown
+                       tun.events <- EventDown
                default:
                        return
                }
@@ -148,14 +148,14 @@ func (tun *NativeTun) routineNetlinkListener() {
                                }
 
                                if info.Flags&unix.IFF_RUNNING != 0 {
-                                       tun.events <- TUNEventUp
+                                       tun.events <- EventUp
                                }
 
                                if info.Flags&unix.IFF_RUNNING == 0 {
-                                       tun.events <- TUNEventDown
+                                       tun.events <- EventDown
                                }
 
-                               tun.events <- TUNEventMTUUpdate
+                               tun.events <- EventMTUUpdate
 
                        default:
                                remain = remain[hdr.Len:]
@@ -342,7 +342,7 @@ func (tun *NativeTun) Read(buff []byte, offset int) (int, error) {
        }
 }
 
-func (tun *NativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan Event {
        return tun.events
 }
 
@@ -364,7 +364,7 @@ func (tun *NativeTun) Close() error {
        return err2
 }
 
-func CreateTUN(name string, mtu int) (TUNDevice, error) {
+func CreateTUN(name string, mtu int) (Device, error) {
        nfd, err := unix.Open(cloneDevicePath, os.O_RDWR, 0)
        if err != nil {
                return nil, err
@@ -400,10 +400,10 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
        return CreateTUNFromFile(fd, mtu)
 }
 
-func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
+func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {
        tun := &NativeTun{
                tunFile:                 file,
-               events:                  make(chan TUNEvent, 5),
+               events:                  make(chan Event, 5),
                errors:                  make(chan error, 5),
                statusListenersShutdown: make(chan struct{}),
                nopi:                    false,
@@ -445,7 +445,7 @@ func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
        return tun, nil
 }
 
-func CreateUnmonitoredTUNFromFD(fd int) (TUNDevice, string, error) {
+func CreateUnmonitoredTUNFromFD(fd int) (Device, string, error) {
        err := unix.SetNonblock(fd, true)
        if err != nil {
                return nil, "", err
@@ -453,7 +453,7 @@ func CreateUnmonitoredTUNFromFD(fd int) (TUNDevice, string, error) {
        file := os.NewFile(uintptr(fd), "/dev/tun")
        tun := &NativeTun{
                tunFile: file,
-               events:  make(chan TUNEvent, 5),
+               events:  make(chan Event, 5),
                errors:  make(chan error, 5),
                nopi:    true,
        }
index b0f5ca4363e5c1ad7eba3651099c00dfac007dc5..1e6191f28d6ede5ae76630c7f871eecbe9874006 100644 (file)
@@ -29,7 +29,7 @@ const _TUNSIFMODE = 0x8004745d
 type NativeTun struct {
        name        string
        tunFile     *os.File
-       events      chan TUNEvent
+       events      chan Event
        errors      chan error
        routeSocket int
 }
@@ -75,16 +75,16 @@ func (tun *NativeTun) routineRouteListener(tunIfindex int) {
                // Up / Down event
                up := (iface.Flags & net.FlagUp) != 0
                if up != statusUp && up {
-                       tun.events <- TUNEventUp
+                       tun.events <- EventUp
                }
                if up != statusUp && !up {
-                       tun.events <- TUNEventDown
+                       tun.events <- EventDown
                }
                statusUp = up
 
                // MTU changes
                if iface.MTU != statusMTU {
-                       tun.events <- TUNEventMTUUpdate
+                       tun.events <- EventMTUUpdate
                }
                statusMTU = iface.MTU
        }
@@ -100,7 +100,7 @@ func errorIsEBUSY(err error) bool {
        return false
 }
 
-func CreateTUN(name string, mtu int) (TUNDevice, error) {
+func CreateTUN(name string, mtu int) (Device, error) {
        ifIndex := -1
        if name != "tun" {
                _, err := fmt.Sscanf(name, "tun%d", &ifIndex)
@@ -139,11 +139,11 @@ func CreateTUN(name string, mtu int) (TUNDevice, error) {
        return tun, err
 }
 
-func CreateTUNFromFile(file *os.File, mtu int) (TUNDevice, error) {
+func CreateTUNFromFile(file *os.File, mtu int) (Device, error) {
 
        tun := &NativeTun{
                tunFile: file,
-               events:  make(chan TUNEvent, 10),
+               events:  make(chan Event, 10),
                errors:  make(chan error, 1),
        }
 
@@ -197,7 +197,7 @@ func (tun *NativeTun) File() *os.File {
        return tun.tunFile
 }
 
-func (tun *NativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan Event {
        return tun.events
 }
 
index 4850bb5444426126c397321a4a4b8f8cc2f4739a..b88129d0c265e56583c0880ea80d3e5239a2519f 100644 (file)
@@ -46,7 +46,7 @@ type NativeTun struct {
        close        bool
        rdBuff       *exchgBufRead
        wrBuff       *exchgBufWrite
-       events       chan TUNEvent
+       events       chan Event
        errors       chan error
        forcedMTU    int
 }
@@ -59,7 +59,7 @@ func packetAlign(size uint32) uint32 {
 // CreateTUN creates a Wintun adapter with the given name. Should a Wintun
 // adapter with the same name exist, it is reused.
 //
-func CreateTUN(ifname string) (TUNDevice, error) {
+func CreateTUN(ifname string) (Device, error) {
        return CreateTUNWithRequestedGUID(ifname, nil)
 }
 
@@ -67,7 +67,7 @@ func CreateTUN(ifname string) (TUNDevice, error) {
 // CreateTUNWithRequestedGUID creates a Wintun adapter with the given name and
 // a requested GUID. Should a Wintun adapter with the same name exist, it is reused.
 //
-func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (TUNDevice, error) {
+func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (Device, error) {
        var err error
        var wt *wintun.Wintun
 
@@ -97,7 +97,7 @@ func CreateTUNWithRequestedGUID(ifname string, requestedGUID *windows.GUID) (TUN
                wt:        wt,
                rdBuff:    &exchgBufRead{},
                wrBuff:    &exchgBufWrite{},
-               events:    make(chan TUNEvent, 10),
+               events:    make(chan Event, 10),
                errors:    make(chan error, 1),
                forcedMTU: 1500,
        }, nil
@@ -202,7 +202,7 @@ func (tun *NativeTun) File() *os.File {
        return nil
 }
 
-func (tun *NativeTun) Events() chan TUNEvent {
+func (tun *NativeTun) Events() chan Event {
        return tun.events
 }