further dns improvements

This commit is contained in:
Daniel Gultsch 2014-03-27 11:03:10 +01:00
parent 4864f7200b
commit f99887d7ca
1 changed files with 72 additions and 60 deletions

View File

@ -26,6 +26,10 @@ public class DNSHelper {
if (value != null && !"".equals(value) && !servers.contains(value))
ip = InetAddress.getByName(value);
servers.add(value);
Bundle result = queryDNS(host, ip);
if (!result.containsKey("error")) {
return result;
}
}
} catch (ClassNotFoundException e) {
ip = InetAddress.getByName("8.8.8.8");
@ -38,10 +42,13 @@ public class DNSHelper {
} catch (InvocationTargetException e) {
ip = InetAddress.getByName("8.8.8.8");
}
return queryDNS(host, ip);
}
Log.d("xmppService","using dns server: "+ip.toString()+" to look up SRV records");
public static Bundle queryDNS(String host, InetAddress dnsServer) {
Bundle namePort = new Bundle();
try {
Log.d("xmppService","using dns server: "+dnsServer.toString()+" to look up "+host);
String[] hostParts = host.split("\\.");
byte[] transId = new byte[2];
Random random = new Random();
@ -67,14 +74,14 @@ public class DNSHelper {
byte[] sendPaket = output.toByteArray();
int realLenght = sendPaket.length - 11;
DatagramPacket packet = new DatagramPacket(sendPaket,
sendPaket.length, ip, 53);
sendPaket.length, dnsServer, 53);
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(packet);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
datagramSocket.setSoTimeout(2000);
datagramSocket.setSoTimeout(3000);
datagramSocket.receive(receivePacket);
if (receiveData[3]!=-128) {
namePort.putString("error", "nosrv");
@ -103,6 +110,11 @@ public class DNSHelper {
}
namePort.putString("name",builder.toString());
return namePort;
} catch (IOException e) {
Log.d("xmppService","io execpiton during dns");
namePort.putString("error", "nosrv");
return namePort;
}
}
static int calcPort(byte hb, byte lb) {