DNSHelper.java

  1package eu.siacs.conversations.utils;
  2
  3import java.io.ByteArrayOutputStream;
  4import java.io.IOException;
  5import java.lang.reflect.InvocationTargetException;
  6import java.lang.reflect.Method;
  7import java.net.DatagramPacket;
  8import java.net.DatagramSocket;
  9import java.net.InetAddress;
 10import java.util.ArrayList;
 11import java.util.Random;
 12
 13import android.os.Bundle;
 14import android.util.Log;
 15
 16public class DNSHelper {
 17	public static Bundle getSRVRecord(String host) throws IOException {
 18		InetAddress ip = InetAddress.getByName("8.8.8.8");
 19		try {
 20			Class<?> SystemProperties = Class.forName("android.os.SystemProperties");
 21			Method method = SystemProperties.getMethod("get", new Class[] { String.class });
 22			ArrayList<String> servers = new ArrayList<String>();
 23			for (String name : new String[] { "net.dns1", "net.dns2", "net.dns3", "net.dns4", }) {
 24			    String value = (String) method.invoke(null, name);
 25			    
 26				if (value != null && !"".equals(value) && !servers.contains(value))
 27			    	ip = InetAddress.getByName(value);
 28			        servers.add(value);
 29			        Bundle result = queryDNS(host, ip);
 30			        if (!result.containsKey("error")) {
 31			        	return result;
 32			        }
 33			}
 34		} catch (ClassNotFoundException e) {
 35			ip = InetAddress.getByName("8.8.8.8");
 36		} catch (NoSuchMethodException e) {
 37			ip = InetAddress.getByName("8.8.8.8");
 38		} catch (IllegalAccessException e) {
 39			ip = InetAddress.getByName("8.8.8.8");
 40		} catch (IllegalArgumentException e) {
 41			ip = InetAddress.getByName("8.8.8.8");
 42		} catch (InvocationTargetException e) {
 43			ip = InetAddress.getByName("8.8.8.8");
 44		}
 45		return queryDNS(host, ip);
 46	}
 47	
 48	public static Bundle queryDNS(String host, InetAddress dnsServer) {
 49		Bundle namePort = new Bundle();
 50		try {
 51		Log.d("xmppService","using dns server: "+dnsServer.toString()+" to look up "+host);
 52		String[] hostParts = host.split("\\.");
 53		byte[] transId = new byte[2];
 54		Random random = new Random();
 55		random.nextBytes(transId);
 56		byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
 57				0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63,
 58				0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 };
 59		byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29,
 60				0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 61		ByteArrayOutputStream output = new ByteArrayOutputStream();
 62		output.write(transId);
 63		output.write(header);
 64		for (int i = 0; i < hostParts.length; ++i) {
 65			char[] tmpChars = hostParts[i].toCharArray();
 66			byte[] tmp = new byte[tmpChars.length];
 67			for (int j = 0; j < tmpChars.length; ++j) {
 68				tmp[j] = (byte) tmpChars[j];
 69			}
 70			output.write(tmp.length);
 71			output.write(tmp);
 72		}
 73		output.write(rest);
 74		byte[] sendPaket = output.toByteArray();
 75		int realLenght = sendPaket.length - 11;
 76		DatagramPacket packet = new DatagramPacket(sendPaket,
 77				sendPaket.length, dnsServer, 53);
 78		DatagramSocket datagramSocket = new DatagramSocket();
 79		datagramSocket.send(packet);
 80		byte[] receiveData = new byte[1024];
 81
 82		DatagramPacket receivePacket = new DatagramPacket(receiveData,
 83				receiveData.length);
 84		datagramSocket.setSoTimeout(3000);
 85		datagramSocket.receive(receivePacket);
 86		if (receiveData[3]!=-128) {
 87			namePort.putString("error", "nosrv");
 88			return namePort;
 89		}
 90		namePort.putInt("port",calcPort(receiveData[realLenght + 16],
 91						receiveData[realLenght + 17]));
 92		int i = realLenght + 18;
 93		int wordLenght = 0;
 94		StringBuilder builder = new StringBuilder();
 95		while (receiveData[i] != 0) {
 96			if (wordLenght > 0) {
 97				builder.append((char) receiveData[i]);
 98				--wordLenght;
 99			} else {
100				wordLenght = receiveData[i];
101				builder.append(".");
102			}
103			++i;
104		}
105		builder.replace(0, 1, "");
106		byte type = receiveData[i+1];
107		if (type!=-64) {
108			namePort.putString("error", "nosrv");
109			return namePort;
110		}
111		namePort.putString("name",builder.toString());
112		return namePort;
113		} catch (IOException e) {
114			Log.d("xmppService","io execpiton during dns");
115			namePort.putString("error", "nosrv");
116			return namePort;
117		}
118	}
119
120	static int calcPort(byte hb, byte lb) {
121		int port = ((int) hb << 8) | ((int) lb & 0xFF);
122		if (port>=0) {
123			return port;
124		} else {
125			return 65536 + port;
126		}
127	}
128	
129	final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
130	public static String bytesToHex(byte[] bytes) {
131	    char[] hexChars = new char[bytes.length * 2];
132	    for ( int j = 0; j < bytes.length; j++ ) {
133	        int v = bytes[j] & 0xFF;
134	        hexChars[j * 2] = hexArray[v >>> 4];
135	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
136	    }
137	    return new String(hexChars);
138	}
139}