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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
/*
* Copyright (C) 2008-2010 The QXmpp developers
*
* Author:
* Jeremy Lainé
*
* Source:
* http://code.google.com/p/qxmpp
*
* This file is a part of QXmpp library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
#include <QDomElement>
#include <QSslKey>
#include <QSslSocket>
#include "QXmppConstants.h"
#include "QXmppDialback.h"
#include "QXmppOutgoingServer.h"
#include "QXmppSrvInfo.h"
#include "QXmppStreamFeatures.h"
#include "QXmppUtils.h"
class QXmppOutgoingServerPrivate
{
public:
QString localDomain;
QString localStreamKey;
QString remoteDomain;
QString verifyId;
QString verifyKey;
bool ready;
};
/// Constructs a new outgoing server-to-server stream.
///
/// \param socket
/// \param domain the local domain
/// \param parent the parent object
///
QXmppOutgoingServer::QXmppOutgoingServer(const QString &domain, QObject *parent)
: QXmppStream(parent),
d(new QXmppOutgoingServerPrivate)
{
QSslSocket *socket = new QSslSocket(this);
setSocket(socket);
d->localDomain = domain;
d->ready = false;
bool check = connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
this, SLOT(slotSslErrors(QList<QSslError>)));
Q_ASSERT(check);
Q_UNUSED(check);
}
/// Destroys the stream.
///
QXmppOutgoingServer::~QXmppOutgoingServer()
{
delete d;
}
void QXmppOutgoingServer::connectToHost(const QString &domain)
{
d->remoteDomain = domain;
QString host;
quint16 port;
// lookup server for domain
debug(QString("Looking up server for domain %1").arg(domain));
QXmppSrvInfo serviceInfo = QXmppSrvInfo::fromName("_xmpp-server._tcp." + domain);
if (!serviceInfo.records().isEmpty())
{
// take the first returned record
host = serviceInfo.records().first().hostName();
port = serviceInfo.records().first().port();
} else {
// as a fallback, use domain as the host name
warning(QString("Lookup for domain %1 failed: %2").arg(domain, serviceInfo.errorString()));
host = domain;
port = 5269;
}
// connect to server
info(QString("Connecting to %1:%2").arg(host, QString::number(port)));
socket()->connectToHost(host, port);
}
void QXmppOutgoingServer::handleStart()
{
QString data = QString("<?xml version='1.0'?><stream:stream"
" xmlns='%1' xmlns:db='%2' xmlns:stream='%3' version='1.0'>").arg(
ns_server,
ns_server_dialback,
ns_stream);
sendData(data.toUtf8());
}
void QXmppOutgoingServer::handleStream(const QDomElement &streamElement)
{
Q_UNUSED(streamElement);
}
void QXmppOutgoingServer::handleStanza(const QDomElement &stanza)
{
const QString ns = stanza.namespaceURI();
if(QXmppStreamFeatures::isStreamFeatures(stanza))
{
QXmppStreamFeatures features;
features.parse(stanza);
if (!socket()->isEncrypted())
{
// check we can satisfy TLS constraints
if (!socket()->supportsSsl() &&
features.tlsMode() == QXmppStreamFeatures::Required)
{
warning("Disconnecting as TLS is required, but SSL support is not available");
disconnectFromHost();
return;
}
// enable TLS if possible
if (socket()->supportsSsl() &&
features.tlsMode() != QXmppStreamFeatures::Disabled)
{
sendData("<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>");
return;
}
}
if (!d->localStreamKey.isEmpty())
{
// send dialback key
QXmppDialback dialback;
dialback.setCommand(QXmppDialback::Result);
dialback.setFrom(d->localDomain);
dialback.setTo(d->remoteDomain);
dialback.setKey(d->localStreamKey);
sendPacket(dialback);
}
else if (!d->verifyId.isEmpty() && !d->verifyKey.isEmpty())
{
// send dialback verify
QXmppDialback verify;
verify.setCommand(QXmppDialback::Verify);
verify.setId(d->verifyId);
verify.setFrom(d->localDomain);
verify.setTo(d->remoteDomain);
verify.setKey(d->verifyKey);
sendPacket(verify);
}
}
else if (ns == ns_tls)
{
if (stanza.tagName() == "proceed")
{
debug("Starting encryption");
socket()->startClientEncryption();
return;
}
}
else if (QXmppDialback::isDialback(stanza))
{
QXmppDialback response;
response.parse(stanza);
// check the request is valid
if (response.from().isEmpty() ||
response.to() != d->localDomain ||
response.type().isEmpty())
{
warning("Invalid dialback response received");
return;
}
if (response.command() == QXmppDialback::Result)
{
if (response.type() == "valid")
{
info("Outgoing stream is ready");
d->ready = true;
emit connected();
}
}
else if (response.command() == QXmppDialback::Verify)
{
emit dialbackResponseReceived(response);
}
}
}
/// Returns true if the socket is connected and authentication succeeded.
///
bool QXmppOutgoingServer::isConnected() const
{
return QXmppStream::isConnected() && d->ready;
}
QString QXmppOutgoingServer::localStreamKey() const
{
return d->localStreamKey;
}
void QXmppOutgoingServer::setLocalStreamKey(const QString &key)
{
d->localStreamKey = key;
}
void QXmppOutgoingServer::setVerify(const QString &id, const QString &key)
{
d->verifyId = id;
d->verifyKey = key;
}
QString QXmppOutgoingServer::remoteDomain() const
{
return d->remoteDomain;
}
void QXmppOutgoingServer::slotSslErrors(const QList<QSslError> &errors)
{
warning("SSL errors");
for(int i = 0; i < errors.count(); ++i)
warning(errors.at(i).errorString());
socket()->ignoreSslErrors();
}
|