more error handling in xmppconnection

This commit is contained in:
iNPUTmice 2014-11-03 10:03:45 +01:00
parent 1ea2e7dc3b
commit 27f42dfb63

View File

@ -1,5 +1,19 @@
package eu.siacs.conversations.xmpp; package eu.siacs.conversations.xmpp;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.SparseArray;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -22,31 +36,16 @@ import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager; import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.StrictHostnameVerifier;
import org.xmlpull.v1.XmlPullParserException;
import de.duenndns.ssl.MemorizingTrustManager; import de.duenndns.ssl.MemorizingTrustManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.util.Log;
import android.util.SparseArray;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.DNSHelper; import eu.siacs.conversations.utils.DNSHelper;
import eu.siacs.conversations.utils.zlib.ZLibOutputStream;
import eu.siacs.conversations.utils.zlib.ZLibInputStream; import eu.siacs.conversations.utils.zlib.ZLibInputStream;
import eu.siacs.conversations.utils.zlib.ZLibOutputStream;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xml.Tag; import eu.siacs.conversations.xml.Tag;
import eu.siacs.conversations.xml.TagWriter; import eu.siacs.conversations.xml.TagWriter;
@ -173,7 +172,7 @@ public class XmppConnection implements Runnable {
int srvRecordPort = namePort.getInt("port"); int srvRecordPort = namePort.getInt("port");
String srvIpServer = namePort.getString("ipv4"); String srvIpServer = namePort.getString("ipv4");
InetSocketAddress addr; InetSocketAddress addr;
if (srvIpServer!=null) { if (srvIpServer != null) {
addr = new InetSocketAddress(srvIpServer, srvRecordPort); addr = new InetSocketAddress(srvIpServer, srvRecordPort);
Log.d(Config.LOGTAG, account.getJid() Log.d(Config.LOGTAG, account.getJid()
+ ": using values from dns " + srvRecordServer + ": using values from dns " + srvRecordServer
@ -188,13 +187,22 @@ public class XmppConnection implements Runnable {
socket.connect(addr, 20000); socket.connect(addr, 20000);
socketError = false; socketError = false;
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
i++; i++;
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
i++; i++;
} }
} }
if (socketError) { if (socketError) {
throw new IOException(); this.changeStatus(Account.STATUS_SERVER_NOT_FOUND);
if (wakeLock.isHeld()) {
try {
wakeLock.release();
} catch (RuntimeException re) {
}
}
return;
} }
} else if (result.containsKey("error") } else if (result.containsKey("error")
&& "nosrv".equals(result.getString("error", null))) { && "nosrv".equals(result.getString("error", null))) {
@ -235,6 +243,7 @@ public class XmppConnection implements Runnable {
} }
return; return;
} catch (IOException e) { } catch (IOException e) {
Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
this.changeStatus(Account.STATUS_OFFLINE); this.changeStatus(Account.STATUS_OFFLINE);
if (wakeLock.isHeld()) { if (wakeLock.isHeld()) {
try { try {
@ -244,6 +253,7 @@ public class XmppConnection implements Runnable {
} }
return; return;
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
this.changeStatus(Account.STATUS_OFFLINE); this.changeStatus(Account.STATUS_OFFLINE);
Log.d(Config.LOGTAG, "compression exception " + e.getMessage()); Log.d(Config.LOGTAG, "compression exception " + e.getMessage());
if (wakeLock.isHeld()) { if (wakeLock.isHeld()) {
@ -254,8 +264,8 @@ public class XmppConnection implements Runnable {
} }
return; return;
} catch (XmlPullParserException e) { } catch (XmlPullParserException e) {
Log.d(Config.LOGTAG, account.getJid() + ": " + e.getMessage());
this.changeStatus(Account.STATUS_OFFLINE); this.changeStatus(Account.STATUS_OFFLINE);
Log.d(Config.LOGTAG, "xml exception " + e.getMessage());
if (wakeLock.isHeld()) { if (wakeLock.isHeld()) {
try { try {
wakeLock.release(); wakeLock.release();
@ -549,10 +559,14 @@ public class XmppConnection implements Runnable {
try { try {
SSLContext sc = SSLContext.getInstance("TLS"); SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, sc.init(null,
new X509TrustManager[] { this.mMemorizingTrustManager }, new X509TrustManager[]{this.mMemorizingTrustManager},
mRandom); mRandom);
SSLSocketFactory factory = sc.getSocketFactory(); SSLSocketFactory factory = sc.getSocketFactory();
if (factory == null) {
throw new IOException("SSLSocketFactory was null");
}
HostnameVerifier verifier = this.mMemorizingTrustManager HostnameVerifier verifier = this.mMemorizingTrustManager
.wrapHostnameVerifier(new StrictHostnameVerifier()); .wrapHostnameVerifier(new StrictHostnameVerifier());
SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket, SSLSocket sslSocket = (SSLSocket) factory.createSocket(socket,
@ -578,10 +592,8 @@ public class XmppConnection implements Runnable {
if (verifier != null if (verifier != null
&& !verifier.verify(account.getServer(), && !verifier.verify(account.getServer(),
sslSocket.getSession())) { sslSocket.getSession())) {
Log.d(Config.LOGTAG, account.getJid()
+ ": host mismatch in TLS connection");
sslSocket.close(); sslSocket.close();
throw new IOException(); throw new IOException("host mismatch in TLS connection");
} }
tagReader.setInputStream(sslSocket.getInputStream()); tagReader.setInputStream(sslSocket.getInputStream());
tagWriter.setOutputStream(sslSocket.getOutputStream()); tagWriter.setOutputStream(sslSocket.getOutputStream());