-
Notifications
You must be signed in to change notification settings - Fork 8
/
if.go
81 lines (69 loc) · 1.96 KB
/
if.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package water
import (
"errors"
"io"
"reflect"
)
// Interface is a TUN/TAP interface.
//
// MultiQueue(Linux kernel > 3.8): With MultiQueue enabled, user should hold multiple
// interfaces to send/receive packet in parallel.
// Kernel document about MultiQueue: https://www.kernel.org/doc/Documentation/networking/tuntap.txt
type Interface struct {
isTAP bool
io.ReadWriteCloser
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 reflect.DeepEqual(config, zeroConfig) {
config = defaultConfig()
}
if reflect.DeepEqual(config.PlatformSpecificParams, zeroConfig.PlatformSpecificParams) {
config.PlatformSpecificParams = defaultPlatformSpecificParams()
}
switch config.DeviceType {
case TUN, TAP:
return openDev(config)
default:
return nil, errors.New("unknown device type")
}
}
// IsTUN returns true if ifce is a TUN interface.
func (ifce *Interface) IsTUN() bool {
return !ifce.isTAP
}
// IsTAP returns true if ifce is a TAP interface.
func (ifce *Interface) IsTAP() bool {
return ifce.isTAP
}
// Name returns the interface name of ifce, e.g. tun0, tap1, tun0, etc..
func (ifce *Interface) Name() string {
return ifce.name
}