Simplifies Platform Specific Interface Creation

The interface `Config` object is now passed directly into each creation
function in order to more easily use additional options.

In addition, the linux and darwin parameters were split to better
capture different options for each platform.
This commit is contained in:
Matthew Ellison 2017-05-22 12:25:48 -04:00
parent 80f6655041
commit 70591d2499
No known key found for this signature in database
GPG key ID: A815A44BDC8DD409
9 changed files with 38 additions and 48 deletions

12
if.go
View file

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

View file

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

View file

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

13
params_darwin.go Normal file
View file

@ -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{}
}

View file

@ -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

View file

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

View file

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

View file

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

View file

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