Skip to content

android: cache default network and set callback for binding fn#754

Open
kari-ts wants to merge 1 commit intomainfrom
kari/bindsock
Open

android: cache default network and set callback for binding fn#754
kari-ts wants to merge 1 commit intomainfrom
kari/bindsock

Conversation

@kari-ts
Copy link
Collaborator

@kari-ts kari-ts commented Mar 6, 2026

After switching from cellular to wifi without ipv6, ForeachInterface still sees rmnet prefixes, so HaveV6 stays true, and magicsock keeps attempting ipv6 connections that either route through cellular or time out for users on wifi without ipv6

This
-Caches cachedDefaultNetwork on nevwork event in NetworkChangeCallback
-Implements BindSocketToNetwork, a callback that binds the socket to the currently selected Network
-Sets the callback function whtne the VPN starts and clears it on disconnect

Updates tailscale/tailscale#6152

After switching from cellular to wifi without ipv6, ForeachInterface still sees rmnet prefixes, so HaveV6 stays true, and magicsock keeps attempting ipv6 connections that either route through cellular or time out for users on wifi without ipv6

This
-Caches cachedDefaultNetwork on nevwork event in NetworkChangeCallbac
-Implements BindSocketToNetwork, a callback that binds the socket to the currently selected Network
-Sets the callback function whtne the VPN starts and clears it on disconnect

Updates tailscale/tailscale#6152

Signed-off-by: kari-ts <kari@tailscale.com>
Copy link
Member

@nickkhyl nickkhyl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM overall. I'm not approving it mainly because it requires an OSS bump and may require some changes (or at least a rebase) after the OSS changes land.

TSLog.w(TAG, "bindSocketToActiveNetwork: bind failed fd=$fd net=$net iface=$iface: $e")
false
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The indentation here seems off (the body has the same indentation level as the method declaration).

Comment on lines +459 to +460
val iface = NetworkChangeCallback.cachedDefaultInterfaceName
?: NetworkChangeCallback.cachedDefaultNetworkInfo?.linkProps?.interfaceName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we actually need the fallback here? From reading the code, it seems that cachedDefaultInterfaceName is updated atomically with cachedDefaultNetworkInfo.

So if the interface name is immutable and cannot change after we cache it in cachedDefaultInterfaceName, then we should always use it:

Suggested change
val iface = NetworkChangeCallback.cachedDefaultInterfaceName
?: NetworkChangeCallback.cachedDefaultNetworkInfo?.linkProps?.interfaceName
val iface = NetworkChangeCallback.cachedDefaultInterfaceName

Otherwise, we should remove cachedDefaultInterfaceName and always read it from linkProps:

Suggested change
val iface = NetworkChangeCallback.cachedDefaultInterfaceName
?: NetworkChangeCallback.cachedDefaultNetworkInfo?.linkProps?.interfaceName
val iface = NetworkChangeCallback.cachedDefaultNetworkInfo?.linkProps?.interfaceName

The doc says the field was added for convenience, and there isn't much convenience in checking two fields 😸

// Convenience: cached interface name for logging.
@Volatile
var cachedDefaultInterfaceName: String? = null
private set

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants