Skip to content

Commit

Permalink
android: pass interface name to go (#336)
Browse files Browse the repository at this point in the history
Use Android API to pass interface name to Tailscale on network updates

Fixes tailscale/corp#19215

Signed-off-by: kari-ts <[email protected]>
  • Loading branch information
kari-ts authored Apr 18, 2024
1 parent bc89851 commit bbb3c86
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions android/src/main/java/com/tailscale/ipn/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ class App : Application(), libtailscale.AppContext {
}

if (dns.updateDNSFromNetwork(sb.toString())) {
Libtailscale.onDnsConfigChanged()
Libtailscale.onDnsConfigChanged(linkProperties?.getInterfaceName())
}
}

override fun onLost(network: Network) {
super.onLost(network)
if (dns.updateDNSFromNetwork("")) {
Libtailscale.onDnsConfigChanged()
Libtailscale.onDnsConfigChanged("")
}
}
})
Expand Down
4 changes: 2 additions & 2 deletions libtailscale/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ func (a *App) runBackend(ctx context.Context) error {
netns.SetAndroidProtectFunc(nil)
service = nil
}
case <-onDNSConfigChanged:
case i := <-onDNSConfigChanged:
if b != nil {
go b.NetworkChanged()
go b.NetworkChanged(i)
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions libtailscale/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ var (
// onGoogleToken receives google ID tokens.
onGoogleToken = make(chan string)

// onDNSConfigChanged is notified when the network changes and the DNS config needs to be updated.
onDNSConfigChanged = make(chan struct{}, 1)
// onDNSConfigChanged is notified when the network changes and the DNS config needs to be updated. It receives the updated interface name.
onDNSConfigChanged = make(chan string, 1)
)

func OnDnsConfigChanged() {
// ifname is the interface name retrieved from LinkProperties on network change, if any.
func OnDnsConfigChanged(ifname string) {
select {
case onDNSConfigChanged <- struct{}{}:
case onDNSConfigChanged <- ifname:
default:
}
}
Expand Down
4 changes: 3 additions & 1 deletion libtailscale/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ func (b *backend) CloseTUNs() {
b.devices.Shutdown()
}

func (b *backend) NetworkChanged() {
// ifname is the interface name retrieved from LinkProperties on network change. If a network is lost, an empty string is passed in.
func (b *backend) NetworkChanged(ifname string) {
defer func() {
if p := recover(); p != nil {
log.Printf("panic in NetworkChanged %s: %s", p, debug.Stack())
Expand All @@ -239,6 +240,7 @@ func (b *backend) NetworkChanged() {
if nm, ok := b.sys.NetMon.GetOK(); ok {
nm.InjectEvent()
}
interfaces.UpdateLastKnownDefaultRouteInterface(ifname)

Check failure on line 243 in libtailscale/net.go

View workflow job for this annotation

GitHub Actions / build

undefined: interfaces.UpdateLastKnownDefaultRouteInterface
}
}

Expand Down

0 comments on commit bbb3c86

Please sign in to comment.