Repository navigation
feat(network_info_plus): read the gateway from the default route and expose the DHCP server - #4028
Open
git-elliot wants to merge 2 commits into
Open
git-elliot wants to merge 2 commits into
git-elliot wants to merge 2 commits into
Conversation
`getWifiGatewayIP()` returned `LinkProperties.dhcpServerAddress` on API 31+, so on networks where routing and DHCP sit on different hosts the field labelled "gateway" reported the lease server instead. iOS, macOS, Linux and Windows all walk the routing table, which left Android as the outlier. Take the next hop of the default route instead, preferring the IPv4 hop so a dual-stack connection does not surface an IPv6 address. The pre-31 branch is untouched: `DhcpInfo.gateway` is DHCP option 3, the router the server advertised.
With the gateway read from the route table, the lease becomes a separate answer rather than a coincidence: Android API 30+ reports `LinkProperties.getDhcpServerAddress()` and below that `DhcpInfo.serverAddress`. iOS, macOS, Linux and Windows return null because none of them expose the lease to third-party apps, Web throws like its neighbouring methods, and the platform interface keeps a throwing default so implementations that do not override it are unaffected.
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
getWifiGatewayIP()returns the DHCP server, not the gateway, on Android 12 and newer.On API 31+ the Android implementation read
LinkProperties.dhcpServerAddress, so any network whose router and DHCP server are different hosts gets the wrong value in the field labelled "gateway". The pre-31 branch was doing the opposite of the right thing:DhcpInfo.gatewayis DHCP option 3, i.e. the router the server advertised, which is a gateway. iOS, macOS, Linux and Windows all walk the routing table and already return the real next hop, so Android was the only platform that conflated the two.This PR:
LinkProperties.routes(RouteInfo.isDefaultRoute,RouteInfo.getGateway) on API 31+, preferring the IPv4 hop because a dual-stack connection also carries an IPv6 default route.getWifiDhcpServerIP(), which exposes the address that actually leased the lease, so callers can label the two separately instead of guessing. Android readsLinkProperties.getDhcpServerAddress()on API 30+ andDhcpInfo.serverAddressbelow that. iOS, macOS, Linux and Windows returnnull(no public lease API for third-party apps), and Web keeps its existing "not supported" behaviour.The new platform-interface method follows the note at the top of
NetworkInfoPlatform: extending classes inherit the default, so this is not a breaking change.Related Issues
Build.VERSION_CODES.Sgate to API 24 so Ethernet works below Android 12. This PR does not change that gate; if [Request]: network_info_plus: use ConnectivityManager for getWifiIP and getWifiGatewayIP from Android 7 (N), so ethernet works below Android 12 #3998 lands, the new route-based branch simply starts serving API 24-30 too, which is exactly what it wants.Verification
flutter testinnetwork_info_plus_platform_interface: 8/8 pass, including the new channel test.flutter testinnetwork_info_plus: 22/22 pass, including the newgetWifiDhcpServerIPtest.flutter analyzeinnetwork_info_plus_platform_interface: no issues.dart format: no changes needed.Changed Kotlin compiles:
./gradlew :network_info_plus:compileDebugKotlinagainst a throwawayhost app that path-depends on this plugin and overrides the platform interface, because the
in-repo example cannot be built with the installed Flutter (its Gradle/AGP versions predate it).
Not verified on device: the route read has not been checked on real hardware in a network where
the DHCP server and the gateway are different hosts. The gateway branch is a re-reading of the same
LinkProperties.routesdata the existing code already uses on API 31+, so the risk is the choice ofroute, not a new API surface.
The only analyzer finding in the package is a pre-existing
deprecated_member_useinfo inexample/lib/main.dart, which this diff does not touch and which only surfaces on a newer SDK.Checklist
CHANGELOG.mdnor the plugin version inpubspec.yamlfiles.flutter analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?
!in the title as explained in Conventional Commits).