From e065d45d1675e3b36d23acb642835828ba5eb0cb Mon Sep 17 00:00:00 2001 From: akwizgran Date: Mon, 9 Mar 2020 13:23:22 +0000 Subject: [PATCH] Recognise unusual AP addresses. --- .../plugin/tcp/AndroidLanTcpPlugin.java | 45 ++++++++++++++++--- .../bramble/plugin/tcp/LanTcpPlugin.java | 7 ++- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java index bb5bd6e26..bddc94cc4 100644 --- a/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java +++ b/bramble-android/src/main/java/org/briarproject/bramble/plugin/tcp/AndroidLanTcpPlugin.java @@ -18,6 +18,7 @@ import org.briarproject.bramble.api.settings.Settings; import java.io.IOException; import java.net.InetAddress; +import java.net.InterfaceAddress; import java.net.NetworkInterface; import java.net.Socket; import java.net.SocketException; @@ -110,23 +111,53 @@ class AndroidLanTcpPlugin extends LanTcpPlugin { if (info != null && info.getIpAddress() != 0) { return new Pair<>(intToInetAddress(info.getIpAddress()), false); } - List addrs = getLocalInetAddresses(); + List ifAddrs = getLocalInterfaceAddresses(); // If we're providing a normal access point, return its address - for (InetAddress addr : addrs) { - if (WIFI_AP_ADDRESS.equals(addr)) { - return new Pair<>(addr, true); + for (InterfaceAddress ifAddr : ifAddrs) { + if (isAndroidWifiApAddress(ifAddr)) { + return new Pair<>(ifAddr.getAddress(), true); } } // If we're providing a wifi direct access point, return its address - for (InetAddress addr : addrs) { - if (WIFI_DIRECT_AP_ADDRESS.equals(addr)) { - return new Pair<>(addr, true); + for (InterfaceAddress ifAddr : ifAddrs) { + if (isAndroidWifiDirectApAddress(ifAddr)) { + return new Pair<>(ifAddr.getAddress(), true); } } // Not connected to wifi return null; } + /** + * Returns true if the given address belongs to a network provided by an + * Android access point (including the access point's own address). + *

+ * The access point's address is usually 192.168.43.1, but at least one + * device (Honor 8A) may use other addresses in the range 192.168.43.0/24. + */ + private boolean isAndroidWifiApAddress(InterfaceAddress ifAddr) { + if (ifAddr.getNetworkPrefixLength() != 24) return false; + byte[] ip = ifAddr.getAddress().getAddress(); + return ip.length == 4 + && ip[0] == (byte) 192 + && ip[1] == (byte) 168 + && ip[2] == (byte) 43; + } + + /** + * Returns true if the given address belongs to a network provided by an + * Android wifi direct legacy mode access point (including the access + * point's own address). + */ + private boolean isAndroidWifiDirectApAddress(InterfaceAddress ifAddr) { + if (ifAddr.getNetworkPrefixLength() != 24) return false; + byte[] ip = ifAddr.getAddress().getAddress(); + return ip.length == 4 + && ip[0] == (byte) 192 + && ip[1] == (byte) 168 + && ip[2] == (byte) 49; + } + @Nullable private InetAddress getSlaacAddressForInterface(InetAddress wifi) { try { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java index b3c583728..27ceddbaa 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/plugin/tcp/LanTcpPlugin.java @@ -67,14 +67,17 @@ class LanTcpPlugin extends TcpPlugin { /** * The IP address of an Android device providing a wifi access point. + *

+ * Most devices use this address, but at least one device (Honor 8A) may + * use other addresses in the range 192.168.43.0/24. */ - protected static final InetAddress WIFI_AP_ADDRESS; + private static final InetAddress WIFI_AP_ADDRESS; /** * The IP address of an Android device providing a wifi direct * legacy mode access point. */ - protected static final InetAddress WIFI_DIRECT_AP_ADDRESS; + private static final InetAddress WIFI_DIRECT_AP_ADDRESS; static { try {