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

View file

@ -17,13 +17,17 @@ 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",
"net.dns3", "net.dns4", }) {
String value = (String) method.invoke(null, name); String value = (String) method.invoke(null, name);
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); Bundle result = queryDNS(host, ip);
@ -31,6 +35,7 @@ public class DNSHelper {
return result; return result;
} }
} }
}
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
ip = InetAddress.getByName("8.8.8.8"); ip = InetAddress.getByName("8.8.8.8");
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
@ -48,7 +53,8 @@ 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()
+ " 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();
@ -84,10 +90,13 @@ public class DNSHelper {
datagramSocket.setSoTimeout(3000); datagramSocket.setSoTimeout(3000);
datagramSocket.receive(receivePacket); datagramSocket.receive(receivePacket);
if (receiveData[3] != -128) { if (receiveData[3] != -128) {
Log.d("xmppService", "not the right count");
namePort.putString("error", "nosrv"); namePort.putString("error", "nosrv");
return namePort; return namePort;
} }
namePort.putInt("port",calcPort(receiveData[realLenght + 16], namePort.putInt(
"port",
calcPort(receiveData[realLenght + 16],
receiveData[realLenght + 17])); receiveData[realLenght + 17]));
int i = realLenght + 18; int i = realLenght + 18;
int wordLenght = 0; int wordLenght = 0;
@ -104,12 +113,16 @@ public class DNSHelper {
} }
builder.replace(0, 1, ""); builder.replace(0, 1, "");
byte type = receiveData[i + 1]; byte type = receiveData[i + 1];
if (type!=-64) { 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"); namePort.putString("error", "nosrv");
return namePort; 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");
@ -127,6 +140,7 @@ 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++) {