diff options
Diffstat (limited to 'tuntap')
-rw-r--r-- | tuntap/tuntap.go | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/tuntap/tuntap.go b/tuntap/tuntap.go index 3dde53b..27fd2b1 100644 --- a/tuntap/tuntap.go +++ b/tuntap/tuntap.go @@ -32,6 +32,7 @@ package tuntap import ( "fmt" + "strings" extlib "github.com/lab11/go-tuntap/tuntap" ) @@ -78,3 +79,23 @@ func NewTapDevice(name string) (dev *Device, err error) { } return } + +func NewDevice(iftype, name string) (dev *Device, err error) { + if iftype == "" { + if strings.HasPrefix(name, "tun") { + return NewTunDevice(name) + } + if strings.HasPrefix(name, "tap") { + return NewTapDevice(name) + } + return nil, fmt.Errorf("no device type is set and the given interface name(%s) doesn't start with 'tun' or 'tap'", name) + } + + switch strings.ToLower(iftype) { + case "tun": + return NewTunDevice(name) + case "tap": + return NewTapDevice(name) + } + return nil, fmt.Errorf("invalid interface type: %s", iftype) +} |