diff --git a/packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfo.kt b/packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfo.kt index 61318091ba..e1c53571c9 100644 --- a/packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfo.kt +++ b/packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfo.kt @@ -81,11 +81,17 @@ internal class NetworkInfo( fun getGatewayIPAddress(): String? { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - val linkAddresses = - connectivityManager?.getLinkProperties(connectivityManager.activeNetwork) - val dhcpServer = linkAddresses?.dhcpServerAddress?.hostAddress - - dhcpServer + val gateways = connectivityManager + ?.getLinkProperties(connectivityManager.activeNetwork) + ?.routes + ?.filter { it.isDefaultRoute } + ?.mapNotNull { it.gateway } + + // The DHCP server is only the gateway by coincidence, so read the + // next hop from the route table. Prefer the IPv4 hop, since a + // dual-stack connection also carries an IPv6 default route. + (gateways?.firstOrNull { it is Inet4Address } ?: gateways?.firstOrNull()) + ?.hostAddress } else { @Suppress("DEPRECATION") val dhcpInfo = wifiManager.dhcpInfo @@ -95,6 +101,20 @@ internal class NetworkInfo( } } + fun getDhcpServerIPAddress(): String? { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + connectivityManager + ?.getLinkProperties(connectivityManager.activeNetwork) + ?.dhcpServerAddress + ?.hostAddress + } else { + @Suppress("DEPRECATION") + val serverAddressInt = wifiManager.dhcpInfo?.serverAddress + + serverAddressInt?.takeIf { it != 0 }?.let { formatIPAddress(it) } + } + } + private fun formatIPAddress(intIP: Int): String = String.format( "%d.%d.%d.%d", diff --git a/packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfoMethodChannelHandler.kt b/packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfoMethodChannelHandler.kt index 88d1408db8..ae413af0d2 100644 --- a/packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfoMethodChannelHandler.kt +++ b/packages/network_info_plus/network_info_plus/android/src/main/kotlin/dev/fluttercommunity/plus/network_info/NetworkInfoMethodChannelHandler.kt @@ -19,6 +19,7 @@ internal class NetworkInfoMethodChannelHandler(private val networkInfo: NetworkI "wifiBroadcast" -> result.success(networkInfo.getBroadcastIP()) "wifiSubmask" -> result.success(networkInfo.getWifiSubnetMask()) "wifiGatewayAddress" -> result.success(networkInfo.getGatewayIPAddress()) + "wifiDhcpServerAddress" -> result.success(networkInfo.getDhcpServerIPAddress()) "wifiIPv6Address" -> result.success(networkInfo.getIpV6()) else -> result.notImplemented() } diff --git a/packages/network_info_plus/network_info_plus/ios/network_info_plus/Sources/network_info_plus/FPPNetworkInfoPlusPlugin.m b/packages/network_info_plus/network_info_plus/ios/network_info_plus/Sources/network_info_plus/FPPNetworkInfoPlusPlugin.m index 64161b9280..e056ad0fe0 100644 --- a/packages/network_info_plus/network_info_plus/ios/network_info_plus/Sources/network_info_plus/FPPNetworkInfoPlusPlugin.m +++ b/packages/network_info_plus/network_info_plus/ios/network_info_plus/Sources/network_info_plus/FPPNetworkInfoPlusPlugin.m @@ -127,6 +127,9 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result([self getWifiBroadcast]); } else if ([call.method isEqualToString:@"wifiGatewayAddress"]) { result([self getGatewayIP]); + } else if ([call.method isEqualToString:@"wifiDhcpServerAddress"]) { + // SystemConfiguration exposes no DHCP lease to third-party apps. + result(nil); } else { result(FlutterMethodNotImplemented); } diff --git a/packages/network_info_plus/network_info_plus/lib/network_info_plus.dart b/packages/network_info_plus/network_info_plus/lib/network_info_plus.dart index b53d236e95..d383d895be 100644 --- a/packages/network_info_plus/network_info_plus/lib/network_info_plus.dart +++ b/packages/network_info_plus/network_info_plus/lib/network_info_plus.dart @@ -78,6 +78,15 @@ class NetworkInfo { return _platform.getWifiGatewayIP(); } + /// Obtains the DHCP server IP address of the connected wifi network + /// + /// Returns null on iOS, macOS, Linux and Windows, which do not expose the + /// lease, and throws on Web. On Android this is a different host from + /// [getWifiGatewayIP] whenever the network separates the two roles. + Future getWifiDhcpServerIP() { + return _platform.getWifiDhcpServerIP(); + } + /// Obtains the broadcast of the connected wifi network Future getWifiBroadcast() { return _platform.getWifiBroadcast(); diff --git a/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_linux.dart b/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_linux.dart index f77ed922bc..9a3505ceb6 100644 --- a/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_linux.dart +++ b/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_linux.dart @@ -66,6 +66,14 @@ class NetworkInfoPlusLinuxPlugin extends NetworkInfoPlatform { ); } + /// Obtains the DHCP server IP address of the connected wifi network + @override + Future getWifiDhcpServerIP() async { + // NetworkManager keeps the lease options on the device's Dhcp4 object + // rather than on the connection, which this class never reads. + return null; + } + /// Obtains the broadcast of the connected wifi network @override Future getWifiBroadcast() { diff --git a/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_web.dart b/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_web.dart index 38734c607e..ef3d22cefc 100644 --- a/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_web.dart +++ b/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_web.dart @@ -26,4 +26,10 @@ class NetworkInfoPlusWebPlugin extends NetworkInfoPlatform { Future getWifiIP() { throw UnsupportedError('getWifiIP() is not supported on Web.'); } + + /// Obtains the DHCP server IP address of the connected wifi network + @override + Future getWifiDhcpServerIP() { + throw UnsupportedError('getWifiDhcpServerIP() is not supported on Web.'); + } } diff --git a/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart b/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart index 870f85028c..38c6e26cec 100644 --- a/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart +++ b/packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart @@ -355,4 +355,12 @@ class NetworkInfoPlusWindowsPlugin extends NetworkInfoPlatform { }), ); } + + /// Obtains the DHCP server IP address of the connected wifi network + @override + Future getWifiDhcpServerIP() async { + // IP_ADAPTER_ADDRESSES_LH reports gateway and DNS but not the server that + // leased the address, so there is nothing to read here. + return null; + } } diff --git a/packages/network_info_plus/network_info_plus/macos/network_info_plus/Sources/network_info_plus/NetworkInfoPlusPlugin.swift b/packages/network_info_plus/network_info_plus/macos/network_info_plus/Sources/network_info_plus/NetworkInfoPlusPlugin.swift index 0a05a35924..9ae25dfacf 100644 --- a/packages/network_info_plus/network_info_plus/macos/network_info_plus/Sources/network_info_plus/NetworkInfoPlusPlugin.swift +++ b/packages/network_info_plus/network_info_plus/macos/network_info_plus/Sources/network_info_plus/NetworkInfoPlusPlugin.swift @@ -48,6 +48,11 @@ public class NetworkInfoPlusPlugin: NSObject, FlutterPlugin { result(getWifiBroadcast()) case "wifiGatewayAddress": result(getDefaultGateway()) + case "wifiDhcpServerAddress": + // The routing table read by getDefaultGateway() has the next hop but no + // lease information, and SystemConfiguration exposes none to third-party + // apps. + result(nil) default: result(FlutterMethodNotImplemented) } diff --git a/packages/network_info_plus/network_info_plus/test/network_info_test.dart b/packages/network_info_plus/network_info_plus/test/network_info_test.dart index 20efe9493f..77df285fcd 100644 --- a/packages/network_info_plus/network_info_plus/test/network_info_test.dart +++ b/packages/network_info_plus/network_info_plus/test/network_info_test.dart @@ -14,6 +14,7 @@ const String kWifiIpAddressResult = '127.0.0.1'; const String kWifiIpV6 = '2002:7f00:0001:0:0:0:0:0'; const String kWifiBroadcast = '127.0.0.255'; const String kWifiGatewayIP = '127.0.0.0'; +const String kWifiDhcpServerIP = '192.168.1.2'; const String kWifiSubmask = '255.255.255.0'; const LocationAuthorizationStatus kRequestLocationResult = LocationAuthorizationStatus.authorizedAlways; @@ -64,6 +65,11 @@ void main() { final result = await networkInfo.getWifiGatewayIP(); expect(result, kWifiGatewayIP); }); + + test('getWifiDhcpServerIP', () async { + final result = await networkInfo.getWifiDhcpServerIP(); + expect(result, kWifiDhcpServerIP); + }); }); } @@ -85,6 +91,11 @@ class MockNetworkInfoPlatform extends Mock return kWifiGatewayIP; } + @override + Future getWifiDhcpServerIP() async { + return kWifiDhcpServerIP; + } + @override Future getWifiSubmask() async { return kWifiSubmask; diff --git a/packages/network_info_plus/network_info_plus_platform_interface/lib/method_channel_network_info.dart b/packages/network_info_plus/network_info_plus_platform_interface/lib/method_channel_network_info.dart index 58ca7a8d36..4c6f686770 100644 --- a/packages/network_info_plus/network_info_plus_platform_interface/lib/method_channel_network_info.dart +++ b/packages/network_info_plus/network_info_plus_platform_interface/lib/method_channel_network_info.dart @@ -52,6 +52,11 @@ class MethodChannelNetworkInfo extends NetworkInfoPlatform { return methodChannel.invokeMethod('wifiGatewayAddress'); } + @override + Future getWifiDhcpServerIP() { + return methodChannel.invokeMethod('wifiDhcpServerAddress'); + } + @override Future getWifiBroadcast() { return methodChannel.invokeMethod('wifiBroadcast'); diff --git a/packages/network_info_plus/network_info_plus_platform_interface/lib/network_info_plus_platform_interface.dart b/packages/network_info_plus/network_info_plus_platform_interface/lib/network_info_plus_platform_interface.dart index 2438f51258..1d435f9569 100644 --- a/packages/network_info_plus/network_info_plus_platform_interface/lib/network_info_plus_platform_interface.dart +++ b/packages/network_info_plus/network_info_plus_platform_interface/lib/network_info_plus_platform_interface.dart @@ -67,6 +67,18 @@ abstract class NetworkInfoPlatform extends PlatformInterface { throw UnimplementedError('getWifiGatewayIP() has not been implemented.'); } + /// Obtains the IP address of the DHCP server that leased the connected + /// network's address. + /// + /// This is a different host from the gateway whenever the network separates + /// the two roles, so it must not be used as a gateway. + /// + /// Returns null on the platforms that do not expose the lease (iOS, macOS, + /// Linux and Windows); Web throws. + Future getWifiDhcpServerIP() { + throw UnimplementedError('getWifiDhcpServerIP() has not been implemented.'); + } + /// Obtains the broadcast of the connected wifi network Future getWifiBroadcast() { throw UnimplementedError('getWifiBroadcast() has not been implemented.'); diff --git a/packages/network_info_plus/network_info_plus_platform_interface/test/method_channel_network_info_test.dart b/packages/network_info_plus/network_info_plus_platform_interface/test/method_channel_network_info_test.dart index 13c188537c..e2bfc58d39 100644 --- a/packages/network_info_plus/network_info_plus_platform_interface/test/method_channel_network_info_test.dart +++ b/packages/network_info_plus/network_info_plus_platform_interface/test/method_channel_network_info_test.dart @@ -34,6 +34,8 @@ void main() { return '127.0.0.255'; case 'wifiGatewayAddress': return '127.0.0.0'; + case 'wifiDhcpServerAddress': + return '127.0.0.1'; case 'wifiSubmask': return '255.255.255.0'; case 'requestLocationServiceAuthorization': @@ -85,6 +87,14 @@ void main() { ]); }); + test('getWifiDhcpServerIP', () async { + final result = await methodChannelNetworkInfo.getWifiDhcpServerIP(); + expect(result, '127.0.0.1'); + expect(log, [ + isMethodCall('wifiDhcpServerAddress', arguments: null), + ]); + }); + test('getWifiSubmask', () async { final result = await methodChannelNetworkInfo.getWifiSubmask(); expect(result, '255.255.255.0');