mirror of
https://github.com/yggdrasil-network/water.git
synced 2025-05-19 16:35:10 +03:00
add new interface for init configuration (#15)
This commit is contained in:
parent
98078a8ac5
commit
37d6645a51
5 changed files with 109 additions and 0 deletions
46
if.go
46
if.go
|
@ -9,15 +9,61 @@ type Interface struct {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeviceType is the type for specifying device types.
|
||||||
|
type DeviceType int
|
||||||
|
|
||||||
|
// TUN and TAP device types.
|
||||||
|
const (
|
||||||
|
_ = iota
|
||||||
|
TUN
|
||||||
|
TAP
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config defines parameters required to create a TUN/TAP interface. It's only
|
||||||
|
// used when the device is initialized. A zero-value Config is a valid
|
||||||
|
// configuration.
|
||||||
|
type Config struct {
|
||||||
|
// DeviceType specifies whether the device is a TUN or TAP interface. A
|
||||||
|
// zero-value is treated as TUN.
|
||||||
|
DeviceType DeviceType
|
||||||
|
|
||||||
|
// PlatformSpecificParams defines parameters that differ on different
|
||||||
|
// platforms. See comments for the type for more details.
|
||||||
|
PlatformSpecificParams
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultConfig() Config {
|
||||||
|
return Config{
|
||||||
|
DeviceType: TUN,
|
||||||
|
PlatformSpecificParams: defaultPlatformSpecificParams(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var zeroConfig Config
|
||||||
|
|
||||||
|
// New creates a new TUN/TAP interface using config.
|
||||||
|
func New(config Config) (ifce *Interface, err error) {
|
||||||
|
if zeroConfig == config {
|
||||||
|
config = defaultConfig()
|
||||||
|
}
|
||||||
|
return newDev(config)
|
||||||
|
}
|
||||||
|
|
||||||
// NewTAP creates a new TAP interface whose name is ifName. If ifName is empty, a
|
// NewTAP creates a new TAP interface whose name is ifName. If ifName is empty, a
|
||||||
// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed
|
// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed
|
||||||
// 16 bytes. TAP interfaces are not supported on darwin.
|
// 16 bytes. TAP interfaces are not supported on darwin.
|
||||||
|
//
|
||||||
|
// Note: this function is deprecated and will be removed from the library.
|
||||||
|
// Please use New() instead.
|
||||||
func NewTAP(ifName string) (ifce *Interface, err error) {
|
func NewTAP(ifName string) (ifce *Interface, err error) {
|
||||||
return newTAP(ifName)
|
return newTAP(ifName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTUN creates a new TUN interface whose name is ifName. If ifName is empty, a
|
// NewTUN creates a new TUN interface whose name is ifName. If ifName is empty, a
|
||||||
// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed
|
// default name (tap0, tap1, ... ) will be assigned. ifName should not exceed
|
||||||
|
//
|
||||||
|
// Note: this function is deprecated and will be removed from the library.
|
||||||
|
// Please use New() instead.
|
||||||
// 16 bytes. Setting interface name is NOT supported on darwin.
|
// 16 bytes. Setting interface name is NOT supported on darwin.
|
||||||
func NewTUN(ifName string) (ifce *Interface, err error) {
|
func NewTUN(ifName string) (ifce *Interface, err error) {
|
||||||
return newTUN(ifName)
|
return newTUN(ifName)
|
||||||
|
|
16
if_unix.go
Normal file
16
if_unix.go
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
// +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")
|
||||||
|
}
|
||||||
|
}
|
11
params_others.go
Normal file
11
params_others.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
// +build !linux,!darwin,!windows
|
||||||
|
|
||||||
|
package water
|
||||||
|
|
||||||
|
// PlatformSpeficParams
|
||||||
|
type PlatformSpecificParams struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultPlatformSpecificParams() PlatformSpecificParams {
|
||||||
|
return PlatformSpecificParams{}
|
||||||
|
}
|
18
params_unix.go
Normal file
18
params_unix.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
// +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
|
||||||
|
// 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 emapty string, indicates that the default name should be
|
||||||
|
// used.
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultPlatformSpecificParams() PlatformSpecificParams {
|
||||||
|
return PlatformSpecificParams{}
|
||||||
|
}
|
18
params_windows.go
Normal file
18
params_windows.go
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
package water
|
||||||
|
|
||||||
|
// PlatformSpecificParams defines parameters in Config that are specific to
|
||||||
|
// Windows. A zero-value of such type is valid.
|
||||||
|
type PlatformSpecificParams struct {
|
||||||
|
// ComponentID associates with the virtual adapter that exists in Windows.
|
||||||
|
// This is usually configured when driver for the adapter is installed. A
|
||||||
|
// zero-value of this field, i.e., an empty string, causes the interface to
|
||||||
|
// use the default ComponentId. The default ComponentId is set to tap0901,
|
||||||
|
// the one used by OpenVPN.
|
||||||
|
ComponentID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultPlatformSpecificParams() PlatformSpecificParams {
|
||||||
|
return PlatformSpecificParams{
|
||||||
|
ComponentId: "tap0901",
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue