From 0a666df164f2ef04a15acd6378ab3ef64dde3246 Mon Sep 17 00:00:00 2001 From: akwizgran Date: Thu, 12 Mar 2020 16:43:46 +0000 Subject: [PATCH] Correctly handle connectivity events on IPv6-only networks. --- .../plugin/tcp/AndroidLanTcpPlugin.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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 3510e571b..cabb2f653 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 @@ -252,7 +252,7 @@ class AndroidLanTcpPlugin extends LanTcpPlugin { connectionStatusExecutor.execute(() -> { State s = getState(); if (s != ACTIVE && s != INACTIVE) return; - Pair wifi = getWifiIpv4Address(); + Pair wifi = getPreferredWifiAddress(); if (wifi == null) { LOG.info("Not connected to wifi"); socketFactory = SocketFactory.getDefault(); @@ -280,4 +280,23 @@ class AndroidLanTcpPlugin extends LanTcpPlugin { } }); } + + /** + * Returns a {@link Pair} where the first element is an IP address (IPv4 if + * available, otherwise IPv6) of the wifi interface and the second element + * is true if this device is providing an access point, or false if this + * device is a client. Returns null if this device isn't connected to wifi + * as an access point or client. + */ + @Nullable + private Pair getPreferredWifiAddress() { + Pair wifi = getWifiIpv4Address(); + // If there's no wifi IPv4 address, we might be a client on an + // IPv6-only wifi network. We can only detect this on API 21+ + if (wifi == null && SDK_INT >= 21) { + InetAddress ipv6 = getWifiClientIpv6Address(); + if (ipv6 != null) return new Pair<>(ipv6, false); + } + return wifi; + } } \ No newline at end of file