1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-23 09:52:16 -05:00

Simplify ImapConnection#connect

This commit is contained in:
Jan Berkel 2015-01-14 04:22:04 +01:00
parent 9523a589fc
commit ecd316d0af
2 changed files with 8 additions and 12 deletions

View File

@ -555,14 +555,14 @@ class ImapConnection {
private static Socket connect(ImapSettings settings, TrustedSocketFactory socketFactory) private static Socket connect(ImapSettings settings, TrustedSocketFactory socketFactory)
throws GeneralSecurityException, MessagingException, IOException { throws GeneralSecurityException, MessagingException, IOException {
// Try all IPv4 and IPv6 addresses of the host // Try all IPv4 and IPv6 addresses of the host
InetAddress[] addresses = InetAddress.getAllByName(settings.getHost()); Exception connectException = null;
for (int i = 0; i < addresses.length; i++) { for (InetAddress address : InetAddress.getAllByName(settings.getHost())) {
try { try {
if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) { if (K9MailLib.isDebug() && DEBUG_PROTOCOL_IMAP) {
Log.d(LOG_TAG, "Connecting to " + settings.getHost() + " as " + addresses[i]); Log.d(LOG_TAG, "Connecting to " + settings.getHost() + " as " + address);
} }
SocketAddress socketAddress = new InetSocketAddress(addresses[i], settings.getPort()); SocketAddress socketAddress = new InetSocketAddress(address, settings.getPort());
Socket socket; Socket socket;
if (settings.getConnectionSecurity() == ConnectionSecurity.SSL_TLS_REQUIRED) { if (settings.getConnectionSecurity() == ConnectionSecurity.SSL_TLS_REQUIRED) {
socket = socketFactory.createSocket( socket = socketFactory.createSocket(
@ -576,15 +576,12 @@ class ImapConnection {
socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); socket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT);
// Successfully connected to the server; don't try any other addresses // Successfully connected to the server; don't try any other addresses
return socket; return socket;
} catch (SocketException e) { } catch (IOException e) {
if (i < (addresses.length - 1)) { Log.w(LOG_TAG, "could not connect to "+address, e);
// There are still other addresses for that host to try connectException = e;
continue;
}
throw new MessagingException("Cannot connect to host", e);
} }
} }
throw new MessagingException("Cannot connect to host"); throw new MessagingException("Cannot connect to host", connectException);
} }
private void adjustDNSCacheTTL() { private void adjustDNSCacheTTL() {

View File

@ -15,7 +15,6 @@ import com.fsck.k9.mail.ConnectionSecurity;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;