Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String?> getWifiDhcpServerIP() {
return _platform.getWifiDhcpServerIP();
}

/// Obtains the broadcast of the connected wifi network
Future<String?> getWifiBroadcast() {
return _platform.getWifiBroadcast();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ class NetworkInfoPlusLinuxPlugin extends NetworkInfoPlatform {
);
}

/// Obtains the DHCP server IP address of the connected wifi network
@override
Future<String?> 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<String?> getWifiBroadcast() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ class NetworkInfoPlusWebPlugin extends NetworkInfoPlatform {
Future<String?> getWifiIP() {
throw UnsupportedError('getWifiIP() is not supported on Web.');
}

/// Obtains the DHCP server IP address of the connected wifi network
@override
Future<String?> getWifiDhcpServerIP() {
throw UnsupportedError('getWifiDhcpServerIP() is not supported on Web.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,18 @@
try {
GetAdaptersAddresses(
family,
GET_ADAPTERS_ADDRESSES_FLAGS(0),

Check notice on line 197 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
null,
ulSize,
);
pIpAdapterAddress = HeapAlloc(
GetProcessHeap().value,
HEAP_FLAGS(0),

Check notice on line 203 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
ulSize.value,
).cast();
GetAdaptersAddresses(
family,
GET_ADAPTERS_ADDRESSES_FLAGS(0),

Check notice on line 208 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
pIpAdapterAddress,
ulSize,
);
Expand Down Expand Up @@ -247,18 +247,18 @@
try {
GetAdaptersAddresses(
AF_INET,
GET_ADAPTERS_ADDRESSES_FLAGS(0),

Check notice on line 250 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
null,
ulSize,
);
pIpAdapterAddress = HeapAlloc(
GetProcessHeap().value,
HEAP_FLAGS(0),

Check notice on line 256 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
ulSize.value,
).cast();
GetAdaptersAddresses(
AF_INET,
GET_ADAPTERS_ADDRESSES_FLAGS(0),

Check notice on line 261 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
pIpAdapterAddress,
ulSize,
);
Expand Down Expand Up @@ -325,18 +325,18 @@
try {
GetAdaptersAddresses(
AF_INET,
GET_ADAPTERS_ADDRESSES_FLAGS(0x80),

Check notice on line 328 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
null,
ulSize,
);
pIpAdapterAddress = HeapAlloc(
GetProcessHeap().value,
HEAP_FLAGS(0),

Check notice on line 334 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
ulSize.value,
).cast();
GetAdaptersAddresses(
AF_INET,
GET_ADAPTERS_ADDRESSES_FLAGS(0x80),

Check notice on line 339 in packages/network_info_plus/network_info_plus/lib/src/network_info_plus_windows.dart

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Use 'const' with the constructor to improve performance.

Try adding the 'const' keyword to the constructor invocation. See https://dart.dev/diagnostics/prefer_const_constructors to learn more about this problem.
pIpAdapterAddress,
ulSize,
);
Expand All @@ -355,4 +355,12 @@
}),
);
}

/// Obtains the DHCP server IP address of the connected wifi network
@override
Future<String?> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
});
}

Expand All @@ -85,6 +91,11 @@ class MockNetworkInfoPlatform extends Mock
return kWifiGatewayIP;
}

@override
Future<String> getWifiDhcpServerIP() async {
return kWifiDhcpServerIP;
}

@override
Future<String> getWifiSubmask() async {
return kWifiSubmask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class MethodChannelNetworkInfo extends NetworkInfoPlatform {
return methodChannel.invokeMethod<String>('wifiGatewayAddress');
}

@override
Future<String?> getWifiDhcpServerIP() {
return methodChannel.invokeMethod<String>('wifiDhcpServerAddress');
}

@override
Future<String?> getWifiBroadcast() {
return methodChannel.invokeMethod<String>('wifiBroadcast');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String?> getWifiDhcpServerIP() {
throw UnimplementedError('getWifiDhcpServerIP() has not been implemented.');
}

/// Obtains the broadcast of the connected wifi network
Future<String?> getWifiBroadcast() {
throw UnimplementedError('getWifiBroadcast() has not been implemented.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -85,6 +87,14 @@ void main() {
]);
});

test('getWifiDhcpServerIP', () async {
final result = await methodChannelNetworkInfo.getWifiDhcpServerIP();
expect(result, '127.0.0.1');
expect(log, <Matcher>[
isMethodCall('wifiDhcpServerAddress', arguments: null),
]);
});

test('getWifiSubmask', () async {
final result = await methodChannelNetworkInfo.getWifiSubmask();
expect(result, '255.255.255.0');
Expand Down
Loading