-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtaptun_linux.go
53 lines (43 loc) · 1.13 KB
/
taptun_linux.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
package taptun
import (
"errors"
"os"
"syscall"
"unsafe"
)
type ifreq struct {
name [syscall.IFNAMSIZ]byte // c string
flags uint16 // c short
_pad [24 - unsafe.Sizeof(uint16(0))]byte
}
func createInterface(flags uint16, name string) (string, *os.File, error) {
// Last byte of name must be nil for C string, so name must be
// short enough to allow that
if len(name) > syscall.IFNAMSIZ-1 {
return "", nil, errors.New("device name too long")
}
f, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0600)
if err != nil {
return "", nil, err
}
var nbuf [syscall.IFNAMSIZ]byte
copy(nbuf[:], []byte(name))
fd := f.Fd()
ifr := ifreq{
name: nbuf,
flags: flags,
}
if err := ioctl(fd, syscall.TUNSETIFF, unsafe.Pointer(&ifr)); err != nil {
return "", nil, err
}
return cstringToGoString(ifr.name[:]), f, nil
}
func destroyInterface(name string) error {
return nil
}
func openTun(name string) (string, *os.File, error) {
return createInterface(syscall.IFF_TUN|syscall.IFF_NO_PI, name)
}
func openTap(name string) (string, *os.File, error) {
return createInterface(syscall.IFF_TAP|syscall.IFF_NO_PI, name)
}