Skip to content

Commit

Permalink
fix: Android DNS hack for go 1.17
Browse files Browse the repository at this point in the history
Signed-off-by: aeddi <antoine.e.b@gmail.com>
  • Loading branch information
aeddi authored and D4ryl00 committed Dec 15, 2021
1 parent 1e7669c commit 840b642
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ public void testNullParams() throws Exception {
} catch (Exception e) { /* ignore */ }


// DNS pair setter tests
try {
IPFS.setDNSPair(null, "foo");
fail("setDNSPair() should fail with a null primary");
} catch (Exception e) { /* ignore */ }

try {
IPFS.setDNSPair("foo", null);
fail("setDNSPair() should fail with a null secondary");
} catch (Exception e) { /* ignore */ }
// DNS pair setter tests
try {
IPFS.setDNSPair(null, "foo");
fail("setDNSPair() should fail with a null primary");
} catch (Exception e) { /* ignore */ }

try {
IPFS.setDNSPair("foo", null);
fail("setDNSPair() should fail with a null secondary");
} catch (Exception e) { /* ignore */ }


// Request tests
Expand Down
24 changes: 12 additions & 12 deletions android/bridge/src/main/java/ipfs/gomobile/android/IPFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,18 @@ synchronized public RequestBuilder newRequest(@NonNull String command)
return new RequestBuilder(requestBuilder);
}

/**
* Sets the primary and secondary DNS for gomobile (hacky, will be removed in future version)
*
* @param primary The primary DNS address in the form {@code <ip4>:<port>}
* @param secondary The secondary DNS address in the form {@code <ip4>:<port>}
*/
public static void setDNSPair(@NonNull String primary, @NonNull String secondary) {
Objects.requireNonNull(primary, "primary should not be null");
Objects.requireNonNull(secondary, "secondary should not be null");

Core.setDNSPair(primary, secondary, false);
}
/**
* Sets the primary and secondary DNS for gomobile (hacky, will be removed in future version)
*
* @param primary The primary DNS address in the form {@code <ip4>:<port>}
* @param secondary The secondary DNS address in the form {@code <ip4>:<port>}
*/
public static void setDNSPair(@NonNull String primary, @NonNull String secondary) {
Objects.requireNonNull(primary, "primary should not be null");
Objects.requireNonNull(secondary, "secondary should not be null");

Core.setDNSPair(primary, secondary, false);
}

/**
* Internal helper that opens the repo if it is closed.
Expand Down
34 changes: 19 additions & 15 deletions go/bind/core/dnsconfig.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build go1.17
// +build go1.17

// from: https://gist.github.com/cs8425/107e01a0652f1f1f6e033b5b68364b5e

//nolint
Expand All @@ -16,22 +19,23 @@ import (
var defaultNS []string

// need to keep sync with go version
//go:linkname resolvConf net.resolvConf
var resolvConf resolverConfig

// copy from /src/net/dnsconfig_unix.go
type dnsConfig struct {
servers []string // server addresses (in host:port form) to use
search []string // rooted suffixes to append to local name
ndots int // number of dots in name to trigger absolute lookup
timeout time.Duration // wait before giving up on a query, including retries
attempts int // lost packets before giving up on server
rotate bool // round robin among servers
unknownOpt bool // anything unknown was encountered
lookup []string // OpenBSD top-level database "lookup" order
err error // any error that occurs during open of resolv.conf
mtime time.Time // time of resolv.conf modification
soffset uint32 // used by serverOffset
servers []string // server addresses (in host:port form) to use
search []string // rooted suffixes to append to local name
ndots int // number of dots in name to trigger absolute lookup
timeout time.Duration // wait before giving up on a query, including retries
attempts int // lost packets before giving up on server
rotate bool // round robin among servers
unknownOpt bool // anything unknown was encountered
lookup []string // OpenBSD top-level database "lookup" order
err error // any error that occurs during open of resolv.conf
mtime time.Time // time of resolv.conf modification
soffset uint32 // used by serverOffset
singleRequest bool // use sequential A and AAAA queries instead of parallel queries
useTCP bool // force usage of TCP for DNS resolutions
}

// copy from /src/net/dnsclient_unix.go
Expand All @@ -47,14 +51,14 @@ type resolverConfig struct {
dnsConfig *dnsConfig // parsed resolv.conf structure used in lookups
}

//go:linkname (*resolverConfig).tryUpdate net.(*resolverConfig).tryUpdate
func (conf *resolverConfig) tryUpdate(name string)
//go:linkname tryUpdate net.(*resolverConfig).tryUpdate
func tryUpdate(conf *resolverConfig, name string)

// Need an empty .s file (dnsconfig_empty.s)

func setDefaultNS(addrs []string, loadFromSystem bool) {
if resolvConf.dnsConfig == nil {
resolvConf.tryUpdate("")
tryUpdate(&resolvConf, "")
}

if loadFromSystem {
Expand Down
1 change: 1 addition & 0 deletions go/bind/core/dnsconfig_deprecated.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//go:build !go1.17
// +build !go1.17

// from: https://gist.github.com/cs8425/107e01a0652f1f1f6e033b5b68364b5e

Expand Down
2 changes: 1 addition & 1 deletion go/bind/core/dnsconfig_empty.s
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// Required by dnsconfig_android.go
// Required by dnsconfig.go
2 changes: 1 addition & 1 deletion go/bind/core/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestNode(t *testing.T) {
repo, clean := testingRepo(t, path)
defer clean()

node, err := NewNode(repo)
node, err := NewNode(repo, nil)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 840b642

Please sign in to comment.