aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java
diff options
context:
space:
mode:
authorChristian Schneppe <christian@pix-art.de>2016-11-20 15:04:01 +0100
committerChristian Schneppe <christian@pix-art.de>2016-11-20 15:04:01 +0100
commit386927c122e961e776e3af57358c011f1b070451 (patch)
tree338a1c33eddb7f80db019bde760340e5adcf9ea4 /src/main/java/de/pixart/messenger/xmpp/XmppConnection.java
parentf04842b893a82583d094a9204f532841094ef525 (diff)
close socket after failed stream open
Diffstat (limited to 'src/main/java/de/pixart/messenger/xmpp/XmppConnection.java')
-rw-r--r--src/main/java/de/pixart/messenger/xmpp/XmppConnection.java97
1 files changed, 50 insertions, 47 deletions
diff --git a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java
index 36fb9f078..dd78a3eab 100644
--- a/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java
+++ b/src/main/java/de/pixart/messenger/xmpp/XmppConnection.java
@@ -227,9 +227,9 @@ public class XmppConnection implements Runnable {
}
protected synchronized void changeStatus(final Account.State nextStatus) {
- if (Thread.currentThread().isInterrupted()) {
- Log.d(Config.LOGTAG,account.getJid().toBareJid()+": not changing status to "+nextStatus+" because thread was interrupted");
- }
+ if (Thread.currentThread().isInterrupted()) {
+ Log.d(Config.LOGTAG,account.getJid().toBareJid()+": not changing status to "+nextStatus+" because thread was interrupted");
+ }
if (account.getStatus() != nextStatus) {
if ((nextStatus == Account.State.OFFLINE)
&& (account.getStatus() != Account.State.CONNECTING)
@@ -307,14 +307,14 @@ public class XmppConnection implements Runnable {
}
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": connect to " + destination + " via Tor");
localSocket = SocksSocketFactory.createSocketOverTor(destination, account.getPort());
- try {
- startXmpp(localSocket);
- } catch (InterruptedException e) {
- Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream");
- return;
- } catch (Exception e) {
- throw new IOException(e.getMessage());
- }
+ try {
+ startXmpp(localSocket);
+ } catch (InterruptedException e) {
+ Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream");
+ return;
+ } catch (Exception e) {
+ throw new IOException(e.getMessage());
+ }
} else if (extended && account.getHostname() != null && !account.getHostname().isEmpty()) {
InetSocketAddress address = new InetSocketAddress(account.getHostname(), account.getPort());
@@ -326,8 +326,8 @@ public class XmppConnection implements Runnable {
try {
final TlsFactoryVerifier tlsFactoryVerifier = getTlsFactoryVerifier();
localSocket = tlsFactoryVerifier.factory.createSocket();
- localSocket.connect(address, Config.SOCKET_TIMEOUT * 1000);
- final SSLSession session = ((SSLSocket) localSocket).getSession();
+ localSocket.connect(address, Config.SOCKET_TIMEOUT * 1000);
+ final SSLSession session = ((SSLSocket) localSocket).getSession();
if (!tlsFactoryVerifier.verifier.verify(account.getServer().getDomainpart(), session)) {
Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": TLS certificate verification failed");
throw new SecurityException();
@@ -338,19 +338,19 @@ public class XmppConnection implements Runnable {
}
} else {
localSocket = new Socket();
- localSocket.connect(address, Config.SOCKET_TIMEOUT * 1000);
+ localSocket.connect(address, Config.SOCKET_TIMEOUT * 1000);
}
} catch (IOException e) {
throw new UnknownHostException();
}
try {
- startXmpp(localSocket);
- } catch (InterruptedException e) {
- Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream");
- return;
- } catch (Exception e) {
- throw new IOException(e.getMessage());
- }
+ startXmpp(localSocket);
+ } catch (InterruptedException e) {
+ Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream");
+ return;
+ } catch (Exception e) {
+ throw new IOException(e.getMessage());
+ }
} else if (DNSHelper.isIp(account.getServer().toString())) {
localSocket = new Socket();
try {
@@ -359,13 +359,13 @@ public class XmppConnection implements Runnable {
throw new UnknownHostException();
}
try {
- startXmpp(localSocket);
- } catch (InterruptedException e) {
- Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream");
- return;
- } catch (Exception e) {
- throw new IOException(e.getMessage());
- }
+ startXmpp(localSocket);
+ } catch (InterruptedException e) {
+ Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream");
+ return;
+ } catch (Exception e) {
+ throw new IOException(e.getMessage());
+ }
} else {
final Bundle result = DNSHelper.getSRVRecord(account.getServer(), mXmppConnectionService);
final ArrayList<Parcelable> values = result.getParcelableArrayList("values");
@@ -402,7 +402,7 @@ public class XmppConnection implements Runnable {
if (!features.encryptionEnabled) {
localSocket = new Socket();
- localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
+ localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
} else {
final TlsFactoryVerifier tlsFactoryVerifier = getTlsFactoryVerifier();
localSocket = tlsFactoryVerifier.factory.createSocket();
@@ -412,8 +412,8 @@ public class XmppConnection implements Runnable {
}
SSLSocketHelper.setSecurity((SSLSocket) localSocket);
- SSLSocketHelper.setSNIHost(tlsFactoryVerifier.factory, (SSLSocket) localSocket, account.getServer().getDomainpart());
- SSLSocketHelper.setAlpnProtocol(tlsFactoryVerifier.factory, (SSLSocket) localSocket, "xmpp-client");
+ SSLSocketHelper.setSNIHost(tlsFactoryVerifier.factory, (SSLSocket) localSocket, account.getServer().getDomainpart());
+ SSLSocketHelper.setAlpnProtocol(tlsFactoryVerifier.factory, (SSLSocket) localSocket, "xmpp-client");
localSocket.connect(addr, Config.SOCKET_TIMEOUT * 1000);
@@ -423,13 +423,16 @@ public class XmppConnection implements Runnable {
}
}
- if (startXmpp(localSocket))
+ if (startXmpp(localSocket)) {
break; // successfully connected to server that speaks xmpp
+ } else {
+ localSocket.close();
+ }
} catch (final SecurityException e) {
throw e;
} catch (InterruptedException e) {
- Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream");
- return;
+ Log.d(Config.LOGTAG,account.getJid().toBareJid()+": thread was interrupted before beginning stream");
+ return;
} catch (final Throwable e) {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage() + "(" + e.getClass().getName() + ")");
if (!iterator.hasNext()) {
@@ -467,15 +470,15 @@ public class XmppConnection implements Runnable {
this.attempt = Math.max(0, this.attempt - 1);
} finally {
if (!Thread.currentThread().isInterrupted()) {
- forceCloseSocket();
- if (wakeLock.isHeld()) {
- try {
- wakeLock.release();
- } catch (final RuntimeException ignored) {
- }
+ forceCloseSocket();
+ if (wakeLock.isHeld()) {
+ try {
+ wakeLock.release();
+ } catch (final RuntimeException ignored) {
+ }
}
} else {
- Log.d(Config.LOGTAG,account.getJid().toBareJid()+": not force closing socket and releasing wake lock because thread was interrupted");
+ Log.d(Config.LOGTAG,account.getJid().toBareJid()+": not force closing socket and releasing wake lock because thread was interrupted");
}
}
@@ -487,16 +490,16 @@ public class XmppConnection implements Runnable {
* @return true if server returns with valid xmpp, false otherwise
*/
private boolean startXmpp(Socket socket) throws Exception {
- if (Thread.currentThread().isInterrupted()) {
- throw new InterruptedException();
- }
- this.socket = socket;
+ if (Thread.currentThread().isInterrupted()) {
+ throw new InterruptedException();
+ }
+ this.socket = socket;
tagWriter.setOutputStream(socket.getOutputStream());
tagReader.setInputStream(socket.getInputStream());
tagWriter.beginDocument();
sendStartStream();
final Tag tag = tagReader.readTag();
- return tag != null && tag.isStart("stream");
+ return tag != null && tag.isStart("stream");
}
private static class TlsFactoryVerifier {
@@ -865,7 +868,7 @@ public class XmppConnection implements Runnable {
throw new IncompatibleServerException();
}
} else if (!this.streamFeatures.hasChild("register") && account.isOptionSet(Account.OPTION_REGISTER)) {
- throw new RegistrationNotSupportedException();
+ throw new RegistrationNotSupportedException();
} else if (this.streamFeatures.hasChild("mechanisms")
&& shouldAuthenticate
&& (features.encryptionEnabled || Config.ALLOW_NON_TLS_CONNECTIONS)) {