From bc176d72a3665373c5e14694ece96c23990214cf Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 28 Apr 2014 08:29:12 +0200 Subject: [PATCH] Throw exceptions of SSLSocket.startHandshake() in connect() instead of throwing a NoResponseException in case startHandshake() throws an Exception. The NoResponseException was then thrown when performing SASL auth, which usually directly followes securing the connection via TLS (if enabled and provided by the server). --- .../org/jivesoftware/smack/XMPPConnection.java | 18 ++++++++++++++++++ .../org/jivesoftware/smack/PacketReader.java | 8 +++++--- .../jivesoftware/smack/XMPPTCPConnection.java | 11 +++++++++-- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java b/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java index a66cfaccc..6485ac946 100644 --- a/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java +++ b/core/src/main/java/org/jivesoftware/smack/XMPPConnection.java @@ -218,6 +218,11 @@ public abstract class XMPPConnection { private boolean bindingRequired; private boolean sessionSupported; + /** + * + */ + private IOException connectionException; + /** * Create an executor to deliver incoming packets to listeners. We'll use a single thread with an unbounded queue. */ @@ -355,6 +360,7 @@ public abstract class XMPPConnection { saslAuthentication.init(); bindingRequired = false; sessionSupported = false; + connectionException = null; connectInternal(); } @@ -485,6 +491,18 @@ public abstract class XMPPConnection { return userJID; } + protected void setConnectionException(IOException exception) { + connectionException = exception; + } + + protected void throwConnectionExceptionOrNoResponse() throws IOException, NoResponseException { + if (connectionException != null) { + throw connectionException; + } else { + throw new NoResponseException(); + } + } + /** * Sends the specified packet to the server. * diff --git a/tcp/src/main/java/org/jivesoftware/smack/PacketReader.java b/tcp/src/main/java/org/jivesoftware/smack/PacketReader.java index 7968da2c3..a4c270e95 100644 --- a/tcp/src/main/java/org/jivesoftware/smack/PacketReader.java +++ b/tcp/src/main/java/org/jivesoftware/smack/PacketReader.java @@ -17,6 +17,8 @@ package org.jivesoftware.smack; +import java.io.IOException; + import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Presence; @@ -26,7 +28,6 @@ import org.jivesoftware.smack.sasl.SASLMechanism.Challenge; import org.jivesoftware.smack.sasl.SASLMechanism.SASLFailure; import org.jivesoftware.smack.sasl.SASLMechanism.Success; import org.jivesoftware.smack.util.PacketParserUtils; - import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.SecurityRequiredException; import org.jivesoftware.smack.XMPPException.StreamErrorException; @@ -92,8 +93,9 @@ class PacketReader { * * @throws NoResponseException if the server fails to send an opening stream back * within packetReplyTimeout. + * @throws IOException */ - synchronized public void startup() throws NoResponseException { + synchronized public void startup() throws NoResponseException, IOException { readerThread.start(); // Wait for stream tag before returning. We'll wait a couple of seconds before // giving up and throwing an error. @@ -108,7 +110,7 @@ class PacketReader { // Ignore. } if (!lastFeaturesParsed) { - throw new NoResponseException(); + connection.throwConnectionExceptionOrNoResponse(); } } diff --git a/tcp/src/main/java/org/jivesoftware/smack/XMPPTCPConnection.java b/tcp/src/main/java/org/jivesoftware/smack/XMPPTCPConnection.java index 07a826d12..89206c99f 100644 --- a/tcp/src/main/java/org/jivesoftware/smack/XMPPTCPConnection.java +++ b/tcp/src/main/java/org/jivesoftware/smack/XMPPTCPConnection.java @@ -716,8 +716,15 @@ public class XMPPTCPConnection extends XMPPConnection { plain.getInetAddress().getHostAddress(), plain.getPort(), true); // Initialize the reader and writer with the new secured version initReaderAndWriter(); - // Proceed to do the handshake - ((SSLSocket) socket).startHandshake(); + + try { + // Proceed to do the handshake + ((SSLSocket) socket).startHandshake(); + } + catch (IOException e) { + setConnectionException(e); + throw e; + } //if (((SSLSocket) socket).getWantClientAuth()) { // System.err.println("XMPPConnection wants client auth"); //}