Skip to content

Commit a31ac06

Browse files
committed
Initial FreeBSD/OpenBSD/NetBSD support (doesn't have syscalls for interface config yet)
1 parent 8567d15 commit a31ac06

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

params_bsd.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// +build openbsd freebsd netbsd
2+
3+
package water
4+
5+
// PlatformSpecificParams defines parameters in Config that are specific to
6+
// Linux. A zero-value of such type is valid, yielding an interface
7+
// with OS defined name.
8+
type PlatformSpecificParams struct {
9+
// Name is the name to be set for the interface to be created. This overrides
10+
// the default name assigned by OS such as tap0 or tun0. A zero-value of this
11+
// field, i.e. an empty string, indicates that the default name should be
12+
// used.
13+
Name string
14+
}
15+
16+
func defaultPlatformSpecificParams() PlatformSpecificParams {
17+
return PlatformSpecificParams{
18+
Name: "/dev/tap0",
19+
}
20+
}

params_others.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !linux,!darwin,!windows
1+
// +build !linux,!darwin,!windows,!freebsd,!netbsd,!openbsd
22

33
package water
44

syscalls_bsd.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// +build openbsd freebsd netbsd
2+
3+
package water
4+
5+
import (
6+
"errors"
7+
"os"
8+
)
9+
10+
func openDev(config Config) (ifce *Interface, err error) {
11+
switch config.Name[:8] {
12+
case "/dev/tap":
13+
return newTAP(config)
14+
case "/dev/tun":
15+
return newTUN(config)
16+
default:
17+
return nil, errors.New("unrecognized driver")
18+
}
19+
}
20+
21+
func newTAP(config Config) (ifce *Interface, err error) {
22+
if config.Name[:8] != "/dev/tap" {
23+
panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
24+
}
25+
26+
file, err := os.OpenFile(config.Name, os.O_RDWR, 0)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
ifce = &Interface{isTAP: true, ReadWriteCloser: file, name: config.Name[5:]}
32+
return
33+
}
34+
35+
func newTUN(config Config) (ifce *Interface, err error) {
36+
if config.Name[:8] != "/dev/tun" {
37+
panic("TUN/TAP name must be in format /dev/tunX or /dev/tapX")
38+
}
39+
40+
file, err := os.OpenFile(config.Name, os.O_RDWR, 0)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
ifce = &Interface{isTAP: false, ReadWriteCloser: file, name: config.Name[5:]}
46+
return
47+
}

syscalls_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// +build !linux,!darwin,!windows
1+
// +build !linux,!darwin,!windows,!freebsd,!netbsd,!openbsd
22

33
package water
44

0 commit comments

Comments
 (0)