Skip to content

Commit

Permalink
android: pass interface name to go (#340)
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 <kari@tailscale.com>
  • Loading branch information
kari-ts committed Apr 26, 2024
1 parent 24dd830 commit a6bc224
Show file tree
Hide file tree
Showing 4 changed files with 13 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 @@ -168,14 +168,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. An empty string is used if there is no network available.
func OnDNSConfigChanged(ifname string) {
select {
case onDNSConfigChanged <- struct{}{}:
case onDNSConfigChanged <- ifname:
default:
}
}
Expand Down
5 changes: 4 additions & 1 deletion libtailscale/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,17 @@ 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())
panic(p)
}
}()

// Set the interface name and alert the monitor.
interfaces.UpdateLastKnownDefaultRouteInterface(ifname)
if b.sys != nil {
if nm, ok := b.sys.NetMon.GetOK(); ok {
nm.InjectEvent()
Expand Down

0 comments on commit a6bc224

Please sign in to comment.