Check if ip4 on prefer ip4

This commit is contained in:
M66B 2022-03-14 15:18:43 +01:00
parent 51ce4ce450
commit 8d843bc209
1 changed files with 49 additions and 41 deletions

View File

@ -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<NetworkInterface> 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<NetworkInterface> 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<String, String> getId(Context context) {
Map<String, String> id = new LinkedHashMap<>();
id.put("name", context.getString(R.string.app_name));