Skip to content

Commit

Permalink
REFAC: Use dedicated struct for packet stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Hartmnt committed Jan 11, 2025
1 parent 48a893b commit 47797b7
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 60 deletions.
18 changes: 9 additions & 9 deletions src/crypto/CryptState.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
#include "Timer.h"
#include <string>

struct PacketStats {
unsigned int good = 0;
unsigned int late = 0;
unsigned int lost = 0;
unsigned int resync = 0;
};

class CryptState {
private:
Q_DISABLE_COPY(CryptState)
public:
unsigned int uiGood = 0;
unsigned int uiLate = 0;
unsigned int uiLost = 0;
unsigned int uiResync = 0;

unsigned int uiRemoteGood = 0;
unsigned int uiRemoteLate = 0;
unsigned int uiRemoteLost = 0;
unsigned int uiRemoteResync = 0;
PacketStats m_statsLocal = {};
PacketStats m_statsRemote = {};

Timer tLastGood;
Timer tLastRequest;
Expand Down
18 changes: 9 additions & 9 deletions src/crypto/CryptStateOCB2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,18 @@ bool CryptStateOCB2::decrypt(const unsigned char *source, unsigned char *dst, un
if (restore)
memcpy(decrypt_iv, saveiv, AES_BLOCK_SIZE);

uiGood++;
// uiLate += late, but we have to make sure we don't cause wrap-arounds on the unsigned lhs
m_statsLocal.good++;
// m_statsLocal.late += late, but we have to make sure we don't cause wrap-arounds on the unsigned lhs
if (late > 0) {
uiLate += static_cast< unsigned int >(late);
} else if (static_cast< int >(uiLate) > std::abs(late)) {
uiLate -= static_cast< unsigned int >(std::abs(late));
m_statsLocal.late += static_cast< unsigned int >(late);
} else if (static_cast< int >(m_statsLocal.late) > std::abs(late)) {
m_statsLocal.late -= static_cast< unsigned int >(std::abs(late));
}
// uiLost += lost, but we have to make sure we don't cause wrap-arounds on the unsigned lhs
// m_statsLocal.lost += lost, but we have to make sure we don't cause wrap-arounds on the unsigned lhs
if (lost > 0) {
uiLost += static_cast< unsigned int >(lost);
} else if (static_cast< int >(uiLost) > std::abs(lost)) {
uiLost -= static_cast< unsigned int >(std::abs(lost));
m_statsLocal.lost += static_cast< unsigned int >(lost);
} else if (static_cast< int >(m_statsLocal.lost) > std::abs(lost)) {
m_statsLocal.lost -= static_cast< unsigned int >(std::abs(lost));
}

tLastGood.restart();
Expand Down
2 changes: 1 addition & 1 deletion src/mumble/Messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ void MainWindow::msgCryptSetup(const MumbleProto::CryptSetup &msg) {
} else if (msg.has_server_nonce()) {
const std::string &server_nonce = msg.server_nonce();
if (server_nonce.size() == AES_BLOCK_SIZE) {
c->csCrypt->uiResync++;
c->csCrypt->m_statsLocal.resync++;
if (!c->csCrypt->setDecryptIV(server_nonce)) {
qWarning("Messages: Cipher resync failed: Invalid nonce from the server!");
}
Expand Down
27 changes: 14 additions & 13 deletions src/mumble/ServerHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,10 @@ void ServerHandler::sendPingInternal() {
MumbleProto::Ping mpp;

mpp.set_timestamp(t);
mpp.set_good(connection->csCrypt->uiGood);
mpp.set_late(connection->csCrypt->uiLate);
mpp.set_lost(connection->csCrypt->uiLost);
mpp.set_resync(connection->csCrypt->uiResync);
mpp.set_good(connection->csCrypt->m_statsLocal.good);
mpp.set_late(connection->csCrypt->m_statsLocal.late);
mpp.set_lost(connection->csCrypt->m_statsLocal.lost);
mpp.set_resync(connection->csCrypt->m_statsLocal.resync);


if (boost::accumulators::count(accUDP)) {
Expand Down Expand Up @@ -639,20 +639,20 @@ void ServerHandler::message(Mumble::Protocol::TCPMessageType type, const QByteAr
// connection is still OK.
iInFlightTCPPings = 0;

connection->csCrypt->uiRemoteGood = msg.good();
connection->csCrypt->uiRemoteLate = msg.late();
connection->csCrypt->uiRemoteLost = msg.lost();
connection->csCrypt->uiRemoteResync = msg.resync();
connection->csCrypt->m_statsRemote.good = msg.good();
connection->csCrypt->m_statsRemote.late = msg.late();
connection->csCrypt->m_statsRemote.lost = msg.lost();
connection->csCrypt->m_statsRemote.resync = msg.resync();
accTCP(static_cast< double >(tTimestamp.elapsed() - msg.timestamp()) / 1000.0);

if (((connection->csCrypt->uiRemoteGood == 0) || (connection->csCrypt->uiGood == 0)) && bUdp
&& (tTimestamp.elapsed() > 20000000ULL)) {
if (((connection->csCrypt->m_statsRemote.good == 0) || (connection->csCrypt->m_statsLocal.good == 0))
&& bUdp && (tTimestamp.elapsed() > 20000000ULL)) {
bUdp = false;
if (!NetworkConfig::TcpModeEnabled()) {
if ((connection->csCrypt->uiRemoteGood == 0) && (connection->csCrypt->uiGood == 0))
if ((connection->csCrypt->m_statsRemote.good == 0) && (connection->csCrypt->m_statsLocal.good == 0))
Global::get().mw->msgBox(
tr("UDP packets cannot be sent to or received from the server. Switching to TCP mode."));
else if (connection->csCrypt->uiRemoteGood == 0)
else if (connection->csCrypt->m_statsRemote.good == 0)
Global::get().mw->msgBox(
tr("UDP packets cannot be sent to the server. Switching to TCP mode."));
else
Expand All @@ -661,7 +661,8 @@ void ServerHandler::message(Mumble::Protocol::TCPMessageType type, const QByteAr

database->setUdp(qbaDigest, false);
}
} else if (!bUdp && (connection->csCrypt->uiRemoteGood > 3) && (connection->csCrypt->uiGood > 3)) {
} else if (!bUdp && (connection->csCrypt->m_statsRemote.good > 3)
&& (connection->csCrypt->m_statsLocal.good > 3)) {
bUdp = true;
if (!NetworkConfig::TcpModeEnabled()) {
Global::get().mw->msgBox(
Expand Down
16 changes: 8 additions & 8 deletions src/mumble/ServerInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ void ServerInformation::populateUDPStatistics(const Connection &connection) {
constexpr int LOST_ROW = 2;
constexpr int RESYNC_ROW = 3;

QTableWidgetItem *toGoodItem = new QTableWidgetItem(QString::number(connection.csCrypt->uiRemoteGood));
QTableWidgetItem *fromGoodItem = new QTableWidgetItem(QString::number(connection.csCrypt->uiGood));
QTableWidgetItem *toLateItem = new QTableWidgetItem(QString::number(connection.csCrypt->uiRemoteLate));
QTableWidgetItem *fromLateItem = new QTableWidgetItem(QString::number(connection.csCrypt->uiLate));
QTableWidgetItem *toLostItem = new QTableWidgetItem(QString::number(connection.csCrypt->uiRemoteLost));
QTableWidgetItem *fromLostItem = new QTableWidgetItem(QString::number(connection.csCrypt->uiLost));
QTableWidgetItem *toResyncItem = new QTableWidgetItem(QString::number(connection.csCrypt->uiRemoteResync));
QTableWidgetItem *fromResyncItem = new QTableWidgetItem(QString::number(connection.csCrypt->uiResync));
QTableWidgetItem *toGoodItem = new QTableWidgetItem(QString::number(connection.csCrypt->m_statsRemote.good));
QTableWidgetItem *fromGoodItem = new QTableWidgetItem(QString::number(connection.csCrypt->m_statsLocal.good));
QTableWidgetItem *toLateItem = new QTableWidgetItem(QString::number(connection.csCrypt->m_statsRemote.late));
QTableWidgetItem *fromLateItem = new QTableWidgetItem(QString::number(connection.csCrypt->m_statsLocal.late));
QTableWidgetItem *toLostItem = new QTableWidgetItem(QString::number(connection.csCrypt->m_statsRemote.lost));
QTableWidgetItem *fromLostItem = new QTableWidgetItem(QString::number(connection.csCrypt->m_statsLocal.lost));
QTableWidgetItem *toResyncItem = new QTableWidgetItem(QString::number(connection.csCrypt->m_statsRemote.resync));
QTableWidgetItem *fromResyncItem = new QTableWidgetItem(QString::number(connection.csCrypt->m_statsLocal.resync));

connection_udp_statisticsTable->setItem(GOOD_ROW, TO_SERVER_COL, toGoodItem);
connection_udp_statisticsTable->setItem(GOOD_ROW, FROM_SERVER_COL, fromGoodItem);
Expand Down
34 changes: 17 additions & 17 deletions src/murmur/Messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1997,10 +1997,10 @@ void Server::msgPing(ServerUser *uSource, MumbleProto::Ping &msg) {

QMutexLocker l(&uSource->qmCrypt);

uSource->csCrypt->uiRemoteGood = msg.good();
uSource->csCrypt->uiRemoteLate = msg.late();
uSource->csCrypt->uiRemoteLost = msg.lost();
uSource->csCrypt->uiRemoteResync = msg.resync();
uSource->csCrypt->m_statsRemote.good = msg.good();
uSource->csCrypt->m_statsRemote.late = msg.late();
uSource->csCrypt->m_statsRemote.lost = msg.lost();
uSource->csCrypt->m_statsRemote.resync = msg.resync();

uSource->dUDPPingAvg = msg.udp_ping_avg();
uSource->dUDPPingVar = msg.udp_ping_var();
Expand All @@ -2013,10 +2013,10 @@ void Server::msgPing(ServerUser *uSource, MumbleProto::Ping &msg) {

msg.Clear();
msg.set_timestamp(ts);
msg.set_good(uSource->csCrypt->uiGood);
msg.set_late(uSource->csCrypt->uiLate);
msg.set_lost(uSource->csCrypt->uiLost);
msg.set_resync(uSource->csCrypt->uiResync);
msg.set_good(uSource->csCrypt->m_statsLocal.good);
msg.set_late(uSource->csCrypt->m_statsLocal.late);
msg.set_lost(uSource->csCrypt->m_statsLocal.lost);
msg.set_resync(uSource->csCrypt->m_statsLocal.resync);

sendMessage(uSource, msg);
}
Expand All @@ -2034,7 +2034,7 @@ void Server::msgCryptSetup(ServerUser *uSource, MumbleProto::CryptSetup &msg) {
sendMessage(uSource, msg);
} else {
const std::string &str = msg.client_nonce();
uSource->csCrypt->uiResync++;
uSource->csCrypt->m_statsLocal.resync++;
if (!uSource->csCrypt->setDecryptIV(str)) {
qWarning("Messages: Cipher resync failed: Invalid nonce from the client!");
}
Expand Down Expand Up @@ -2275,16 +2275,16 @@ void Server::msgUserStats(ServerUser *uSource, MumbleProto::UserStats &msg) {
QMutexLocker l(&pDstServerUser->qmCrypt);

mpusss = msg.mutable_from_client();
mpusss->set_good(pDstServerUser->csCrypt->uiGood);
mpusss->set_late(pDstServerUser->csCrypt->uiLate);
mpusss->set_lost(pDstServerUser->csCrypt->uiLost);
mpusss->set_resync(pDstServerUser->csCrypt->uiResync);
mpusss->set_good(pDstServerUser->csCrypt->m_statsLocal.good);
mpusss->set_late(pDstServerUser->csCrypt->m_statsLocal.late);
mpusss->set_lost(pDstServerUser->csCrypt->m_statsLocal.lost);
mpusss->set_resync(pDstServerUser->csCrypt->m_statsLocal.resync);

mpusss = msg.mutable_from_server();
mpusss->set_good(pDstServerUser->csCrypt->uiRemoteGood);
mpusss->set_late(pDstServerUser->csCrypt->uiRemoteLate);
mpusss->set_lost(pDstServerUser->csCrypt->uiRemoteLost);
mpusss->set_resync(pDstServerUser->csCrypt->uiRemoteResync);
mpusss->set_good(pDstServerUser->csCrypt->m_statsRemote.good);
mpusss->set_late(pDstServerUser->csCrypt->m_statsRemote.late);
mpusss->set_lost(pDstServerUser->csCrypt->m_statsRemote.lost);
mpusss->set_resync(pDstServerUser->csCrypt->m_statsRemote.resync);
}

msg.set_udp_packets(pDstServerUser->uiUDPPackets);
Expand Down
2 changes: 1 addition & 1 deletion src/tests/Benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void Client::readyRead() {
} else if (msg.has_server_nonce()) {
const std::string &server_nonce = msg.server_nonce();
if (server_nonce.size() == AES_BLOCK_SIZE) {
crypt.uiResync++;
crypt.m_statsLocal.resync++;
crypt.setDecryptIV(server_nonce);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/TestCrypt/TestCrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ void TestCrypt::ivrecovery() {

// Wraparound.
for (int i = 0; i < 128; i++) {
dec.uiLost = 0;
dec.m_statsLocal.lost = 0;
for (int j = 0; j < 15; j++)
enc.encrypt(secret, crypted, 10);
QVERIFY(dec.decrypt(crypted, decr, 14));
QCOMPARE(dec.uiLost, 14U);
QCOMPARE(dec.m_statsLocal.lost, 14U);
}

QVERIFY(enc.getEncryptIV() == dec.getDecryptIV());
Expand Down

0 comments on commit 47797b7

Please sign in to comment.