-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtap_device.go
More file actions
68 lines (57 loc) · 1.48 KB
/
tap_device.go
File metadata and controls
68 lines (57 loc) · 1.48 KB
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
package l2
import (
"github.com/AutoRoute/tuntap"
"log"
"os/exec"
)
type tapDevice struct {
dev *tuntap.Interface
}
// A Tap Device is a new networking device that this program has created. In this case
// the normal semantics are inverted, in that frames sent to the device
// are what this interface will read and vice versa. Note that this device must be closed
// when you are done using it.
func NewTapDevice(mac, dev string) (FrameReadWriteCloser, error) {
fd, err := tuntap.Open(dev, tuntap.DevTap)
if err != nil {
return nil, err
}
ip_path, err := exec.LookPath("ip")
if err != nil {
return nil, err
}
if mac != "" {
cmd := exec.Command(ip_path, "link", "set", "dev", dev, "address", mac)
output, err := cmd.CombinedOutput()
if err != nil {
log.Print("Command output:", string(output))
return nil, err
}
}
cmd := exec.Command(ip_path, "link", "set", "dev", dev, "up")
output, err := cmd.CombinedOutput()
if err != nil {
log.Print("Command output:", string(output))
return nil, err
}
return &tapDevice{fd}, nil
}
func (t *tapDevice) String() string {
return "TapDevice{" + t.dev.Name() + "}"
}
func (t *tapDevice) Close() error {
return t.dev.Close()
}
func (t *tapDevice) ReadFrame() (EthFrame, error) {
p, err := t.dev.ReadPacket()
if err != nil {
return nil, err
}
return p.Packet, nil
}
func (t *tapDevice) WriteFrame(data EthFrame) error {
return t.dev.WritePacket(
&tuntap.Packet{
Protocol: int(EthFrame(data).Type()),
Packet: data})
}