-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
net: support for android without cgo #8877
Comments
Please note that this issue is present when building with cgo for Android and looking up DNS records not supported by |
Note that there was an attempt: https://codereview.appspot.com/142200044/ |
@crawshaw, what is your plan here? I'm not sure this needs to be a Go 1.5 issue. The proper way to do DNS on Android is to ask the local DNS proxy cache on the device for it, but that necessarily involves Binder calls and whatnot, and the details of how aren't a public interface AFAIU. (as least they weren't awhile ago). The number of cases where you'd want a Go binary to do its own DNS lookups is rare. In those cases, we can do the whole getprop thing I suppose, but I'm not even sure I understand the advantages to not using cgo once our toolchain and linker are happier with Android stuff. |
Will look at in 1.6. Go binaries without cgo are not supported for apps, so this is low priority for me. |
It seems that some breeds of android manage to resolve DNS without cgo, while others fail. What's the right approach to fix this for the ones that don't? |
The right fix is probably implement the Android property system
and query net.dns property?
|
For the systems that do have /etc/resolve.conf is it appropriate to have a knob to use the Go resolver? |
@Gaillard, you're jumping on the wrong bug. Your question has nothing to do with Android, right? Also, the knob you want already exists. https://golang.org/pkg/net/#Resolver.PreferGo |
I actually meant for android because its hard coded to not use the netdns value. But I appreciate the link I didn't know that was there. |
Here are some of hacks I've seen being used to enable DNS resolution w/out cgo on android: OpenSilk/SyncthingAndroid@7a61add#diff-95f9e0a51eeab8f39703f35c52a5ed75 and my own variation for go 1.8: commit 7d091b4af03b6db3b8d3eb60f1a4738c25330309
Author: Aman Gupta <[email protected]>
Date: Thu May 11 16:00:43 2017 -0700
add hack for dns lookups on android
diff --git a/src/net/dnsconfig_unix.go b/src/net/dnsconfig_unix.go
index 9c8108d11c..b3156d03ea 100644
--- a/src/net/dnsconfig_unix.go
+++ b/src/net/dnsconfig_unix.go
@@ -10,6 +10,8 @@ package net
import (
"os"
+ "os/exec"
+ "strings"
"sync/atomic"
"time"
)
@@ -33,8 +35,42 @@ type dnsConfig struct {
soffset uint32 // used by serverOffset
}
-// See resolv.conf(5) on a Linux machine.
func dnsReadConfig(filename string) *dnsConfig {
+ if _, err := os.Stat("/system/bin/getprop"); err == nil {
+ return dnsReadConfigAndroid()
+ } else {
+ return dnsReadConfigUnix(filename)
+ }
+}
+
+func dnsReadConfigAndroid() *dnsConfig {
+ conf := &dnsConfig{
+ ndots: 1,
+ timeout: 5 * time.Second,
+ attempts: 2,
+ rotate: false,
+ }
+
+ for _, prop := range []string{"net.dns1", "net.dns2"} {
+ out, err := exec.Command("/system/bin/getprop", prop).Output()
+ if err != nil {
+ continue
+ }
+ ip := strings.TrimSpace(string(out))
+ if ParseIP(ip) != nil {
+ conf.servers = append(conf.servers, JoinHostPort(ip, "53"))
+ }
+ }
+
+ if len(conf.servers) == 0 {
+ conf.servers = []string{"8.8.8.8:53", "8.8.4.4:53", "4.2.2.1:53"}
+ }
+
+ return conf
+}
+
+// See resolv.conf(5) on a Linux machine.
+func dnsReadConfigUnix(filename string) *dnsConfig {
conf := &dnsConfig{
ndots: 1,
timeout: 5 * time.Second, |
I am also seeing this bug when building an Android SDK (.aar) using gomobile. Not sure if that's expected or not. It looks like maybe #10714 describes this but it says it was fixed and I'm still experiencing it. |
I was not able to use the fix from @cs8425 for my Android AAR file. Instead I used a fix I got from here: var dialer net.Dialer
net.DefaultResolver = &net.Resolver{
PreferGo: false,
Dial: func(context context.Context, _, _ string) (net.Conn, error) {
conn, err := dialer.DialContext(context, "udp", "1.1.1.1:53")
if err != nil {
return nil, err
}
return conn, nil
},
} This fix worked for me, although obviously DNS is hardcoded instead of pulling the server address from the OS. |
Not in use by any other packages in Guix, it was a temporary solution implemented 5 years ago according to project's about field: "androiddnsfix is a hack around the issues in <golang/go#8877>" * gnu/packages/golang.scm (go-github.looper.workers.dev-mtibben-androiddnsfix, go-github.looper.workers.dev-androiddnsfix): Delete variables. Change-Id: Ie2fda123b03b1cf99c70b39198eaabfe3e6baa8f
Not in use by any other packages in Guix, it was a temporary solution implemented 5 years ago according to project's about field: "androiddnsfix is a hack around the issues in <golang/go#8877>" * gnu/packages/golang.scm (go-github.looper.workers.dev-mtibben-androiddnsfix, go-github.looper.workers.dev-androiddnsfix): Delete variables. Change-Id: Ie2fda123b03b1cf99c70b39198eaabfe3e6baa8f
Not in use by any other packages in Guix, it was a temporary solution implemented 5 years ago according to project's about field: "androiddnsfix is a hack around the issues in <golang/go#8877>" * gnu/packages/golang.scm (go-github.looper.workers.dev-mtibben-androiddnsfix, go-github.looper.workers.dev-androiddnsfix): Delete variables. Change-Id: Ie2fda123b03b1cf99c70b39198eaabfe3e6baa8f
Not in use by any other packages in Guix, it was a temporary solution implemented 5 years ago according to project's about field: "androiddnsfix is a hack around the issues in <golang/go#8877>" * gnu/packages/golang.scm (go-github.looper.workers.dev-mtibben-androiddnsfix, go-github.looper.workers.dev-androiddnsfix): Delete variables. Change-Id: Ie2fda123b03b1cf99c70b39198eaabfe3e6baa8f
Not in use by any other packages in Guix, it was a temporary solution implemented 5 years ago according to project's about field: "androiddnsfix is a hack around the issues in <golang/go#8877>" * gnu/packages/golang.scm (go-github.looper.workers.dev-mtibben-androiddnsfix, go-github.looper.workers.dev-androiddnsfix): Delete variables. Change-Id: Ie2fda123b03b1cf99c70b39198eaabfe3e6baa8f
It is possible to build Android OS binaries (not apps) without cgo. Those apps will fail
because the Android OS does not define /etc/resolve.conf. Some more details on #8020 and
the linked CL.
The text was updated successfully, but these errors were encountered: