forked from songgao/water
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathif.go
63 lines (52 loc) · 1.51 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
package water
import (
"os"
)
// Interface is a TUN/TAP interface.
type Interface struct {
isTAP bool
file *os.File
name string
}
// Create a new TAP interface whose name is ifName.
// If ifName is empty, a default name (tap0, tap1, ... ) will be assigned.
// ifName should not exceed 16 bytes.
func NewTAP(ifName string) (ifce *Interface, err error) {
return newTAP(ifName)
}
// Create a new TUN interface whose name is ifName.
// If ifName is empty, a default name (tap0, tap1, ... ) will be assigned.
// ifName should not exceed 16 bytes.
func NewTUN(ifName string) (ifce *Interface, err error) {
return newTUN(ifName)
}
// Sets the TUN/TAP device in persistent mode.
func (ifce *Interface) SetPersistent(persistent bool) error {
return setPersistent(ifce.file.Fd(), persistent)
}
// Returns true if ifce is a TUN interface, otherwise returns false;
func (ifce *Interface) IsTUN() bool {
return !ifce.isTAP
}
// Returns true if ifce is a TAP interface, otherwise returns false;
func (ifce *Interface) IsTAP() bool {
return ifce.isTAP
}
// Returns the interface name of ifce, e.g. tun0, tap1, etc..
func (ifce *Interface) Name() string {
return ifce.name
}
// Closes the TUN/TAP interface.
func (ifce *Interface) Close() error {
return ifce.file.Close()
}
// Implement io.Writer interface.
func (ifce *Interface) Write(p []byte) (n int, err error) {
n, err = ifce.file.Write(p)
return
}
// Implement io.Reader interface.
func (ifce *Interface) Read(p []byte) (n int, err error) {
n, err = ifce.file.Read(p)
return
}