From 70591d249921d075889cc49aaef072987e6b354a Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Mon, 22 May 2017 12:25:48 -0400 Subject: [PATCH] Simplifies Platform Specific Interface Creation The interface `Config` object is now passed directly into each creation function in order to more easily use additional options. In addition, the linux and darwin parameters were split to better capture different options for each platform. --- if.go | 12 +++++++++++- if_unix.go | 16 ---------------- if_windows.go | 12 ------------ params_darwin.go | 13 +++++++++++++ params_unix.go => params_linux.go | 3 +-- syscalls_darwin.go | 10 +++++----- syscalls_linux.go | 8 ++++---- syscalls_other.go | 4 ++-- syscalls_windows.go | 8 ++------ 9 files changed, 38 insertions(+), 48 deletions(-) delete mode 100644 if_unix.go delete mode 100644 if_windows.go create mode 100644 params_darwin.go rename params_unix.go => params_linux.go (83%) diff --git a/if.go b/if.go index 2131379..af0adf6 100644 --- a/if.go +++ b/if.go @@ -2,6 +2,7 @@ package water import ( "io" + "errors" ) // Interface is a TUN/TAP interface. @@ -48,7 +49,14 @@ func New(config Config) (ifce *Interface, err error) { if zeroConfig == config { config = defaultConfig() } - return newDev(config) + switch config.DeviceType { + case TUN: + return newTUN(config) + case TAP: + return newTAP(config) + default: + return nil, errors.New("unknown device type") + } } // IsTUN returns true if ifce is a TUN interface. @@ -65,3 +73,5 @@ func (ifce *Interface) IsTAP() bool { func (ifce *Interface) Name() string { return ifce.name } + + diff --git a/if_unix.go b/if_unix.go deleted file mode 100644 index f2de759..0000000 --- a/if_unix.go +++ /dev/null @@ -1,16 +0,0 @@ -// +build linux darwin - -package water - -import "errors" - -func newDev(config Config) (ifce *Interface, err error) { - switch config.DeviceType { - case TUN: - return newTUN(config.Name) - case TAP: - return newTAP(config.Name) - default: - return nil, errors.New("unknown device type") - } -} diff --git a/if_windows.go b/if_windows.go deleted file mode 100644 index 3ef1f8e..0000000 --- a/if_windows.go +++ /dev/null @@ -1,12 +0,0 @@ -// +build windows - -package water - -import "errors" - -func newDev(config Config) (ifce *Interface, err error) { - if config.DeviceType != TAP && config.DeviceType != TUN { - return nil, errors.New("unknown device type") - } - return openDev(config) -} diff --git a/params_darwin.go b/params_darwin.go new file mode 100644 index 0000000..13c6b18 --- /dev/null +++ b/params_darwin.go @@ -0,0 +1,13 @@ + +package water + +// PlatformSpecificParams defines parameters in Config that are specific to +// macOS. A zero-value of such type is valid, yielding an interface +// with OS defined name. +// Currently it is not possible to set the interface name in macOS. +type PlatformSpecificParams struct { +} + +func defaultPlatformSpecificParams() PlatformSpecificParams { + return PlatformSpecificParams{} +} diff --git a/params_unix.go b/params_linux.go similarity index 83% rename from params_unix.go rename to params_linux.go index e0e613a..9449f31 100644 --- a/params_unix.go +++ b/params_linux.go @@ -1,9 +1,8 @@ -// +build linux darwin package water // PlatformSpecificParams defines parameters in Config that are specific to -// Linux and macOS. A zero-value of such type is valid, yielding an interface +// Linux. A zero-value of such type is valid, yielding an interface // with OS defined name. type PlatformSpecificParams struct { // Name is the name to be set for the interface to be created. This overrides diff --git a/syscalls_darwin.go b/syscalls_darwin.go index 6a473f8..c998557 100644 --- a/syscalls_darwin.go +++ b/syscalls_darwin.go @@ -61,7 +61,7 @@ type sockaddrCtl struct { var sockaddrCtlSize uintptr = 32 -func newTUN(string) (ifce *Interface, err error) { +func newTUN(config Config) (ifce *Interface, err error) { var fd int // Supposed to be socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), but ... // @@ -122,6 +122,10 @@ func newTUN(string) (ifce *Interface, err error) { }, nil } +func newTAP(config Config) (ifce *Interface, err error) { + return nil, errors.New("tap interface not implemented on this platform") +} + // tunReadCloser is a hack to work around the first 4 bytes "packet // information" because there doesn't seem to be an IFF_NO_PI for darwin. type tunReadCloser struct { @@ -189,7 +193,3 @@ func (t *tunReadCloser) Close() error { return t.f.Close() } - -func newTAP(ifName string) (ifce *Interface, err error) { - return nil, errors.New("tap interface not implemented on this platform") -} diff --git a/syscalls_linux.go b/syscalls_linux.go index e1ba63f..420a645 100644 --- a/syscalls_linux.go +++ b/syscalls_linux.go @@ -21,12 +21,12 @@ type ifReq struct { pad [0x28 - 0x10 - 2]byte } -func newTAP(ifName string) (ifce *Interface, err error) { +func newTAP(config Config) (ifce *Interface, err error) { file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) if err != nil { return nil, err } - name, err := createInterface(file.Fd(), ifName, cIFF_TAP|cIFF_NO_PI) + name, err := createInterface(file.Fd(), config.Name, cIFF_TAP|cIFF_NO_PI) if err != nil { return nil, err } @@ -34,12 +34,12 @@ func newTAP(ifName string) (ifce *Interface, err error) { return } -func newTUN(ifName string) (ifce *Interface, err error) { +func newTUN(config Config) (ifce *Interface, err error) { file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0) if err != nil { return nil, err } - name, err := createInterface(file.Fd(), ifName, cIFF_TUN|cIFF_NO_PI) + name, err := createInterface(file.Fd(), config.Name, cIFF_TUN|cIFF_NO_PI) if err != nil { return nil, err } diff --git a/syscalls_other.go b/syscalls_other.go index ab3b9e5..ed49244 100644 --- a/syscalls_other.go +++ b/syscalls_other.go @@ -4,10 +4,10 @@ package water import "errors" -func newTAP(ifName string) (ifce *Interface, err error) { +func newTAP(config Config) (ifce *Interface, err error) { return nil, errors.New("tap interface not implemented on this platform") } -func newTUN(ifName string) (ifce *Interface, err error) { +func newTUN(config Config) (ifce *Interface, err error) { return nil, errors.New("tap interface not implemented on this platform") } diff --git a/syscalls_windows.go b/syscalls_windows.go index 5dc4013..a06c572 100644 --- a/syscalls_windows.go +++ b/syscalls_windows.go @@ -289,14 +289,10 @@ func openDev(config Config) (ifce *Interface, err error) { return nil, errIfceNameNotFound } -func newTAP(ifName string) (ifce *Interface, err error) { - config := defaultConfig() - config.DeviceType = TAP +func newTAP(config Config) (ifce *Interface, err error) { return openDev(config) } -func newTUN(ifName string) (ifce *Interface, err error) { - config := defaultConfig() - config.DeviceType = TUN +func newTUN(config Config) (ifce *Interface, err error) { return openDev(config) }