From 8d843bc2090a8602388e4a338a64a024143b6c8c Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 14 Mar 2022 15:18:43 +0100 Subject: [PATCH] Check if ip4 on prefer ip4 --- .../java/eu/faircode/email/EmailService.java | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/EmailService.java b/app/src/main/java/eu/faircode/email/EmailService.java index 4abaf80208..6071548bb4 100644 --- a/app/src/main/java/eu/faircode/email/EmailService.java +++ b/app/src/main/java/eu/faircode/email/EmailService.java @@ -536,18 +536,20 @@ public class EmailService implements AutoCloseable { } boolean prefer_ip4 = prefs.getBoolean("prefer_ip4", true); - if (prefer_ip4 && - main instanceof Inet6Address) - try { - for (InetAddress iaddr : InetAddress.getAllByName(host)) - if (iaddr instanceof Inet4Address) { - main = iaddr; - EntityLog.log(context, "Preferring=" + main); - break; - } - } catch (UnknownHostException ex) { - Log.w(ex); - } + if (prefer_ip4 && main instanceof Inet6Address) { + boolean[] has46 = has46(context); + if (has46[0]) + try { + for (InetAddress iaddr : InetAddress.getAllByName(host)) + if (iaddr instanceof Inet4Address) { + main = iaddr; + EntityLog.log(context, "Preferring=" + main); + break; + } + } catch (UnknownHostException ex) { + Log.w(ex); + } + } _connect(main, port, require_id, user, factory); } catch (UnknownHostException ex) { @@ -618,36 +620,12 @@ public class EmailService implements AutoCloseable { int ip4 = (main instanceof Inet4Address ? 1 : 0); int ip6 = (main instanceof Inet6Address ? 1 : 0); - boolean has4 = false; - boolean has6 = false; - try { - Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); - while (interfaces != null && interfaces.hasMoreElements()) { - NetworkInterface ni = interfaces.nextElement(); - for (InterfaceAddress iaddr : ni.getInterfaceAddresses()) { - InetAddress addr = iaddr.getAddress(); - boolean local = (addr.isLoopbackAddress() || addr.isLinkLocalAddress()); - EntityLog.log(context, "Interface=" + ni + " addr=" + addr + " local=" + local); - if (!local) - if (addr instanceof Inet4Address) - has4 = true; - else if (addr instanceof Inet6Address) - has6 = true; - } - } - } catch (Throwable ex2) { - Log.e(ex2); - /* - java.lang.NullPointerException: Attempt to read from field 'java.util.List java.net.NetworkInterface.childs' on a null object reference - at java.net.NetworkInterface.getAll(NetworkInterface.java:498) - at java.net.NetworkInterface.getNetworkInterfaces(NetworkInterface.java:398) - */ - } + boolean[] has46 = has46(context); EntityLog.log(context, "Address main=" + main + " count=" + iaddrs.length + - " ip4=" + ip4 + " max4=" + MAX_IPV4 + " has4=" + has4 + - " ip6=" + ip6 + " max6=" + MAX_IPV6 + " has6=" + has6); + " ip4=" + ip4 + " max4=" + MAX_IPV4 + " has4=" + has46[0] + + " ip6=" + ip6 + " max6=" + MAX_IPV6 + " has6=" + has46[1]); for (InetAddress iaddr : iaddrs) { EntityLog.log(context, "Address resolved=" + iaddr); @@ -656,13 +634,13 @@ public class EmailService implements AutoCloseable { continue; if (iaddr instanceof Inet4Address) { - if (!has4 || ip4 >= MAX_IPV4) + if (!has46[0] || ip4 >= MAX_IPV4) continue; ip4++; } if (iaddr instanceof Inet6Address) { - if (!has6 || ip6 >= MAX_IPV6) + if (!has46[1] || ip6 >= MAX_IPV6) continue; ip6++; } @@ -797,6 +775,36 @@ public class EmailService implements AutoCloseable { throw new NoSuchProviderException(protocol); } + static boolean[] has46(Context context) { + boolean has4 = false; + boolean has6 = false; + try { + Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); + while (interfaces != null && interfaces.hasMoreElements()) { + NetworkInterface ni = interfaces.nextElement(); + for (InterfaceAddress iaddr : ni.getInterfaceAddresses()) { + InetAddress addr = iaddr.getAddress(); + boolean local = (addr.isLoopbackAddress() || addr.isLinkLocalAddress()); + EntityLog.log(context, "Interface=" + ni + " addr=" + addr + " local=" + local); + if (!local) + if (addr instanceof Inet4Address) + has4 = true; + else if (addr instanceof Inet6Address) + has6 = true; + } + } + } catch (Throwable ex) { + Log.e(ex); + /* + java.lang.NullPointerException: Attempt to read from field 'java.util.List java.net.NetworkInterface.childs' on a null object reference + at java.net.NetworkInterface.getAll(NetworkInterface.java:498) + at java.net.NetworkInterface.getNetworkInterfaces(NetworkInterface.java:398) + */ + } + + return new boolean[]{has4, has6}; + } + static Map getId(Context context) { Map id = new LinkedHashMap<>(); id.put("name", context.getString(R.string.app_name));