From 1f48971a80f48257e478e532f6971d0557026120 Mon Sep 17 00:00:00 2001 From: Matt Layher Date: Mon, 10 Jun 2019 17:33:40 -0400 Subject: tun: remove TUN prefix from types to reduce stutter elsewhere Signed-off-by: Matt Layher --- device/device.go | 5 ++--- device/tun.go | 6 +++--- device/tun_test.go | 18 +++++++++--------- main.go | 2 +- tun/tun.go | 12 ++++++------ tun/tun_darwin.go | 16 ++++++++-------- tun/tun_freebsd.go | 16 ++++++++-------- tun/tun_linux.go | 32 ++++++++++++++++---------------- tun/tun_openbsd.go | 16 ++++++++-------- tun/tun_windows.go | 10 +++++----- 10 files changed, 66 insertions(+), 67 deletions(-) diff --git a/device/device.go b/device/device.go index 7775844..a583fa9 100644 --- a/device/device.go +++ b/device/device.go @@ -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] diff --git a/device/tun.go b/device/tun.go index fe1158c..0a3fc79 100644 --- a/device/tun.go +++ b/device/tun.go @@ -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() diff --git a/device/tun_test.go b/device/tun_test.go index fbe4c1d..5614771 100644 --- a/device/tun_test.go +++ b/device/tun_test.go @@ -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 77a660f..12753d6 100644 --- 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) diff --git a/tun/tun.go b/tun/tun.go index 12febb8..5395bdb 100644 --- a/tun/tun.go +++ b/tun/tun.go @@ -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 } diff --git a/tun/tun_darwin.go b/tun/tun_darwin.go index 8cd22a8..0815495 100644 --- a/tun/tun_darwin.go +++ b/tun/tun_darwin.go @@ -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 } diff --git a/tun/tun_freebsd.go b/tun/tun_freebsd.go index df43ab7..6cf9313 100644 --- a/tun/tun_freebsd.go +++ b/tun/tun_freebsd.go @@ -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 } diff --git a/tun/tun_linux.go b/tun/tun_linux.go index 2168f5b..61902e9 100644 --- a/tun/tun_linux.go +++ b/tun/tun_linux.go @@ -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, } diff --git a/tun/tun_openbsd.go b/tun/tun_openbsd.go index b0f5ca4..1e6191f 100644 --- a/tun/tun_openbsd.go +++ b/tun/tun_openbsd.go @@ -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 } diff --git a/tun/tun_windows.go b/tun/tun_windows.go index 4850bb5..b88129d 100644 --- a/tun/tun_windows.go +++ b/tun/tun_windows.go @@ -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 } -- cgit v1.2.3