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