Skip to content

Commit

Permalink
More robust IP discovery, attempted fix for #17
Browse files Browse the repository at this point in the history
  • Loading branch information
shayne committed Mar 15, 2020
1 parent d9a11d5 commit 1c9065d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/shayne/go-wsl2-host

go 1.12
go 1.14

require (
github.com/stretchr/testify v1.4.0
Expand Down
113 changes: 107 additions & 6 deletions pkg/wslcli/wslcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"errors"
"fmt"
"io/ioutil"
"math/bits"
"os/exec"
"regexp"
"strconv"
"strings"

"golang.org/x/text/encoding/unicode"
Expand Down Expand Up @@ -41,10 +43,105 @@ func ListAll() (string, error) {
return decoded, nil
}

func netmaskToBits(mask uint32) int {
return bits.OnesCount32(mask)
}
func hexToUint32LE(hex string) (uint32, error) {
i, err := strconv.ParseInt(hex[6:8]+hex[4:6]+hex[2:4]+hex[0:2], 16, 64)
if err != nil {
return 0, err
}
return uint32(i), nil
}

type routeInfo struct {
net uint32
mask uint32
}

func getRouteInfo(name string) (*routeInfo, error) {
cmd := exec.Command("wsl.exe", "-d", name, "--", "cat", "/proc/net/route")
out, err := cmd.Output()
if err != nil {
return nil, err
}
ri := &routeInfo{}
sout := string(out)
sout = strings.TrimSpace(sout)
lines := strings.Split(sout, "\n")
lines = lines[1:]
for _, line := range lines {
fs := strings.Fields(line)
if ri.mask > 0 && ri.net > 0 {
break
}
if fs[0] != "eth0" {
continue
}
if fs[1] != "00000000" {
net, err := hexToUint32LE(fs[1])
if err != nil {
return nil, fmt.Errorf("failed to convert network to Uint32: %w", err)
}
ri.net = net
}
if fs[7] != "00000000" {
mask, err := hexToUint32LE(fs[7])
if err != nil {
return nil, fmt.Errorf("failed to convert netmask to Uint32: %w", err)
}
ri.mask = mask
}
}

return ri, nil
}

func isIPInRange(ri *routeInfo, ip uint32) bool {
return (ri.net & ri.mask) == (ip & ri.mask)
}

func ipToUint32(ip string) (uint32, error) {
octets := strings.Split(ip, ".")
if len(octets) != 4 {
return 0, errors.New("invalid IP address")
}

var io uint32

o1, err := strconv.Atoi(octets[0])
if err != nil {
return 0, fmt.Errorf("failed to parse IP address, %s: %w", ip, err)
}
io += uint32(o1 << 24)
o2, err := strconv.Atoi(octets[1])
if err != nil {
return 0, fmt.Errorf("failed to parse IP address, %s: %w", ip, err)
}
io += uint32(o2 << 16)
o3, err := strconv.Atoi(octets[2])
if err != nil {
return 0, fmt.Errorf("failed to parse IP address, %s: %w", ip, err)
}
io += uint32(o3 << 8)
o4, err := strconv.Atoi(octets[3])
if err != nil {
return 0, fmt.Errorf("failed to parse IP address, %s: %w", ip, err)
}
io += uint32(o4)

return io, nil
}

// GetIP returns the IP address of the given distro
// Suggest check if running before calling this function as
// it has the side-effect of starting the distro
func GetIP(name string) (string, error) {
ri, err := getRouteInfo(name)
if err != nil {
return "", err
}

cmd := exec.Command("wsl.exe", "-d", name, "--", "cat", "/proc/net/fib_trie")
out, err := cmd.Output()
if err != nil {
Expand All @@ -56,13 +153,17 @@ func GetIP(name string) (string, error) {
return "", errors.New("invalid output from fib_trie")
}
lines := strings.Split(sout, "\n")
for i, line := range lines {
for i := len(lines) - 1; i >= 0; i-- {
line := lines[i]
if strings.Index(line, "32 host LOCAL") != -1 {
if i > 0 && strings.Index(lines[i-1], "127.0.0.1") == -1 {
if fs := strings.Fields(lines[i-1]); len(fs) > 1 {
return strings.TrimSpace(fs[1]), nil
}
break
fs := strings.Fields(lines[i-1])
ipstr := strings.TrimSpace(fs[1])
ip, err := ipToUint32(ipstr)
if err != nil {
return "", fmt.Errorf("failed to convert ip, %s: %w", ipstr, err)
}
if isIPInRange(ri, ip) {
return ipstr, nil
}
}
}
Expand Down

0 comments on commit 1c9065d

Please sign in to comment.