Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add android os #25

Merged
merged 2 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
jefferai marked this conversation as resolved.
Show resolved Hide resolved

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