From a31ac06cb2c2f51e2a5e404f06032b702d8d1ebe Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 20 Jul 2019 10:59:45 +0100 Subject: [PATCH] Initial FreeBSD/OpenBSD/NetBSD support (doesn't have syscalls for interface config yet) --- params_bsd.go | 20 ++++++++++++++++++++ params_others.go | 2 +- syscalls_bsd.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ syscalls_other.go | 2 +- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 params_bsd.go create mode 100644 syscalls_bsd.go diff --git a/params_bsd.go b/params_bsd.go new file mode 100644 index 0000000..aca5cee --- /dev/null +++ b/params_bsd.go @@ -0,0 +1,20 @@ +// +build openbsd freebsd netbsd + +package water + +// PlatformSpecificParams defines parameters in Config that are specific to +// 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 + // the default name assigned by OS such as tap0 or tun0. A zero-value of this + // field, i.e. an empty string, indicates that the default name should be + // used. + Name string +} + +func defaultPlatformSpecificParams() PlatformSpecificParams { + return PlatformSpecificParams{ + Name: "/dev/tap0", + } +} diff --git a/params_others.go b/params_others.go index c5c4f6e..574aeb2 100644 --- a/params_others.go +++ b/params_others.go @@ -1,4 +1,4 @@ -// +build !linux,!darwin,!windows +// +build !linux,!darwin,!windows,!freebsd,!netbsd,!openbsd package water diff --git a/syscalls_bsd.go b/syscalls_bsd.go new file mode 100644 index 0000000..70af690 --- /dev/null +++ b/syscalls_bsd.go @@ -0,0 +1,47 @@ +// +build openbsd freebsd netbsd + +package water + +import ( + "errors" + "os" +) + +func openDev(config Config) (ifce *Interface, err error) { + switch config.Name[:8] { + case "/dev/tap": + return newTAP(config) + case "/dev/tun": + return newTUN(config) + default: + return nil, errors.New("unrecognized driver") + } +} + +func newTAP(config Config) (ifce *Interface, err error) { + if config.Name[:8] != "/dev/tap" { + panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX") + } + + file, err := os.OpenFile(config.Name, os.O_RDWR, 0) + if err != nil { + return nil, err + } + + ifce = &Interface{isTAP: true, ReadWriteCloser: file, name: config.Name[5:]} + return +} + +func newTUN(config Config) (ifce *Interface, err error) { + if config.Name[:8] != "/dev/tun" { + panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX") + } + + file, err := os.OpenFile(config.Name, os.O_RDWR, 0) + if err != nil { + return nil, err + } + + ifce = &Interface{isTAP: false, ReadWriteCloser: file, name: config.Name[5:]} + return +} diff --git a/syscalls_other.go b/syscalls_other.go index c5ed1b3..536c388 100644 --- a/syscalls_other.go +++ b/syscalls_other.go @@ -1,4 +1,4 @@ -// +build !linux,!darwin,!windows +// +build !linux,!darwin,!windows,!freebsd,!netbsd,!openbsd package water