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
|
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include "Swiften/Network/PlatformDomainNameResolver.h"
// Putting this early on, because some system types conflict with thread
#include "Swiften/Network/PlatformDomainNameServiceQuery.h"
#include <string>
#include <vector>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <algorithm>
#include "Swiften/Base/String.h"
#include "Swiften/IDN/IDNA.h"
#include "Swiften/Network/HostAddress.h"
#include "Swiften/EventLoop/EventLoop.h"
#include "Swiften/Network/HostAddressPort.h"
#include "Swiften/Network/DomainNameAddressQuery.h"
using namespace Swift;
namespace {
struct AddressQuery : public DomainNameAddressQuery, public boost::enable_shared_from_this<AddressQuery>, public EventOwner {
AddressQuery(const String& host, EventLoop* eventLoop) : hostname(host), eventLoop(eventLoop), thread(NULL), safeToJoin(false) {}
~AddressQuery() {
if (safeToJoin) {
thread->join();
}
else {
// FIXME: UGLYYYYY
}
delete thread;
}
void run() {
safeToJoin = false;
thread = new boost::thread(boost::bind(&AddressQuery::doRun, shared_from_this()));
}
void doRun() {
//std::cout << "PlatformDomainNameResolver::doRun()" << std::endl;
boost::asio::ip::tcp::resolver resolver(ioService);
boost::asio::ip::tcp::resolver::query query(hostname.getUTF8String(), "5222");
try {
//std::cout << "PlatformDomainNameResolver::doRun(): Resolving" << std::endl;
boost::asio::ip::tcp::resolver::iterator endpointIterator = resolver.resolve(query);
//std::cout << "PlatformDomainNameResolver::doRun(): Resolved" << std::endl;
if (endpointIterator == boost::asio::ip::tcp::resolver::iterator()) {
//std::cout << "PlatformDomainNameResolver::doRun(): Error 1" << std::endl;
emitError();
}
else {
std::vector<HostAddress> results;
for ( ; endpointIterator != boost::asio::ip::tcp::resolver::iterator(); ++endpointIterator) {
boost::asio::ip::address address = (*endpointIterator).endpoint().address();
results.push_back(address.is_v4() ? HostAddress(&address.to_v4().to_bytes()[0], 4) : HostAddress(&address.to_v6().to_bytes()[0], 16));
}
//std::cout << "PlatformDomainNameResolver::doRun(): Success" << std::endl;
eventLoop->postEvent(
boost::bind(boost::ref(onResult), results, boost::optional<DomainNameResolveError>()),
shared_from_this());
}
}
catch (...) {
//std::cout << "PlatformDomainNameResolver::doRun(): Error 2" << std::endl;
emitError();
}
safeToJoin = true;
}
void emitError() {
eventLoop->postEvent(boost::bind(boost::ref(onResult), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this());
}
boost::asio::io_service ioService;
String hostname;
EventLoop* eventLoop;
boost::thread* thread;
bool safeToJoin;
};
}
namespace Swift {
PlatformDomainNameResolver::PlatformDomainNameResolver(EventLoop* eventLoop) : eventLoop(eventLoop) {
}
boost::shared_ptr<DomainNameServiceQuery> PlatformDomainNameResolver::createServiceQuery(const String& name) {
return boost::shared_ptr<DomainNameServiceQuery>(new PlatformDomainNameServiceQuery(IDNA::getEncoded(name), eventLoop));
}
boost::shared_ptr<DomainNameAddressQuery> PlatformDomainNameResolver::createAddressQuery(const String& name) {
return boost::shared_ptr<DomainNameAddressQuery>(new AddressQuery(IDNA::getEncoded(name), eventLoop));
}
}
|