more dns fixes. hopefully the last

This commit is contained in:
Daniel Gultsch 2014-03-27 14:31:55 +01:00
parent 800c18956b
commit 8988873d77
1 changed files with 96 additions and 82 deletions

View File

@ -17,19 +17,24 @@ public class DNSHelper {
public static Bundle getSRVRecord(String host) throws IOException { public static Bundle getSRVRecord(String host) throws IOException {
InetAddress ip = InetAddress.getByName("8.8.8.8"); InetAddress ip = InetAddress.getByName("8.8.8.8");
try { try {
Class<?> SystemProperties = Class.forName("android.os.SystemProperties"); Class<?> SystemProperties = Class
Method method = SystemProperties.getMethod("get", new Class[] { String.class }); .forName("android.os.SystemProperties");
Method method = SystemProperties.getMethod("get",
new Class[] { String.class });
ArrayList<String> servers = new ArrayList<String>(); ArrayList<String> servers = new ArrayList<String>();
for (String name : new String[] { "net.dns1", "net.dns2", "net.dns3", "net.dns4", }) { for (String name : new String[] { "net.dns1", "net.dns2",
String value = (String) method.invoke(null, name); "net.dns3", "net.dns4", }) {
String value = (String) method.invoke(null, name);
if (value != null && !"".equals(value) && !servers.contains(value)) if (value != null && !"".equals(value)
ip = InetAddress.getByName(value); && !servers.contains(value)) {
servers.add(value); ip = InetAddress.getByName(value);
Bundle result = queryDNS(host, ip); servers.add(value);
if (!result.containsKey("error")) { Bundle result = queryDNS(host, ip);
return result; 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");
@ -48,70 +53,78 @@ public class DNSHelper {
public static Bundle queryDNS(String host, InetAddress dnsServer) { public static Bundle queryDNS(String host, InetAddress dnsServer) {
Bundle namePort = new Bundle(); Bundle namePort = new Bundle();
try { try {
Log.d("xmppService","using dns server: "+dnsServer.toString()+" to look up "+host); Log.d("xmppService", "using dns server: " + dnsServer.toString()
String[] hostParts = host.split("\\."); + " to look up " + host);
byte[] transId = new byte[2]; String[] hostParts = host.split("\\.");
Random random = new Random(); byte[] transId = new byte[2];
random.nextBytes(transId); Random random = new Random();
byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, random.nextBytes(transId);
0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63, byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 }; 0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63,
byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 };
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29,
ByteArrayOutputStream output = new ByteArrayOutputStream(); 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
output.write(transId); ByteArrayOutputStream output = new ByteArrayOutputStream();
output.write(header); output.write(transId);
for (int i = 0; i < hostParts.length; ++i) { output.write(header);
char[] tmpChars = hostParts[i].toCharArray(); for (int i = 0; i < hostParts.length; ++i) {
byte[] tmp = new byte[tmpChars.length]; char[] tmpChars = hostParts[i].toCharArray();
for (int j = 0; j < tmpChars.length; ++j) { byte[] tmp = new byte[tmpChars.length];
tmp[j] = (byte) tmpChars[j]; for (int j = 0; j < tmpChars.length; ++j) {
tmp[j] = (byte) tmpChars[j];
}
output.write(tmp.length);
output.write(tmp);
} }
output.write(tmp.length); output.write(rest);
output.write(tmp); byte[] sendPaket = output.toByteArray();
} int realLenght = sendPaket.length - 11;
output.write(rest); DatagramPacket packet = new DatagramPacket(sendPaket,
byte[] sendPaket = output.toByteArray(); sendPaket.length, dnsServer, 53);
int realLenght = sendPaket.length - 11; DatagramSocket datagramSocket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(sendPaket, datagramSocket.send(packet);
sendPaket.length, dnsServer, 53); byte[] receiveData = new byte[1024];
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(3000); datagramSocket.setSoTimeout(3000);
datagramSocket.receive(receivePacket); datagramSocket.receive(receivePacket);
if (receiveData[3]!=-128) { if (receiveData[3] != -128) {
namePort.putString("error", "nosrv"); Log.d("xmppService", "not the right count");
return namePort; namePort.putString("error", "nosrv");
} return namePort;
namePort.putInt("port",calcPort(receiveData[realLenght + 16], }
receiveData[realLenght + 17])); namePort.putInt(
int i = realLenght + 18; "port",
int wordLenght = 0; calcPort(receiveData[realLenght + 16],
StringBuilder builder = new StringBuilder(); receiveData[realLenght + 17]));
while (receiveData[i] != 0) { int i = realLenght + 18;
if (wordLenght > 0) { int wordLenght = 0;
builder.append((char) receiveData[i]); StringBuilder builder = new StringBuilder();
--wordLenght; while (receiveData[i] != 0) {
} else { if (wordLenght > 0) {
wordLenght = receiveData[i]; builder.append((char) receiveData[i]);
builder.append("."); --wordLenght;
} else {
wordLenght = receiveData[i];
builder.append(".");
}
++i;
}
builder.replace(0, 1, "");
byte type = receiveData[i + 1];
byte type2 = receiveData[i + 2];
if ((type == -64) || (type == type2)) {
namePort.putString("name", builder.toString());
return namePort;
} else {
Log.d("xmppService", "type=" + type + " type2=" + type2 + " "
+ builder.toString());
namePort.putString("error", "nosrv");
return namePort;
} }
++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;
} catch (IOException e) { } catch (IOException e) {
Log.d("xmppService","io execpiton during dns"); Log.d("xmppService", "io execpiton during dns");
namePort.putString("error", "nosrv"); namePort.putString("error", "nosrv");
return namePort; return namePort;
} }
@ -119,7 +132,7 @@ public class DNSHelper {
static int calcPort(byte hb, byte lb) { static int calcPort(byte hb, byte lb) {
int port = ((int) hb << 8) | ((int) lb & 0xFF); int port = ((int) hb << 8) | ((int) lb & 0xFF);
if (port>=0) { if (port >= 0) {
return port; return port;
} else { } else {
return 65536 + port; return 65536 + port;
@ -127,13 +140,14 @@ public class DNSHelper {
} }
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
public static String bytesToHex(byte[] bytes) { public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2]; char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) { for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF; int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F]; hexChars[j * 2 + 1] = hexArray[v & 0x0F];
} }
return new String(hexChars); return new String(hexChars);
} }
} }