forked from getlantern/tunio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure.go
More file actions
88 lines (69 loc) · 1.56 KB
/
configure.go
File metadata and controls
88 lines (69 loc) · 1.56 KB
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
package tunio
import (
"errors"
"math/rand"
"net"
"sync"
"time"
"unsafe"
)
/*
#include "tun2io.h"
#include "tun2io.c"
*/
import "C"
type dialer func(proto, addr string) (net.Conn, error)
var (
debug = true
)
var (
errBufferIsFull = errors.New("Buffer is full.")
)
const (
readBufSize = 1024 * 64
)
var ioTimeout = time.Second * 30
var (
tunnels map[uint32]*TunIO
tunnelMu sync.Mutex
)
func init() {
tunnels = make(map[uint32]*TunIO)
//rand.Seed(time.Now().UnixNano())
rand.Seed(1)
}
var Dialer dialer
func dummyDialer(proto, addr string) (net.Conn, error) {
return net.Dial(proto, addr)
}
type Status uint
const (
StatusNew Status = iota // 0
StatusConnecting // 1
StatusConnectionFailed // 2
StatusConnected // 3
StatusReady // 4
StatusProxying // 5
StatusClosing // 6
StatusClosed // 7
)
// Configure sets up the tundevice, this is equivalent to the badvpn-tun2socks
// configuration, except for the --socks-server-addr.
func Configure(tundev, ipaddr, netmask string, d dialer) error {
if d == nil {
d = dummyDialer
}
Dialer = d
ctundev := C.CString(tundev)
cipaddr := C.CString(ipaddr)
cnetmask := C.CString(netmask)
defer func() {
C.free(unsafe.Pointer(ctundev))
C.free(unsafe.Pointer(cipaddr))
C.free(unsafe.Pointer(cnetmask))
}()
if err_t := C.configure(ctundev, cipaddr, cnetmask); err_t != C.ERR_OK {
return errors.New("Failed to configure device.")
}
return nil
}