mirror of
https://github.com/yggdrasil-network/water.git
synced 2025-05-20 00:45:09 +03:00
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:
parent
80f6655041
commit
70591d2499
9 changed files with 38 additions and 48 deletions
12
if.go
12
if.go
|
@ -2,6 +2,7 @@ package water
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Interface is a TUN/TAP interface.
|
// Interface is a TUN/TAP interface.
|
||||||
|
@ -48,7 +49,14 @@ func New(config Config) (ifce *Interface, err error) {
|
||||||
if zeroConfig == config {
|
if zeroConfig == config {
|
||||||
config = defaultConfig()
|
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.
|
// IsTUN returns true if ifce is a TUN interface.
|
||||||
|
@ -65,3 +73,5 @@ func (ifce *Interface) IsTAP() bool {
|
||||||
func (ifce *Interface) Name() string {
|
func (ifce *Interface) Name() string {
|
||||||
return ifce.name
|
return ifce.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
16
if_unix.go
16
if_unix.go
|
@ -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")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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
13
params_darwin.go
Normal 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{}
|
||||||
|
}
|
|
@ -1,9 +1,8 @@
|
||||||
// +build linux darwin
|
|
||||||
|
|
||||||
package water
|
package water
|
||||||
|
|
||||||
// PlatformSpecificParams defines parameters in Config that are specific to
|
// 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.
|
// with OS defined name.
|
||||||
type PlatformSpecificParams struct {
|
type PlatformSpecificParams struct {
|
||||||
// Name is the name to be set for the interface to be created. This overrides
|
// Name is the name to be set for the interface to be created. This overrides
|
|
@ -61,7 +61,7 @@ type sockaddrCtl struct {
|
||||||
|
|
||||||
var sockaddrCtlSize uintptr = 32
|
var sockaddrCtlSize uintptr = 32
|
||||||
|
|
||||||
func newTUN(string) (ifce *Interface, err error) {
|
func newTUN(config Config) (ifce *Interface, err error) {
|
||||||
var fd int
|
var fd int
|
||||||
// Supposed to be socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), but ...
|
// Supposed to be socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL), but ...
|
||||||
//
|
//
|
||||||
|
@ -122,6 +122,10 @@ func newTUN(string) (ifce *Interface, err error) {
|
||||||
}, nil
|
}, 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
|
// 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.
|
// information" because there doesn't seem to be an IFF_NO_PI for darwin.
|
||||||
type tunReadCloser struct {
|
type tunReadCloser struct {
|
||||||
|
@ -189,7 +193,3 @@ func (t *tunReadCloser) Close() error {
|
||||||
|
|
||||||
return t.f.Close()
|
return t.f.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTAP(ifName string) (ifce *Interface, err error) {
|
|
||||||
return nil, errors.New("tap interface not implemented on this platform")
|
|
||||||
}
|
|
||||||
|
|
|
@ -21,12 +21,12 @@ type ifReq struct {
|
||||||
pad [0x28 - 0x10 - 2]byte
|
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)
|
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -34,12 +34,12 @@ func newTAP(ifName string) (ifce *Interface, err error) {
|
||||||
return
|
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)
|
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,10 +4,10 @@ package water
|
||||||
|
|
||||||
import "errors"
|
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")
|
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")
|
return nil, errors.New("tap interface not implemented on this platform")
|
||||||
}
|
}
|
||||||
|
|
|
@ -289,14 +289,10 @@ func openDev(config Config) (ifce *Interface, err error) {
|
||||||
return nil, errIfceNameNotFound
|
return nil, errIfceNameNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTAP(ifName string) (ifce *Interface, err error) {
|
func newTAP(config Config) (ifce *Interface, err error) {
|
||||||
config := defaultConfig()
|
|
||||||
config.DeviceType = TAP
|
|
||||||
return openDev(config)
|
return openDev(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTUN(ifName string) (ifce *Interface, err error) {
|
func newTUN(config Config) (ifce *Interface, err error) {
|
||||||
config := defaultConfig()
|
|
||||||
config.DeviceType = TUN
|
|
||||||
return openDev(config)
|
return openDev(config)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue