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) }