let DnsHelper provide a fallback solution

This commit is contained in:
Daniel Gultsch 2015-09-19 17:31:24 +02:00
parent 069ddddbc1
commit a954e32b16
2 changed files with 41 additions and 43 deletions

View file

@ -7,6 +7,7 @@ import android.net.LinkProperties;
import android.net.Network;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import java.io.IOException;
@ -46,13 +47,19 @@ public class DNSHelper {
public static Bundle getSRVRecord(final Jid jid, Context context) throws IOException {
final String host = jid.getDomainpart();
final List<InetAddress> servers = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDnsServers(context) : getDnsServersPreLollipop();
Bundle b = null;
Bundle b = new Bundle();
for(InetAddress server : servers) {
b = queryDNS(host, server);
if (b.containsKey("values")) {
return b;
}
}
if (!b.containsKey("values")) {
Log.d(Config.LOGTAG,"all dns queries failed. provide fallback A record");
ArrayList<Parcelable> values = new ArrayList<>();
values.add(createNamePortBundle(host,5222));
b.putParcelableArrayList("values",values);
}
return b;
}

View file

@ -160,13 +160,7 @@ public class XmppConnection implements Runnable {
}
} else {
final Bundle result = DNSHelper.getSRVRecord(account.getServer(),mXmppConnectionService);
if (result == null) {
throw new IOException("unhandled exception in DNS resolver");
}
final ArrayList<Parcelable> values = result.getParcelableArrayList("values");
if ("timeout".equals(result.getString("error"))) {
throw new DnsTimeoutException();
} else if (values != null) {
int i = 0;
boolean socketError = true;
while (socketError && values.size() > i) {
@ -204,9 +198,6 @@ public class XmppConnection implements Runnable {
if (socketError) {
throw new UnknownHostException();
}
} else {
throw new IOException("unhandled exception in DNS resolver");
}
}
final OutputStream out = socket.getOutputStream();
tagWriter.setOutputStream(out);