# Workaround for Android local discovery

**URL:** https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403
**Category:** Development
**Created:** [June 20, 2023, 5:46am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403 "2023-06-20T05:46:42Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![bt90](https://forum.syncthing.net/user_avatar/forum.syncthing.net/bt90/32/18565_2.png) [@bt90](https://forum.syncthing.net/u/bt90)
#### Post date: [June 20, 2023, 5:46am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/1 "2023-06-20T05:46:42Z")

</div>

I fear the topic has seen quite its share of discussions already but hear me out.

The problem nowadays is that the discovery of interface IPs throws an error on Android. As far as I’m aware broadcast/multicast would still work, we’re just erroring out before we get to that point?

Would it be possible to catch that error and still send the broad- or multicast? The idea here would be that the other client can obtain the address from the packet it just received.

We’re doing something similar for global discovery if i’m not mistaken. The approach would work even more reliably for local networks, as we’re not dealing with NAT.

---

<div class="post-metadata">

### Author: ![calmh](https://forum.syncthing.net/user_avatar/forum.syncthing.net/calmh/32/15311_2.png) [@calmh](https://forum.syncthing.net/u/calmh)
#### Post date: [June 20, 2023, 5:58am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/2 "2023-06-20T05:58:39Z")

</div>

The code is in here if you want to experiment:

> **[syncthing/lib/beacon at main · syncthing/syncthing](https://github.com/syncthing/syncthing/tree/main/lib/beacon)**
>
> main/lib/beacon

We list addresses on IPv4 in order to send directed broadcasts to each subnet, but there’s also a fallback to all-ones so that could work.

For IPv6 we don’t care about addresses, but we need to bind to each interface (hence list them) as the multicasts are link local.

---

<div class="post-metadata">

### Author: ![bt90](https://forum.syncthing.net/user_avatar/forum.syncthing.net/bt90/32/18565_2.png) [@bt90](https://forum.syncthing.net/u/bt90)
#### Post date: [June 20, 2023, 6:06am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/3 "2023-06-20T06:06:03Z")

</div>

Not a go dev but it looks like we break out of the loop on the first interface discovery error?

> <https://github.com/syncthing/syncthing/blob/27aba3567bd7cfbb74b2f21ee91dcfe1c5ba92fd/lib/beacon/broadcast.go#L50>

And also for the IPs

> <https://github.com/syncthing/syncthing/blob/27aba3567bd7cfbb74b2f21ee91dcfe1c5ba92fd/lib/beacon/broadcast.go#L62>

---

<div class="post-metadata">

### Author: ![calmh](https://forum.syncthing.net/user_avatar/forum.syncthing.net/calmh/32/15311_2.png) [@calmh](https://forum.syncthing.net/u/calmh)
#### Post date: [June 20, 2023, 9:22am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/4 "2023-06-20T09:22:53Z")

</div>

Yep

---

<div class="post-metadata">

### Author: ![bt90](https://forum.syncthing.net/user_avatar/forum.syncthing.net/bt90/32/18565_2.png) [@bt90](https://forum.syncthing.net/u/bt90)
#### Post date: [June 20, 2023, 9:41am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/5 "2023-06-20T09:41:20Z")

</div>

Wouldn’t it be enough to capture and log the error thrown by `intf.Addrs()` and carry on with an empty array of addresses?

The fallback provided by `len(dsts) == 0` should be enough to send a broadcast.

The receiving party would need to generate an address based on the origin of the broadcast.

---

<div class="post-metadata">

### Author: ![calmh](https://forum.syncthing.net/user_avatar/forum.syncthing.net/calmh/32/15311_2.png) [@calmh](https://forum.syncthing.net/u/calmh)
#### Post date: [June 20, 2023, 9:59am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/6 "2023-06-20T09:59:41Z")

</div>

Maybe, if there’s a case where asking for interface addresses returns an error but it’s still possible to use the interface. Maybe android is that case, I don’t know.

---

<div class="post-metadata">

### Author: ![bt90](https://forum.syncthing.net/user_avatar/forum.syncthing.net/bt90/32/18565_2.png) [@bt90](https://forum.syncthing.net/u/bt90)
#### Post date: [June 20, 2023, 10:44am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/7 "2023-06-20T10:44:41Z")

</div>

Exactly. That functionality is broken on Android.

> <https://github.com/golang/go/issues/40569>
>
> \<!--
> Please answer these questions before submitting your issue. Thanks!
> For q…uestions please use one of our forums: https://github.com/golang/go/wiki/Questions
> \--\>
> 
> \### What version of Go are you using (\`go version\`)?
> 
> \<pre\>
> $ go version
> go version go1.14.6 darwin/amd64
> 
> $gomobile version
> gomobile version +973feb4 Sat Aug 1 11:21:45 2020 +0000 (android,ios); androidSDK=/sdk/platforms/android-30
> \</pre\>
> 
> \### Does this issue reproduce with the latest release?
> Yes
> 
> 
> \### What operating system and processor architecture are you using (\`go env\`)?
> 
> 
> \<details\>\<summary\>\<code\>go env\</code\> Output\</summary\>\<br\>\<pre\>
> $ go env
> GO111MODULE="on"
> GOARCH="amd64"
> GOBIN=""
> GOCACHE="/Users/user/Library/Caches/go-build"
> GOENV="/Users/user/Library/Application Support/go/env"
> GOEXE=""
> GOFLAGS=""
> GOHOSTARCH="amd64"
> GOHOSTOS="darwin"
> GOINSECURE=""
> GONOPROXY=""
> GONOSUMDB=""
> GOOS="darwin"
> GOPATH="/Users/user/golang"
> GOPRIVATE=""
> GOPROXY="https://proxy.golang.org,direct"
> GOROOT="/usr/local/Cellar/go/1.14.6/libexec"
> GOSUMDB="sum.golang.org"
> GOTMPDIR=""
> GOTOOLDIR="/usr/local/Cellar/go/1.14.6/libexec/pkg/tool/darwin\_amd64"
> GCCGO="gccgo"
> AR="ar"
> CC="clang"
> CXX="clang++"
> CGO\_ENABLED="1"
> GOMOD="/Users/user/golang/src/golang.org/x/mobile/go.mod"
> CGO\_CFLAGS="-g -O2"
> CGO\_CPPFLAGS=""
> CGO\_CXXFLAGS="-g -O2"
> CGO\_FFLAGS="-g -O2"
> CGO\_LDFLAGS="-g -O2"
> PKG\_CONFIG="pkg-config"
> GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/kq/3436m\_v11sg0l7zqtmv2r1gw0000gn/T/go-build713467523=/tmp/go-build -gno-record-gcc-switches -fno-common"
> \</pre\>\</details\>
> 
> \### What did you do?
> 
> \<!--
> If possible, provide a recipe for reproducing the error.
> A complete runnable program is good.
> A link on play.golang.org is best.
> \--\>
> 
> Calling \`net.InterfaceAddrs()\` fails on Android app targetting SDK version 30. With \`build.gradle\` looking like:
> \`\`\`
> android {
> compileSdkVersion 30
> buildToolsVersion "30.0.0"
> defaultConfig {
> applicationId "com.example.testapp"
> minSdkVersion 29
> targetSdkVersion 30
> versionCode 1
> versionName "1.0"
> testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
> }
> buildTypes {
> release {
> minifyEnabled false
> proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
> }
> }
> }
> \`\`\`
> 
> Building the exact same app targetting SDK 29 works, and returns no error:
> \`\`\`
> android {
> compileSdkVersion 30
> buildToolsVersion "30.0.0"
> defaultConfig {
> applicationId "com.example.testapp"
> minSdkVersion 29
> targetSdkVersion 29
> versionCode 1
> versionName "1.0"
> testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
> }
> buildTypes {
> release {
> minifyEnabled false
> proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
> }
> }
> }
> \`\`\`
> 
> 
> \### What did you expect to see?
> Targetting SDK 30 would behave no differently.
> 
> 
> \### What did you see instead?
> Calling \`net.InterfaceAddrs()\` results in error \`route ip+net: netlinkrib: permission denied\` when embedded in Android app targetting SDK 30 (R):
> \`\`\`
> 2020-08-04 15:10:21.386 15754-15754/? W/Thread-2: type=1400 audit(0.0:616): avc: denied { bind } for scontext=u:r:untrusted\_app:s0:c158,c256,c512,c768 tcontext=u:r:untrusted\_app:s0:c158,c256,c512,c768 tclass=netlink\_route\_socket permissive=0 b/155595000 app=com.example.testapp
> \`\`\`

The best case scenario would be to have the IP discovery a bit more resiliant and try to send a broadcast in any case. That would avoid Android specific hacks and still fix local discovery.

---

<div class="post-metadata">

### Author: ![calmh](https://forum.syncthing.net/user_avatar/forum.syncthing.net/calmh/32/15311_2.png) [@calmh](https://forum.syncthing.net/u/calmh)
#### Post date: [June 20, 2023, 11:27am UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/8 "2023-06-20T11:27:26Z")

</div>

Someone should try it. That call failing doesn’t necessarily mean everything else will work if we just ignore the failure…

---

<div class="post-metadata">

### Author: ![bt90](https://forum.syncthing.net/user_avatar/forum.syncthing.net/bt90/32/18565_2.png) [@bt90](https://forum.syncthing.net/u/bt90)
#### Post date: [June 25, 2025, 8:59pm UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/11 "2025-06-25T20:59:33Z")

</div>

Oh nice. That’s quite an improvement!

---

<div class="post-metadata">

### Author: ![tomasz86](https://forum.syncthing.net/letter_avatar_proxy/v4/letter/t/96bed5/32.png) [@tomasz86](https://forum.syncthing.net/u/tomasz86)
#### Post date: [July 6, 2025, 7:34pm UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/12 "2025-07-06T19:34:38Z")

</div>

With this change implemented, Syncthing fails to compile for me with the error:

```plaintext
link: github.com/wlynxg/anet: invalid reference to net.zoneCache

```

This happens when cross-compiling for Android arm64 under Windows.

@Catfriend1 Have you got any ideas what the problem may be about?

---

<div class="post-metadata">

### Author: ![marbens](https://forum.syncthing.net/user_avatar/forum.syncthing.net/marbens/32/18260_2.png) [@marbens](https://forum.syncthing.net/u/marbens)
#### Post date: [July 6, 2025, 7:56pm UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/13 "2025-07-06T19:56:48Z")

</div>

> <https://github.com/syncthing/syncthing/pull/10211>
>
> https://github.com/wlynxg/anet#how-to-build-with-go-1230-or-later
> 
> \### Purpose…
> 
> Fixes https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/12
> 
> \### Testing
> 
> \`go run build.go --goos=android --goarch=arm64 build\`

---

<div class="post-metadata">

### Author: ![marbens](https://forum.syncthing.net/user_avatar/forum.syncthing.net/marbens/32/18260_2.png) [@marbens](https://forum.syncthing.net/u/marbens)
#### Post date: [July 6, 2025, 8:34pm UTC](https://forum.syncthing.net/t/workaround-for-android-local-discovery/20403/16 "2025-07-06T20:34:49Z")

</div>

> [@Catfriend1](#):
>
> We just need it for Android builds.

Indeed. Upstream recommends it in general though.
