-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtap.go
160 lines (141 loc) · 3.08 KB
/
tap.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package goTunTap
/*
#include"tapUtils.h"
*/
import "C"
import (
"os"
"errors"
"syscall"
"sync"
"io"
)
type TapManager struct {
sock C.int
name string
up bool
}
func CheckBridge(bridge string) error {
if(C.CheckBridge(C.CString(bridge)) < 0) {
return errors.New("Bridge does not exist")
}
return nil
}
func CreateBridge(name string) error {
if CheckBridge(name) == nil {
return nil
}
if(C.CreateBridge(C.CString(name)) < 0) {
return errors.New("Failed to create bridge")
}
return nil
}
func DeleteBridge(name string) error {
if(C.DeleteBridge(C.CString(name)) < 0) {
return errors.New("Failed to delete bridge")
}
return nil
}
func AddTapToBridge(bridge, tap string) error {
if(C.AddTapToBridge(C.CString(bridge), C.CString(tap)) < 0) {
return errors.New("Failed to add tap to bridge")
}
return nil
}
func RemoveTapFromBridge(bridge, tap string) error {
if(C.RemoveTapFromBridge(C.CString(bridge), C.CString(tap)) < 0) {
return errors.New("Failed to remove tap from bridge")
}
return nil
}
func CreateTap(name string) (*TapManager, error) {
tap := &TapManager{0, name, false};
if os.Geteuid() != 0 {
return nil, errors.New("Must execute as ROOT")
}
return tap, tap.Start()
}
func (t *TapManager) Start() error {
sock := C.StartTap(C.CString(t.name))
if sock <= 0 {
return errors.New("Failed to create tap")
}
t.sock = sock
return nil
}
func (t *TapManager) Stop() error {
if C.StopTap(t.sock, C.CString(t.name)) != 0 {
return errors.New("Failed to destroy tap")
}
return nil
}
func (t *TapManager) Read(b []byte) (int, error) {
var n int
var err error
if t == nil {
return 0, os.ErrInvalid
}
if len(b) > 0 {
n, err = syscall.Read(int(t.sock), b)
if n == 0 && err == nil {
return n, io.EOF
}
if err != nil {
return 0, err
}
}
return n, err
}
func (t *TapManager) Write(b []byte) (int, error) {
var n int
var err error
if t == nil {
return 0, os.ErrInvalid
}
if len(b) > 0 {
n, err = syscall.Write(int(t.sock), b)
if n == 0 && err == nil {
return n, io.EOF
}
if err != nil {
return 0, err
}
}
return n, err
}
//Relay relays data from a reader/Writer. Like a net.Conn
//to and from the tap
func (t* TapManager) Relay(c io.ReadWriter) error {
wg := sync.WaitGroup{}
wg.Add(2)
//relay from outside conn to the tap
go func(rdr io.Reader, wtr io.Writer, wg *sync.WaitGroup) {
defer wg.Done()
io.Copy(wtr, rdr)
}(c, t, &wg)
//relay from tap to outside
go func(rdr io.Reader, wtr io.Writer, wg *sync.WaitGroup) {
defer wg.Done()
io.Copy(wtr, rdr)
}(t, c, &wg)
wg.Wait()
return nil
}
func (t* TapManager) AddToBridge(bridge string) error {
err := CheckBridge(bridge)
if err != nil {
if C.CreateBridge(C.CString(bridge)) != 0 {
return errors.New("Failed to create bridge")
}
}
if C.AddTapToBridge(C.CString(bridge), C.CString(t.name)) != 0 {
return errors.New("Failed to add tap to bridge")
}
return nil
}
func (t* TapManager) RemoveFromBridge(bridge string) error {
if C.RemoveTapFromBridge(C.CString(bridge), C.CString(t.name)) != 0 {
return errors.New("Failed to add tap to bridge")
}
return nil
}