Skip to content

Commit

Permalink
Initial FreeBSD/OpenBSD/NetBSD support (doesn't have syscalls for int…
Browse files Browse the repository at this point in the history
…erface config yet)
  • Loading branch information
neilalexander committed Jul 20, 2019
1 parent 8567d15 commit a31ac06
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
20 changes: 20 additions & 0 deletions params_bsd.go
Original file line number Diff line number Diff line change
@@ -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",
}
}
2 changes: 1 addition & 1 deletion params_others.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !linux,!darwin,!windows
// +build !linux,!darwin,!windows,!freebsd,!netbsd,!openbsd

package water

Expand Down
47 changes: 47 additions & 0 deletions syscalls_bsd.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion syscalls_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !linux,!darwin,!windows
// +build !linux,!darwin,!windows,!freebsd,!netbsd,!openbsd

package water

Expand Down

0 comments on commit a31ac06

Please sign in to comment.