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)) 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,71 +42,79 @@ 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();
String[] hostParts = host.split("\\."); try {
byte[] transId = new byte[2]; Log.d("xmppService","using dns server: "+dnsServer.toString()+" to look up "+host);
Random random = new Random(); String[] hostParts = host.split("\\.");
random.nextBytes(transId); byte[] transId = new byte[2];
byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, Random random = new Random();
0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63, random.nextBytes(transId);
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 }; byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29, 0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 };
ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29,
output.write(transId); 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
output.write(header); ByteArrayOutputStream output = new ByteArrayOutputStream();
for (int i = 0; i < hostParts.length; ++i) { output.write(transId);
char[] tmpChars = hostParts[i].toCharArray(); output.write(header);
byte[] tmp = new byte[tmpChars.length]; for (int i = 0; i < hostParts.length; ++i) {
for (int j = 0; j < tmpChars.length; ++j) { char[] tmpChars = hostParts[i].toCharArray();
tmp[j] = (byte) tmpChars[j]; byte[] tmp = new byte[tmpChars.length];
} for (int j = 0; j < tmpChars.length; ++j) {
output.write(tmp.length); tmp[j] = (byte) tmpChars[j];
output.write(tmp);
} }
output.write(rest); output.write(tmp.length);
byte[] sendPaket = output.toByteArray(); output.write(tmp);
int realLenght = sendPaket.length - 11; }
DatagramPacket packet = new DatagramPacket(sendPaket, output.write(rest);
sendPaket.length, ip, 53); byte[] sendPaket = output.toByteArray();
DatagramSocket datagramSocket = new DatagramSocket(); int realLenght = sendPaket.length - 11;
datagramSocket.send(packet); DatagramPacket packet = new DatagramPacket(sendPaket,
byte[] receiveData = new byte[1024]; sendPaket.length, dnsServer, 53);
DatagramSocket datagramSocket = new DatagramSocket();
datagramSocket.send(packet);
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");
return namePort; return namePort;
}
namePort.putInt("port",calcPort(receiveData[realLenght + 16],
receiveData[realLenght + 17]));
int i = realLenght + 18;
int wordLenght = 0;
StringBuilder builder = new StringBuilder();
while (receiveData[i] != 0) {
if (wordLenght > 0) {
builder.append((char) receiveData[i]);
--wordLenght;
} else {
wordLenght = receiveData[i];
builder.append(".");
} }
namePort.putInt("port",calcPort(receiveData[realLenght + 16], ++i;
receiveData[realLenght + 17])); }
int i = realLenght + 18; builder.replace(0, 1, "");
int wordLenght = 0; byte type = receiveData[i+1];
StringBuilder builder = new StringBuilder(); if (type!=-64) {
while (receiveData[i] != 0) { namePort.putString("error", "nosrv");
if (wordLenght > 0) { return namePort;
builder.append((char) receiveData[i]); }
--wordLenght; namePort.putString("name",builder.toString());
} else {
wordLenght = receiveData[i];
builder.append(".");
}
++i;
}
builder.replace(0, 1, "");
byte type = receiveData[i+1];
if (type!=-64) {
namePort.putString("error", "nosrv");
return namePort;
}
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) {