summaryrefslogtreecommitdiffstats
blob: d2bbf0d1a221ef93075596014cc88899493e3004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "Swiften/Network/DomainNameResolver.h"
#include "Swiften/Base/Platform.h"

#include <stdlib.h>
#include <boost/asio.hpp>
#include <idna.h>
#ifdef SWIFTEN_PLATFORM_WINDOWS
#undef UNICODE
#include <windows.h>
#include <windns.h>
#ifndef DNS_TYPE_SRV
#define DNS_TYPE_SRV 33
#endif
#else
#include <arpa/nameser.h>
#include <arpa/nameser_compat.h>
#include <resolv.h>
#endif

#include "Swiften/Network/DomainNameResolveException.h"
#include "Swiften/Base/String.h"
#include "Swiften/Base/ByteArray.h"

namespace Swift {

DomainNameResolver::DomainNameResolver() {
}

DomainNameResolver::~DomainNameResolver() {
}

HostAddressPort DomainNameResolver::resolve(const String& domain) {
  char* output;
  if (idna_to_ascii_8z(domain.getUTF8Data(), &output, 0) == IDNA_SUCCESS) {
    std::string outputString(output);
    free(output);
    return resolveDomain(outputString);
  }
  else {
    return resolveDomain(domain.getUTF8String());
  }
}

HostAddressPort DomainNameResolver::resolveDomain(const std::string& domain) {
	try {
		return resolveXMPPService(domain);
	}
	catch (const DomainNameResolveException&) {
	}
	return HostAddressPort(resolveHostName(domain), 5222);
}

HostAddressPort DomainNameResolver::resolveXMPPService(const std::string& domain) {
  std::string srvQuery = "_xmpp-client._tcp." + domain;

#if defined(SWIFTEN_PLATFORM_WINDOWS)
	DNS_RECORD* responses;
	// FIXME: This conversion doesn't work if unicode is deffed above
  	if (DnsQuery(srvQuery.c_str(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &responses, NULL) != ERROR_SUCCESS) {
		throw DomainNameResolveException();
  	}

	DNS_RECORD* currentEntry = responses;
	while (currentEntry) {
		if (currentEntry->wType == DNS_TYPE_SRV) {
			int port = currentEntry->Data.SRV.wPort;
			try {
				// The pNameTarget is actually a PCWSTR, so I would have expected this 
				// conversion to not work at all, but it does.
				// Actually, it doesn't. Fix this and remove explicit cast
				// Remove unicode undef above as well
				std::string hostname((const char*) currentEntry->Data.SRV.pNameTarget);
				HostAddress address = resolveHostName(hostname);
				DnsRecordListFree(responses, DnsFreeRecordList);
				return HostAddressPort(address, port);
			}
			catch (const DomainNameResolveException&) {
			}
		}
		currentEntry = currentEntry->pNext;
	}
	DnsRecordListFree(responses, DnsFreeRecordList);

#else

  ByteArray response;
  response.resize(NS_PACKETSZ);
  int responseLength = res_query(const_cast<char*>(srvQuery.c_str()), ns_c_in, ns_t_srv, reinterpret_cast<u_char*>(response.getData()), response.getSize());
  if (responseLength == -1) {
		throw DomainNameResolveException();
	}

	// Parse header
	HEADER* header = reinterpret_cast<HEADER*>(response.getData());
	unsigned char* messageStart = reinterpret_cast<unsigned char*>(response.getData());
	unsigned char* messageEnd = messageStart + responseLength;
	unsigned char* currentEntry = messageStart + NS_HFIXEDSZ;

	// Skip over the queries
	int queriesCount = ntohs(header->qdcount);
	while (queriesCount > 0) {
		int entryLength = dn_skipname(currentEntry, messageEnd);
		if (entryLength < 0) {
			throw DomainNameResolveException();
		}
		currentEntry += entryLength + NS_QFIXEDSZ;
		queriesCount--;
	}

	// Process the SRV answers
	int answersCount = ntohs(header->ancount);
	while (answersCount > 0) {
		int entryLength = dn_skipname(currentEntry, messageEnd);
		currentEntry += entryLength;
		currentEntry += NS_RRFIXEDSZ;

		// Uninteresting information
		currentEntry += 2; // PRIORITY
		currentEntry += 2; // WEIGHT

		// Port
		if (currentEntry >= messageEnd) {
			throw DomainNameResolveException();
		}
		int port = ns_get16(currentEntry);
		currentEntry += 2; 

		// Hostname
		if (currentEntry >= messageEnd) {
			throw DomainNameResolveException();
		}
		ByteArray entry;
		entry.resize(NS_MAXDNAME);
		entryLength = dn_expand(messageStart, messageEnd, currentEntry, entry.getData(), entry.getSize());
		if (entryLength < 0) {
			throw DomainNameResolveException();
		}
		try {
			// Resolve the hostname
			std::string hostname(entry.getData(), entryLength);
			HostAddress address = resolveHostName(hostname);
			return HostAddressPort(address, port);
		}
		catch (const DomainNameResolveException&) {
		}
		currentEntry += entryLength;
		answersCount--;
	}
#endif

	throw DomainNameResolveException();
}

HostAddress DomainNameResolver::resolveHostName(const std::string& hostname) {
	boost::asio::io_service ioService;
	boost::asio::ip::tcp::resolver resolver(ioService);
	boost::asio::ip::tcp::resolver::query query(hostname, "5222");
	try {
		boost::asio::ip::tcp::resolver::iterator endpointIterator = resolver.resolve(query);
		if (endpointIterator == boost::asio::ip::tcp::resolver::iterator()) {
			throw DomainNameResolveException();
		}
		boost::asio::ip::address address = (*endpointIterator).endpoint().address();
		if (address.is_v4()) {
			return HostAddress(&address.to_v4().to_bytes()[0], 4);
		}
		else {
			return HostAddress(&address.to_v6().to_bytes()[0], 16);
		}
	}
	catch (...) {
		throw DomainNameResolveException();
	}
}

}