Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add early FreeBSD, NetBSD, OpenBSD support #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Linux/BSD

// 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")
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these fields need to be exported?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it seems lines above this are not used at all. I was imagining you needed to make some syscalls to setup the tun/tap device. In your use case, do you call external commands to set them up? I think if that's the preferred use case, you wouldn't need this package at all. You can just use os.OpenFile directly.

I see there the second commit removed a ioctl function. Did you attempt to implement it first and removed it? I think it'd be nice to have syscalls handled in Go!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do appear to have left this definition in by accident, as this would be handled elsewhere in my code (with the help of FD() above).

I did attempt to implement an ioctl function although then realised that other platforms do not necessarily have this implemented either, hence removal.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then realised that other platforms do not necessarily have this implemented either, hence removal.

I think all three platforms that this package has support for already handle syscalls properly. They might be structured in different ways because of different underlying designs of the OS APIs, but the goal is a user of the package doesn't need to make syscalls before or after creating the interface using this package.

I imagine the OpenBSD version could be similar to syscalls_darwin.go and syscalls_linux.go.


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