further dns improvements

This commit is contained in:
Daniel Gultsch 2014-03-27 11:03:10 +01:00
parent 4864f7200b
commit f99887d7ca

View file

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