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			}
 30		} catch (ClassNotFoundException e) {
 31			ip = InetAddress.getByName("8.8.8.8");
 32		} catch (NoSuchMethodException e) {
 33			ip = InetAddress.getByName("8.8.8.8");
 34		} catch (IllegalAccessException e) {
 35			ip = InetAddress.getByName("8.8.8.8");
 36		} catch (IllegalArgumentException e) {
 37			ip = InetAddress.getByName("8.8.8.8");
 38		} catch (InvocationTargetException e) {
 39			ip = InetAddress.getByName("8.8.8.8");
 40		}
 41		
 42		Log.d("xmppService","using dns server: "+ip.toString()+" to look up SRV records");
 43		
 44		Bundle namePort = new Bundle();
 45			String[] hostParts = host.split("\\.");
 46			byte[] transId = new byte[2];
 47			Random random = new Random();
 48			random.nextBytes(transId);
 49			byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
 50					0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63,
 51					0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 };
 52			byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29,
 53					0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
 54			ByteArrayOutputStream output = new ByteArrayOutputStream();
 55			output.write(transId);
 56			output.write(header);
 57			for (int i = 0; i < hostParts.length; ++i) {
 58				char[] tmpChars = hostParts[i].toCharArray();
 59				byte[] tmp = new byte[tmpChars.length];
 60				for (int j = 0; j < tmpChars.length; ++j) {
 61					tmp[j] = (byte) tmpChars[j];
 62				}
 63				output.write(tmp.length);
 64				output.write(tmp);
 65			}
 66			output.write(rest);
 67			byte[] sendPaket = output.toByteArray();
 68			int realLenght = sendPaket.length - 11;
 69			DatagramPacket packet = new DatagramPacket(sendPaket,
 70					sendPaket.length, ip, 53);
 71			DatagramSocket datagramSocket = new DatagramSocket();
 72			datagramSocket.send(packet);
 73			byte[] receiveData = new byte[1024];
 74
 75			DatagramPacket receivePacket = new DatagramPacket(receiveData,
 76					receiveData.length);
 77			datagramSocket.setSoTimeout(2000);
 78			datagramSocket.receive(receivePacket);
 79			if (receiveData[3]!=-128) {
 80				namePort.putString("error", "nosrv");
 81				return namePort;
 82			}
 83			namePort.putInt("port",calcPort(receiveData[realLenght + 16],
 84							receiveData[realLenght + 17]));
 85			int i = realLenght + 18;
 86			int wordLenght = 0;
 87			StringBuilder builder = new StringBuilder();
 88			while (receiveData[i] != 0) {
 89				if (wordLenght > 0) {
 90					builder.append((char) receiveData[i]);
 91					--wordLenght;
 92				} else {
 93					wordLenght = receiveData[i];
 94					builder.append(".");
 95				}
 96				++i;
 97			}
 98			builder.replace(0, 1, "");
 99			namePort.putString("name",builder.toString());
100		return namePort;
101	}
102
103	static int calcPort(byte hb, byte lb) {
104		return ((int) hb << 8) | ((int) lb & 0xFF);
105	}
106	
107	final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
108	public static String bytesToHex(byte[] bytes) {
109	    char[] hexChars = new char[bytes.length * 2];
110	    for ( int j = 0; j < bytes.length; j++ ) {
111	        int v = bytes[j] & 0xFF;
112	        hexChars[j * 2] = hexArray[v >>> 4];
113	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
114	    }
115	    return new String(hexChars);
116	}
117}