-
Notifications
You must be signed in to change notification settings - Fork 127
/
syscalls_linux.go
64 lines (51 loc) · 1.18 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
62
63
64
//
// date : 2017-07-14
// author: xjdrew
//
package kone
import (
"fmt"
"net"
"os/exec"
"strings"
"github.com/songgao/water"
)
func execCommand(name, sargs string) error {
args := strings.Split(sargs, " ")
cmd := exec.Command(name, args...)
logger.Infof("exec command: %s %s", name, sargs)
return cmd.Run()
}
func initTun(tun string, ipNet *net.IPNet, mtu int) error {
sargs := fmt.Sprintf("addr add %s dev %s", ipNet, tun)
if err := execCommand("ip", sargs); err != nil {
return err
}
// brings the link up
sargs = fmt.Sprintf("link set dev %s up mtu %d qlen 1000", tun, mtu)
return execCommand("ip", sargs)
}
func addRoute(tun string, subnet *net.IPNet) error {
sargs := fmt.Sprintf("route add %s dev %s", subnet, tun)
return execCommand("ip", sargs)
}
func createTun(ip net.IP, mask net.IPMask) (*water.Interface, error) {
ifce, err := water.New(water.Config{
DeviceType: water.TUN,
})
if err != nil {
return nil, err
}
logger.Infof("create %s", ifce.Name())
ipNet := &net.IPNet{
IP: ip,
Mask: mask,
}
if err := initTun(ifce.Name(), ipNet, MTU); err != nil {
return nil, err
}
return ifce, nil
}
func fixTunIP(ip net.IP) net.IP {
return ip
}