forked from hsheth2/water
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyscalls_darwin.go
80 lines (66 loc) · 1.97 KB
/
syscalls_darwin.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
// +build darwin
package water
/*
#include <stdlib.h>
*/
import "C"
import (
"errors"
"os"
"syscall"
"unsafe"
"github.com/inercia/kernctl"
)
const utunControlName = "com.apple.net.utun_control"
const utunOptIfName = 2
var errorNotDeviceFound = errors.New("could not find valid tun/tap device")
// Create a new TAP interface whose name is ifName.
// If ifName is empty, a default name (tap0, tap1, ... ) will be assigned.
// ifName should not exceed 16 bytes.
func newTAP(ifName string) (ifce *Interface, err error) {
name, file, err := createInterface(ifName)
if err != nil {
return nil, err
}
ifce = &Interface{isTAP: true, ReadWriteCloser: file, name: name}
return
}
// Create a new TUN interface whose name is ifName.
// If ifName is empty, a default name (utun0, utun1, ... ) will be assigned.
// ifName should not exceed 16 bytes.
func newTUN(ifName string) (ifce *Interface, err error) {
name, file, err := createInterface(ifName)
if err != nil {
return nil, err
}
ifce = &Interface{isTAP: false, ReadWriteCloser: file, name: name}
return
}
func createInterface(ifName string) (createdIFName string, file *os.File, err error) {
file = nil
err = errorNotDeviceFound
var readBufLen C.int = 20
var readBuf = C.CString(" ")
defer C.free(unsafe.Pointer(readBuf))
for utunnum := 0; utunnum < 255; utunnum++ {
conn := kernctl.NewConnByName(utunControlName)
conn.UnitId = uint32(utunnum + 1)
conn.Connect()
_, _, gserr := syscall.Syscall6(syscall.SYS_GETSOCKOPT,
uintptr(conn.Fd),
uintptr(kernctl.SYSPROTO_CONTROL), uintptr(utunOptIfName),
uintptr(unsafe.Pointer(readBuf)), uintptr(unsafe.Pointer(&readBufLen)), 0)
if gserr != 0 {
continue
} else {
createdIFName = C.GoStringN(readBuf, C.int(readBufLen))
file = os.NewFile(uintptr(conn.Fd), createdIFName)
err = nil
break
}
}
return createdIFName, file, err
}
func setPersistent(fd uintptr, persistent bool) error {
return errors.New("setPersistent not defined on OS X")
}