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