From ba24fdc742eced3e49c60aed8e09f6f8a5a36c39 Mon Sep 17 00:00:00 2001 From: Marcus Wolschon Date: Mon, 24 Jan 2011 15:09:36 +0000 Subject: [PATCH] update issue 2744 try all addresses of a host. (Fix is required for proper operation in IPv4-only/IPv6-only networks for IPv4/IPv6 dual-stack mail-hosts) --- .../fsck/k9/mail/transport/SmtpTransport.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/src/com/fsck/k9/mail/transport/SmtpTransport.java b/src/com/fsck/k9/mail/transport/SmtpTransport.java index 3d0bd276f..38a306b9a 100644 --- a/src/com/fsck/k9/mail/transport/SmtpTransport.java +++ b/src/com/fsck/k9/mail/transport/SmtpTransport.java @@ -30,6 +30,7 @@ import org.apache.commons.codec.binary.Hex; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.Iterator; import java.util.List; public class SmtpTransport extends Transport @@ -152,25 +153,37 @@ public class SmtpTransport extends Transport { try { - SocketAddress socketAddress = new InetSocketAddress(mHost, mPort); - if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED || - mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) - { - SSLContext sslContext = SSLContext.getInstance("TLS"); - boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED; - sslContext.init(null, new TrustManager[] - { - TrustManagerFactory.get(mHost, secure) - }, new SecureRandom()); - mSocket = sslContext.getSocketFactory().createSocket(); - mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); - mSecure = true; - } - else - { - mSocket = new Socket(); - mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); - } + InetAddress[] addresses = InetAddress.getAllByName(mHost); + for (int i = 0; i < addresses.length; i++) { + try { + SocketAddress socketAddress = new InetSocketAddress(addresses[i], mPort); + if (mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED || + mConnectionSecurity == CONNECTION_SECURITY_SSL_OPTIONAL) + { + SSLContext sslContext = SSLContext.getInstance("TLS"); + boolean secure = mConnectionSecurity == CONNECTION_SECURITY_SSL_REQUIRED; + sslContext.init(null, new TrustManager[] + { + TrustManagerFactory.get(mHost, secure) + }, new SecureRandom()); + mSocket = sslContext.getSocketFactory().createSocket(); + mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); + mSecure = true; + } + else + { + mSocket = new Socket(); + mSocket.connect(socketAddress, SOCKET_CONNECT_TIMEOUT); + } + } catch (ConnectException e) { + if (i < (addresses.length - 1)) { + // there are still other addresses for that host to try + continue; + } + throw new MessagingException("Cannot connect to host", e); + } + break; // connection success + } // RFC 1047 mSocket.setSoTimeout(SOCKET_READ_TIMEOUT);