forked from songgao/water
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syscalls_linux.go
61 lines (54 loc) · 1.25 KB
/
syscalls_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
54
55
56
57
58
59
60
61
// +build linux
package water
import (
"os"
"strings"
"syscall"
"unsafe"
)
const (
cIFF_TUN = 0x0001
cIFF_TAP = 0x0002
cIFF_NO_PI = 0x1000
)
type ifReq struct {
Name [0x10]byte
Flags uint16
pad [0x28 - 0x10 - 2]byte
}
func newTAP(ifName string) (ifce *Interface, err error) {
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return nil, err
}
name, err := createInterface(file.Fd(), ifName, cIFF_TAP|cIFF_NO_PI)
if err != nil {
return nil, err
}
ifce = &Interface{isTAP: true, file: file, name: name}
return
}
func newTUN(ifName string) (ifce *Interface, err error) {
file, err := os.OpenFile("/dev/net/tun", os.O_RDWR, 0)
if err != nil {
return nil, err
}
name, err := createInterface(file.Fd(), ifName, cIFF_TUN|cIFF_NO_PI)
if err != nil {
return nil, err
}
ifce = &Interface{isTAP: false, file: file, name: name}
return
}
func createInterface(fd uintptr, ifName string, flags uint16) (createdIFName string, err error) {
var req ifReq
req.Flags = flags
copy(req.Name[:], ifName)
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(syscall.TUNSETIFF), uintptr(unsafe.Pointer(&req)))
if errno != 0 {
err = errno
return
}
createdIFName = strings.Trim(string(req.Name[:]), "\x00")
return
}