Skip to content

Commit

Permalink
Add android os (#25)
Browse files Browse the repository at this point in the history
* Add android os

Allthough android is linux the 'ip route' command doesn't return 'default' line.
Android has only one interface active at any time.
  • Loading branch information
amfern authored and jefferai committed Mar 8, 2019
1 parent 3aed17b commit c7188e7
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 11 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ doc::

world::
@set -e; \
for os in solaris darwin freebsd linux windows; do \
for os in solaris darwin freebsd linux windows android; do \
for arch in amd64; do \
printf "Building on %s-%s\n" "$${os}" "$${arch}" ; \
env GOOS="$${os}" GOARCH="$${arch}" go build -o /dev/null; \
Expand Down
2 changes: 1 addition & 1 deletion cmd/sockaddr/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ test:: $(BIN)
.PHONY: world
world::
mkdir -p bin
gox -os="solaris darwin freebsd linux windows" -arch="386 amd64 arm" -output="bin/sockaddr_{{.OS}}_{{.Arch}}" .
gox -os="solaris darwin freebsd linux windows android" -arch="386 amd64 arm" -output="bin/sockaddr_{{.OS}}_{{.Arch}}" .
41 changes: 32 additions & 9 deletions ifaddrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1197,23 +1197,46 @@ func parseDefaultIfNameFromRoute(routeOut string) (string, error) {
// parseDefaultIfNameFromIPCmd parses the default interface from ip(8) for
// Linux.
func parseDefaultIfNameFromIPCmd(routeOut string) (string, error) {
parsedLines := parseIfNameFromIPCmd(routeOut)
for _, parsedLine := range parsedLines {
if parsedLine[0] == "default" &&
parsedLine[1] == "via" &&
parsedLine[3] == "dev" {
ifName := strings.TrimSpace(parsedLine[4])
return ifName, nil
}
}

return "", errors.New("No default interface found")
}

// parseDefaultIfNameFromIPCmdAndroid parses the default interface from ip(8) for
// Android.
func parseDefaultIfNameFromIPCmdAndroid(routeOut string) (string, error) {
parsedLines := parseIfNameFromIPCmd(routeOut)
if (len(parsedLines) > 0) {
ifName := strings.TrimSpace(parsedLines[0][4])
return ifName, nil
}

return "", errors.New("No default interface found")
}


// parseIfNameFromIPCmd parses interfaces from ip(8) for
// Linux.
func parseIfNameFromIPCmd(routeOut string) [][]string {
lines := strings.Split(routeOut, "\n")
re := whitespaceRE.Copy()
parsedLines := make([][]string, 0, len(lines))
for _, line := range lines {
kvs := re.Split(line, -1)
if len(kvs) < 5 {
continue
}

if kvs[0] == "default" &&
kvs[1] == "via" &&
kvs[3] == "dev" {
ifName := strings.TrimSpace(kvs[4])
return ifName, nil
}
parsedLines = append(parsedLines, kvs)
}

return "", errors.New("No default interface found")
return parsedLines
}

// parseDefaultIfNameWindows parses the default interface from `netstat -rn` and
Expand Down
34 changes: 34 additions & 0 deletions route_info_android.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sockaddr

import (
"errors"
"os/exec"
)

type routeInfo struct {
cmds map[string][]string
}

// NewRouteInfo returns a Android-specific implementation of the RouteInfo
// interface.
func NewRouteInfo() (routeInfo, error) {
return routeInfo{
cmds: map[string][]string{"ip": {"/system/bin/ip", "route", "get", "8.8.8.8"}},
}, nil
}

// GetDefaultInterfaceName returns the interface name attached to the default
// route on the default interface.
func (ri routeInfo) GetDefaultInterfaceName() (string, error) {
out, err := exec.Command(ri.cmds["ip"][0], ri.cmds["ip"][1:]...).Output()
if err != nil {
return "", err
}


var ifName string
if ifName, err = parseDefaultIfNameFromIPCmdAndroid(string(out)); err != nil {
return "", errors.New("No default interface found")
}
return ifName, nil
}
2 changes: 2 additions & 0 deletions route_info_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build !android

package sockaddr

import (
Expand Down

0 comments on commit c7188e7

Please sign in to comment.