From 258732c9dfe2d1dde770a6117b104267fe434d67 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Fri, 11 Dec 2015 00:14:49 +0100 Subject: Removed stupid DNS query code --- .../conversationsplus/xmpp/XmppConnection.java | 78 +++++++--------------- 1 file changed, 23 insertions(+), 55 deletions(-) (limited to 'src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java') diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java index 37137eb3..d5ab9f90 100644 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/XmppConnection.java @@ -1,8 +1,6 @@ package de.thedevstack.conversationsplus.xmpp; import android.content.Context; -import android.os.Bundle; -import android.os.Parcelable; import android.os.PowerManager; import android.os.PowerManager.WakeLock; import android.os.SystemClock; @@ -19,7 +17,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; import java.net.ConnectException; -import java.net.IDN; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; @@ -35,6 +32,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.TreeSet; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; @@ -48,6 +46,7 @@ import de.thedevstack.conversationsplus.crypto.sasl.DigestMd5; import de.thedevstack.conversationsplus.crypto.sasl.Plain; import de.thedevstack.conversationsplus.crypto.sasl.SaslMechanism; import de.thedevstack.conversationsplus.crypto.sasl.ScramSha1; +import de.thedevstack.conversationsplus.dto.SrvRecord; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.generator.IqGenerator; import de.thedevstack.conversationsplus.services.XmppConnectionService; @@ -74,11 +73,10 @@ import de.thedevstack.conversationsplus.xmpp.stanzas.streammgmt.RequestPacket; import de.thedevstack.conversationsplus.xmpp.stanzas.streammgmt.ResumePacket; public class XmppConnection implements Runnable { - + private static final int DEFAULT_PORT = 5222; private static final int PACKET_IQ = 0; private static final int PACKET_MESSAGE = 1; private static final int PACKET_PRESENCE = 2; - private final Context applicationContext; protected Account account; private final WakeLock wakeLock; private Socket socket; @@ -120,7 +118,6 @@ public class XmppConnection implements Runnable { PowerManager.PARTIAL_WAKE_LOCK, account.getJid().toBareJid().toString()); tagWriter = new TagWriter(); mXmppConnectionService = service; - applicationContext = service.getApplicationContext(); } protected void changeStatus(final Account.State nextStatus) { @@ -156,59 +153,30 @@ public class XmppConnection implements Runnable { if (DNSHelper.isIp(account.getServer().toString())) { socket = new Socket(); try { - socket.connect(new InetSocketAddress(account.getServer().toString(), 5222), Config.SOCKET_TIMEOUT * 1000); + socket.connect(new InetSocketAddress(account.getServer().toString(), DEFAULT_PORT), Config.SOCKET_TIMEOUT * 1000); } catch (IOException e) { throw new UnknownHostException(); } } else { - final Bundle result = DNSHelper.getSRVRecord(account.getServer()); - final ArrayList values = result.getParcelableArrayList("values"); - if ("timeout".equals(result.getString("error"))) { - throw new IOException("timeout in dns"); - } else if (values != null) { - int i = 0; - boolean socketError = true; - while (socketError && values.size() > i) { - final Bundle namePort = (Bundle) values.get(i); - try { - String srvRecordServer; - try { - srvRecordServer = IDN.toASCII(namePort.getString("name")); - } catch (final IllegalArgumentException e) { - // TODO: Handle me?` - srvRecordServer = ""; - } - final int srvRecordPort = namePort.getInt("port"); - final String srvIpServer = namePort.getString("ip"); - final InetSocketAddress addr; - if (srvIpServer != null) { - addr = new InetSocketAddress(srvIpServer, srvRecordPort); - Logging.d(Config.LOGTAG, account.getJid().toBareJid().toString() - + ": using values from dns " + srvRecordServer - + "[" + srvIpServer + "]:" + srvRecordPort); - } else { - addr = new InetSocketAddress(srvRecordServer, srvRecordPort); - Logging.d(Config.LOGTAG, account.getJid().toBareJid().toString() - + ": using values from dns " - + srvRecordServer + ":" + srvRecordPort); - } - socket = new Socket(); - socket.connect(addr, Config.SOCKET_TIMEOUT * 1000); - socketError = false; - } catch (final UnknownHostException e) { - Logging.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage()); - i++; - } catch (final IOException e) { - Logging.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage()); - i++; - } - } - if (socketError) { - throw new UnknownHostException(); - } - } else { - throw new IOException("unhandled exception in DNS resolver"); - } + socket = new Socket(); + TreeSet result = DNSHelper.querySrvRecord(account.getServer()); + if (!result.isEmpty()) { + for (SrvRecord record : result) { + try { + socket.connect(new InetSocketAddress(record.getName(), record.getPort()), Config.SOCKET_TIMEOUT * 1000); + break; + } catch (IOException e) { + Logging.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": " + e.getMessage()); + } + } + } + if (result.isEmpty() || !socket.isConnected()){ + try { + socket.connect(new InetSocketAddress(account.getServer().getDomainpart(), DEFAULT_PORT), Config.SOCKET_TIMEOUT * 1000); + } catch (IOException e) { + throw new UnknownHostException(); + } + } } final OutputStream out = socket.getOutputStream(); tagWriter.setOutputStream(out); -- cgit v1.2.3