Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ServerResolver.cpp « src - github.com/mumble-voip/mumble.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 544677e5445b2b8cd291e9e81b8e2803c8560545 (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
// Copyright 2005-2020 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.

#include "ServerResolver.h"

#include <QtNetwork/QDnsLookup>
#include <QtNetwork/QHostInfo>

static qint64 normalizeSrvPriority(quint16 priority, quint16 weight) {
	return static_cast< qint64 >((65535U * priority) + weight);
}

class ServerResolverPrivate : public QObject {
private:
	Q_OBJECT
	Q_DISABLE_COPY(ServerResolverPrivate)
public:
	ServerResolverPrivate(QObject *parent);

	void resolve(QString hostname, quint16 port);
	QList< ServerResolverRecord > records();

	QString m_origHostname;
	quint16 m_origPort;

	QList< QDnsServiceRecord > m_srvQueue;
	QMap< int, int > m_hostInfoIdToIndexMap;
	int m_srvQueueRemain;

	QList< ServerResolverRecord > m_resolved;

signals:
	void resolved();

public slots:
	void srvResolved();
	void hostResolved(QHostInfo hostInfo);
	void hostFallbackResolved(QHostInfo hostInfo);
};

ServerResolverPrivate::ServerResolverPrivate(QObject *parent) : QObject(parent), m_origPort(0), m_srvQueueRemain(0) {
}

void ServerResolverPrivate::resolve(QString hostname, quint16 port) {
	m_origHostname = hostname;
	m_origPort     = port;

	QDnsLookup *resolver = new QDnsLookup(this);
	connect(resolver, SIGNAL(finished()), this, SLOT(srvResolved()));
	resolver->setType(QDnsLookup::SRV);

	resolver->setName(QLatin1String("_mumble._tcp.") + hostname);
	resolver->lookup();
}

QList< ServerResolverRecord > ServerResolverPrivate::records() {
	return m_resolved;
}

void ServerResolverPrivate::srvResolved() {
	QDnsLookup *resolver = qobject_cast< QDnsLookup * >(sender());

	m_srvQueue       = resolver->serviceRecords();
	m_srvQueueRemain = m_srvQueue.count();

	if (resolver->error() == QDnsLookup::NoError && m_srvQueueRemain > 0) {
		for (int i = 0; i < m_srvQueue.count(); i++) {
			QDnsServiceRecord record = m_srvQueue.at(i);
			int hostInfoId           = QHostInfo::lookupHost(record.target(), this, SLOT(hostResolved(QHostInfo)));
			m_hostInfoIdToIndexMap[hostInfoId] = i;
		}
	} else {
		QHostInfo::lookupHost(m_origHostname, this, SLOT(hostFallbackResolved(QHostInfo)));
	}

	delete resolver;
}

void ServerResolverPrivate::hostResolved(QHostInfo hostInfo) {
	int lookupId             = hostInfo.lookupId();
	int idx                  = m_hostInfoIdToIndexMap[lookupId];
	QDnsServiceRecord record = m_srvQueue.at(idx);

	if (hostInfo.error() == QHostInfo::NoError) {
		QList< QHostAddress > resolvedAddresses = hostInfo.addresses();

		// Convert QHostAddress -> HostAddress.
		QList< HostAddress > addresses;
		foreach (QHostAddress ha, resolvedAddresses) { addresses << HostAddress(ha); }

		qint64 priority = normalizeSrvPriority(record.priority(), record.weight());
		m_resolved << ServerResolverRecord(m_origHostname, record.port(), priority, addresses);
	}

	m_srvQueueRemain -= 1;
	if (m_srvQueueRemain == 0) {
		emit resolved();
	}
}

void ServerResolverPrivate::hostFallbackResolved(QHostInfo hostInfo) {
	if (hostInfo.error() == QHostInfo::NoError) {
		QList< QHostAddress > resolvedAddresses = hostInfo.addresses();

		// Convert QHostAddress -> HostAddress.
		QList< HostAddress > addresses;
		foreach (QHostAddress ha, resolvedAddresses) { addresses << HostAddress(ha); }

		m_resolved << ServerResolverRecord(m_origHostname, m_origPort, 0, addresses);
	}

	emit resolved();
}

ServerResolver::ServerResolver(QObject *parent) : QObject(parent) {
	d = new ServerResolverPrivate(this);
}

QString ServerResolver::hostname() {
	if (d) {
		return d->m_origHostname;
	}

	return QString();
}

quint16 ServerResolver::port() {
	if (d) {
		return d->m_origPort;
	}

	return 0;
}

void ServerResolver::resolve(QString hostname, quint16 port) {
	if (d) {
		connect(d, SIGNAL(resolved()), this, SIGNAL(resolved()));
		d->resolve(hostname, port);
	}
}

QList< ServerResolverRecord > ServerResolver::records() {
	if (d) {
		return d->records();
	}
	return QList< ServerResolverRecord >();
}

#include "ServerResolver.moc"