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")||("nosrv".equals(result.getString("error")))) {
35 return result;
36 }
37 }
38 }
39 } catch (Exception e) {
40 Log.d("xmppService","error during system calls");
41 }
42 ip = InetAddress.getByName("8.8.8.8");
43 return queryDNS(host, ip);
44 }
45
46 public static Bundle queryDNS(String host, InetAddress dnsServer) {
47 Bundle namePort = new Bundle();
48 try {
49 Log.d("xmppService", "using dns server: " + dnsServer.toString()
50 + " to look up " + host);
51 String[] hostParts = host.split("\\.");
52 byte[] transId = new byte[2];
53 Random random = new Random();
54 random.nextBytes(transId);
55 byte[] header = { 0x01, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x01, 0x0c, 0x5f, 0x78, 0x6d, 0x70, 0x70, 0x2d, 0x63,
57 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x04, 0x5f, 0x74, 0x63, 0x70 };
58 byte[] rest = { 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x29,
59 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
60 ByteArrayOutputStream output = new ByteArrayOutputStream();
61 output.write(transId);
62 output.write(header);
63 for (int i = 0; i < hostParts.length; ++i) {
64 char[] tmpChars = hostParts[i].toCharArray();
65 byte[] tmp = new byte[tmpChars.length];
66 for (int j = 0; j < tmpChars.length; ++j) {
67 tmp[j] = (byte) tmpChars[j];
68 }
69 output.write(tmp.length);
70 output.write(tmp);
71 }
72 output.write(rest);
73 byte[] sendPaket = output.toByteArray();
74 int realLenght = sendPaket.length - 11;
75 DatagramPacket packet = new DatagramPacket(sendPaket,
76 sendPaket.length, dnsServer, 53);
77 DatagramSocket datagramSocket = new DatagramSocket();
78 datagramSocket.send(packet);
79 byte[] receiveData = new byte[1024];
80
81 DatagramPacket receivePacket = new DatagramPacket(receiveData,
82 receiveData.length);
83 datagramSocket.setSoTimeout(7000); //die sieben ist meine zahl
84 datagramSocket.receive(receivePacket);
85 if (receiveData[3] != -128) {
86 namePort.putString("error", "nosrv");
87 return namePort;
88 }
89 namePort.putInt(
90 "port",
91 calcPort(receiveData[realLenght + 16],
92 receiveData[realLenght + 17]));
93 int i = realLenght + 18;
94 int wordLenght = 0;
95 StringBuilder builder = new StringBuilder();
96 while (receiveData[i] != 0) {
97 if (wordLenght > 0) {
98 builder.append((char) receiveData[i]);
99 --wordLenght;
100 } else {
101 wordLenght = receiveData[i];
102 builder.append(".");
103 }
104 ++i;
105 }
106 builder.replace(0, 1, "");
107 byte type = receiveData[i + 1];
108 byte type2 = receiveData[i + 2];
109 if ((type == -64) || (type == type2)) {
110 namePort.putString("name", builder.toString());
111 return namePort;
112 } else {
113 Log.d("xmppService", "type=" + type + " type2=" + type2 + " "
114 + builder.toString());
115 namePort.putString("error", "nosrv");
116 return namePort;
117 }
118 } catch (IOException e) {
119 Log.d("xmppService", "io execpiton during dns");
120 namePort.putString("error", "timeout");
121 return namePort;
122 }
123 }
124
125 static int calcPort(byte hb, byte lb) {
126 int port = ((int) hb << 8) | ((int) lb & 0xFF);
127 if (port >= 0) {
128 return port;
129 } else {
130 return 65536 + port;
131 }
132 }
133
134 final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
135
136 public static String bytesToHex(byte[] bytes) {
137 char[] hexChars = new char[bytes.length * 2];
138 for (int j = 0; j < bytes.length; j++) {
139 int v = bytes[j] & 0xFF;
140 hexChars[j * 2] = hexArray[v >>> 4];
141 hexChars[j * 2 + 1] = hexArray[v & 0x0F];
142 }
143 return new String(hexChars);
144 }
145}