1 // Copyright 2005-2019 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5 
6 #ifdef MUMBLE
7 	#include "mumble_pch.hpp"
8 #else
9 	#include "murmur_pch.h"
10 #endif
11 
12 #include "ServerResolver.h"
13 
14 #include <QtNetwork/QHostInfo>
15 
16 class ServerResolverPrivate : public QObject {
17 	private:
18 		Q_OBJECT
19 		Q_DISABLE_COPY(ServerResolverPrivate)
20 	public:
21 		ServerResolverPrivate(QObject *parent);
22 
23 		void resolve(QString hostname, quint16 port);
24 		QList<ServerResolverRecord> records();
25 
26 		QString m_origHostname;
27 		quint16 m_origPort;
28 
29 		QList<ServerResolverRecord> m_resolved;
30 
31 	signals:
32 		void resolved();
33 
34 	public slots:
35 		void hostResolved(QHostInfo hostInfo);
36 };
37 
ServerResolverPrivate(QObject * parent)38 ServerResolverPrivate::ServerResolverPrivate(QObject *parent)
39 	: QObject(parent)
40 	, m_origPort(0) {
41 }
42 
resolve(QString hostname,quint16 port)43 void ServerResolverPrivate::resolve(QString hostname, quint16 port) {
44 	m_origHostname = hostname;
45 	m_origPort = port;
46 
47 	QHostInfo::lookupHost(hostname, this, SLOT(hostResolved(QHostInfo)));
48 }
49 
records()50 QList<ServerResolverRecord> ServerResolverPrivate::records() {
51 	return m_resolved;
52 }
53 
hostResolved(QHostInfo hostInfo)54 void ServerResolverPrivate::hostResolved(QHostInfo hostInfo) {
55 	if (hostInfo.error() == QHostInfo::NoError) {
56 		QList<QHostAddress> resolvedAddresses = hostInfo.addresses();
57 
58 		// Convert QHostAddress -> HostAddress.
59 		QList<HostAddress> addresses;
60 		foreach (QHostAddress ha, resolvedAddresses) {
61 			addresses << HostAddress(ha);
62 		}
63 
64 		m_resolved << ServerResolverRecord(m_origHostname, m_origPort, 0, addresses);
65 	}
66 
67 	emit resolved();
68 }
69 
ServerResolver(QObject * parent)70 ServerResolver::ServerResolver(QObject *parent)
71 	: QObject(parent) {
72 
73 	d = new ServerResolverPrivate(this);
74 }
75 
hostname()76 QString ServerResolver::hostname() {
77 	if (d) {
78 		return d->m_origHostname;
79 	}
80 
81 	return QString();
82 }
83 
port()84 quint16 ServerResolver::port() {
85 	if (d) {
86 		return d->m_origPort;
87 	}
88 
89 	return 0;
90 }
91 
resolve(QString hostname,quint16 port)92 void ServerResolver::resolve(QString hostname, quint16 port) {
93 	if (d) {
94 		connect(d, SIGNAL(resolved()), this, SIGNAL(resolved()));
95 		d->resolve(hostname, port);
96 	}
97 }
98 
records()99 QList<ServerResolverRecord> ServerResolver::records() {
100 	if (d) {
101 		return d->records();
102 	}
103 	return QList<ServerResolverRecord>();
104 }
105 
106 #include "ServerResolver_nosrv.moc"
107