This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
linux_tun.go
320 lines (266 loc) · 6.6 KB
/
linux_tun.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package secretun
// +build linux
/*
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <linux/if_tun.h>
int create_tun(int fd, char *name) {
int err;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
if (name && name[0]) {
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
}
ifr.ifr_flags = IFF_NO_PI | IFF_TUN;
if ((err = ioctl(fd, TUNSETIFF, (void*)&ifr)) < 0) {
return err;
}
if (name) {
strcpy(name, ifr.ifr_name);
}
return 0;
}
int if_ioctl(int cmd, struct ifreq *ifr) {
int ret;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
return -1;
}
ret = ioctl(sock, cmd, (void*)ifr);
close(sock);
return ret;
}
int if_setaddr(int cmd, const char *name, const char *ip) {
int err;
struct ifreq ifr;
struct sockaddr_in *addr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, name);
addr = (struct sockaddr_in*)&ifr.ifr_ifru.ifru_dstaddr;
addr->sin_family = AF_INET;
if ((err = inet_aton(ip, &addr->sin_addr)) < 0) {
return err;
}
return if_ioctl(cmd, &ifr);
}
int if_getaddr(int cmd, const char *name, char **addr) {
int err;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, name);
if ((err = if_ioctl(cmd, &ifr)) < 0) {
return err;
}
*addr = inet_ntoa(((struct sockaddr_in*)&ifr.ifr_ifru.ifru_addr)->sin_addr);
return 0;
}
int if_setmtu(const char *name, int mtu) {
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, name);
ifr.ifr_ifru.ifru_mtu = mtu;
return if_ioctl(SIOCSIFMTU, &ifr);
}
int if_getmtu(const char *name) {
int err;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, name);
if ((err = if_ioctl(SIOCGIFMTU, &ifr)) < 0) {
return err;
}
return ifr.ifr_ifru.ifru_mtu;
}
int if_up(const char *name) {
int err;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, name);
if ((err = if_ioctl(SIOCGIFFLAGS, &ifr)) < 0) {
return err;
}
if (!(ifr.ifr_flags & IFF_UP)) {
ifr.ifr_flags |= IFF_UP;
return if_ioctl(SIOCSIFFLAGS, &ifr) < 0;
}
return 0;
}
int if_down(const char *name) {
int err;
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
strcpy(ifr.ifr_name, name);
if ((err = if_ioctl(SIOCGIFFLAGS, &ifr)) < 0) {
return err;
}
if (ifr.ifr_flags & IFF_UP) {
ifr.ifr_flags &= ~IFF_UP;
return if_ioctl(SIOCSIFFLAGS, &ifr) < 0;
}
return 0;
}
*/
import "C"
import (
"fmt"
"log"
"net"
"os"
"unsafe"
)
const TUN_DEV = "/dev/net/tun"
type Tun struct {
Name string
file *os.File
}
func CreateTun(name string) (*Tun, error) {
f, err := os.OpenFile(TUN_DEV, os.O_RDWR, 0)
if err != nil {
return nil, err
}
cbuf := (*C.char)(C.malloc(C.IFNAMSIZ))
defer C.free(unsafe.Pointer(cbuf))
C.memset(unsafe.Pointer(cbuf), 0, C.IFNAMSIZ)
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
C.strncpy(cbuf, c_name, C.IFNAMSIZ-1)
if C.create_tun(C.int(f.Fd()), cbuf) < 0 {
return nil, fmt.Errorf("create tun fail")
}
return &Tun{C.GoString(cbuf), f}, nil
}
func (t *Tun) Close() error {
t.Down()
return t.file.Close()
}
func (t *Tun) SetSelfAddr(ip net.IP) error {
c_name, c_addr := C.CString(t.Name), C.CString(ip.String())
defer C.free(unsafe.Pointer(c_name))
defer C.free(unsafe.Pointer(c_addr))
if C.if_setaddr(C.SIOCSIFADDR, c_name, c_addr) < 0 {
return fmt.Errorf("set addr fail")
}
return nil
}
func (t *Tun) GetSelfAddr() (ip net.IP, err error) {
var c_addr *C.char
c_name := C.CString(t.Name)
defer C.free(unsafe.Pointer(c_name))
if C.if_getaddr(C.SIOCGIFADDR, c_name, &c_addr) < 0 {
err = fmt.Errorf("get self addr fail")
return
}
return net.ParseIP(C.GoString(c_addr)), nil
}
func (t *Tun) SetDestAddr(ip net.IP) error {
c_name, c_addr := C.CString(t.Name), C.CString(ip.String())
defer C.free(unsafe.Pointer(c_name))
defer C.free(unsafe.Pointer(c_addr))
if C.if_setaddr(C.SIOCSIFDSTADDR, c_name, c_addr) < 0 {
return fmt.Errorf("set dest addr fail")
}
return nil
}
func (t *Tun) GetDestAddr() (ip net.IP, err error) {
var c_addr *C.char
c_name := C.CString(t.Name)
defer C.free(unsafe.Pointer(c_name))
if C.if_getaddr(C.SIOCGIFDSTADDR, c_name, &c_addr) < 0 {
err = fmt.Errorf("get dest addr fail")
return
}
return net.ParseIP(C.GoString(c_addr)), nil
}
func (t *Tun) SetAddr(self net.IP, dest net.IP) error {
if err := t.SetSelfAddr(self); err != nil {
return err
}
return t.SetDestAddr(dest)
}
func (t *Tun) SetNetmask(mask net.IPMask) error {
c_name, c_mask := C.CString(t.Name), C.CString(net.IP(mask).String())
defer C.free(unsafe.Pointer(c_name))
defer C.free(unsafe.Pointer(c_mask))
if C.if_setaddr(C.SIOCSIFNETMASK, c_name, c_mask) < 0 {
return fmt.Errorf("set netmask fail")
}
return nil
}
func (t *Tun) GetNetmask() (mask net.IPMask, err error) {
var c_addr *C.char
c_name := C.CString(t.Name)
defer C.free(unsafe.Pointer(c_name))
if C.if_getaddr(C.SIOCGIFNETMASK, c_name, &c_addr) < 0 {
err = fmt.Errorf("get netmask fail")
return
}
return net.IPMask(net.ParseIP(C.GoString(c_addr))), nil
}
func (t *Tun) SetMTU(mtu int) error {
c_name := C.CString(t.Name)
defer C.free(unsafe.Pointer(c_name))
if C.if_setmtu(c_name, C.int(mtu)) < 0 {
return fmt.Errorf("set mtu fail")
}
return nil
}
func (t *Tun) GetMTU() (int, error) {
c_name := C.CString(t.Name)
defer C.free(unsafe.Pointer(c_name))
if mtu := int(C.if_getmtu(c_name)); mtu < 0 {
return 0, fmt.Errorf("get mtu fail")
} else {
return mtu, nil
}
return 0, nil
}
func (t *Tun) Up() error {
c_name := C.CString(t.Name)
defer C.free(unsafe.Pointer(c_name))
if C.if_up(c_name) < 0 {
return fmt.Errorf("if up fail")
}
return nil
}
func (t *Tun) Down() error {
c_name := C.CString(t.Name)
defer C.free(unsafe.Pointer(c_name))
if C.if_down(c_name) < 0 {
return fmt.Errorf("if down fail")
}
return nil
}
func (t *Tun) Read(p []byte) (int, error) {
return t.file.Read(p)
}
func (t *Tun) Write(p []byte) (n int, err error) {
return t.file.Write(p)
}
func (t *Tun) ReadChan() (chan []byte, error) {
ch := make(chan []byte)
mtu, err := t.GetMTU()
if err != nil {
return nil, err
}
buf := make([]byte, mtu*2)
go func() {
defer close(ch)
for {
if n, err := t.Read(buf); err != nil {
log.Println("read tun fail:", err)
return
} else {
snd := make([]byte, n)
copy(snd, buf[:n])
ch <- snd
}
}
}()
return ch, err
}