aboutsummaryrefslogtreecommitdiff
path: root/src/base/QXmppStun.cpp
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2015-08-25 09:02:02 +0200
committerJeremy Lainé <jeremy.laine@m4x.org>2015-08-25 09:03:17 +0200
commit5a5a2ed8cdad82740d95292853481d6ce0891df0 (patch)
tree939356450eed7d3ec310ef7bf2b0dcc123bdd89c /src/base/QXmppStun.cpp
parent722c1fc2f807f6da6a9a7996dc86c23b61c554a8 (diff)
downloadqxmpp-5a5a2ed8cdad82740d95292853481d6ce0891df0.tar.gz
ICE: rework sending STUN packet
Diffstat (limited to 'src/base/QXmppStun.cpp')
-rw-r--r--src/base/QXmppStun.cpp57
1 files changed, 26 insertions, 31 deletions
diff --git a/src/base/QXmppStun.cpp b/src/base/QXmppStun.cpp
index e4b33e7c..46591fd4 100644
--- a/src/base/QXmppStun.cpp
+++ b/src/base/QXmppStun.cpp
@@ -1632,6 +1632,7 @@ public:
QXmppIceComponentPrivate(QXmppIceComponent *qq);
CandidatePair* findPair(QXmppStunTransaction *transaction);
void performCheck(CandidatePair *pair);
+ void writeStun(const QXmppStunMessage &message, QUdpSocket *socket, const QHostAddress &remoteHost, quint16 remotePort);
CandidatePair *activePair;
int component;
@@ -1709,6 +1710,24 @@ void QXmppIceComponentPrivate::performCheck(CandidatePair *pair)
pair->transaction = new QXmppStunTransaction(message, q);
}
+void QXmppIceComponentPrivate::writeStun(const QXmppStunMessage &message, QUdpSocket *socket, const QHostAddress &address, quint16 port)
+{
+ const QString messagePassword = (message.type() & 0xFF00) ? localPassword : remotePassword;
+ const QByteArray data = message.encode(messagePassword.toUtf8());
+ if (socket)
+ socket->writeDatagram(data, address, port);
+ else if (turnAllocation->state() == QXmppTurnAllocation::ConnectedState)
+ socket->writeDatagram(data, address, port);
+ else
+ return;
+#ifdef QXMPP_DEBUG_STUN
+ q->logSent(QString("STUN packet to %1 port %2\n%3").arg(
+ address.toString(),
+ QString::number(port),
+ message.toString()));
+#endif
+}
+
/// Constructs a new QXmppIceComponent.
///
/// \param parent
@@ -2209,16 +2228,16 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr
CandidatePair *pair = 0;
if (message.messageClass() == QXmppStunMessage::Request)
{
- // add remote candidate
- pair = addRemoteCandidate(socket, remoteHost, remotePort, message.priority());
-
// send a binding response
QXmppStunMessage response;
response.setId(message.id());
response.setType(QXmppStunMessage::Binding | QXmppStunMessage::Response);
- response.xorMappedHost = pair->remote.host();
- response.xorMappedPort = pair->remote.port();
- writeStun(response, pair);
+ response.xorMappedHost = remoteHost;
+ response.xorMappedPort = remotePort;
+ d->writeStun(response, socket, remoteHost, remotePort);
+
+ // add remote candidate
+ pair = addRemoteCandidate(socket, remoteHost, remotePort, message.priority());
// update state
if (d->iceControlling || message.useCandidate) {
@@ -2439,31 +2458,7 @@ void QXmppIceComponent::writeStun(const QXmppStunMessage &message)
{
CandidatePair *pair = d->findPair(qobject_cast<QXmppStunTransaction*>(sender()));
if (pair)
- writeStun(message, pair);
-}
-
-/// Sends a STUN packet to the remote party.
-
-qint64 QXmppIceComponent::writeStun(const QXmppStunMessage &message, CandidatePair *pair)
-{
- qint64 ret;
- const QString messagePassword = (message.type() & 0xFF00) ? d->localPassword : d->remotePassword;
- if (pair->socket)
- ret = pair->socket->writeDatagram(
- message.encode(messagePassword.toUtf8()),
- pair->remote.host(),
- pair->remote.port());
- else if (d->turnAllocation->state() == QXmppTurnAllocation::ConnectedState)
- ret = d->turnAllocation->writeDatagram(
- message.encode(messagePassword.toUtf8()),
- pair->remote.host(),
- pair->remote.port());
- else
- return -1;
-#ifdef QXMPP_DEBUG_STUN
- logSent(QString("Sent to %1\n%2").arg(pair->toString(), message.toString()));
-#endif
- return ret;
+ d->writeStun(message, pair->socket, pair->remote.host(), pair->remote.port());
}
/// Constructs a new ICE connection.