DNSHelper.java

 1package eu.siacs.conversations.utils;
 2
 3import java.io.ByteArrayOutputStream;
 4import java.io.IOException;
 5import java.net.DatagramPacket;
 6import java.net.DatagramSocket;
 7import java.net.InetAddress;
 8import java.util.Random;
 9
10import android.os.Bundle;
11
12public class DNSHelper {
13	public static Bundle getSRVRecord(String host) throws IOException {
14		Bundle namePort = new Bundle();
15			String[] hostParts = host.split("\\.");
16			byte[] transId = new byte[2];
17			Random random = new Random();
18			random.nextBytes(transId);
19			byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
20					0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63,
21					0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 };
22			byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29,
23					0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
24			ByteArrayOutputStream output = new ByteArrayOutputStream();
25			output.write(transId);
26			output.write(header);
27			for (int i = 0; i < hostParts.length; ++i) {
28				char[] tmpChars = hostParts[i].toCharArray();
29				byte[] tmp = new byte[tmpChars.length];
30				for (int j = 0; j < tmpChars.length; ++j) {
31					tmp[j] = (byte) tmpChars[j];
32				}
33				output.write(tmp.length);
34				output.write(tmp);
35			}
36			output.write(rest);
37			byte[] sendPaket = output.toByteArray();
38			byte[] addr = { 0x8, 0x8, 0x8, 0x8 };
39			int realLenght = sendPaket.length - 11;
40			DatagramPacket packet = new DatagramPacket(sendPaket,
41					sendPaket.length, InetAddress.getByAddress(addr), 53);
42			DatagramSocket datagramSocket = new DatagramSocket();
43			datagramSocket.send(packet);
44			byte[] receiveData = new byte[1024];
45
46			DatagramPacket receivePacket = new DatagramPacket(receiveData,
47					receiveData.length);
48			datagramSocket.setSoTimeout(2000);
49			datagramSocket.receive(receivePacket);
50			if (receiveData[3]!=-128) {
51				namePort.putString("error", "nosrv");
52				return namePort;
53			}
54			namePort.putInt("port",calcPort(receiveData[realLenght + 16],
55							receiveData[realLenght + 17]));
56			int i = realLenght + 18;
57			int wordLenght = 0;
58			StringBuilder builder = new StringBuilder();
59			while (receiveData[i] != 0) {
60				if (wordLenght > 0) {
61					builder.append((char) receiveData[i]);
62					--wordLenght;
63				} else {
64					wordLenght = receiveData[i];
65					builder.append(".");
66				}
67				++i;
68			}
69			builder.replace(0, 1, "");
70			namePort.putString("name",builder.toString());
71		return namePort;
72	}
73
74	static int calcPort(byte hb, byte lb) {
75		return ((int) hb << 8) | ((int) lb & 0xFF);
76	}
77	
78	final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
79	public static String bytesToHex(byte[] bytes) {
80	    char[] hexChars = new char[bytes.length * 2];
81	    for ( int j = 0; j < bytes.length; j++ ) {
82	        int v = bytes[j] & 0xFF;
83	        hexChars[j * 2] = hexArray[v >>> 4];
84	        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
85	    }
86	    return new String(hexChars);
87	}
88}