Skip to content

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
fluttercommunity:mainfrom
git-elliot:fix/network_info_plus_gateway_dhcp
Open

git-elliot wants to merge 2 commits into
fluttercommunity:mainfrom
git-elliot:fix/network_info_plus_gateway_dhcp

Conversation

@git-elliot

Copy link
Copy Markdown

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.gateway is 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:

  • reads the default route's next hop from LinkProperties.routes (RouteInfo.isDefaultRoute, RouteInfo.getGateway) on API 31+, preferring the IPv4 hop because a dual-stack connection also carries an IPv6 default route.
  • adds getWifiDhcpServerIP(), which exposes the address that actually leased the lease, so callers can label the two separately instead of guessing. Android reads LinkProperties.getDhcpServerAddress() on API 30+ and DhcpInfo.serverAddress below that. iOS, macOS, Linux and Windows return null (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

Verification

  • flutter test in network_info_plus_platform_interface: 8/8 pass, including the new channel test.

  • flutter test in network_info_plus: 22/22 pass, including the new getWifiDhcpServerIP test.

  • flutter analyze in network_info_plus_platform_interface: no issues.

  • dart format: no changes needed.

  • Changed Kotlin compiles: ./gradlew :network_info_plus:compileDebugKotlin against a throwaway
    host 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.routes data the existing code already uses on API 31+, so the risk is the choice of
route, not a new API surface.

The only analyzer finding in the package is a pre-existing deprecated_member_use info in
example/lib/main.dart, which this diff does not touch and which only surfaces on a newer SDK.

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I titled the PR using Conventional Commits.
  • I did not modify the CHANGELOG.md nor the plugin version in pubspec.yaml files.
  • All existing and new tests are passing.
  • The analyzer (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?

  • Yes, this is a breaking change (please indicate that with a ! in the title as explained in Conventional Commits).
  • No, this is not a breaking change.

`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.
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.

[Bug]: wrong gateway returned

1 participant