diff options
| author | Linus Jahn <lnj@kaidan.im> | 2020-02-02 18:14:27 +0100 |
|---|---|---|
| committer | LNJ <lnj@kaidan.im> | 2020-02-03 00:11:55 +0100 |
| commit | 57f1b775197aed1d32e98b0299c0999339a7d5f6 (patch) | |
| tree | ce5702781e15f3a5c438ab153edf06f51a501b29 /src/base/QXmppStun.cpp | |
| parent | 99b850a3008bbb03d3449d683adb87839f7d5c4b (diff) | |
| download | qxmpp-57f1b775197aed1d32e98b0299c0999339a7d5f6.tar.gz | |
Run clang-format also on all cpp files
Diffstat (limited to 'src/base/QXmppStun.cpp')
| -rw-r--r-- | src/base/QXmppStun.cpp | 475 |
1 files changed, 205 insertions, 270 deletions
diff --git a/src/base/QXmppStun.cpp b/src/base/QXmppStun.cpp index 2d4bbdc5..703ca962 100644 --- a/src/base/QXmppStun.cpp +++ b/src/base/QXmppStun.cpp @@ -42,13 +42,13 @@ static const quint16 STUN_HEADER = 20; static const quint8 STUN_IPV4 = 0x01; static const quint8 STUN_IPV6 = 0x02; -static const char* gathering_states[] = { +static const char *gathering_states[] = { "new", "gathering", "complete" }; -static const char* pair_states[] = { +static const char *pair_states[] = { "frozen", "waiting", "in-progress", @@ -57,31 +57,31 @@ static const char* pair_states[] = { }; enum AttributeType { - MappedAddress = 0x0001, // RFC5389 - ChangeRequest = 0x0003, // RFC5389 - SourceAddress = 0x0004, // RFC5389 - ChangedAddress = 0x0005, // RFC5389 - Username = 0x0006, // RFC5389 - MessageIntegrity = 0x0008, // RFC5389 - ErrorCode = 0x0009, // RFC5389 - ChannelNumber = 0x000c, // RFC5766 : TURN - Lifetime = 0x000d, // RFC5766 : TURN - XorPeerAddress = 0x0012, // RFC5766 : TURN - DataAttr = 0x0013, // RFC5766 : TURN - Realm = 0x0014, // RFC5389 - Nonce = 0x0015, // RFC5389 - XorRelayedAddress= 0x0016, // RFC5766 : TURN - EvenPort = 0x0018, // RFC5766 : TURN - RequestedTransport=0x0019, // RFC5766 : TURN - XorMappedAddress = 0x0020, // RFC5389 - ReservationToken = 0x0022, // RFC5766 : TURN - Priority = 0x0024, // RFC5245 - UseCandidate = 0x0025, // RFC5245 - Software = 0x8022, // RFC5389 - Fingerprint = 0x8028, // RFC5389 - IceControlled = 0x8029, // RFC5245 - IceControlling = 0x802a, // RFC5245 - OtherAddress = 0x802c // RFC5780 + MappedAddress = 0x0001, // RFC5389 + ChangeRequest = 0x0003, // RFC5389 + SourceAddress = 0x0004, // RFC5389 + ChangedAddress = 0x0005, // RFC5389 + Username = 0x0006, // RFC5389 + MessageIntegrity = 0x0008, // RFC5389 + ErrorCode = 0x0009, // RFC5389 + ChannelNumber = 0x000c, // RFC5766 : TURN + Lifetime = 0x000d, // RFC5766 : TURN + XorPeerAddress = 0x0012, // RFC5766 : TURN + DataAttr = 0x0013, // RFC5766 : TURN + Realm = 0x0014, // RFC5389 + Nonce = 0x0015, // RFC5389 + XorRelayedAddress = 0x0016, // RFC5766 : TURN + EvenPort = 0x0018, // RFC5766 : TURN + RequestedTransport = 0x0019, // RFC5766 : TURN + XorMappedAddress = 0x0020, // RFC5389 + ReservationToken = 0x0022, // RFC5766 : TURN + Priority = 0x0024, // RFC5245 + UseCandidate = 0x0025, // RFC5245 + Software = 0x8022, // RFC5389 + Fingerprint = 0x8028, // RFC5389 + IceControlled = 0x8029, // RFC5245 + IceControlling = 0x802a, // RFC5245 + OtherAddress = 0x802c // RFC5780 }; // FIXME : we need to set local preference to discriminate between @@ -89,8 +89,7 @@ enum AttributeType { static quint32 candidatePriority(const QXmppJingleCandidate &candidate, int localPref = 65535) { int typePref; - switch (candidate.type()) - { + switch (candidate.type()) { case QXmppJingleCandidate::HostType: typePref = 126; break; @@ -104,9 +103,9 @@ static quint32 candidatePriority(const QXmppJingleCandidate &candidate, int loca typePref = 0; } - return (1 << 24) * typePref + \ - (1 << 8) * localPref + \ - (256 - candidate.component()); + return (1 << 24) * typePref + + (1 << 8) * localPref + + (256 - candidate.component()); } static QString computeFoundation(QXmppJingleCandidate::Type type, const QString &protocol, const QHostAddress &baseAddress) @@ -133,7 +132,7 @@ static bool isLoopbackAddress(const QHostAddress &addr) static bool isCompatibleAddress(const QHostAddress &a1, const QHostAddress &a2) { return a1.protocol() == a2.protocol() && - isIPv6LinkLocalAddress(a1) == isIPv6LinkLocalAddress(a2); + isIPv6LinkLocalAddress(a1) == isIPv6LinkLocalAddress(a2); } static bool decodeAddress(QDataStream &stream, quint16 a_length, QHostAddress &address, quint16 &port, const QByteArray &xorId = QByteArray()) @@ -149,8 +148,7 @@ static bool decodeAddress(QDataStream &stream, quint16 a_length, QHostAddress &a port = rawPort; else port = rawPort ^ (STUN_MAGIC >> 16); - if (protocol == STUN_IPV4) - { + if (protocol == STUN_IPV4) { if (a_length != 8) return false; quint32 addr; @@ -163,9 +161,8 @@ static bool decodeAddress(QDataStream &stream, quint16 a_length, QHostAddress &a if (a_length != 20) return false; Q_IPV6ADDR addr; - stream.readRawData((char*)&addr, sizeof(addr)); - if (!xorId.isEmpty()) - { + stream.readRawData((char *)&addr, sizeof(addr)); + if (!xorId.isEmpty()) { QByteArray xpad; QDataStream(&xpad, QIODevice::WriteOnly) << STUN_MAGIC; xpad += xorId; @@ -182,15 +179,13 @@ static bool decodeAddress(QDataStream &stream, quint16 a_length, QHostAddress &a static void encodeAddress(QDataStream &stream, quint16 type, const QHostAddress &address, quint16 port, const QByteArray &xorId = QByteArray()) { const quint8 reserved = 0; - if (address.protocol() == QAbstractSocket::IPv4Protocol) - { + if (address.protocol() == QAbstractSocket::IPv4Protocol) { stream << type; stream << quint16(8); stream << reserved; stream << quint8(STUN_IPV4); quint32 addr = address.toIPv4Address(); - if (!xorId.isEmpty()) - { + if (!xorId.isEmpty()) { port ^= (STUN_MAGIC >> 16); addr ^= STUN_MAGIC; } @@ -202,8 +197,7 @@ static void encodeAddress(QDataStream &stream, quint16 type, const QHostAddress stream << reserved; stream << quint8(STUN_IPV6); Q_IPV6ADDR addr = address.toIPv6Address(); - if (!xorId.isEmpty()) - { + if (!xorId.isEmpty()) { port ^= (STUN_MAGIC >> 16); QByteArray xpad; QDataStream(&xpad, QIODevice::WriteOnly) << STUN_MAGIC; @@ -212,7 +206,7 @@ static void encodeAddress(QDataStream &stream, quint16 type, const QHostAddress addr[i] ^= xpad[i]; } stream << port; - stream.writeRawData((char*)&addr, sizeof(addr)); + stream.writeRawData((char *)&addr, sizeof(addr)); } else { qWarning("Cannot write STUN attribute for unknown IP version"); } @@ -222,8 +216,7 @@ static void addAddress(QDataStream &stream, quint16 type, const QHostAddress &ho { if (port && !host.isNull() && (host.protocol() == QAbstractSocket::IPv4Protocol || - host.protocol() == QAbstractSocket::IPv6Protocol)) - { + host.protocol() == QAbstractSocket::IPv6Protocol)) { encodeAddress(stream, type, host, port, xorId); } } @@ -234,8 +227,7 @@ static void encodeString(QDataStream &stream, quint16 type, const QString &strin stream << type; stream << quint16(utf8string.size()); stream.writeRawData(utf8string.data(), utf8string.size()); - if (utf8string.size() % 4) - { + if (utf8string.size() % 4) { const QByteArray padding(4 - (utf8string.size() % 4), 0); stream.writeRawData(padding.data(), padding.size()); } @@ -252,20 +244,20 @@ static void setBodyLength(QByteArray &buffer, qint16 length) QXmppStunMessage::QXmppStunMessage() : errorCode(0), - changedPort(0), - mappedPort(0), - otherPort(0), - sourcePort(0), - xorMappedPort(0), - xorPeerPort(0), - xorRelayedPort(0), - useCandidate(false), - m_cookie(STUN_MAGIC), - m_type(0), - m_changeRequest(0), - m_channelNumber(0), - m_lifetime(0), - m_priority(0) + changedPort(0), + mappedPort(0), + otherPort(0), + sourcePort(0), + xorMappedPort(0), + xorPeerPort(0), + xorRelayedPort(0), + useCandidate(false), + m_cookie(STUN_MAGIC), + m_type(0), + m_changeRequest(0), + m_channelNumber(0), + m_lifetime(0), + m_priority(0) { m_id = QByteArray(STUN_ID_SIZE, 0); } @@ -519,8 +511,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q if (!errors) errors = &silent; - if (buffer.size() < STUN_HEADER) - { + if (buffer.size() < STUN_HEADER) { *errors << QLatin1String("Received a truncated STUN packet"); return false; } @@ -533,8 +524,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q stream >> m_cookie; stream.readRawData(m_id.data(), m_id.size()); - if (length != buffer.size() - STUN_HEADER) - { + if (length != buffer.size() - STUN_HEADER) { *errors << QLatin1String("Received an invalid STUN packet"); return false; } @@ -542,24 +532,21 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q // parse STUN attributes int done = 0; bool after_integrity = false; - while (done < length) - { + while (done < length) { quint16 a_type, a_length; stream >> a_type; stream >> a_length; const int pad_length = 4 * ((a_length + 3) / 4) - a_length; // only FINGERPRINT is allowed after MESSAGE-INTEGRITY - if (after_integrity && a_type != Fingerprint) - { + if (after_integrity && a_type != Fingerprint) { *errors << QString("Skipping attribute %1 after MESSAGE-INTEGRITY").arg(QString::number(a_type)); stream.skipRawData(a_length + pad_length); done += 4 + a_length + pad_length; continue; } - if (a_type == Priority) - { + if (a_type == Priority) { // PRIORITY if (a_length != sizeof(m_priority)) return false; @@ -664,8 +651,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q } else if (a_type == MappedAddress) { // MAPPED-ADDRESS - if (!decodeAddress(stream, a_length, mappedHost, mappedPort)) - { + if (!decodeAddress(stream, a_length, mappedHost, mappedPort)) { *errors << QLatin1String("Bad MAPPED-ADDRESS"); return false; } @@ -681,8 +667,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q } else if (a_type == SourceAddress) { // SOURCE-ADDRESS - if (!decodeAddress(stream, a_length, sourceHost, sourcePort)) - { + if (!decodeAddress(stream, a_length, sourceHost, sourcePort)) { *errors << QLatin1String("Bad SOURCE-ADDRESS"); return false; } @@ -690,8 +675,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q } else if (a_type == ChangedAddress) { // CHANGED-ADDRESS - if (!decodeAddress(stream, a_length, changedHost, changedPort)) - { + if (!decodeAddress(stream, a_length, changedHost, changedPort)) { *errors << QLatin1String("Bad CHANGED-ADDRESS"); return false; } @@ -699,8 +683,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q } else if (a_type == OtherAddress) { // OTHER-ADDRESS - if (!decodeAddress(stream, a_length, otherHost, otherPort)) - { + if (!decodeAddress(stream, a_length, otherHost, otherPort)) { *errors << QLatin1String("Bad OTHER-ADDRESS"); return false; } @@ -708,8 +691,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q } else if (a_type == XorMappedAddress) { // XOR-MAPPED-ADDRESS - if (!decodeAddress(stream, a_length, xorMappedHost, xorMappedPort, m_id)) - { + if (!decodeAddress(stream, a_length, xorMappedHost, xorMappedPort, m_id)) { *errors << QLatin1String("Bad XOR-MAPPED-ADDRESS"); return false; } @@ -717,8 +699,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q } else if (a_type == XorPeerAddress) { // XOR-PEER-ADDRESS - if (!decodeAddress(stream, a_length, xorPeerHost, xorPeerPort, m_id)) - { + if (!decodeAddress(stream, a_length, xorPeerHost, xorPeerPort, m_id)) { *errors << QLatin1String("Bad XOR-PEER-ADDRESS"); return false; } @@ -726,8 +707,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q } else if (a_type == XorRelayedAddress) { // XOR-RELAYED-ADDRESS - if (!decodeAddress(stream, a_length, xorRelayedHost, xorRelayedPort, m_id)) - { + if (!decodeAddress(stream, a_length, xorRelayedHost, xorRelayedPort, m_id)) { *errors << QLatin1String("Bad XOR-RELAYED-ADDRESS"); return false; } @@ -741,12 +721,10 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q stream.readRawData(integrity.data(), integrity.size()); // check HMAC-SHA1 - if (!key.isEmpty()) - { + if (!key.isEmpty()) { QByteArray copy = buffer.left(STUN_HEADER + done); setBodyLength(copy, done + 24); - if (integrity != QXmppUtils::generateHmacSha1(key, copy)) - { + if (integrity != QXmppUtils::generateHmacSha1(key, copy)) { *errors << QLatin1String("Bad message integrity"); return false; } @@ -767,8 +745,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q QByteArray copy = buffer.left(STUN_HEADER + done); setBodyLength(copy, done + 8); const quint32 expected = QXmppUtils::generateCrc32(copy) ^ 0x5354554eL; - if (fingerprint != expected) - { + if (fingerprint != expected) { *errors << QLatin1String("Bad fingerprint"); return false; } @@ -784,7 +761,7 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q iceControlling.resize(a_length); stream.readRawData(iceControlling.data(), iceControlling.size()); - } else if (a_type == IceControlled) { + } else if (a_type == IceControlled) { /// ICE-CONTROLLED if (a_length != 8) @@ -797,7 +774,6 @@ bool QXmppStunMessage::decode(const QByteArray &buffer, const QByteArray &key, Q // Unknown attribute stream.skipRawData(a_length); *errors << QString("Skipping unknown attribute %1").arg(QString::number(a_type)); - } stream.skipRawData(pad_length); done += 4 + a_length + pad_length; @@ -852,8 +828,7 @@ QByteArray QXmppStunMessage::encode(const QByteArray &key, bool addFingerprint) addAddress(stream, XorRelayedAddress, xorRelayedHost, xorRelayedPort, m_id); // ERROR-CODE - if (errorCode) - { + if (errorCode) { const quint16 reserved = 0; const quint8 errorCodeHigh = errorCode / 100; const quint8 errorCodeLow = errorCode % 100; @@ -864,24 +839,21 @@ QByteArray QXmppStunMessage::encode(const QByteArray &key, bool addFingerprint) stream << errorCodeHigh; stream << errorCodeLow; stream.writeRawData(phrase.data(), phrase.size()); - if (phrase.size() % 4) - { - const QByteArray padding(4 - (phrase.size() % 4), 0); + if (phrase.size() % 4) { + const QByteArray padding(4 - (phrase.size() % 4), 0); stream.writeRawData(padding.data(), padding.size()); } } // PRIORITY - if (m_attributes.contains(Priority)) - { + if (m_attributes.contains(Priority)) { stream << quint16(Priority); stream << quint16(sizeof(m_priority)); stream << m_priority; } // USE-CANDIDATE - if (useCandidate) - { + if (useCandidate) { stream << quint16(UseCandidate); stream << quint16(0); } @@ -900,7 +872,7 @@ QByteArray QXmppStunMessage::encode(const QByteArray &key, bool addFingerprint) stream << quint16(m_data.size()); stream.writeRawData(m_data.data(), m_data.size()); if (m_data.size() % 4) { - const QByteArray padding(4 - (m_data.size() % 4), 0); + const QByteArray padding(4 - (m_data.size() % 4), 0); stream.writeRawData(padding.data(), padding.size()); } } @@ -948,8 +920,7 @@ QByteArray QXmppStunMessage::encode(const QByteArray &key, bool addFingerprint) encodeString(stream, Username, m_username); // ICE-CONTROLLING or ICE-CONTROLLED - if (!iceControlling.isEmpty()) - { + if (!iceControlling.isEmpty()) { stream << quint16(IceControlling); stream << quint16(iceControlling.size()); stream.writeRawData(iceControlling.data(), iceControlling.size()); @@ -963,8 +934,7 @@ QByteArray QXmppStunMessage::encode(const QByteArray &key, bool addFingerprint) setBodyLength(buffer, buffer.size() - STUN_HEADER); // MESSAGE-INTEGRITY - if (!key.isEmpty()) - { + if (!key.isEmpty()) { setBodyLength(buffer, buffer.size() - STUN_HEADER + 24); QByteArray integrity = QXmppUtils::generateHmacSha1(key, buffer); stream << quint16(MessageIntegrity); @@ -973,8 +943,7 @@ QByteArray QXmppStunMessage::encode(const QByteArray &key, bool addFingerprint) } // FINGERPRINT - if (addFingerprint) - { + if (addFingerprint) { setBodyLength(buffer, buffer.size() - STUN_HEADER + 8); quint32 fingerprint = QXmppUtils::generateCrc32(buffer) ^ 0x5354554eL; stream << quint16(Fingerprint); @@ -1017,29 +986,54 @@ QString QXmppStunMessage::toString() const { QStringList dumpLines; QString typeName; - switch (messageMethod()) - { - case Binding: typeName = "Binding"; break; - case SharedSecret: typeName = "Shared Secret"; break; - case Allocate: typeName = "Allocate"; break; - case Refresh: typeName = "Refresh"; break; - case Send: typeName = "Send"; break; - case Data: typeName = "Data"; break; - case CreatePermission: typeName = "CreatePermission"; break; - case ChannelBind: typeName = "ChannelBind"; break; - default: typeName = "Unknown"; break; - } - switch (messageClass()) - { - case Request: typeName += " Request"; break; - case Indication: typeName += " Indication"; break; - case Response: typeName += " Response"; break; - case Error: typeName += " Error"; break; - default: break; + switch (messageMethod()) { + case Binding: + typeName = "Binding"; + break; + case SharedSecret: + typeName = "Shared Secret"; + break; + case Allocate: + typeName = "Allocate"; + break; + case Refresh: + typeName = "Refresh"; + break; + case Send: + typeName = "Send"; + break; + case Data: + typeName = "Data"; + break; + case CreatePermission: + typeName = "CreatePermission"; + break; + case ChannelBind: + typeName = "ChannelBind"; + break; + default: + typeName = "Unknown"; + break; + } + switch (messageClass()) { + case Request: + typeName += " Request"; + break; + case Indication: + typeName += " Indication"; + break; + case Response: + typeName += " Response"; + break; + case Error: + typeName += " Error"; + break; + default: + break; } dumpLines << QString(" type %1 (%2)") - .arg(typeName) - .arg(QString::number(m_type)); + .arg(typeName) + .arg(QString::number(m_type)); dumpLines << QString(" id %1").arg(QString::fromLatin1(m_id.toHex())); // attributes @@ -1047,7 +1041,7 @@ QString QXmppStunMessage::toString() const dumpLines << QString(" * CHANNEL-NUMBER %1").arg(QString::number(m_channelNumber)); if (errorCode) dumpLines << QString(" * ERROR-CODE %1 %2") - .arg(QString::number(errorCode), errorPhrase); + .arg(QString::number(errorCode), errorPhrase); if (m_attributes.contains(Lifetime)) dumpLines << QString(" * LIFETIME %1").arg(QString::number(m_lifetime)); if (m_attributes.contains(Nonce)) @@ -1064,36 +1058,36 @@ QString QXmppStunMessage::toString() const dumpLines << QString(" * USERNAME %1").arg(m_username); if (mappedPort) dumpLines << QString(" * MAPPED-ADDRESS %1 %2") - .arg(mappedHost.toString(), QString::number(mappedPort)); + .arg(mappedHost.toString(), QString::number(mappedPort)); if (m_attributes.contains(ChangeRequest)) dumpLines << QString(" * CHANGE-REQUEST %1") - .arg(QString::number(m_changeRequest)); + .arg(QString::number(m_changeRequest)); if (sourcePort) dumpLines << QString(" * SOURCE-ADDRESS %1 %2") - .arg(sourceHost.toString(), QString::number(sourcePort)); + .arg(sourceHost.toString(), QString::number(sourcePort)); if (changedPort) dumpLines << QString(" * CHANGED-ADDRESS %1 %2") - .arg(changedHost.toString(), QString::number(changedPort)); + .arg(changedHost.toString(), QString::number(changedPort)); if (otherPort) dumpLines << QString(" * OTHER-ADDRESS %1 %2") - .arg(otherHost.toString(), QString::number(otherPort)); + .arg(otherHost.toString(), QString::number(otherPort)); if (xorMappedPort) dumpLines << QString(" * XOR-MAPPED-ADDRESS %1 %2") - .arg(xorMappedHost.toString(), QString::number(xorMappedPort)); + .arg(xorMappedHost.toString(), QString::number(xorMappedPort)); if (xorPeerPort) dumpLines << QString(" * XOR-PEER-ADDRESS %1 %2") - .arg(xorPeerHost.toString(), QString::number(xorPeerPort)); + .arg(xorPeerHost.toString(), QString::number(xorPeerPort)); if (xorRelayedPort) dumpLines << QString(" * XOR-RELAYED-ADDRESS %1 %2") - .arg(xorRelayedHost.toString(), QString::number(xorRelayedPort)); + .arg(xorRelayedHost.toString(), QString::number(xorRelayedPort)); if (m_attributes.contains(Priority)) dumpLines << QString(" * PRIORITY %1").arg(QString::number(m_priority)); if (!iceControlling.isEmpty()) dumpLines << QString(" * ICE-CONTROLLING %1") - .arg(QString::fromLatin1(iceControlling.toHex())); + .arg(QString::fromLatin1(iceControlling.toHex())); if (!iceControlled.isEmpty()) dumpLines << QString(" * ICE-CONTROLLED %1") - .arg(QString::fromLatin1(iceControlled.toHex())); + .arg(QString::fromLatin1(iceControlled.toHex())); if (useCandidate) dumpLines << QString(" * USE-CANDIDATE"); @@ -1107,8 +1101,8 @@ QString QXmppStunMessage::toString() const QXmppStunTransaction::QXmppStunTransaction(const QXmppStunMessage &request, QObject *receiver) : QXmppLoggable(receiver), - m_request(request), - m_tries(0) + m_request(request), + m_tries(0) { bool check; Q_UNUSED(check) @@ -1125,7 +1119,7 @@ QXmppStunTransaction::QXmppStunTransaction(const QXmppStunMessage &request, QObj m_retryTimer = new QTimer(this); m_retryTimer->setSingleShot(true); connect(m_retryTimer, &QTimer::timeout, - this, &QXmppStunTransaction::retry); + this, &QXmppStunTransaction::retry); // send packet immediately m_retryTimer->start(0); @@ -1176,27 +1170,27 @@ void QXmppStunTransaction::retry() QXmppTurnAllocation::QXmppTurnAllocation(QObject *parent) : QXmppIceTransport(parent), - m_relayedPort(0), - m_turnPort(0), - m_channelNumber(0x4000), - m_lifetime(600), - m_state(UnconnectedState) + m_relayedPort(0), + m_turnPort(0), + m_channelNumber(0x4000), + m_lifetime(600), + m_state(UnconnectedState) { socket = new QUdpSocket(this); connect(socket, &QIODevice::readyRead, - this, &QXmppTurnAllocation::readyRead); + this, &QXmppTurnAllocation::readyRead); m_timer = new QTimer(this); m_timer->setSingleShot(true); connect(m_timer, &QTimer::timeout, - this, &QXmppTurnAllocation::refresh); + this, &QXmppTurnAllocation::refresh); // channels are valid 600s, we refresh every 500s m_channelTimer = new QTimer(this); m_channelTimer->setInterval(500 * 1000); connect(m_channelTimer, &QTimer::timeout, - this, &QXmppTurnAllocation::refreshChannels); + this, &QXmppTurnAllocation::refreshChannels); } /// Destroys the TURN allocation. @@ -1304,7 +1298,7 @@ void QXmppTurnAllocation::handleDatagram(const QByteArray &buffer, const QHostAd stream >> length; if (m_state == ConnectedState && m_channels.contains(channel) && length <= buffer.size() - 4) { emit datagramReceived(buffer.mid(4, length), m_channels[channel].first, - m_channels[channel].second); + m_channels[channel].second); } return; } @@ -1319,10 +1313,7 @@ void QXmppTurnAllocation::handleDatagram(const QByteArray &buffer, const QHostAd } #ifdef QXMPP_DEBUG_STUN - logReceived(QString("TURN packet from %1 port %2\n%3").arg( - remoteHost.toString(), - QString::number(remotePort), - message.toString())); + logReceived(QString("TURN packet from %1 port %2\n%3").arg(remoteHost.toString(), QString::number(remotePort), message.toString())); #endif // find transaction @@ -1434,7 +1425,7 @@ void QXmppTurnAllocation::setState(AllocationState state) void QXmppTurnAllocation::transactionFinished() { - auto *transaction = qobject_cast<QXmppStunTransaction*>(sender()); + auto *transaction = qobject_cast<QXmppStunTransaction *>(sender()); if (!transaction || !m_transactions.removeAll(transaction)) return; transaction->deleteLater(); @@ -1443,8 +1434,7 @@ void QXmppTurnAllocation::transactionFinished() const QXmppStunMessage reply = transaction->response(); if (reply.messageClass() == QXmppStunMessage::Error && reply.errorCode == 401 && - (reply.nonce() != m_nonce && reply.realm() != m_realm)) - { + (reply.nonce() != m_nonce && reply.realm() != m_realm)) { // update long-term credentials m_nonce = reply.nonce(); m_realm = reply.realm(); @@ -1466,8 +1456,7 @@ void QXmppTurnAllocation::transactionFinished() if (method == QXmppStunMessage::Allocate) { if (reply.messageClass() == QXmppStunMessage::Error) { - warning(QString("Allocation failed: %1 %2").arg( - QString::number(reply.errorCode), reply.errorPhrase)); + warning(QString("Allocation failed: %1 %2").arg(QString::number(reply.errorCode), reply.errorPhrase)); setState(UnconnectedState); return; } @@ -1492,8 +1481,7 @@ void QXmppTurnAllocation::transactionFinished() } else if (method == QXmppStunMessage::ChannelBind) { if (reply.messageClass() == QXmppStunMessage::Error) { - warning(QString("ChannelBind failed: %1 %2").arg( - QString::number(reply.errorCode), reply.errorPhrase)); + warning(QString("ChannelBind failed: %1 %2").arg(QString::number(reply.errorCode), reply.errorPhrase)); // remove channel m_channels.remove(transaction->request().channelNumber()); @@ -1505,8 +1493,7 @@ void QXmppTurnAllocation::transactionFinished() } else if (method == QXmppStunMessage::Refresh) { if (reply.messageClass() == QXmppStunMessage::Error) { - warning(QString("Refresh failed: %1 %2").arg( - QString::number(reply.errorCode), reply.errorPhrase)); + warning(QString("Refresh failed: %1 %2").arg(QString::number(reply.errorCode), reply.errorPhrase)); setState(UnconnectedState); return; } @@ -1519,7 +1506,6 @@ void QXmppTurnAllocation::transactionFinished() // schedule refresh m_lifetime = reply.lifetime(); m_timer->start((m_lifetime - 60) * 1000); - } } @@ -1569,16 +1555,12 @@ void QXmppTurnAllocation::writeStun(const QXmppStunMessage &message) { socket->writeDatagram(message.encode(m_key), m_turnHost, m_turnPort); #ifdef QXMPP_DEBUG_STUN - logSent(QString("TURN packet to %1 port %2\n%3").arg( - m_turnHost.toString(), - QString::number(m_turnPort), - message.toString())); + logSent(QString("TURN packet to %1 port %2\n%3").arg(m_turnHost.toString(), QString::number(m_turnPort), message.toString())); #endif } QXmppUdpTransport::QXmppUdpTransport(QUdpSocket *socket, QObject *parent) - : QXmppIceTransport(parent) - , m_socket(socket) + : QXmppIceTransport(parent), m_socket(socket) { connect(m_socket, &QIODevice::readyRead, this, &QXmppUdpTransport::readyRead); @@ -1672,14 +1654,7 @@ static bool candidatePairPtrLessThan(const CandidatePair *p1, const CandidatePai } CandidatePair::CandidatePair(int component, bool controlling, QObject *parent) - : QXmppLoggable(parent) - , nominated(false) - , nominating(false) - , transport(nullptr) - , transaction(nullptr) - , m_component(component) - , m_controlling(controlling) - , m_state(WaitingState) + : QXmppLoggable(parent), nominated(false), nominating(false), transport(nullptr), transaction(nullptr), m_component(component), m_controlling(controlling), m_state(WaitingState) { } @@ -1733,8 +1708,7 @@ public: }; QXmppIcePrivate::QXmppIcePrivate() - : iceControlling(false) - , stunPort(0) + : iceControlling(false), stunPort(0) { localUser = QXmppUtils::generateStanzaHash(4); localPassword = QXmppUtils::generateStanzaHash(22); @@ -1746,9 +1720,9 @@ class QXmppIceComponentPrivate public: QXmppIceComponentPrivate(int component, QXmppIcePrivate *config, QXmppIceComponent *qq); bool addRemoteCandidate(const QXmppJingleCandidate &candidate); - CandidatePair* findPair(QXmppStunTransaction *transaction); + CandidatePair *findPair(QXmppStunTransaction *transaction); void performCheck(CandidatePair *pair, bool nominate); - void setSockets(QList<QUdpSocket*> sockets); + void setSockets(QList<QUdpSocket *> sockets); void setTurnServer(const QHostAddress &host, quint16 port); void setTurnUser(const QString &user); void setTurnPassword(const QString &password); @@ -1756,7 +1730,7 @@ public: CandidatePair *activePair; const int component; - const QXmppIcePrivate* const config; + const QXmppIcePrivate *const config; CandidatePair *fallbackPair; QXmppIceConnection::GatheringState gatheringState; @@ -1766,12 +1740,12 @@ public: quint32 peerReflexivePriority; QList<QXmppJingleCandidate> remoteCandidates; - QList<CandidatePair*> pairs; - QList<QXmppIceTransport*> transports; + QList<CandidatePair *> pairs; + QList<QXmppIceTransport *> transports; QTimer *timer; // STUN server - QMap<QXmppStunTransaction*, QXmppIceTransport*> stunTransactions; + QMap<QXmppStunTransaction *, QXmppIceTransport *> stunTransactions; // TURN server QXmppTurnAllocation *turnAllocation; @@ -1782,16 +1756,7 @@ private: }; QXmppIceComponentPrivate::QXmppIceComponentPrivate(int component_, QXmppIcePrivate *config_, QXmppIceComponent *qq) - : activePair(nullptr) - , component(component_) - , config(config_) - , fallbackPair(nullptr) - , gatheringState(QXmppIceConnection::NewGatheringState) - , peerReflexivePriority(0) - , timer(nullptr) - , turnAllocation(nullptr) - , turnConfigured(false) - , q(qq) + : activePair(nullptr), component(component_), config(config_), fallbackPair(nullptr), gatheringState(QXmppIceConnection::NewGatheringState), peerReflexivePriority(0), timer(nullptr), turnAllocation(nullptr), turnConfigured(false), q(qq) { } @@ -1803,7 +1768,7 @@ bool QXmppIceComponentPrivate::addRemoteCandidate(const QXmppJingleCandidate &ca candidate.type() != QXmppJingleCandidate::ServerReflexiveType) || candidate.protocol() != "udp" || (candidate.host().protocol() != QAbstractSocket::IPv4Protocol && - candidate.host().protocol() != QAbstractSocket::IPv6Protocol)) + candidate.host().protocol() != QAbstractSocket::IPv6Protocol)) return false; for (const auto &c : remoteCandidates) @@ -1831,7 +1796,7 @@ bool QXmppIceComponentPrivate::addRemoteCandidate(const QXmppJingleCandidate &ca return true; } -CandidatePair* QXmppIceComponentPrivate::findPair(QXmppStunTransaction *transaction) +CandidatePair *QXmppIceComponentPrivate::findPair(QXmppStunTransaction *transaction) { for (auto *pair : pairs) { if (pair->transaction == transaction) @@ -1858,7 +1823,7 @@ void QXmppIceComponentPrivate::performCheck(CandidatePair *pair, bool nominate) pair->transaction = new QXmppStunTransaction(message, q); } -void QXmppIceComponentPrivate::setSockets(QList<QUdpSocket*> sockets) +void QXmppIceComponentPrivate::setSockets(QList<QUdpSocket *> sockets) { // clear previous candidates and sockets @@ -1875,7 +1840,7 @@ void QXmppIceComponentPrivate::setSockets(QList<QUdpSocket*> sockets) auto *transport = new QXmppUdpTransport(socket, q); QObject::connect(transport, &QXmppIceTransport::datagramReceived, - q, &QXmppIceComponent::handleDatagram); + q, &QXmppIceComponent::handleDatagram); QXmppJingleCandidate candidate = transport->localCandidate(component); @@ -1931,10 +1896,7 @@ void QXmppIceComponentPrivate::writeStun(const QXmppStunMessage &message, QXmppI const QByteArray data = message.encode(messagePassword.toUtf8()); transport->writeDatagram(data, address, port); #ifdef QXMPP_DEBUG_STUN - q->logSent(QString("STUN packet to %1 port %2\n%3").arg( - address.toString(), - QString::number(port), - message.toString())); + q->logSent(QString("STUN packet to %1 port %2\n%3").arg(address.toString(), QString::number(port), message.toString())); #endif } @@ -1951,15 +1913,15 @@ QXmppIceComponent::QXmppIceComponent(int component, QXmppIcePrivate *config, QOb d->timer = new QTimer(this); d->timer->setInterval(500); connect(d->timer, &QTimer::timeout, - this, &QXmppIceComponent::checkCandidates); + this, &QXmppIceComponent::checkCandidates); d->turnAllocation = new QXmppTurnAllocation(this); connect(d->turnAllocation, &QXmppTurnAllocation::connected, - this, &QXmppIceComponent::turnConnected); + this, &QXmppIceComponent::turnConnected); connect(d->turnAllocation, &QXmppIceTransport::datagramReceived, - this, &QXmppIceComponent::handleDatagram); + this, &QXmppIceComponent::handleDatagram); connect(d->turnAllocation, &QXmppTurnAllocation::disconnected, - this, &QXmppIceComponent::updateGatheringState); + this, &QXmppIceComponent::updateGatheringState); // calculate peer-reflexive candidate priority // see RFC 5245 - 7.1.2.1. PRIORITY and USE-CANDIDATE @@ -2039,7 +2001,7 @@ QList<QXmppJingleCandidate> QXmppIceComponent::localCandidates() const void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddress &remoteHost, quint16 remotePort) { - auto *transport = qobject_cast<QXmppIceTransport*>(sender()); + auto *transport = qobject_cast<QXmppIceTransport *>(sender()); if (!transport) return; @@ -2047,8 +2009,7 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr quint32 messageCookie; QByteArray messageId; quint16 messageType = QXmppStunMessage::peekType(buffer, messageCookie, messageId); - if (!messageType || messageCookie != STUN_MAGIC) - { + if (!messageType || messageCookie != STUN_MAGIC) { // use this as an opportunity to flag a potential pair for (auto *pair : d->pairs) { if (pair->remote.host() == remoteHost && @@ -2088,10 +2049,7 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr return; } #ifdef QXMPP_DEBUG_STUN - logReceived(QString("STUN packet from %1 port %2\n%3").arg( - remoteHost.toString(), - QString::number(remotePort), - message.toString())); + logReceived(QString("STUN packet from %1 port %2\n%3").arg(remoteHost.toString(), QString::number(remotePort), message.toString())); #endif // we only want binding requests and responses @@ -2106,8 +2064,7 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr // process message from peer CandidatePair *pair = nullptr; - if (message.messageClass() == QXmppStunMessage::Request) - { + if (message.messageClass() == QXmppStunMessage::Request) { // check for role conflict if (d->config->iceControlling && (!message.iceControlling.isEmpty() || message.useCandidate)) { warning("Role conflict, expected to be controlling"); @@ -2151,9 +2108,7 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr // construct pair for (auto *ptr : d->pairs) { - if (ptr->transport == transport - && ptr->remote.host() == remoteHost - && ptr->remote.port() == remotePort) { + if (ptr->transport == transport && ptr->remote.host() == remoteHost && ptr->remote.port() == remotePort) { pair = ptr; break; } @@ -2185,8 +2140,7 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr break; } - } else if (message.messageClass() == QXmppStunMessage::Response - || message.messageClass() == QXmppStunMessage::Error) { + } else if (message.messageClass() == QXmppStunMessage::Response || message.messageClass() == QXmppStunMessage::Error) { // find the pair for this transaction for (auto *ptr : d->pairs) { @@ -2202,9 +2156,7 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr if (remoteHost != pair->remote.host() || remotePort != pair->remote.port()) { QXmppStunMessage error; error.setType(QXmppStunMessage::Error); - error.errorPhrase = QString("Received response from unexpected %1:%1").arg( - remoteHost.toString(), - QString::number(remotePort)); + error.errorPhrase = QString("Received response from unexpected %1:%1").arg(remoteHost.toString(), QString::number(remotePort)); pair->transaction->readStun(error); return; } @@ -2216,8 +2168,7 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr if (pair && pair->nominated) { d->timer->stop(); if (!d->activePair || pair->priority() > d->activePair->priority()) { - info(QString("ICE pair selected %1 (priority: %2)").arg( - pair->toString(), QString::number(pair->priority()))); + info(QString("ICE pair selected %1 (priority: %2)").arg(pair->toString(), QString::number(pair->priority()))); const bool wasConnected = (d->activePair != nullptr); d->activePair = pair; if (!wasConnected) @@ -2228,7 +2179,7 @@ void QXmppIceComponent::handleDatagram(const QByteArray &buffer, const QHostAddr void QXmppIceComponent::transactionFinished() { - auto *transaction = qobject_cast<QXmppStunTransaction*>(sender()); + auto *transaction = qobject_cast<QXmppStunTransaction *>(sender()); transaction->deleteLater(); // ICE checks @@ -2248,9 +2199,7 @@ void QXmppIceComponent::transactionFinished() pair->nominated = true; } } else { - debug(QString("ICE forward check failed %1 (error %2)").arg( - pair->toString(), - transaction->response().errorPhrase)); + debug(QString("ICE forward check failed %1 (error %2)").arg(pair->toString(), transaction->response().errorPhrase)); pair->setState(CandidatePair::FailedState); } pair->transaction = nullptr; @@ -2303,8 +2252,7 @@ void QXmppIceComponent::transactionFinished() emit localCandidatesChanged(); } else { - debug(QString("STUN test failed (error %1)").arg( - transaction->response().errorPhrase)); + debug(QString("STUN test failed (error %1)").arg(transaction->response().errorPhrase)); } d->stunTransactions.remove(transaction); updateGatheringState(); @@ -2317,18 +2265,16 @@ void QXmppIceComponent::turnConnected() const QXmppJingleCandidate candidate = d->turnAllocation->localCandidate(d->component); // add the new local candidate - debug(QString("Adding relayed candidate %1 port %2").arg( - candidate.host().toString(), - QString::number(candidate.port()))); + debug(QString("Adding relayed candidate %1 port %2").arg(candidate.host().toString(), QString::number(candidate.port()))); d->localCandidates << candidate; emit localCandidatesChanged(); updateGatheringState(); } -static QList<QUdpSocket*> reservePort(const QList<QHostAddress> &addresses, quint16 port, QObject *parent) +static QList<QUdpSocket *> reservePort(const QList<QHostAddress> &addresses, quint16 port, QObject *parent) { - QList<QUdpSocket*> sockets; + QList<QUdpSocket *> sockets; for (const auto &address : addresses) { auto *socket = new QUdpSocket(parent); sockets << socket; @@ -2384,9 +2330,9 @@ QList<QHostAddress> QXmppIceComponent::discoverAddresses() /// \param count The number of ports to reserve. /// \param parent The parent object for the sockets. -QList<QUdpSocket*> QXmppIceComponent::reservePorts(const QList<QHostAddress> &addresses, int count, QObject *parent) +QList<QUdpSocket *> QXmppIceComponent::reservePorts(const QList<QHostAddress> &addresses, int count, QObject *parent) { - QList<QUdpSocket*> sockets; + QList<QUdpSocket *> sockets; if (addresses.isEmpty() || !count) return sockets; @@ -2396,7 +2342,7 @@ QList<QUdpSocket*> QXmppIceComponent::reservePorts(const QList<QHostAddress> &ad // reserve first port (even number) if (port % 2) port++; - QList<QUdpSocket*> socketChunk; + QList<QUdpSocket *> socketChunk; while (socketChunk.isEmpty() && port <= 65536 - count) { socketChunk = reservePort(addresses, port, parent); if (socketChunk.isEmpty()) @@ -2440,8 +2386,7 @@ void QXmppIceComponent::updateGatheringState() QXmppIceConnection::GatheringState newGatheringState; if (d->transports.isEmpty()) newGatheringState = QXmppIceConnection::NewGatheringState; - else if (!d->stunTransactions.isEmpty() - || d->turnAllocation->state() == QXmppTurnAllocation::ConnectingState) + else if (!d->stunTransactions.isEmpty() || d->turnAllocation->state() == QXmppTurnAllocation::ConnectingState) newGatheringState = QXmppIceConnection::BusyGatheringState; else newGatheringState = QXmppIceConnection::CompleteGatheringState; @@ -2454,7 +2399,7 @@ void QXmppIceComponent::updateGatheringState() void QXmppIceComponent::writeStun(const QXmppStunMessage &message) { - auto *transaction = qobject_cast<QXmppStunTransaction*>(sender()); + auto *transaction = qobject_cast<QXmppStunTransaction *>(sender()); // ICE checks CandidatePair *pair = d->findPair(transaction); @@ -2468,10 +2413,7 @@ void QXmppIceComponent::writeStun(const QXmppStunMessage &message) if (transport) { transport->writeDatagram(message.encode(), d->config->stunHost, d->config->stunPort); #ifdef QXMPP_DEBUG_STUN - logSent(QString("STUN packet to %1 port %2\n%3").arg( - d->config->stunHost.toString(), - QString::number(d->config->stunPort), - message.toString())); + logSent(QString("STUN packet to %1 port %2\n%3").arg(d->config->stunHost.toString(), QString::number(d->config->stunPort), message.toString())); #endif return; } @@ -2482,7 +2424,7 @@ class QXmppIceConnectionPrivate : public QXmppIcePrivate public: QXmppIceConnectionPrivate(); - QMap<int, QXmppIceComponent*> components; + QMap<int, QXmppIceComponent *> components; QTimer *connectTimer; QXmppIceConnection::GatheringState gatheringState; @@ -2494,9 +2436,7 @@ public: }; QXmppIceConnectionPrivate::QXmppIceConnectionPrivate() - : connectTimer(nullptr) - , gatheringState(QXmppIceConnection::NewGatheringState) - , turnPort(0) + : connectTimer(nullptr), gatheringState(QXmppIceConnection::NewGatheringState), turnPort(0) { } @@ -2505,8 +2445,7 @@ QXmppIceConnectionPrivate::QXmppIceConnectionPrivate() /// \param parent QXmppIceConnection::QXmppIceConnection(QObject *parent) - : QXmppLoggable(parent) - , d(new QXmppIceConnectionPrivate()) + : QXmppLoggable(parent), d(new QXmppIceConnectionPrivate()) { // timer to limit connection time to 30 seconds @@ -2514,7 +2453,7 @@ QXmppIceConnection::QXmppIceConnection(QObject *parent) d->connectTimer->setInterval(30000); d->connectTimer->setSingleShot(true); connect(d->connectTimer, &QTimer::timeout, - this, &QXmppIceConnection::slotTimeout); + this, &QXmppIceConnection::slotTimeout); } QXmppIceConnection::~QXmppIceConnection() @@ -2550,13 +2489,13 @@ void QXmppIceConnection::addComponent(int component) socket->d->setTurnPassword(d->turnPassword); connect(socket, &QXmppIceComponent::localCandidatesChanged, - this, &QXmppIceConnection::localCandidatesChanged); + this, &QXmppIceConnection::localCandidatesChanged); connect(socket, &QXmppIceComponent::connected, - this, &QXmppIceConnection::slotConnected); + this, &QXmppIceConnection::slotConnected); connect(socket, &QXmppIceComponent::gatheringStateChanged, - this, &QXmppIceConnection::slotGatheringStateChanged); + this, &QXmppIceConnection::slotGatheringStateChanged); d->components[component] = socket; } @@ -2569,8 +2508,7 @@ void QXmppIceConnection::addRemoteCandidate(const QXmppJingleCandidate &candidat { QXmppIceComponent *socket = d->components.value(candidate.component()); if (!socket) { - warning(QString("Not adding candidate for unknown component %1").arg( - QString::number(candidate.component()))); + warning(QString("Not adding candidate for unknown component %1").arg(QString::number(candidate.component()))); return; } socket->d->addRemoteCandidate(candidate); @@ -2583,7 +2521,7 @@ void QXmppIceConnection::addRemoteCandidate(const QXmppJingleCandidate &candidat bool QXmppIceConnection::bind(const QList<QHostAddress> &addresses) { // reserve ports - QList<QUdpSocket*> sockets = QXmppIceComponent::reservePorts(addresses, d->components.size()); + QList<QUdpSocket *> sockets = QXmppIceComponent::reservePorts(addresses, d->components.size()); if (sockets.isEmpty() && !addresses.isEmpty()) return false; @@ -2620,7 +2558,6 @@ void QXmppIceConnection::connectToHost() d->connectTimer->start(); } - /// Returns true if ICE negotiation completed, false otherwise. bool QXmppIceConnection::isConnected() const @@ -2776,9 +2713,7 @@ void QXmppIceConnection::slotGatheringStateChanged() newGatheringState = BusyGatheringState; if (newGatheringState != d->gatheringState) { - info(QString("ICE gathering state changed from '%1' to '%2'").arg( - gathering_states[d->gatheringState], - gathering_states[newGatheringState])); + info(QString("ICE gathering state changed from '%1' to '%2'").arg(gathering_states[d->gatheringState], gathering_states[newGatheringState])); d->gatheringState = newGatheringState; emit gatheringStateChanged(); } |
