Skip to content

Commit

Permalink
tap: make net parameter optional (ginuerzh#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginuerzh committed Jan 22, 2020
1 parent 59372dd commit 7961f1f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
22 changes: 14 additions & 8 deletions tuntap_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
}

func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
if err != nil {
return
var ip net.IP
var ipNet *net.IPNet
if cfg.Addr != "" {
ip, ipNet, err = net.ParseCIDR(cfg.Addr)
if err != nil {
return
}
}

ifce, err := water.New(water.Config{
Expand Down Expand Up @@ -106,11 +110,13 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
return
}

cmd = fmt.Sprintf("ip address add %s dev %s", cfg.Addr, ifce.Name())
log.Log("[tap]", cmd)
if er := link.SetLinkIp(ip, ipNet); er != nil {
err = fmt.Errorf("%s: %v", cmd, er)
return
if cfg.Addr != "" {
cmd = fmt.Sprintf("ip address add %s dev %s", cfg.Addr, ifce.Name())
log.Log("[tap]", cmd)
if er := link.SetLinkIp(ip, ipNet); er != nil {
err = fmt.Errorf("%s: %v", cmd, er)
return
}
}

cmd = fmt.Sprintf("ip link set dev %s up", ifce.Name())
Expand Down
12 changes: 7 additions & 5 deletions tuntap_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
}

func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
ip, _, err := net.ParseCIDR(cfg.Addr)
if err != nil {
return
}
ip, _, _ := net.ParseCIDR(cfg.Addr)

ifce, err := water.New(water.Config{
DeviceType: water.TAP,
Expand All @@ -72,7 +69,12 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
mtu = DefaultMTU
}

cmd := fmt.Sprintf("ifconfig %s inet %s mtu %d up", ifce.Name(), cfg.Addr, mtu)
var cmd string
if cfg.Addr != "" {
cmd = fmt.Sprintf("ifconfig %s inet %s mtu %d up", ifce.Name(), cfg.Addr, mtu)
} else {
cmd = fmt.Sprintf("ifconfig %s mtu %d up", ifce.Name(), mtu)
}
log.Log("[tap]", cmd)
args := strings.Split(cmd, " ")
if er := exec.Command(args[0], args[1:]...).Run(); er != nil {
Expand Down
23 changes: 11 additions & 12 deletions tuntap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ func createTun(cfg TunConfig) (conn net.Conn, itf *net.Interface, err error) {
}

func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
ip, ipNet, err := net.ParseCIDR(cfg.Addr)
if err != nil {
return
}
ip, ipNet, _ := net.ParseCIDR(cfg.Addr)

ifce, err := water.New(water.Config{
DeviceType: water.TAP,
Expand All @@ -72,14 +69,16 @@ func createTap(cfg TapConfig) (conn net.Conn, itf *net.Interface, err error) {
return
}

cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
"source=static addr=%s mask=%s gateway=none",
ifce.Name(), ip.String(), ipMask(ipNet.Mask))
log.Log("[tap]", cmd)
args := strings.Split(cmd, " ")
if er := exec.Command(args[0], args[1:]...).Run(); er != nil {
err = fmt.Errorf("%s: %v", cmd, er)
return
if ip != nil && ipNet != nil {
cmd := fmt.Sprintf("netsh interface ip set address name=%s "+
"source=static addr=%s mask=%s gateway=none",
ifce.Name(), ip.String(), ipMask(ipNet.Mask))
log.Log("[tap]", cmd)
args := strings.Split(cmd, " ")
if er := exec.Command(args[0], args[1:]...).Run(); er != nil {
err = fmt.Errorf("%s: %v", cmd, er)
return
}
}

if err = addTapRoutes(ifce.Name(), cfg.Gateway, cfg.Routes...); err != nil {
Expand Down

0 comments on commit 7961f1f

Please sign in to comment.